Skip to content

Commit 5d4f6c7

Browse files
committed
Add umf_ba_linear_pool_contains_pointer
Add umf_ba_linear_pool_contains_pointer(). It returns: - 0 if ptr does not belong to the pool or - size (> 0) of the memory region from ptr to the end of the pool if ptr belongs to the pool. It will be useful to implement realloc() in the proxy library. Signed-off-by: Lukasz Dorau <[email protected]>
1 parent 317521a commit 5d4f6c7

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

src/base_alloc/base_alloc_linear.c

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,3 +175,33 @@ void umf_ba_linear_destroy(umf_ba_linear_pool_t *pool) {
175175
util_mutex_destroy_not_free(&pool->metadata.lock);
176176
ba_os_free(pool, pool->metadata.pool_size);
177177
}
178+
179+
// umf_ba_linear_pool_contains_pointer() returns:
180+
// - 0 if ptr does not belong to the pool or
181+
// - size (> 0) of the memory region from ptr
182+
// to the end of the pool if ptr belongs to the pool
183+
size_t umf_ba_linear_pool_contains_pointer(umf_ba_linear_pool_t *pool,
184+
void *ptr) {
185+
util_mutex_lock(&pool->metadata.lock);
186+
char *cptr = (char *)ptr;
187+
if (cptr >= pool->data &&
188+
cptr < ((char *)(pool)) + pool->metadata.pool_size) {
189+
size_t size = ((char *)(pool)) + pool->metadata.pool_size - cptr;
190+
util_mutex_unlock(&pool->metadata.lock);
191+
return size;
192+
}
193+
194+
umf_ba_next_linear_pool_t *next_pool = pool->next_pool;
195+
while (next_pool) {
196+
if (cptr >= next_pool->data &&
197+
cptr < ((char *)(next_pool)) + next_pool->pool_size) {
198+
size_t size = ((char *)(next_pool)) + next_pool->pool_size - cptr;
199+
util_mutex_unlock(&pool->metadata.lock);
200+
return size;
201+
}
202+
next_pool = next_pool->next_pool;
203+
}
204+
205+
util_mutex_unlock(&pool->metadata.lock);
206+
return 0;
207+
}

src/base_alloc/base_alloc_linear.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ typedef struct umf_ba_linear_pool umf_ba_linear_pool_t;
2626
umf_ba_linear_pool_t *umf_ba_linear_create(size_t pool_size);
2727
void *umf_ba_linear_alloc(umf_ba_linear_pool_t *pool, size_t size);
2828
void umf_ba_linear_destroy(umf_ba_linear_pool_t *pool);
29+
size_t umf_ba_linear_pool_contains_pointer(umf_ba_linear_pool_t *pool,
30+
void *ptr);
2931

3032
#ifdef __cplusplus
3133
}

0 commit comments

Comments
 (0)