@@ -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 ;
@@ -468,7 +468,7 @@ static size_t size_to_idx(disjoint_pool_t *pool, size_t size) {
468
468
// get the position of the leftmost set bit
469
469
size_t position = getLeftmostSetBitPos (size );
470
470
471
- bool is_power_of_2 = 0 == (size & ( size - 1 ) );
471
+ bool is_power_of_2 = IS_POWER_OF_2 (size );
472
472
bool larger_than_halfway_between_powers_of_2 =
473
473
!is_power_of_2 &&
474
474
(bool )((size - 1 ) & ((uint64_t )(1 ) << (position - 1 )));
@@ -630,8 +630,9 @@ umf_result_t disjoint_pool_initialize(umf_memory_provider_handle_t provider,
630
630
disjoint_pool -> buckets_num = 1 ;
631
631
size_t Size2 = Size1 + Size1 / 2 ;
632
632
size_t ts2 = Size2 , ts1 = Size1 ;
633
- for (; Size2 < CutOff ; Size1 *= 2 , Size2 *= 2 ) {
633
+ while ( Size2 < CutOff ) {
634
634
disjoint_pool -> buckets_num += 2 ;
635
+ Size2 *= 2 ;
635
636
}
636
637
disjoint_pool -> buckets = umf_ba_global_alloc (
637
638
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) {
767
768
return aligned_ptr ;
768
769
}
769
770
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
+
770
779
size_t disjoint_pool_malloc_usable_size (void * pool , void * ptr ) {
771
780
disjoint_pool_t * disjoint_pool = (disjoint_pool_t * )pool ;
772
781
if (ptr == NULL ) {
@@ -788,10 +797,8 @@ size_t disjoint_pool_malloc_usable_size(void *pool, void *ptr) {
788
797
}
789
798
// Get the unaligned pointer
790
799
// 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 );
795
802
796
803
ptrdiff_t diff = (ptrdiff_t )ptr - (ptrdiff_t )unaligned_ptr ;
797
804
@@ -847,10 +854,8 @@ umf_result_t disjoint_pool_free(void *pool, void *ptr) {
847
854
848
855
// Get the unaligned pointer
849
856
// 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 );
854
859
855
860
utils_annotate_memory_inaccessible (unaligned_ptr , bucket -> size );
856
861
bucket_free_chunk (bucket , unaligned_ptr , slab , & to_pool );
@@ -876,13 +881,11 @@ umf_result_t disjoint_pool_free(void *pool, void *ptr) {
876
881
877
882
umf_result_t disjoint_pool_get_last_allocation_error (void * pool ) {
878
883
(void )pool ;
879
-
880
884
return TLS_last_allocation_error ;
881
885
}
882
886
883
887
// Define destructor for use with unique_ptr
884
888
void disjoint_pool_finalize (void * pool ) {
885
-
886
889
disjoint_pool_t * hPool = (disjoint_pool_t * )pool ;
887
890
888
891
if (hPool -> params .pool_trace > 1 ) {
@@ -937,7 +940,7 @@ void umfDisjointPoolSharedLimitsDestroy(
937
940
938
941
umf_result_t
939
942
umfDisjointPoolParamsCreate (umf_disjoint_pool_params_handle_t * hParams ) {
940
- static const char * DEFAULT_NAME = "disjoint_pool" ;
943
+ static char * DEFAULT_NAME = "disjoint_pool" ;
941
944
942
945
if (!hParams ) {
943
946
LOG_ERR ("disjoint pool params handle is NULL" );
@@ -951,20 +954,16 @@ umfDisjointPoolParamsCreate(umf_disjoint_pool_params_handle_t *hParams) {
951
954
return UMF_RESULT_ERROR_OUT_OF_HOST_MEMORY ;
952
955
}
953
956
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
+ };
968
967
969
968
* hParams = params ;
970
969
@@ -975,7 +974,6 @@ umf_result_t
975
974
umfDisjointPoolParamsDestroy (umf_disjoint_pool_params_handle_t hParams ) {
976
975
// NOTE: dereferencing hParams when BA is already destroyed leads to crash
977
976
if (hParams && !umf_ba_global_is_destroyed ()) {
978
- umf_ba_global_free (hParams -> name );
979
977
umf_ba_global_free (hParams );
980
978
}
981
979
@@ -1067,15 +1065,6 @@ umfDisjointPoolParamsSetName(umf_disjoint_pool_params_handle_t hParams,
1067
1065
return UMF_RESULT_ERROR_INVALID_ARGUMENT ;
1068
1066
}
1069
1067
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 );
1080
1069
return UMF_RESULT_SUCCESS ;
1081
1070
}
0 commit comments