src/mempool.c

changeset 1325
20caf6efaf07
parent 1324
399f7bb81d11
--- a/src/mempool.c	Thu May 22 21:00:33 2025 +0200
+++ b/src/mempool.c	Thu May 22 22:22:14 2025 +0200
@@ -564,6 +564,15 @@
     return pool;
 }
 
+void cxMempoolGlobalDestructor(CxMempool *pool, cx_destructor_func fnc) {
+    pool->destr = fnc;
+}
+
+void cxMempoolGlobalDestructor2(CxMempool *pool, cx_destructor_func2 fnc, void *data) {
+    pool->destr2 = fnc;
+    pool->destr2_data = data;
+}
+
 static void cx_mempool_free_transferred_allocator(void *al) {
     cxFreeDefault(al);
 }
@@ -628,14 +637,14 @@
     // safety check
     if (source == dest) return 1;
 
-    // first, make sure that the dest pool can take the object
-    if (cx_mempool_ensure_capacity(dest, dest->size + 1)) {
-        return 1; // LCOV_EXCL_LINE
-    }
     // search for the object
     for (size_t i = 0; i < source->size; i++) {
         struct cx_mempool_memory_s *mem = source->data[i];
         if (mem->c == obj) {
+            // first, make sure that the dest pool can take the object
+            if (cx_mempool_ensure_capacity(dest, dest->size + 1)) {
+                return 1; // LCOV_EXCL_LINE
+            }
             // remove from the source pool
             size_t last_index = source->size - 1;
             if (i != last_index) {
@@ -648,6 +657,27 @@
             return 0;
         }
     }
+    // search in the registered objects
+    for (size_t i = 0; i < source->registered_size; i++) {
+        struct cx_mempool_foreign_memory_s *mem = &source->registered[i];
+        if (mem->mem == obj) {
+            // first, make sure that the dest pool can take the object
+            if (cx_mempool_ensure_registered_capacity(dest,
+                    dest->registered_size + 1)) {
+                return 1; // LCOV_EXCL_LINE
+            }
+            dest->registered[dest->registered_size++] = *mem;
+            // remove from the source pool
+            size_t last_index = source->registered_size - 1;
+            if (i != last_index) {
+                source->registered[i] = source->registered[last_index];
+                memset(&source->registered[last_index], 0,
+                    sizeof(struct cx_mempool_foreign_memory_s));
+            }
+            source->registered_size--;
+            return 0;
+        }
+    }
     // not found
     return 1;
 }

mercurial