Skip to content

Commit f0fa06f

Browse files
committed
[disjoint] Change pool name setting and cleanup
1 parent 2ed7caf commit f0fa06f

File tree

3 files changed

+30
-41
lines changed

3 files changed

+30
-41
lines changed

include/umf/pools/pool_disjoint.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ umf_result_t umfDisjointPoolParamsSetSharedLimits(
100100

101101
/// @brief Set custom name of the disjoint pool to be used in the traces.
102102
/// @param hParams handle to the parameters of the disjoint pool.
103-
/// @param name custom name of the pool.
103+
/// @param name custom name of the pool. Name longer than 64 characters will be truncated.
104104
/// @return UMF_RESULT_SUCCESS on success or appropriate error code on failure.
105105
umf_result_t
106106
umfDisjointPoolParamsSetName(umf_disjoint_pool_params_handle_t hParams,

src/pool/pool_disjoint.c

Lines changed: 28 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ static __TLS umf_result_t TLS_last_allocation_error;
5959
// The largest size which is allocated via the allocator.
6060
// Allocations with size > CutOff bypass the pool and
6161
// go directly to the provider.
62-
static size_t CutOff = (size_t)1 << 31; // 2GB
62+
static const size_t CutOff = (size_t)1 << 31; // 2GB
6363

6464
static size_t bucket_slab_min_size(bucket_t *bucket) {
6565
return bucket->pool->params.slab_min_size;
@@ -468,7 +468,7 @@ static size_t size_to_idx(disjoint_pool_t *pool, size_t size) {
468468
// get the position of the leftmost set bit
469469
size_t position = getLeftmostSetBitPos(size);
470470

471-
bool is_power_of_2 = 0 == (size & (size - 1));
471+
bool is_power_of_2 = IS_POWER_OF_2(size);
472472
bool larger_than_halfway_between_powers_of_2 =
473473
!is_power_of_2 &&
474474
(bool)((size - 1) & ((uint64_t)(1) << (position - 1)));
@@ -630,8 +630,9 @@ umf_result_t disjoint_pool_initialize(umf_memory_provider_handle_t provider,
630630
disjoint_pool->buckets_num = 1;
631631
size_t Size2 = Size1 + Size1 / 2;
632632
size_t ts2 = Size2, ts1 = Size1;
633-
for (; Size2 < CutOff; Size1 *= 2, Size2 *= 2) {
633+
while (Size2 < CutOff) {
634634
disjoint_pool->buckets_num += 2;
635+
Size2 *= 2;
635636
}
636637
disjoint_pool->buckets = umf_ba_global_alloc(
637638
sizeof(*disjoint_pool->buckets) * disjoint_pool->buckets_num);
@@ -767,6 +768,14 @@ void *disjoint_pool_aligned_malloc(void *pool, size_t size, size_t alignment) {
767768
return aligned_ptr;
768769
}
769770

771+
static size_t get_chunk_idx(void *ptr, slab_t *slab) {
772+
return (((uintptr_t)ptr - (uintptr_t)slab->mem_ptr) / slab->bucket->size);
773+
}
774+
775+
static void *get_unaligned_ptr(size_t chunk_idx, slab_t *slab) {
776+
return (void *)((uintptr_t)slab->mem_ptr + chunk_idx * slab->bucket->size);
777+
}
778+
770779
size_t disjoint_pool_malloc_usable_size(void *pool, void *ptr) {
771780
disjoint_pool_t *disjoint_pool = (disjoint_pool_t *)pool;
772781
if (ptr == NULL) {
@@ -788,10 +797,8 @@ size_t disjoint_pool_malloc_usable_size(void *pool, void *ptr) {
788797
}
789798
// Get the unaligned pointer
790799
// NOTE: the base pointer slab->mem_ptr needn't to be aligned to bucket size
791-
size_t chunk_idx =
792-
(((uintptr_t)ptr - (uintptr_t)slab->mem_ptr) / slab->bucket->size);
793-
void *unaligned_ptr =
794-
(void *)((uintptr_t)slab->mem_ptr + chunk_idx * slab->bucket->size);
800+
size_t chunk_idx = get_chunk_idx(ptr, slab);
801+
void *unaligned_ptr = get_unaligned_ptr(chunk_idx, slab);
795802

796803
ptrdiff_t diff = (ptrdiff_t)ptr - (ptrdiff_t)unaligned_ptr;
797804

@@ -847,10 +854,8 @@ umf_result_t disjoint_pool_free(void *pool, void *ptr) {
847854

848855
// Get the unaligned pointer
849856
// NOTE: the base pointer slab->mem_ptr needn't to be aligned to bucket size
850-
size_t chunk_idx =
851-
(((uintptr_t)ptr - (uintptr_t)slab->mem_ptr) / slab->bucket->size);
852-
void *unaligned_ptr =
853-
(void *)((uintptr_t)slab->mem_ptr + chunk_idx * slab->bucket->size);
857+
size_t chunk_idx = get_chunk_idx(ptr, slab);
858+
void *unaligned_ptr = get_unaligned_ptr(chunk_idx, slab);
854859

855860
utils_annotate_memory_inaccessible(unaligned_ptr, bucket->size);
856861
bucket_free_chunk(bucket, unaligned_ptr, slab, &to_pool);
@@ -876,13 +881,11 @@ umf_result_t disjoint_pool_free(void *pool, void *ptr) {
876881

877882
umf_result_t disjoint_pool_get_last_allocation_error(void *pool) {
878883
(void)pool;
879-
880884
return TLS_last_allocation_error;
881885
}
882886

883887
// Define destructor for use with unique_ptr
884888
void disjoint_pool_finalize(void *pool) {
885-
886889
disjoint_pool_t *hPool = (disjoint_pool_t *)pool;
887890

888891
if (hPool->params.pool_trace > 1) {
@@ -937,7 +940,7 @@ void umfDisjointPoolSharedLimitsDestroy(
937940

938941
umf_result_t
939942
umfDisjointPoolParamsCreate(umf_disjoint_pool_params_handle_t *hParams) {
940-
static const char *DEFAULT_NAME = "disjoint_pool";
943+
static char *DEFAULT_NAME = "disjoint_pool";
941944

942945
if (!hParams) {
943946
LOG_ERR("disjoint pool params handle is NULL");
@@ -951,20 +954,16 @@ umfDisjointPoolParamsCreate(umf_disjoint_pool_params_handle_t *hParams) {
951954
return UMF_RESULT_ERROR_OUT_OF_HOST_MEMORY;
952955
}
953956

954-
params->slab_min_size = 0;
955-
params->max_poolable_size = 0;
956-
params->capacity = 0;
957-
params->min_bucket_size = UMF_DISJOINT_POOL_MIN_BUCKET_DEFAULT_SIZE;
958-
params->cur_pool_size = 0;
959-
params->pool_trace = 0;
960-
params->shared_limits = NULL;
961-
params->name = NULL;
962-
963-
umf_result_t ret = umfDisjointPoolParamsSetName(params, DEFAULT_NAME);
964-
if (ret != UMF_RESULT_SUCCESS) {
965-
umf_ba_global_free(params);
966-
return ret;
967-
}
957+
*params = (umf_disjoint_pool_params_t){
958+
.slab_min_size = 0,
959+
.max_poolable_size = 0,
960+
.capacity = 0,
961+
.min_bucket_size = UMF_DISJOINT_POOL_MIN_BUCKET_DEFAULT_SIZE,
962+
.cur_pool_size = 0,
963+
.pool_trace = 0,
964+
.shared_limits = NULL,
965+
.name = {*DEFAULT_NAME},
966+
};
968967

969968
*hParams = params;
970969

@@ -975,7 +974,6 @@ umf_result_t
975974
umfDisjointPoolParamsDestroy(umf_disjoint_pool_params_handle_t hParams) {
976975
// NOTE: dereferencing hParams when BA is already destroyed leads to crash
977976
if (hParams && !umf_ba_global_is_destroyed()) {
978-
umf_ba_global_free(hParams->name);
979977
umf_ba_global_free(hParams);
980978
}
981979

@@ -1067,15 +1065,6 @@ umfDisjointPoolParamsSetName(umf_disjoint_pool_params_handle_t hParams,
10671065
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
10681066
}
10691067

1070-
char *newName = umf_ba_global_alloc(sizeof(*newName) * (strlen(name) + 1));
1071-
if (newName == NULL) {
1072-
LOG_ERR("cannot allocate memory for disjoint pool name");
1073-
return UMF_RESULT_ERROR_OUT_OF_HOST_MEMORY;
1074-
}
1075-
1076-
umf_ba_global_free(hParams->name);
1077-
hParams->name = newName;
1078-
strcpy(hParams->name, name);
1079-
1068+
strncpy(hParams->name, name, sizeof(hParams->name) - 1);
10801069
return UMF_RESULT_SUCCESS;
10811070
}

src/pool/pool_disjoint_internal.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ typedef struct umf_disjoint_pool_params_t {
131131
umf_disjoint_pool_shared_limits_handle_t shared_limits;
132132

133133
// Name used in traces
134-
char *name;
134+
char name[64];
135135
} umf_disjoint_pool_params_t;
136136

137137
typedef struct disjoint_pool_t {

0 commit comments

Comments
 (0)