| 1 | /* |
|---|
| 2 | * Copyright (c) 2004 Michael Stevens |
|---|
| 3 | * Use, modification and distribution are subject to the |
|---|
| 4 | * Boost Software License, Version 1.0. (See accompanying file |
|---|
| 5 | * LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
|---|
| 6 | */ |
|---|
| 7 | |
|---|
| 8 | /* |
|---|
| 9 | * Test placement new and array placement new for uBLAS |
|---|
| 10 | * See if base pointer is effected by array count cookie |
|---|
| 11 | */ |
|---|
| 12 | |
|---|
| 13 | #include <boost/numeric/ublas/storage.hpp> |
|---|
| 14 | #include <iostream> |
|---|
| 15 | |
|---|
| 16 | // User defined type to capture base pointer on construction |
|---|
| 17 | |
|---|
| 18 | class udt { |
|---|
| 19 | public: |
|---|
| 20 | udt () { |
|---|
| 21 | base_pointer = this; |
|---|
| 22 | } |
|---|
| 23 | ~udt () {} // required for GCC prior to 3.4 to generate cookie |
|---|
| 24 | |
|---|
| 25 | static udt* base_pointer; |
|---|
| 26 | }; |
|---|
| 27 | |
|---|
| 28 | udt* udt::base_pointer; |
|---|
| 29 | |
|---|
| 30 | int main () |
|---|
| 31 | { |
|---|
| 32 | udt a; |
|---|
| 33 | udt* ap = &a; |
|---|
| 34 | |
|---|
| 35 | // Capture placement new offsets for a udt |
|---|
| 36 | new (ap) udt; |
|---|
| 37 | int new_offset = int (udt::base_pointer - ap); |
|---|
| 38 | new (ap) udt [1]; |
|---|
| 39 | int array_new_offset = int (udt::base_pointer - ap); |
|---|
| 40 | |
|---|
| 41 | // Print offsets - we expect 0,0 or 0,sizeof(std::size_t) |
|---|
| 42 | std::cout << new_offset <<','<< array_new_offset << std::endl; |
|---|
| 43 | |
|---|
| 44 | // Return status |
|---|
| 45 | if (new_offset != 0) |
|---|
| 46 | return -1; // Very bad if new has an offset |
|---|
| 47 | |
|---|
| 48 | #ifdef BOOST_UBLAS_USEFUL_ARRAY_PLACEMENT_NEW |
|---|
| 49 | bool expect_array_offset = false; |
|---|
| 50 | #else |
|---|
| 51 | bool expect_array_offset = true; |
|---|
| 52 | #endif |
|---|
| 53 | // Check match between config and array |
|---|
| 54 | if (!expect_array_offset && array_new_offset != 0) { |
|---|
| 55 | return -2; // Bad config should not enable array new |
|---|
| 56 | } |
|---|
| 57 | if (expect_array_offset && array_new_offset == 0) { |
|---|
| 58 | return -3; // Config could enable array new |
|---|
| 59 | } |
|---|
| 60 | |
|---|
| 61 | return 0; |
|---|
| 62 | } |
|---|