add CX_STORE_POINTERS special item size for maps

Tue, 21 Mar 2023 17:21:20 +0100

author
Mike Becker <universe@uap-core.de>
date
Tue, 21 Mar 2023 17:21:20 +0100
changeset 668
d7129285ac32
parent 667
2f88a7c13a28
child 669
dce9b8450656

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
--- 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;
 
--- 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;
 }
--- 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<struct cx_hash_map_s *>(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;

mercurial