src/mempool.c

changeset 1283
89935fea4b7c
parent 1281
45746a08c59e
--- a/src/mempool.c	Fri Apr 11 14:49:23 2025 +0200
+++ b/src/mempool.c	Fri Apr 11 15:12:20 2025 +0200
@@ -255,7 +255,7 @@
 
     // ensure enough capacity in the destination pool
     if (cx_mempool_ensure_capacity(dest, dest->size + source->size + 1)) {
-        return 1;
+        return 1; // LCOV_EXCL_LINE
     }
 
     // allocate a replacement allocator for the source pool
@@ -283,3 +283,35 @@
 
     return 0;
 }
+
+int cxMempoolTransferObject(
+        CxMempool *source,
+        CxMempool *dest,
+        const void *obj
+) {
+    // 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) {
+            // remove from the source pool
+            size_t last_index = source->size - 1;
+            if (i != last_index) {
+                source->data[i] = source->data[last_index];
+                source->data[last_index] = NULL;
+            }
+            source->size--;
+            // add to the target pool
+            dest->data[dest->size++] = mem;
+            return 0;
+        }
+    }
+    // not found
+    return 1;
+}

mercurial