Skip to content

Commit f9358ed

Browse files
wip
1 parent 13448e4 commit f9358ed

File tree

2 files changed

+75
-1
lines changed

2 files changed

+75
-1
lines changed

include/util/pool_allocator.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,9 @@ template <typename T, size_t MinItemsInBlock = 1024> class PoolAllocator
8282
void allocate_block(size_t items_in_block)
8383
{
8484
items_in_block = std::max(items_in_block, MinItemsInBlock);
85+
8586
size_t block_size = items_in_block * sizeof(T);
86-
T *block = static_cast<T *>(std::aligned_alloc(alignof(T), block_size));
87+
T *block = static_cast<T *>(std::malloc(block_size));
8788
if (!block)
8889
{
8990
throw std::bad_alloc();

unit_tests/util/pool_allocator.cpp

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
#include "util/pool_allocator.hpp"
2+
#include "util/typedefs.hpp"
3+
#include <boost/test/unit_test.hpp>
4+
5+
#include <unordered_map>
6+
7+
BOOST_AUTO_TEST_SUITE(pool_allocator)
8+
9+
using namespace osrm;
10+
using namespace osrm::util;
11+
12+
// in many of these tests we hope on address sanitizer to alert in the case if we are doing something wrong
13+
BOOST_AUTO_TEST_CASE(smoke)
14+
{
15+
PoolAllocator<int> pool;
16+
auto ptr = pool.allocate(1);
17+
*ptr = 42;
18+
BOOST_CHECK_NE(ptr, nullptr);
19+
pool.deallocate(ptr, 1);
20+
21+
ptr = pool.allocate(2);
22+
*ptr = 42;
23+
*(ptr + 1) = 43;
24+
BOOST_CHECK_NE(ptr, nullptr);
25+
pool.deallocate(ptr, 2);
26+
}
27+
28+
BOOST_AUTO_TEST_CASE(a_lot_of_items)
29+
{
30+
PoolAllocator<int, 1024> pool;
31+
auto ptr = pool.allocate(2048);
32+
for (int i = 0; i < 2048; ++i)
33+
{
34+
ptr[i] = i;
35+
}
36+
37+
for (int i = 0; i < 2048; ++i)
38+
{
39+
BOOST_CHECK_EQUAL(ptr[i], i);
40+
}
41+
42+
pool.deallocate(ptr, 2048);
43+
}
44+
45+
BOOST_AUTO_TEST_CASE(copy)
46+
{
47+
PoolAllocator<int> pool;
48+
auto ptr = pool.allocate(1);
49+
*ptr = 42;
50+
BOOST_CHECK_NE(ptr, nullptr);
51+
pool.deallocate(ptr, 1);
52+
53+
PoolAllocator<int> pool2(pool);
54+
ptr = pool2.allocate(1);
55+
*ptr = 42;
56+
BOOST_CHECK_NE(ptr, nullptr);
57+
pool2.deallocate(ptr, 1);
58+
}
59+
60+
BOOST_AUTO_TEST_CASE(unordered_map)
61+
{
62+
std::unordered_map<int, int, std::hash<int>, std::equal_to<int>, PoolAllocator<std::pair<const int, int>>> map;
63+
map[1] = 42;
64+
BOOST_CHECK_EQUAL(map[1], 42);
65+
66+
map.clear();
67+
68+
map[2] = 43;
69+
70+
BOOST_CHECK_EQUAL(map[2], 43);
71+
}
72+
73+
BOOST_AUTO_TEST_SUITE_END()

0 commit comments

Comments
 (0)