Skip to content

Commit 0e58301

Browse files
jamillgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 158dfef commit 0e58301

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

mem-pool.c

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,3 +96,45 @@ void *mem_pool_calloc(struct mem_pool *mem_pool, size_t count, size_t size)
9696
memset(r, 0, len);
9797
return r;
9898
}
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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,17 @@ void *mem_pool_alloc(struct mem_pool *pool, size_t len);
4141
*/
4242
void *mem_pool_calloc(struct mem_pool *pool, size_t count, size_t size);
4343

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+
4457
#endif

0 commit comments

Comments
 (0)