Skip to content

Commit 0255017

Browse files
committed
fix aligned chunk address calc in disjoint pool
1 parent a9ff7a8 commit 0255017

File tree

2 files changed

+19
-13
lines changed

2 files changed

+19
-13
lines changed

src/pool/pool_disjoint.c

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,6 @@ static slab_t *create_slab(bucket_t *bucket) {
9494
goto free_slab_chunks;
9595
}
9696

97-
// TODO
98-
// ASSERT_IS_ALIGNED((uintptr_t)slab->mem_ptr, bucket->size);
99-
10097
// raw allocation is not available for user so mark it as inaccessible
10198
utils_annotate_memory_inaccessible(slab->mem_ptr, slab->slab_size);
10299

@@ -175,10 +172,10 @@ static void slab_free_chunk(slab_t *slab, void *ptr) {
175172
// Make sure that we're in the right slab
176173
assert(ptr >= slab_get(slab) && ptr < slab_get_end(slab));
177174

178-
// Even if the pointer p was previously aligned, it's still inside the
179-
// corresponding chunk, so we get the correct index here.
180-
size_t chunk_idx =
181-
((uintptr_t)ptr - (uintptr_t)slab->mem_ptr) / slab->bucket->size;
175+
// Get the chunk index
176+
uintptr_t ptr_diff = (uintptr_t)ptr - (uintptr_t)slab->mem_ptr;
177+
assert((ptr_diff % slab->bucket->size) == 0);
178+
size_t chunk_idx = ptr_diff / slab->bucket->size;
182179

183180
// Make sure that the chunk was allocated
184181
assert(slab->chunks[chunk_idx] && "double free detected");
@@ -738,6 +735,10 @@ void *disjoint_pool_aligned_malloc(void *pool, size_t size, size_t alignment) {
738735
}
739736
}
740737

738+
void *aligned_ptr = (void *)ALIGN_UP_SAFE((size_t)ptr, alignment);
739+
VALGRIND_DO_MEMPOOL_ALLOC(disjoint_pool, aligned_ptr, size);
740+
utils_annotate_memory_undefined(aligned_ptr, size);
741+
741742
utils_mutex_unlock(&bucket->bucket_lock);
742743

743744
if (disjoint_pool->params.pool_trace > 2) {
@@ -746,9 +747,6 @@ void *disjoint_pool_aligned_malloc(void *pool, size_t size, size_t alignment) {
746747
(from_pool ? "pool" : "provider"), ptr);
747748
}
748749

749-
void *aligned_ptr = (void *)ALIGN_UP_SAFE((size_t)ptr, alignment);
750-
VALGRIND_DO_MEMPOOL_ALLOC(disjoint_pool, aligned_ptr, size);
751-
utils_annotate_memory_undefined(aligned_ptr, size);
752750
return aligned_ptr;
753751
}
754752

@@ -804,11 +802,18 @@ umf_result_t disjoint_pool_free(void *pool, void *ptr) {
804802

805803
bucket_t *bucket = slab->bucket;
806804

807-
VALGRIND_DO_MEMPOOL_FREE(pool, ptr);
808805
utils_mutex_lock(&bucket->bucket_lock);
806+
VALGRIND_DO_MEMPOOL_FREE(pool, ptr);
807+
808+
// Get the unaligned pointer
809+
// NOTE: the base pointer slab->mem_ptr needn't to be aligned to bucket size
810+
size_t chunk_idx =
811+
(((uintptr_t)ptr - (uintptr_t)slab->mem_ptr) / slab->bucket->size);
812+
void *unaligned_ptr =
813+
(void *)((uintptr_t)slab->mem_ptr + chunk_idx * slab->bucket->size);
809814

810-
utils_annotate_memory_inaccessible(ptr, bucket->size);
811-
bucket_free_chunk(bucket, ptr, slab, &to_pool);
815+
utils_annotate_memory_inaccessible(unaligned_ptr, bucket->size);
816+
bucket_free_chunk(bucket, unaligned_ptr, slab, &to_pool);
812817

813818
if (disjoint_pool->params.pool_trace > 1) {
814819
bucket->free_count++;

src/pool/pool_disjoint_internal.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <assert.h>
1212
#include <ctype.h>
1313
#include <errno.h>
14+
#include <math.h>
1415
#include <stdbool.h>
1516
#include <stdlib.h>
1617
#include <string.h>

0 commit comments

Comments
 (0)