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