git-quiltimport: add --keep-non-patch option
[gitweb.git] / mem-pool.c
index 1769400d2d567020bde81ca2d4dcba7ac6dd1516..a2841a4a9ad7e4871282031f1b18fd62f18e9638 100644 (file)
@@ -50,14 +50,19 @@ void mem_pool_init(struct mem_pool **mem_pool, size_t initial_size)
        *mem_pool = pool;
 }
 
-void mem_pool_discard(struct mem_pool *mem_pool)
+void mem_pool_discard(struct mem_pool *mem_pool, int invalidate_memory)
 {
        struct mp_block *block, *block_to_free;
 
-       while ((block = mem_pool->mp_block))
+       block = mem_pool->mp_block;
+       while (block)
        {
                block_to_free = block;
                block = block->next_block;
+
+               if (invalidate_memory)
+                       memset(block_to_free->space, 0xDD, ((char *)block_to_free->end) - ((char *)block_to_free->space));
+
                free(block_to_free);
        }
 
@@ -96,3 +101,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;
+}