# HG changeset patch # User Mike Becker # Date 1679415680 -3600 # Node ID d7129285ac32dd2c3f1227a7e77982b6a17721a2 # Parent 2f88a7c13a2842fea49c1dcdacd52b36da9702a3 add CX_STORE_POINTERS special item size for maps diff -r 2f88a7c13a28 -r d7129285ac32 src/cx/map.h --- a/src/cx/map.h Tue Mar 21 17:18:29 2023 +0100 +++ b/src/cx/map.h Tue Mar 21 17:21:20 2023 +0100 @@ -46,6 +46,10 @@ extern "C" { #endif +#ifndef CX_STORE_POINTERS +#define CX_STORE_POINTERS 0 +#endif + /** Type for the UCX map. */ typedef struct cx_map_s CxMap; diff -r 2f88a7c13a28 -r d7129285ac32 src/hash_map.c --- a/src/hash_map.c Tue Mar 21 17:18:29 2023 +0100 +++ b/src/hash_map.c Tue Mar 21 17:21:20 2023 +0100 @@ -432,7 +432,12 @@ map->base.cl = &cx_hash_map_class; map->base.allocator = allocator; map->base.size = 0; - map->base.itemsize = itemsize; + + if (itemsize > 0) { + map->base.itemsize = itemsize; + } else { + cxMapStorePointers((CxMap *) map); + } return (CxMap *) map; } diff -r 2f88a7c13a28 -r d7129285ac32 tests/test_map.cpp --- a/tests/test_map.cpp Tue Mar 21 17:18:29 2023 +0100 +++ b/tests/test_map.cpp Tue Mar 21 17:21:20 2023 +0100 @@ -135,6 +135,23 @@ EXPECT_TRUE(allocator.verify()); } +TEST(CxHashMap, CreateForStoringPointers) { + CxTestingAllocator allocator; + auto map = cxHashMapCreate(&allocator, CX_STORE_POINTERS, 0); + auto hmap = reinterpret_cast(map); + EXPECT_GT(hmap->bucket_count, 0); + cx_for_n(i, hmap->bucket_count) { + EXPECT_EQ(hmap->buckets[i], nullptr); + } + EXPECT_EQ(map->size, 0); + EXPECT_EQ(map->allocator, &allocator); + EXPECT_TRUE(map->store_pointers); + EXPECT_EQ(map->itemsize, sizeof(void *)); + + cxMapDestroy(map); + EXPECT_TRUE(allocator.verify()); +} + TEST(CxHashMap, BasicOperations) { // create the map CxTestingAllocator allocator;