Skip to content

Commit 9d6a776

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. Added also the baseAllocLinearPoolContainsPointer test. Signed-off-by: Lukasz Dorau <[email protected]>
1 parent f2b784a commit 9d6a776

File tree

3 files changed

+50
-0
lines changed

3 files changed

+50
-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
}

test/test_base_alloc_linear.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,24 @@ TEST_F(test, baseAllocLinearAllocMoreThanPoolSize) {
2727
memset(ptr, 0, new_size);
2828
}
2929

30+
TEST_F(test, baseAllocLinearPoolContainsPointer) {
31+
auto pool = std::shared_ptr<umf_ba_linear_pool_t>(
32+
umf_ba_linear_create(0 /* minimal pool size (page size) */),
33+
umf_ba_linear_destroy);
34+
35+
size_t size = 16;
36+
void *ptr = umf_ba_linear_alloc(pool.get(), size);
37+
UT_ASSERTne(ptr, NULL);
38+
memset(ptr, 0, size);
39+
40+
// assert pool contains pointer ptr
41+
UT_ASSERTne(umf_ba_linear_pool_contains_pointer(pool.get(), ptr), 0);
42+
43+
// assert pool does NOT contain pointer 0x0123
44+
UT_ASSERTeq(umf_ba_linear_pool_contains_pointer(pool.get(), (void *)0x0123),
45+
0);
46+
}
47+
3048
TEST_F(test, baseAllocLinearMultiThreadedAllocMemset) {
3149
static constexpr int NTHREADS = 10;
3250
static constexpr int ITERATIONS = 1000;

0 commit comments

Comments
 (0)