Tue, 21 Mar 2023 17:21:20 +0100
add CX_STORE_POINTERS special item size for maps
src/cx/map.h | file | annotate | diff | comparison | revisions | |
src/hash_map.c | file | annotate | diff | comparison | revisions | |
tests/test_map.cpp | file | annotate | diff | comparison | revisions |
1.1 --- a/src/cx/map.h Tue Mar 21 17:18:29 2023 +0100 1.2 +++ b/src/cx/map.h Tue Mar 21 17:21:20 2023 +0100 1.3 @@ -46,6 +46,10 @@ 1.4 extern "C" { 1.5 #endif 1.6 1.7 +#ifndef CX_STORE_POINTERS 1.8 +#define CX_STORE_POINTERS 0 1.9 +#endif 1.10 + 1.11 /** Type for the UCX map. */ 1.12 typedef struct cx_map_s CxMap; 1.13
2.1 --- a/src/hash_map.c Tue Mar 21 17:18:29 2023 +0100 2.2 +++ b/src/hash_map.c Tue Mar 21 17:21:20 2023 +0100 2.3 @@ -432,7 +432,12 @@ 2.4 map->base.cl = &cx_hash_map_class; 2.5 map->base.allocator = allocator; 2.6 map->base.size = 0; 2.7 - map->base.itemsize = itemsize; 2.8 + 2.9 + if (itemsize > 0) { 2.10 + map->base.itemsize = itemsize; 2.11 + } else { 2.12 + cxMapStorePointers((CxMap *) map); 2.13 + } 2.14 2.15 return (CxMap *) map; 2.16 }
3.1 --- a/tests/test_map.cpp Tue Mar 21 17:18:29 2023 +0100 3.2 +++ b/tests/test_map.cpp Tue Mar 21 17:21:20 2023 +0100 3.3 @@ -135,6 +135,23 @@ 3.4 EXPECT_TRUE(allocator.verify()); 3.5 } 3.6 3.7 +TEST(CxHashMap, CreateForStoringPointers) { 3.8 + CxTestingAllocator allocator; 3.9 + auto map = cxHashMapCreate(&allocator, CX_STORE_POINTERS, 0); 3.10 + auto hmap = reinterpret_cast<struct cx_hash_map_s *>(map); 3.11 + EXPECT_GT(hmap->bucket_count, 0); 3.12 + cx_for_n(i, hmap->bucket_count) { 3.13 + EXPECT_EQ(hmap->buckets[i], nullptr); 3.14 + } 3.15 + EXPECT_EQ(map->size, 0); 3.16 + EXPECT_EQ(map->allocator, &allocator); 3.17 + EXPECT_TRUE(map->store_pointers); 3.18 + EXPECT_EQ(map->itemsize, sizeof(void *)); 3.19 + 3.20 + cxMapDestroy(map); 3.21 + EXPECT_TRUE(allocator.verify()); 3.22 +} 3.23 + 3.24 TEST(CxHashMap, BasicOperations) { 3.25 // create the map 3.26 CxTestingAllocator allocator;