Skip to content

Commit 2886f96

Browse files
authored
Add support for custom pointers such as Boost Interprocess offset_ptr (#28)
1 parent e33ed00 commit 2886f96

File tree

4 files changed

+104
-36
lines changed

4 files changed

+104
-36
lines changed

CMakeLists.txt

Lines changed: 38 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ option(GTL_BUILD_BENCHMARKS "Whether or not to build the benchmarks" ${GTL_MASTE
102102

103103
if(MSVC)
104104
add_compile_options("$<$<COMPILE_LANGUAGE:CXX>:/bigobj>")
105+
set(natvis include/gtl/debug_vis/gtl.natvis)
105106
endif()
106107

107108
if (GTL_BUILD_TESTS OR GTL_BUILD_EXAMPLES)
@@ -165,48 +166,54 @@ if (GTL_BUILD_EXAMPLES)
165166
set(THREADS_PREFER_PTHREAD_FLAG ON)
166167
find_package(Threads REQUIRED)
167168

168-
gtl_cc_app(ex_btree SRCS examples/btree/btree.cpp include/gtl/debug_vis/gtl.natvis)
169+
gtl_cc_app(ex_btree SRCS examples/btree/btree.cpp ${natvis})
169170

170-
gtl_cc_app(ex_insert_bench SRCS examples/phmap/insert_bench.cpp include/gtl/debug_vis/gtl.natvis)
171-
gtl_cc_app(ex_mt_word_counter SRCS examples/phmap/mt_word_counter.cpp include/gtl/debug_vis/gtl.natvis)
172-
gtl_cc_app(ex_p_bench SRCS examples/phmap/p_bench.cpp include/gtl/debug_vis/gtl.natvis)
171+
gtl_cc_app(ex_insert_bench SRCS examples/phmap/insert_bench.cpp ${natvis})
172+
gtl_cc_app(ex_mt_word_counter SRCS examples/phmap/mt_word_counter.cpp ${natvis})
173+
gtl_cc_app(ex_p_bench SRCS examples/phmap/p_bench.cpp ${natvis})
173174

174175
if(MSVC)
175-
gtl_cc_app(ex_lazy_emplace_l SRCS examples/phmap/lazy_emplace_l.cpp include/gtl/debug_vis/gtl.natvis)
176+
gtl_cc_app(ex_lazy_emplace_l SRCS examples/phmap/lazy_emplace_l.cpp ${natvis})
176177
endif()
177178

178-
gtl_cc_app(ex_allmaps SRCS examples/hmap/allmaps.cpp include/gtl/debug_vis/gtl.natvis)
179-
gtl_cc_app(ex_basic SRCS examples/hmap/basic.cpp include/gtl/debug_vis/gtl.natvis)
180-
gtl_cc_app(ex_bench SRCS examples/hmap/bench.cpp include/gtl/debug_vis/gtl.natvis LIBS Threads::Threads)
181-
gtl_cc_app(ex_emplace SRCS examples/hmap/emplace.cpp include/gtl/debug_vis/gtl.natvis)
179+
gtl_cc_app(ex_allmaps SRCS examples/hmap/allmaps.cpp ${natvis})
180+
gtl_cc_app(ex_basic SRCS examples/hmap/basic.cpp ${natvis})
181+
gtl_cc_app(ex_bench SRCS examples/hmap/bench.cpp ${natvis} LIBS Threads::Threads)
182+
gtl_cc_app(ex_emplace SRCS examples/hmap/emplace.cpp ${natvis})
182183

183-
gtl_cc_app(ex_serialize SRCS examples/hmap/serialize.cpp include/gtl/debug_vis/gtl.natvis)
184+
gtl_cc_app(ex_serialize SRCS examples/hmap/serialize.cpp ${natvis})
184185
#target_include_directories(ex_serialize PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/../cereal/include>)
185186

186-
gtl_cc_app(ex_hash SRCS examples/hmap/hash.cpp include/gtl/debug_vis/gtl.natvis)
187-
gtl_cc_app(ex_hash_std SRCS examples/hmap/hash_std.cpp include/gtl/debug_vis/gtl.natvis)
188-
gtl_cc_app(ex_hash_value SRCS examples/hmap/hash_value.cpp include/gtl/debug_vis/gtl.natvis)
189-
gtl_cc_app(ex_two_files SRCS examples/hmap/f1.cpp examples/hmap/f2.cpp include/gtl/debug_vis/gtl.natvis)
190-
gtl_cc_app(ex_knucleotide SRCS examples/hmap/knucleotide.cpp include/gtl/debug_vis/gtl.natvis LIBS Threads::Threads)
191-
gtl_cc_app(ex_dump_load SRCS examples/hmap/dump_load.cpp include/gtl/debug_vis/gtl.natvis)
192-
gtl_cc_app(ex_dump_nested SRCS examples/hmap/dump_nested.cpp include/gtl/debug_vis/gtl.natvis)
193-
gtl_cc_app(ex_matt SRCS examples/hmap/matt.cpp include/gtl/debug_vis/gtl.natvis)
194-
195-
gtl_cc_app(ex_soa SRCS examples/misc/soa.cpp include/gtl/debug_vis/gtl.natvis)
196-
gtl_cc_app(ex_vec_utils SRCS examples/misc/vec_utils.cpp include/gtl/debug_vis/gtl.natvis)
197-
gtl_cc_app(ex_bit_vector SRCS examples/misc/bit_vector.cpp include/gtl/debug_vis/gtl.natvis)
198-
gtl_cc_app(ex_intrusive SRCS examples/misc/intrusive.cpp include/gtl/debug_vis/gtl.natvis)
199-
gtl_cc_app(ex_utils SRCS "examples/misc/utils.cpp" include/gtl/debug_vis/gtl.natvis)
200-
#gtl_cc_app(ex_adv_utils SRCS "examples/misc/adv_utils.cpp" include/gtl/debug_vis/gtl.natvis)
187+
gtl_cc_app(ex_hash SRCS examples/hmap/hash.cpp ${natvis})
188+
gtl_cc_app(ex_hash_std SRCS examples/hmap/hash_std.cpp ${natvis})
189+
gtl_cc_app(ex_hash_value SRCS examples/hmap/hash_value.cpp ${natvis})
190+
gtl_cc_app(ex_two_files SRCS examples/hmap/f1.cpp examples/hmap/f2.cpp ${natvis})
191+
gtl_cc_app(ex_knucleotide SRCS examples/hmap/knucleotide.cpp ${natvis} LIBS Threads::Threads)
192+
gtl_cc_app(ex_dump_load SRCS examples/hmap/dump_load.cpp ${natvis})
193+
gtl_cc_app(ex_dump_nested SRCS examples/hmap/dump_nested.cpp ${natvis})
194+
gtl_cc_app(ex_matt SRCS examples/hmap/matt.cpp ${natvis})
195+
196+
gtl_cc_app(ex_soa SRCS examples/misc/soa.cpp ${natvis})
197+
gtl_cc_app(ex_vec_utils SRCS examples/misc/vec_utils.cpp ${natvis})
198+
gtl_cc_app(ex_bit_vector SRCS examples/misc/bit_vector.cpp ${natvis})
199+
gtl_cc_app(ex_intrusive SRCS examples/misc/intrusive.cpp ${natvis})
200+
gtl_cc_app(ex_utils SRCS "examples/misc/utils.cpp" ${natvis})
201+
#gtl_cc_app(ex_adv_utils SRCS "examples/misc/adv_utils.cpp" ${natvis})
201202

202203
## cache/memoize
203-
gtl_cc_app(ex_memoize_fib SRCS examples/memoize/memoize_fib.cpp include/gtl/debug_vis/gtl.natvis)
204-
gtl_cc_app(ex_memoize_primes SRCS examples/memoize/memoize_primes.cpp include/gtl/debug_vis/gtl.natvis)
205-
gtl_cc_app(ex_mt_memoize SRCS examples/memoize/mt_memoize.cpp include/gtl/debug_vis/gtl.natvis LIBS Threads::Threads)
206-
gtl_cc_app(ex_mt_memoize_lru SRCS examples/memoize/mt_memoize_lru.cpp include/gtl/debug_vis/gtl.natvis LIBS Threads::Threads)
204+
gtl_cc_app(ex_memoize_fib SRCS examples/memoize/memoize_fib.cpp ${natvis})
205+
gtl_cc_app(ex_memoize_primes SRCS examples/memoize/memoize_primes.cpp ${natvis})
206+
gtl_cc_app(ex_mt_memoize SRCS examples/memoize/mt_memoize.cpp ${natvis} LIBS Threads::Threads)
207+
gtl_cc_app(ex_mt_memoize_lru SRCS examples/memoize/mt_memoize_lru.cpp ${natvis} LIBS Threads::Threads)
208+
209+
find_package(Boost 1.70.0)
210+
if (Boost_FOUND)
211+
gtl_cc_app(ex_custom_pointer SRCS examples/hmap/custom_pointer.cpp ${natvis})
212+
target_include_directories(ex_custom_pointer PRIVATE ${Boost_INCLUDE_DIRS})
213+
endif()
207214
endif()
208215

209216
if (GTL_BUILD_BENCHMARKS)
210-
gtl_cc_app(bench_bit_vector SRCS benchmarks/bitvector_bench.cpp include/gtl/debug_vis/gtl.natvis)
211-
gtl_cc_app(bench_hash SRCS benchmarks/hash_bench.cpp include/gtl/debug_vis/gtl.natvis)
217+
gtl_cc_app(bench_bit_vector SRCS benchmarks/bitvector_bench.cpp ${natvis})
218+
gtl_cc_app(bench_hash SRCS benchmarks/hash_bench.cpp ${natvis})
212219
endif()

examples/hmap/custom_pointer.cpp

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 <gtl/phmap.hpp>
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 = gtl::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 = gtl::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+
}

include/gtl/gtl_base.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -903,7 +903,8 @@ void* Allocate(Alloc* alloc, size_t n) {
903903
using A = typename std::allocator_traits<Alloc>::template rebind_alloc<M>;
904904
using AT = typename std::allocator_traits<Alloc>::template rebind_traits<M>;
905905
A mem_alloc(*alloc);
906-
void* p = AT::allocate(mem_alloc, (n + sizeof(M) - 1) / sizeof(M));
906+
// `&*` below to support custom pointers such as boost offset_ptr.
907+
void* p = &*AT::allocate(mem_alloc, (n + sizeof(M) - 1) / sizeof(M));
907908
assert(reinterpret_cast<uintptr_t>(p) % Alignment == 0 && "allocator does not respect alignment");
908909
return p;
909910
}

include/gtl/phmap.hpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1574,10 +1574,6 @@ class raw_hash_set {
15741574
using IsDecomposable = IsDecomposable<void, PolicyTraits, Hash, Eq, Ts...>;
15751575

15761576
public:
1577-
static_assert(std::is_same_v<pointer, value_type*>, "Allocators with custom pointer types are not supported");
1578-
static_assert(std::is_same_v<const_pointer, const value_type*>,
1579-
"Allocators with custom pointer types are not supported");
1580-
15811577
class iterator {
15821578
friend class raw_hash_set;
15831579

0 commit comments

Comments
 (0)