Skip to content

Commit bcf8763

Browse files
authored
Merge pull request #371 from ldorau/Fix_make_umf_ba_linear_alloc0_always_return_NULL
Fix: make umf_ba_linear_alloc(0) always return NULL
2 parents 8cc5ec7 + b8e8bab commit bcf8763

File tree

2 files changed

+9
-12
lines changed

2 files changed

+9
-12
lines changed

src/base_alloc/base_alloc_linear.c

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,9 @@ umf_ba_linear_pool_t *umf_ba_linear_create(size_t pool_size) {
118118
}
119119

120120
void *umf_ba_linear_alloc(umf_ba_linear_pool_t *pool, size_t size) {
121+
if (size == 0) {
122+
return NULL;
123+
}
121124
size_t aligned_size = ALIGN_UP(size, MEMORY_ALIGNMENT);
122125
util_mutex_lock(&pool->metadata.lock);
123126
if (pool->metadata.size_left < aligned_size) {
@@ -248,14 +251,7 @@ void umf_ba_linear_destroy(umf_ba_linear_pool_t *pool) {
248251
if (pool->metadata.global_n_allocs) {
249252
fprintf(stderr, "umf_ba_linear_destroy(): global_n_allocs = %zu\n",
250253
pool->metadata.global_n_allocs);
251-
// This assert fails sporadically on Windows only.
252-
// TODO: fix this issue and uncomment this assert
253-
// It is safe to comment it out temporarily,
254-
// because the linear base allocator is used
255-
// in the proxy library only but umf_ba_linear_destroy()
256-
// is skipped, so this code is never run in real life.
257-
//
258-
// assert(pool->metadata.global_n_allocs == 0);
254+
assert(pool->metadata.global_n_allocs == 0);
259255
}
260256
#endif /* NDEBUG */
261257

test/test_base_alloc_linear.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,11 @@ TEST_F(test, baseAllocLinearMultiThreadedAllocMemset) {
7373
} buffer[ITERATIONS];
7474

7575
for (int i = 0; i < ITERATIONS; i++) {
76-
buffer[i].size =
77-
(size_t)((rand() / (double)RAND_MAX) * MAX_ALLOCATION_SIZE);
78-
buffer[i].ptr =
79-
(unsigned char *)umf_ba_linear_alloc(pool, buffer[i].size);
76+
// size must be greater than 0
77+
size_t size = (size_t)(1 + (MAX_ALLOCATION_SIZE - 1) *
78+
(rand() / (double)RAND_MAX));
79+
buffer[i].size = size;
80+
buffer[i].ptr = (unsigned char *)umf_ba_linear_alloc(pool, size);
8081
UT_ASSERTne(buffer[i].ptr, NULL);
8182
memset(buffer[i].ptr, (i + TID) & 0xFF, buffer[i].size);
8283
}

0 commit comments

Comments
 (0)