@@ -59,7 +59,7 @@ static __TLS umf_result_t TLS_last_allocation_error;
59
59
// The largest size which is allocated via the allocator.
60
60
// Allocations with size > CutOff bypass the pool and
61
61
// 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
63
63
64
64
static size_t bucket_slab_min_size (bucket_t * bucket ) {
65
65
return bucket -> pool -> params .slab_min_size ;
@@ -457,7 +457,7 @@ static size_t size_to_idx(disjoint_pool_t *pool, size_t size) {
457
457
// get the position of the leftmost set bit
458
458
size_t position = utils_msb64 (size );
459
459
460
- bool is_power_of_2 = 0 == (size & ( size - 1 ) );
460
+ bool is_power_of_2 = IS_POWER_OF_2 (size );
461
461
bool larger_than_halfway_between_powers_of_2 =
462
462
!is_power_of_2 &&
463
463
(bool )((size - 1 ) & ((uint64_t )(1 ) << (position - 1 )));
@@ -625,8 +625,9 @@ umf_result_t disjoint_pool_initialize(umf_memory_provider_handle_t provider,
625
625
disjoint_pool -> buckets_num = 1 ;
626
626
size_t Size2 = Size1 + Size1 / 2 ;
627
627
size_t ts2 = Size2 , ts1 = Size1 ;
628
- for (; Size2 < CutOff ; Size1 *= 2 , Size2 *= 2 ) {
628
+ while ( Size2 < CutOff ) {
629
629
disjoint_pool -> buckets_num += 2 ;
630
+ Size2 *= 2 ;
630
631
}
631
632
632
633
disjoint_pool -> buckets = umf_ba_global_alloc (
@@ -792,6 +793,14 @@ void *disjoint_pool_aligned_malloc(void *pool, size_t size, size_t alignment) {
792
793
return aligned_ptr ;
793
794
}
794
795
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
+
795
804
size_t disjoint_pool_malloc_usable_size (void * pool , void * ptr ) {
796
805
disjoint_pool_t * disjoint_pool = (disjoint_pool_t * )pool ;
797
806
if (ptr == NULL ) {
@@ -813,10 +822,8 @@ size_t disjoint_pool_malloc_usable_size(void *pool, void *ptr) {
813
822
}
814
823
// Get the unaligned pointer
815
824
// 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 );
820
827
821
828
ptrdiff_t diff = (ptrdiff_t )ptr - (ptrdiff_t )unaligned_ptr ;
822
829
@@ -872,10 +879,8 @@ umf_result_t disjoint_pool_free(void *pool, void *ptr) {
872
879
873
880
// Get the unaligned pointer
874
881
// 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 );
879
884
880
885
utils_annotate_memory_inaccessible (unaligned_ptr , bucket -> size );
881
886
bucket_free_chunk (bucket , unaligned_ptr , slab , & to_pool );
@@ -901,13 +906,11 @@ umf_result_t disjoint_pool_free(void *pool, void *ptr) {
901
906
902
907
umf_result_t disjoint_pool_get_last_allocation_error (void * pool ) {
903
908
(void )pool ;
904
-
905
909
return TLS_last_allocation_error ;
906
910
}
907
911
908
912
// Define destructor for use with unique_ptr
909
913
void disjoint_pool_finalize (void * pool ) {
910
-
911
914
disjoint_pool_t * hPool = (disjoint_pool_t * )pool ;
912
915
913
916
if (hPool -> params .pool_trace > 1 ) {
@@ -962,7 +965,7 @@ void umfDisjointPoolSharedLimitsDestroy(
962
965
963
966
umf_result_t
964
967
umfDisjointPoolParamsCreate (umf_disjoint_pool_params_handle_t * hParams ) {
965
- static const char * DEFAULT_NAME = "disjoint_pool" ;
968
+ static char * DEFAULT_NAME = "disjoint_pool" ;
966
969
967
970
if (!hParams ) {
968
971
LOG_ERR ("disjoint pool params handle is NULL" );
@@ -976,20 +979,16 @@ umfDisjointPoolParamsCreate(umf_disjoint_pool_params_handle_t *hParams) {
976
979
return UMF_RESULT_ERROR_OUT_OF_HOST_MEMORY ;
977
980
}
978
981
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
+ };
993
992
994
993
* hParams = params ;
995
994
@@ -1000,7 +999,6 @@ umf_result_t
1000
999
umfDisjointPoolParamsDestroy (umf_disjoint_pool_params_handle_t hParams ) {
1001
1000
// NOTE: dereferencing hParams when BA is already destroyed leads to crash
1002
1001
if (hParams && !umf_ba_global_is_destroyed ()) {
1003
- umf_ba_global_free (hParams -> name );
1004
1002
umf_ba_global_free (hParams );
1005
1003
}
1006
1004
@@ -1092,15 +1090,6 @@ umfDisjointPoolParamsSetName(umf_disjoint_pool_params_handle_t hParams,
1092
1090
return UMF_RESULT_ERROR_INVALID_ARGUMENT ;
1093
1091
}
1094
1092
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 );
1105
1094
return UMF_RESULT_SUCCESS ;
1106
1095
}
0 commit comments