]>
git.saurik.com Git - apple/xnu.git/blob - tests/safe_allocation_src/ctor.allocate.cpp
3 // explicit safe_allocation(size_t n, allocate_memory_t);
6 #include <libkern/c++/safe_allocation.h>
7 #include <darwintest.h>
8 #include "test_utils.h"
18 TrackInit() : initialized(true)
28 tracking_allocator::reset();
30 tracked_safe_allocation
<T
> array(10, libkern::allocate_memory
);
31 CHECK(tracking_allocator::allocated_size
== 10 * sizeof(T
));
32 CHECK(array
.data() != nullptr);
33 CHECK(array
.size() == 10);
34 CHECK(array
.begin() == array
.data());
35 CHECK(array
.end() == array
.data() + 10);
37 CHECK(tracking_allocator::deallocated_size
== 10 * sizeof(T
));
40 // Check with a huge number of elements that will overflow size_t
42 std::size_t max_n
= std::numeric_limits
<std::size_t>::max() / sizeof(T
);
43 tracking_allocator::reset();
46 tracked_safe_allocation
<T
> array(max_n
+ 1, libkern::allocate_memory
);
47 CHECK(array
.data() == nullptr);
48 CHECK(array
.size() == 0);
49 CHECK(array
.begin() == array
.end());
50 CHECK(!tracking_allocator::did_allocate
);
52 CHECK(!tracking_allocator::did_deallocate
);
56 T_DECL(ctor_allocate
, "safe_allocation.ctor.allocate") {
60 // Make sure we value-initialize elements
62 tracked_safe_allocation
<TrackInit
> array(10, libkern::allocate_memory
);
63 for (int i
= 0; i
!= 10; ++i
) {
64 CHECK(array
[i
].initialized
== true);