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 | class udt; |
---|
18 | udt* base_pointer; |
---|
19 | |
---|
20 | class udt { |
---|
21 | public: |
---|
22 | udt () { |
---|
23 | base_pointer = this; |
---|
24 | } |
---|
25 | ~udt () {} // required for GCC prior to 3.4 to generate cookie |
---|
26 | }; |
---|
27 | |
---|
28 | |
---|
29 | int main () |
---|
30 | { |
---|
31 | udt a; |
---|
32 | udt* ap = &a; |
---|
33 | |
---|
34 | // Capture placement new offsets for a udt |
---|
35 | new (ap) udt; |
---|
36 | int new_offset = int (base_pointer - ap); |
---|
37 | new (ap) udt [1]; |
---|
38 | int array_new_offset = int (base_pointer - ap); |
---|
39 | |
---|
40 | // Print offsets - we expect 0,0 or 0,sizeof(std::size_t) |
---|
41 | std::cout << new_offset <<','<< array_new_offset << std::endl; |
---|
42 | |
---|
43 | // Return status |
---|
44 | if (new_offset != 0) |
---|
45 | return -1; // Very bad if new has an offset |
---|
46 | |
---|
47 | #ifdef BOOST_UBLAS_USEFUL_ARRAY_PLACEMENT_NEW |
---|
48 | bool expect_array_offset = false; |
---|
49 | #else |
---|
50 | bool expect_array_offset = true; |
---|
51 | #endif |
---|
52 | // Check match between config and array |
---|
53 | if (!expect_array_offset && array_new_offset != 0) { |
---|
54 | return -2; // Bad config should not enable array new |
---|
55 | } |
---|
56 | if (expect_array_offset && array_new_offset == 0) { |
---|
57 | return -3; // Config could enable array new |
---|
58 | } |
---|
59 | |
---|
60 | return 0; |
---|
61 | } |
---|