1 | /* |
2 | Copyright (c) 2005-2019 Intel Corporation |
3 | |
4 | Licensed under the Apache License, Version 2.0 (the "License"); |
5 | you may not use this file except in compliance with the License. |
6 | You may obtain a copy of the License at |
7 | |
8 | http://www.apache.org/licenses/LICENSE-2.0 |
9 | |
10 | Unless required by applicable law or agreed to in writing, software |
11 | distributed under the License is distributed on an "AS IS" BASIS, |
12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13 | See the License for the specific language governing permissions and |
14 | limitations under the License. |
15 | */ |
16 | |
17 | // Test whether cache_aligned_allocator works with some of the host's STL containers. |
18 | |
19 | #include "tbb/cache_aligned_allocator.h" |
20 | #include "tbb/tbb_allocator.h" |
21 | |
22 | #define HARNESS_NO_PARSE_COMMAND_LINE 1 |
23 | // the real body of the test is there: |
24 | #include "test_allocator.h" |
25 | |
26 | template<> |
27 | struct is_zero_filling<tbb::zero_allocator<void> > { |
28 | static const bool value = true; |
29 | }; |
30 | |
31 | // Test that NFS_Allocate() throws bad_alloc if cannot allocate memory. |
32 | void Test_NFS_Allocate_Throws() { |
33 | #if TBB_USE_EXCEPTIONS && !__TBB_THROW_ACROSS_MODULE_BOUNDARY_BROKEN |
34 | using namespace tbb::internal; |
35 | |
36 | // First, allocate a reasonably big amount of memory, big enough |
37 | // to not cause warp around in system allocator after adding object header |
38 | // during address2 allocation. |
39 | const size_t itemsize = 1024; |
40 | const size_t nitems = 1024; |
41 | void *address1 = NULL; |
42 | try { |
43 | address1 = NFS_Allocate( nitems, itemsize, NULL ); |
44 | } catch( ... ) { |
45 | // intentionally empty |
46 | } |
47 | ASSERT( address1, "NFS_Allocate unable to obtain 1024*1024 bytes" ); |
48 | |
49 | bool exception_caught = false; |
50 | try { |
51 | // Try allocating more memory than left in the address space; should cause std::bad_alloc |
52 | (void) NFS_Allocate( 1, ~size_t(0) - itemsize*nitems + NFS_GetLineSize(), NULL); |
53 | } catch( std::bad_alloc& ) { |
54 | exception_caught = true; |
55 | } catch( ... ) { |
56 | ASSERT( __TBB_EXCEPTION_TYPE_INFO_BROKEN, "Unexpected exception type (std::bad_alloc was expected)" ); |
57 | exception_caught = true; |
58 | } |
59 | ASSERT( exception_caught, "NFS_Allocate did not throw bad_alloc" ); |
60 | |
61 | try { |
62 | NFS_Free( address1 ); |
63 | } catch( ... ) { |
64 | ASSERT( false, "NFS_Free did not accept the address obtained with NFS_Allocate" ); |
65 | } |
66 | #endif /* TBB_USE_EXCEPTIONS && !__TBB_THROW_ACROSS_MODULE_BOUNDARY_BROKEN */ |
67 | } |
68 | |
69 | int TestMain () { |
70 | int result = TestMain<tbb::cache_aligned_allocator<void> >(); |
71 | result += TestMain<tbb::tbb_allocator<void> >(); |
72 | result += TestMain<tbb::zero_allocator<void> >(); |
73 | ASSERT( !result, NULL ); |
74 | Test_NFS_Allocate_Throws(); |
75 | return Harness::Done; |
76 | } |
77 | |