mem-pool: fill out functionality

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>

Jameson Miller committed Jul 2, 2018 at 19:49 UTC 0e58301d8199208d1e48b9f64c4ad1089a355905
2 files changed +55
mem-pool.c
+42
@@ -96,3 +96,45 @@ void *mem_pool_calloc(struct mem_pool *mem_pool, size_t count, size_t size)
96 memset(r, 0, len);
97 return r;
98 }
99 +
100 +int mem_pool_contains(struct mem_pool *mem_pool, void *mem)
101 +{
102 + struct mp_block *p;
103 +
104 + /* Check if memory is allocated in a block */
105 + for (p = mem_pool->mp_block; p; p = p->next_block)
106 + if ((mem >= ((void *)p->space)) &&
107 + (mem < ((void *)p->end)))
108 + return 1;
109 +
110 + return 0;
111 +}
112 +
113 +void mem_pool_combine(struct mem_pool *dst, struct mem_pool *src)
114 +{
115 + struct mp_block *p;
116 +
117 + /* Append the blocks from src to dst */
118 + if (dst->mp_block && src->mp_block) {
119 + /*
120 + * src and dst have blocks, append
121 + * blocks from src to dst.
122 + */
123 + p = dst->mp_block;
124 + while (p->next_block)
125 + p = p->next_block;
126 +
127 + p->next_block = src->mp_block;
128 + } else if (src->mp_block) {
129 + /*
130 + * src has blocks, dst is empty.
131 + */
132 + dst->mp_block = src->mp_block;
133 + } else {
134 + /* src is empty, nothing to do. */
135 + }
136 +
137 + dst->pool_alloc += src->pool_alloc;
138 + src->pool_alloc = 0;
139 + src->mp_block = NULL;
140 +}
mem-pool.h
+13
@@ -41,4 +41,17 @@ void *mem_pool_alloc(struct mem_pool *pool, size_t len);
41 */
42 void *mem_pool_calloc(struct mem_pool *pool, size_t count, size_t size);
43
44 +/*
45 + * Move the memory associated with the 'src' pool to the 'dst' pool. The 'src'
46 + * pool will be empty and not contain any memory. It still needs to be free'd
47 + * with a call to `mem_pool_discard`.
48 + */
49 +void mem_pool_combine(struct mem_pool *dst, struct mem_pool *src);
50 +
51 +/*
52 + * Check if a memory pointed at by 'mem' is part of the range of
53 + * memory managed by the specified mem_pool.
54 + */
55 +int mem_pool_contains(struct mem_pool *mem_pool, void *mem);
56 +
57 #endif