Skip to content

Commit

Permalink
Add support for custom pointers such as Boost Interprocess `offset_pt…
Browse files Browse the repository at this point in the history
…r` (#28)
  • Loading branch information
greg7mdp authored Nov 2, 2024
1 parent e33ed00 commit 2886f96
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 36 deletions.
69 changes: 38 additions & 31 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ option(GTL_BUILD_BENCHMARKS "Whether or not to build the benchmarks" ${GTL_MASTE

if(MSVC)
add_compile_options("$<$<COMPILE_LANGUAGE:CXX>:/bigobj>")
set(natvis include/gtl/debug_vis/gtl.natvis)
endif()

if (GTL_BUILD_TESTS OR GTL_BUILD_EXAMPLES)
Expand Down Expand Up @@ -165,48 +166,54 @@ if (GTL_BUILD_EXAMPLES)
set(THREADS_PREFER_PTHREAD_FLAG ON)
find_package(Threads REQUIRED)

gtl_cc_app(ex_btree SRCS examples/btree/btree.cpp include/gtl/debug_vis/gtl.natvis)
gtl_cc_app(ex_btree SRCS examples/btree/btree.cpp ${natvis})

gtl_cc_app(ex_insert_bench SRCS examples/phmap/insert_bench.cpp include/gtl/debug_vis/gtl.natvis)
gtl_cc_app(ex_mt_word_counter SRCS examples/phmap/mt_word_counter.cpp include/gtl/debug_vis/gtl.natvis)
gtl_cc_app(ex_p_bench SRCS examples/phmap/p_bench.cpp include/gtl/debug_vis/gtl.natvis)
gtl_cc_app(ex_insert_bench SRCS examples/phmap/insert_bench.cpp ${natvis})
gtl_cc_app(ex_mt_word_counter SRCS examples/phmap/mt_word_counter.cpp ${natvis})
gtl_cc_app(ex_p_bench SRCS examples/phmap/p_bench.cpp ${natvis})

if(MSVC)
gtl_cc_app(ex_lazy_emplace_l SRCS examples/phmap/lazy_emplace_l.cpp include/gtl/debug_vis/gtl.natvis)
gtl_cc_app(ex_lazy_emplace_l SRCS examples/phmap/lazy_emplace_l.cpp ${natvis})
endif()

gtl_cc_app(ex_allmaps SRCS examples/hmap/allmaps.cpp include/gtl/debug_vis/gtl.natvis)
gtl_cc_app(ex_basic SRCS examples/hmap/basic.cpp include/gtl/debug_vis/gtl.natvis)
gtl_cc_app(ex_bench SRCS examples/hmap/bench.cpp include/gtl/debug_vis/gtl.natvis LIBS Threads::Threads)
gtl_cc_app(ex_emplace SRCS examples/hmap/emplace.cpp include/gtl/debug_vis/gtl.natvis)
gtl_cc_app(ex_allmaps SRCS examples/hmap/allmaps.cpp ${natvis})
gtl_cc_app(ex_basic SRCS examples/hmap/basic.cpp ${natvis})
gtl_cc_app(ex_bench SRCS examples/hmap/bench.cpp ${natvis} LIBS Threads::Threads)
gtl_cc_app(ex_emplace SRCS examples/hmap/emplace.cpp ${natvis})

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

gtl_cc_app(ex_hash SRCS examples/hmap/hash.cpp include/gtl/debug_vis/gtl.natvis)
gtl_cc_app(ex_hash_std SRCS examples/hmap/hash_std.cpp include/gtl/debug_vis/gtl.natvis)
gtl_cc_app(ex_hash_value SRCS examples/hmap/hash_value.cpp include/gtl/debug_vis/gtl.natvis)
gtl_cc_app(ex_two_files SRCS examples/hmap/f1.cpp examples/hmap/f2.cpp include/gtl/debug_vis/gtl.natvis)
gtl_cc_app(ex_knucleotide SRCS examples/hmap/knucleotide.cpp include/gtl/debug_vis/gtl.natvis LIBS Threads::Threads)
gtl_cc_app(ex_dump_load SRCS examples/hmap/dump_load.cpp include/gtl/debug_vis/gtl.natvis)
gtl_cc_app(ex_dump_nested SRCS examples/hmap/dump_nested.cpp include/gtl/debug_vis/gtl.natvis)
gtl_cc_app(ex_matt SRCS examples/hmap/matt.cpp include/gtl/debug_vis/gtl.natvis)

gtl_cc_app(ex_soa SRCS examples/misc/soa.cpp include/gtl/debug_vis/gtl.natvis)
gtl_cc_app(ex_vec_utils SRCS examples/misc/vec_utils.cpp include/gtl/debug_vis/gtl.natvis)
gtl_cc_app(ex_bit_vector SRCS examples/misc/bit_vector.cpp include/gtl/debug_vis/gtl.natvis)
gtl_cc_app(ex_intrusive SRCS examples/misc/intrusive.cpp include/gtl/debug_vis/gtl.natvis)
gtl_cc_app(ex_utils SRCS "examples/misc/utils.cpp" include/gtl/debug_vis/gtl.natvis)
#gtl_cc_app(ex_adv_utils SRCS "examples/misc/adv_utils.cpp" include/gtl/debug_vis/gtl.natvis)
gtl_cc_app(ex_hash SRCS examples/hmap/hash.cpp ${natvis})
gtl_cc_app(ex_hash_std SRCS examples/hmap/hash_std.cpp ${natvis})
gtl_cc_app(ex_hash_value SRCS examples/hmap/hash_value.cpp ${natvis})
gtl_cc_app(ex_two_files SRCS examples/hmap/f1.cpp examples/hmap/f2.cpp ${natvis})
gtl_cc_app(ex_knucleotide SRCS examples/hmap/knucleotide.cpp ${natvis} LIBS Threads::Threads)
gtl_cc_app(ex_dump_load SRCS examples/hmap/dump_load.cpp ${natvis})
gtl_cc_app(ex_dump_nested SRCS examples/hmap/dump_nested.cpp ${natvis})
gtl_cc_app(ex_matt SRCS examples/hmap/matt.cpp ${natvis})

gtl_cc_app(ex_soa SRCS examples/misc/soa.cpp ${natvis})
gtl_cc_app(ex_vec_utils SRCS examples/misc/vec_utils.cpp ${natvis})
gtl_cc_app(ex_bit_vector SRCS examples/misc/bit_vector.cpp ${natvis})
gtl_cc_app(ex_intrusive SRCS examples/misc/intrusive.cpp ${natvis})
gtl_cc_app(ex_utils SRCS "examples/misc/utils.cpp" ${natvis})
#gtl_cc_app(ex_adv_utils SRCS "examples/misc/adv_utils.cpp" ${natvis})

## cache/memoize
gtl_cc_app(ex_memoize_fib SRCS examples/memoize/memoize_fib.cpp include/gtl/debug_vis/gtl.natvis)
gtl_cc_app(ex_memoize_primes SRCS examples/memoize/memoize_primes.cpp include/gtl/debug_vis/gtl.natvis)
gtl_cc_app(ex_mt_memoize SRCS examples/memoize/mt_memoize.cpp include/gtl/debug_vis/gtl.natvis LIBS Threads::Threads)
gtl_cc_app(ex_mt_memoize_lru SRCS examples/memoize/mt_memoize_lru.cpp include/gtl/debug_vis/gtl.natvis LIBS Threads::Threads)
gtl_cc_app(ex_memoize_fib SRCS examples/memoize/memoize_fib.cpp ${natvis})
gtl_cc_app(ex_memoize_primes SRCS examples/memoize/memoize_primes.cpp ${natvis})
gtl_cc_app(ex_mt_memoize SRCS examples/memoize/mt_memoize.cpp ${natvis} LIBS Threads::Threads)
gtl_cc_app(ex_mt_memoize_lru SRCS examples/memoize/mt_memoize_lru.cpp ${natvis} LIBS Threads::Threads)

find_package(Boost 1.70.0)
if (Boost_FOUND)
gtl_cc_app(ex_custom_pointer SRCS examples/hmap/custom_pointer.cpp ${natvis})
target_include_directories(ex_custom_pointer PRIVATE ${Boost_INCLUDE_DIRS})
endif()
endif()

if (GTL_BUILD_BENCHMARKS)
gtl_cc_app(bench_bit_vector SRCS benchmarks/bitvector_bench.cpp include/gtl/debug_vis/gtl.natvis)
gtl_cc_app(bench_hash SRCS benchmarks/hash_bench.cpp include/gtl/debug_vis/gtl.natvis)
gtl_cc_app(bench_bit_vector SRCS benchmarks/bitvector_bench.cpp ${natvis})
gtl_cc_app(bench_hash SRCS benchmarks/hash_bench.cpp ${natvis})
endif()
64 changes: 64 additions & 0 deletions examples/hmap/custom_pointer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#include <boost/interprocess/managed_mapped_file.hpp>
#include <scoped_allocator>
#include <gtl/phmap.hpp>
#include <vector>
#include <cstdint>
#include <iostream>

using mmap_file_t = boost::interprocess::managed_mapped_file;

template <typename T>
using bi_alloc_t = boost::interprocess::allocator<T, boost::interprocess::managed_mapped_file::segment_manager>;

template <typename T>
using scoped_alloc_t = std::scoped_allocator_adaptor<T>;

void simple_map() {
struct LatpLon {
int32_t latp;
int32_t lon;
};

using nodestore_pair_t = std::pair<const uint64_t, LatpLon>;
using map_t = gtl::flat_hash_map<const uint64_t, LatpLon, std::hash<uint64_t>, std::equal_to<uint64_t>,
bi_alloc_t<nodestore_pair_t>>;

auto mmap_file =
boost::interprocess::managed_mapped_file(boost::interprocess::open_or_create, "map_iv.dat", 1000000);
map_t* map = mmap_file.find_or_construct<map_t>("node_store")(mmap_file.get_segment_manager());

for (unsigned int i = 0; i < 1000; ++i) {
LatpLon p = {10, 10};
map->emplace(i, p);
}

std::cout << map->at(10).latp << " " << map->at(10).lon << std::endl;
}

void scoped_map() {
using way_t = std::vector<uint64_t, bi_alloc_t<uint64_t>>;
using waystore_pair_t = std::pair<const uint64_t, way_t>;
using map_t = gtl::flat_hash_map<const uint64_t, way_t, std::hash<uint64_t>, std::equal_to<uint64_t>,
std::scoped_allocator_adaptor<bi_alloc_t<waystore_pair_t>>>;

auto mmap_file =
boost::interprocess::managed_mapped_file(boost::interprocess::open_or_create, "map_iv.dat", 1000000);
map_t* map = mmap_file.find_or_construct<map_t>("ways_store")(mmap_file.get_segment_manager());

for (unsigned int i = 0; i < 1000; ++i) {
std::vector<uint64_t> init = {1, 2, 3, 4};
map->emplace(std::piecewise_construct, std::forward_as_tuple(i), std::forward_as_tuple(init.begin(), init.end()));
}

std::cout << map->at(10).size() << std::endl;
for (auto const& i : map->at(10))
std::cout << i << " ";
std::cout << std::endl;
}

int main()
{
simple_map();
scoped_map();
return 0;
}
3 changes: 2 additions & 1 deletion include/gtl/gtl_base.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -903,7 +903,8 @@ void* Allocate(Alloc* alloc, size_t n) {
using A = typename std::allocator_traits<Alloc>::template rebind_alloc<M>;
using AT = typename std::allocator_traits<Alloc>::template rebind_traits<M>;
A mem_alloc(*alloc);
void* p = AT::allocate(mem_alloc, (n + sizeof(M) - 1) / sizeof(M));
// `&*` below to support custom pointers such as boost offset_ptr.
void* p = &*AT::allocate(mem_alloc, (n + sizeof(M) - 1) / sizeof(M));
assert(reinterpret_cast<uintptr_t>(p) % Alignment == 0 && "allocator does not respect alignment");
return p;
}
Expand Down
4 changes: 0 additions & 4 deletions include/gtl/phmap.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1574,10 +1574,6 @@ class raw_hash_set {
using IsDecomposable = IsDecomposable<void, PolicyTraits, Hash, Eq, Ts...>;

public:
static_assert(std::is_same_v<pointer, value_type*>, "Allocators with custom pointer types are not supported");
static_assert(std::is_same_v<const_pointer, const value_type*>,
"Allocators with custom pointer types are not supported");

class iterator {
friend class raw_hash_set;

Expand Down

0 comments on commit 2886f96

Please sign in to comment.