@@ -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
6464static size_t bucket_slab_min_size (bucket_t * bucket ) {
6565 return bucket -> pool -> params .slab_min_size ;
@@ -457,7 +457,7 @@ static size_t size_to_idx(disjoint_pool_t *pool, size_t size) {
457457 // get the position of the leftmost set bit
458458 size_t position = utils_msb64 (size );
459459
460- bool is_power_of_2 = 0 == (size & ( size - 1 ) );
460+ bool is_power_of_2 = IS_POWER_OF_2 (size );
461461 bool larger_than_halfway_between_powers_of_2 =
462462 !is_power_of_2 &&
463463 (bool )((size - 1 ) & ((uint64_t )(1 ) << (position - 1 )));
@@ -625,8 +625,9 @@ umf_result_t disjoint_pool_initialize(umf_memory_provider_handle_t provider,
625625 disjoint_pool -> buckets_num = 1 ;
626626 size_t Size2 = Size1 + Size1 / 2 ;
627627 size_t ts2 = Size2 , ts1 = Size1 ;
628- for (; Size2 < CutOff ; Size1 *= 2 , Size2 *= 2 ) {
628+ while ( Size2 < CutOff ) {
629629 disjoint_pool -> buckets_num += 2 ;
630+ Size2 *= 2 ;
630631 }
631632
632633 disjoint_pool -> buckets = umf_ba_global_alloc (
@@ -792,6 +793,14 @@ void *disjoint_pool_aligned_malloc(void *pool, size_t size, size_t alignment) {
792793 return aligned_ptr ;
793794}
794795
796+ static size_t get_chunk_idx (void * ptr , slab_t * slab ) {
797+ return (((uintptr_t )ptr - (uintptr_t )slab -> mem_ptr ) / slab -> bucket -> size );
798+ }
799+
800+ static void * get_unaligned_ptr (size_t chunk_idx , slab_t * slab ) {
801+ return (void * )((uintptr_t )slab -> mem_ptr + chunk_idx * slab -> bucket -> size );
802+ }
803+
795804size_t disjoint_pool_malloc_usable_size (void * pool , void * ptr ) {
796805 disjoint_pool_t * disjoint_pool = (disjoint_pool_t * )pool ;
797806 if (ptr == NULL ) {
@@ -813,10 +822,8 @@ size_t disjoint_pool_malloc_usable_size(void *pool, void *ptr) {
813822 }
814823 // Get the unaligned pointer
815824 // NOTE: the base pointer slab->mem_ptr needn't to be aligned to bucket size
816- size_t chunk_idx =
817- (((uintptr_t )ptr - (uintptr_t )slab -> mem_ptr ) / slab -> bucket -> size );
818- void * unaligned_ptr =
819- (void * )((uintptr_t )slab -> mem_ptr + chunk_idx * slab -> bucket -> size );
825+ size_t chunk_idx = get_chunk_idx (ptr , slab );
826+ void * unaligned_ptr = get_unaligned_ptr (chunk_idx , slab );
820827
821828 ptrdiff_t diff = (ptrdiff_t )ptr - (ptrdiff_t )unaligned_ptr ;
822829
@@ -872,10 +879,8 @@ umf_result_t disjoint_pool_free(void *pool, void *ptr) {
872879
873880 // Get the unaligned pointer
874881 // NOTE: the base pointer slab->mem_ptr needn't to be aligned to bucket size
875- size_t chunk_idx =
876- (((uintptr_t )ptr - (uintptr_t )slab -> mem_ptr ) / slab -> bucket -> size );
877- void * unaligned_ptr =
878- (void * )((uintptr_t )slab -> mem_ptr + chunk_idx * slab -> bucket -> size );
882+ size_t chunk_idx = get_chunk_idx (ptr , slab );
883+ void * unaligned_ptr = get_unaligned_ptr (chunk_idx , slab );
879884
880885 utils_annotate_memory_inaccessible (unaligned_ptr , bucket -> size );
881886 bucket_free_chunk (bucket , unaligned_ptr , slab , & to_pool );
@@ -901,13 +906,11 @@ umf_result_t disjoint_pool_free(void *pool, void *ptr) {
901906
902907umf_result_t disjoint_pool_get_last_allocation_error (void * pool ) {
903908 (void )pool ;
904-
905909 return TLS_last_allocation_error ;
906910}
907911
908912// Define destructor for use with unique_ptr
909913void disjoint_pool_finalize (void * pool ) {
910-
911914 disjoint_pool_t * hPool = (disjoint_pool_t * )pool ;
912915
913916 if (hPool -> params .pool_trace > 1 ) {
@@ -962,7 +965,7 @@ void umfDisjointPoolSharedLimitsDestroy(
962965
963966umf_result_t
964967umfDisjointPoolParamsCreate (umf_disjoint_pool_params_handle_t * hParams ) {
965- static const char * DEFAULT_NAME = "disjoint_pool" ;
968+ static char * DEFAULT_NAME = "disjoint_pool" ;
966969
967970 if (!hParams ) {
968971 LOG_ERR ("disjoint pool params handle is NULL" );
@@ -976,20 +979,16 @@ umfDisjointPoolParamsCreate(umf_disjoint_pool_params_handle_t *hParams) {
976979 return UMF_RESULT_ERROR_OUT_OF_HOST_MEMORY ;
977980 }
978981
979- params -> slab_min_size = 0 ;
980- params -> max_poolable_size = 0 ;
981- params -> capacity = 0 ;
982- params -> min_bucket_size = UMF_DISJOINT_POOL_MIN_BUCKET_DEFAULT_SIZE ;
983- params -> cur_pool_size = 0 ;
984- params -> pool_trace = 0 ;
985- params -> shared_limits = NULL ;
986- params -> name = NULL ;
987-
988- umf_result_t ret = umfDisjointPoolParamsSetName (params , DEFAULT_NAME );
989- if (ret != UMF_RESULT_SUCCESS ) {
990- umf_ba_global_free (params );
991- return ret ;
992- }
982+ * params = (umf_disjoint_pool_params_t ){
983+ .slab_min_size = 0 ,
984+ .max_poolable_size = 0 ,
985+ .capacity = 0 ,
986+ .min_bucket_size = UMF_DISJOINT_POOL_MIN_BUCKET_DEFAULT_SIZE ,
987+ .cur_pool_size = 0 ,
988+ .pool_trace = 0 ,
989+ .shared_limits = NULL ,
990+ .name = {* DEFAULT_NAME },
991+ };
993992
994993 * hParams = params ;
995994
@@ -1000,7 +999,6 @@ umf_result_t
1000999umfDisjointPoolParamsDestroy (umf_disjoint_pool_params_handle_t hParams ) {
10011000 // NOTE: dereferencing hParams when BA is already destroyed leads to crash
10021001 if (hParams && !umf_ba_global_is_destroyed ()) {
1003- umf_ba_global_free (hParams -> name );
10041002 umf_ba_global_free (hParams );
10051003 }
10061004
@@ -1092,15 +1090,6 @@ umfDisjointPoolParamsSetName(umf_disjoint_pool_params_handle_t hParams,
10921090 return UMF_RESULT_ERROR_INVALID_ARGUMENT ;
10931091 }
10941092
1095- char * newName = umf_ba_global_alloc (sizeof (* newName ) * (strlen (name ) + 1 ));
1096- if (newName == NULL ) {
1097- LOG_ERR ("cannot allocate memory for disjoint pool name" );
1098- return UMF_RESULT_ERROR_OUT_OF_HOST_MEMORY ;
1099- }
1100-
1101- umf_ba_global_free (hParams -> name );
1102- hParams -> name = newName ;
1103- strcpy (hParams -> name , name );
1104-
1093+ strncpy (hParams -> name , name , sizeof (hParams -> name ) - 1 );
11051094 return UMF_RESULT_SUCCESS ;
11061095}
0 commit comments