]> git.ipfire.org Git - thirdparty/git.git/commitdiff
mem-pool: fill out functionality
authorJameson Miller <jamill@microsoft.com>
Mon, 2 Jul 2018 19:49:35 +0000 (19:49 +0000)
committerJunio C Hamano <gitster@pobox.com>
Tue, 3 Jul 2018 17:58:27 +0000 (10:58 -0700)
Add functions for:

    - combining two memory pools

    - determining if a memory address is within the range managed by a
      memory pool

These functions will be used by future commits.

Signed-off-by: Jameson Miller <jamill@microsoft.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
mem-pool.c
mem-pool.h

index 1769400d2d567020bde81ca2d4dcba7ac6dd1516..b250a5fe40bb44eccdf053f49f8fd40a1ce10980 100644 (file)
@@ -96,3 +96,45 @@ void *mem_pool_calloc(struct mem_pool *mem_pool, size_t count, size_t size)
        memset(r, 0, len);
        return r;
 }
+
+int mem_pool_contains(struct mem_pool *mem_pool, void *mem)
+{
+       struct mp_block *p;
+
+       /* Check if memory is allocated in a block */
+       for (p = mem_pool->mp_block; p; p = p->next_block)
+               if ((mem >= ((void *)p->space)) &&
+                   (mem < ((void *)p->end)))
+                       return 1;
+
+       return 0;
+}
+
+void mem_pool_combine(struct mem_pool *dst, struct mem_pool *src)
+{
+       struct mp_block *p;
+
+       /* Append the blocks from src to dst */
+       if (dst->mp_block && src->mp_block) {
+               /*
+                * src and dst have blocks, append
+                * blocks from src to dst.
+                */
+               p = dst->mp_block;
+               while (p->next_block)
+                       p = p->next_block;
+
+               p->next_block = src->mp_block;
+       } else if (src->mp_block) {
+               /*
+                * src has blocks, dst is empty.
+                */
+               dst->mp_block = src->mp_block;
+       } else {
+               /* src is empty, nothing to do. */
+       }
+
+       dst->pool_alloc += src->pool_alloc;
+       src->pool_alloc = 0;
+       src->mp_block = NULL;
+}
index f75b3365d5c5d3e6f8b2b1601ef2abc4c54c1749..adeefdcb2863f95f2a98c05593466b756b85b73d 100644 (file)
@@ -41,4 +41,17 @@ void *mem_pool_alloc(struct mem_pool *pool, size_t len);
  */
 void *mem_pool_calloc(struct mem_pool *pool, size_t count, size_t size);
 
+/*
+ * Move the memory associated with the 'src' pool to the 'dst' pool. The 'src'
+ * pool will be empty and not contain any memory. It still needs to be free'd
+ * with a call to `mem_pool_discard`.
+ */
+void mem_pool_combine(struct mem_pool *dst, struct mem_pool *src);
+
+/*
+ * Check if a memory pointed at by 'mem' is part of the range of
+ * memory managed by the specified mem_pool.
+ */
+int mem_pool_contains(struct mem_pool *mem_pool, void *mem);
+
 #endif