Skip to content

Commit 9266273

Browse files
committed
Add support for custom pointers.
1 parent 2d0b499 commit 9266273

File tree

5 files changed

+74
-10
lines changed

5 files changed

+74
-10
lines changed

CMakeLists.txt

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -219,11 +219,17 @@ if (PHMAP_BUILD_EXAMPLES)
219219
target_link_libraries(ex_llil4map PRIVATE OpenMP::OpenMP_CXX)
220220
target_compile_options(ex_llil4map PRIVATE "${OpenMP_CXX_FLAGS}")
221221

222+
file(COPY examples/llil_utils DESTINATION "${CMAKE_CURRENT_BINARY_DIR}")
223+
endif()
224+
225+
if (Boost_FOUND)
226+
add_executable(ex_custom_pointer examples/custom_pointer.cc phmap.natvis)
227+
target_include_directories(ex_custom_pointer PRIVATE ${Boost_INCLUDE_DIRS})
228+
222229
add_executable(ex_llil examples/llil.cc phmap.natvis)
223230
target_include_directories(ex_llil PRIVATE ${Boost_INCLUDE_DIRS})
224231
target_compile_features(ex_llil PUBLIC cxx_std_20)
225-
226-
file(COPY examples/llil_utils DESTINATION "${CMAKE_CURRENT_BINARY_DIR}")
232+
target_link_libraries(ex_llil Threads::Threads)
227233
endif()
228234

229235
target_link_libraries(ex_knucleotide Threads::Threads)

examples/custom_pointer.cc

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#include <boost/interprocess/managed_mapped_file.hpp>
2+
#include <scoped_allocator>
3+
#include <parallel_hashmap/phmap.h>
4+
#include <vector>
5+
#include <cstdint>
6+
#include <iostream>
7+
8+
using mmap_file_t = boost::interprocess::managed_mapped_file;
9+
10+
template <typename T>
11+
using bi_alloc_t = boost::interprocess::allocator<T, boost::interprocess::managed_mapped_file::segment_manager>;
12+
13+
template <typename T>
14+
using scoped_alloc_t = std::scoped_allocator_adaptor<T>;
15+
16+
void simple_map() {
17+
struct LatpLon {
18+
int32_t latp;
19+
int32_t lon;
20+
};
21+
22+
using nodestore_pair_t = std::pair<const uint64_t, LatpLon>;
23+
using map_t = phmap::flat_hash_map<const uint64_t, LatpLon, std::hash<uint64_t>, std::equal_to<uint64_t>,
24+
bi_alloc_t<nodestore_pair_t>>;
25+
26+
auto mmap_file =
27+
boost::interprocess::managed_mapped_file(boost::interprocess::open_or_create, "map_iv.dat", 1000000);
28+
map_t* map = mmap_file.find_or_construct<map_t>("node_store")(mmap_file.get_segment_manager());
29+
30+
for (unsigned int i = 0; i < 1000; ++i) {
31+
LatpLon p = {10, 10};
32+
map->emplace(i, p);
33+
}
34+
35+
std::cout << map->at(10).latp << " " << map->at(10).lon << std::endl;
36+
}
37+
38+
void scoped_map() {
39+
using way_t = std::vector<uint64_t, bi_alloc_t<uint64_t>>;
40+
using waystore_pair_t = std::pair<const uint64_t, way_t>;
41+
using map_t = phmap::flat_hash_map<const uint64_t, way_t, std::hash<uint64_t>, std::equal_to<uint64_t>,
42+
std::scoped_allocator_adaptor<bi_alloc_t<waystore_pair_t>>>;
43+
44+
auto mmap_file =
45+
boost::interprocess::managed_mapped_file(boost::interprocess::open_or_create, "map_iv.dat", 1000000);
46+
map_t* map = mmap_file.find_or_construct<map_t>("ways_store")(mmap_file.get_segment_manager());
47+
48+
for (unsigned int i = 0; i < 1000; ++i) {
49+
std::vector<uint64_t> init = {1, 2, 3, 4};
50+
map->emplace(std::piecewise_construct, std::forward_as_tuple(i), std::forward_as_tuple(init.begin(), init.end()));
51+
}
52+
53+
std::cout << map->at(10).size() << std::endl;
54+
for (auto const& i : map->at(10))
55+
std::cout << i << " ";
56+
std::cout << std::endl;
57+
}
58+
59+
int main()
60+
{
61+
simple_map();
62+
scoped_map();
63+
return 0;
64+
}

examples/llil.cc

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@
6363

6464
#include <utility>
6565
#include <iterator>
66-
#include <execution>
6766
#include <algorithm>
6867
#include <filesystem>
6968

parallel_hashmap/phmap.h

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -926,12 +926,7 @@ class raw_hash_set
926926
using IsDecomposable = IsDecomposable<void, PolicyTraits, Hash, Eq, Ts...>;
927927

928928
public:
929-
static_assert(std::is_same<pointer, value_type*>::value,
930-
"Allocators with custom pointer types are not supported");
931-
static_assert(std::is_same<const_pointer, const value_type*>::value,
932-
"Allocators with custom pointer types are not supported");
933-
934-
class iterator
929+
class iterator
935930
{
936931
friend class raw_hash_set;
937932

parallel_hashmap/phmap_base.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4189,7 +4189,7 @@ void* Allocate(Alloc* alloc, size_t n) {
41894189
using A = typename phmap::allocator_traits<Alloc>::template rebind_alloc<M>;
41904190
using AT = typename phmap::allocator_traits<Alloc>::template rebind_traits<M>;
41914191
A mem_alloc(*alloc);
4192-
void* p = AT::allocate(mem_alloc, (n + sizeof(M) - 1) / sizeof(M));
4192+
void* p = &*AT::allocate(mem_alloc, (n + sizeof(M) - 1) / sizeof(M));
41934193
assert(reinterpret_cast<uintptr_t>(p) % Alignment == 0 &&
41944194
"allocator does not respect alignment");
41954195
return p;

0 commit comments

Comments
 (0)