Skip to content

Commit 3e27c1e

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 eedb2ae commit 3e27c1e

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

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)