Skip to content

Commit 57020cb

Browse files
committed
rebase
1 parent 940b2d4 commit 57020cb

File tree

4 files changed

+231
-64
lines changed

4 files changed

+231
-64
lines changed

src/pool/pool_disjoint.c

Lines changed: 191 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -231,8 +231,9 @@ void slab_unreg(slab_t *slab) {
231231
slab_unreg_by_addr(end_addr, slab);
232232
}
233233

234-
bucket_t *create_bucket(size_t sz, disjoint_pool_t *pool,
235-
umf_disjoint_pool_shared_limits_t *shared_limits) {
234+
bucket_t *
235+
create_bucket(size_t sz, disjoint_pool_t *pool,
236+
umf_disjoint_pool_shared_limits_handle_t shared_limits) {
236237

237238
bucket_t *bucket = (bucket_t *)umf_ba_global_alloc(sizeof(bucket_t));
238239
if (bucket == NULL) {
@@ -344,7 +345,7 @@ size_t bucket_slab_alloc_size(bucket_t *bucket) {
344345
}
345346

346347
size_t bucket_slab_min_size(bucket_t *bucket) {
347-
return bucket->pool->params.SlabMinSize;
348+
return bucket->pool->params.slab_min_size;
348349
}
349350

350351
slab_list_item_t *bucket_get_avail_full_slab(bucket_t *bucket,
@@ -439,12 +440,12 @@ size_t bucket_capacity(bucket_t *bucket) {
439440
if (bucket->size <= bucket_chunk_cut_off(bucket)) {
440441
return 1;
441442
} else {
442-
return bucket->pool->params.Capacity;
443+
return bucket->pool->params.capacity;
443444
}
444445
}
445446

446447
void bucket_update_stats(bucket_t *bucket, int in_use, int in_pool) {
447-
if (bucket->pool->params.PoolTrace == 0) {
448+
if (bucket->pool->params.pool_trace == 0) {
448449
return;
449450
}
450451

@@ -458,7 +459,7 @@ void bucket_update_stats(bucket_t *bucket, int in_use, int in_pool) {
458459

459460
// Increment or decrement current pool sizes based on whether
460461
// slab was added to or removed from pool.
461-
bucket->pool->params.CurPoolSize +=
462+
bucket->pool->params.cur_pool_size +=
462463
in_pool * bucket_slab_alloc_size(bucket);
463464
}
464465

@@ -595,8 +596,8 @@ static size_t size_to_idx(disjoint_pool_t *pool, size_t size) {
595596

596597
umf_disjoint_pool_shared_limits_t *
597598
disjoint_pool_get_limits(disjoint_pool_t *pool) {
598-
if (pool->params.SharedLimits) {
599-
return pool->params.SharedLimits;
599+
if (pool->params.shared_limits) {
600+
return pool->params.shared_limits;
600601
} else {
601602
return pool->default_shared_limits;
602603
}
@@ -644,29 +645,6 @@ void disjoint_pool_print_stats(disjoint_pool_t *pool, bool *title_printed,
644645
}
645646
}
646647

647-
// TODO remove
648-
static umf_result_t memoryProviderFree(umf_memory_provider_handle_t hProvider,
649-
void *ptr) {
650-
size_t size = 0;
651-
652-
if (ptr) {
653-
umf_alloc_info_t allocInfo = {NULL, 0, NULL};
654-
umf_result_t umf_result = umfMemoryTrackerGetAllocInfo(ptr, &allocInfo);
655-
if (umf_result == UMF_RESULT_SUCCESS) {
656-
size = allocInfo.baseSize;
657-
}
658-
}
659-
660-
umf_result_t ret = umfMemoryProviderFree(hProvider, ptr, size);
661-
if (ret != UMF_RESULT_SUCCESS) {
662-
663-
TLS_last_allocation_error = ret;
664-
// throw MemoryProviderError{ret};
665-
return ret;
666-
}
667-
return UMF_RESULT_SUCCESS;
668-
}
669-
670648
void *disjoint_pool_allocate(disjoint_pool_t *pool, size_t size,
671649
bool *from_pool) {
672650
if (size == 0) {
@@ -675,7 +653,7 @@ void *disjoint_pool_allocate(disjoint_pool_t *pool, size_t size,
675653

676654
void *ptr = NULL;
677655

678-
if (size > pool->params.MaxPoolableSize) {
656+
if (size > pool->params.max_poolable_size) {
679657
umf_result_t ret =
680658
umfMemoryProviderAlloc(pool->provider, size, 0, &ptr);
681659
if (ret != UMF_RESULT_SUCCESS) {
@@ -702,7 +680,7 @@ void *disjoint_pool_allocate(disjoint_pool_t *pool, size_t size,
702680
return NULL;
703681
}
704682

705-
if (pool->params.PoolTrace > 1) {
683+
if (pool->params.pool_trace > 1) {
706684
bucket_count_alloc(bucket, from_pool);
707685
}
708686

@@ -757,10 +735,11 @@ umf_result_t disjoint_pool_initialize(umf_memory_provider_handle_t provider,
757735
umf_disjoint_pool_params_t *dp_params =
758736
(umf_disjoint_pool_params_t *)params;
759737

760-
// MinBucketSize parameter must be a power of 2 for bucket sizes
738+
// min_bucket_size parameter must be a power of 2 for bucket sizes
761739
// to generate correctly.
762-
if (!dp_params->MinBucketSize ||
763-
!((dp_params->MinBucketSize & (dp_params->MinBucketSize - 1)) == 0)) {
740+
if (!dp_params->min_bucket_size ||
741+
!((dp_params->min_bucket_size & (dp_params->min_bucket_size - 1)) ==
742+
0)) {
764743
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
765744
}
766745

@@ -774,15 +753,15 @@ umf_result_t disjoint_pool_initialize(umf_memory_provider_handle_t provider,
774753

775754
// Generate buckets sized such as: 64, 96, 128, 192, ..., CutOff.
776755
// Powers of 2 and the value halfway between the powers of 2.
777-
size_t Size1 = disjoint_pool->params.MinBucketSize;
756+
size_t Size1 = disjoint_pool->params.min_bucket_size;
778757

779-
// MinBucketSize cannot be larger than CutOff.
758+
// min_bucket_size cannot be larger than CutOff.
780759
Size1 = utils_min(Size1, CutOff);
781760

782761
// Buckets sized smaller than the bucket default size- 8 aren't needed.
783762
Size1 = utils_max(Size1, UMF_DISJOINT_POOL_MIN_BUCKET_DEFAULT_SIZE);
784763

785-
// Calculate the exponent for MinBucketSize used for finding buckets.
764+
// Calculate the exponent for min_bucket_size used for finding buckets.
786765
disjoint_pool->min_bucket_size_exp = (size_t)log2Utils(Size1);
787766
disjoint_pool->default_shared_limits =
788767
umfDisjointPoolSharedLimitsCreate(SIZE_MAX);
@@ -829,8 +808,8 @@ void *disjoint_pool_malloc(void *pool, size_t size) {
829808
bool from_pool;
830809
void *ptr = disjoint_pool_allocate(hPool, size, &from_pool);
831810

832-
if (hPool->params.PoolTrace > 2) {
833-
const char *MT = hPool->params.Name;
811+
if (hPool->params.pool_trace > 2) {
812+
const char *MT = hPool->params.name;
834813
(void)MT;
835814
//std::cout << "Allocated " << std::setw(8) << size << " " << MT
836815
// << " bytes from " << (FromPool ? "Pool" : "Provider") << " ->"
@@ -890,7 +869,7 @@ void *disjoint_pool_aligned_malloc(void *pool, size_t size, size_t alignment) {
890869
// Check if requested allocation size is within pooling limit.
891870
// If not, just request aligned pointer from the system.
892871
from_pool = false;
893-
if (aligned_size > disjoint_pool->params.MaxPoolableSize) {
872+
if (aligned_size > disjoint_pool->params.max_poolable_size) {
894873

895874
umf_result_t ret = umfMemoryProviderAlloc(disjoint_pool->provider, size,
896875
alignment, &ptr);
@@ -913,7 +892,7 @@ void *disjoint_pool_aligned_malloc(void *pool, size_t size, size_t alignment) {
913892
}
914893

915894
assert(ptr);
916-
if (disjoint_pool->params.PoolTrace > 1) {
895+
if (disjoint_pool->params.pool_trace > 1) {
917896
bucket_count_alloc(bucket, from_pool);
918897
}
919898

@@ -922,8 +901,8 @@ void *disjoint_pool_aligned_malloc(void *pool, size_t size, size_t alignment) {
922901
annotate_memory_undefined((void *)ALIGN_UP((size_t)ptr, alignment), size);
923902
return (void *)ALIGN_UP((size_t)ptr, alignment);
924903

925-
if (disjoint_pool->params.PoolTrace > 2) {
926-
const char *MT = disjoint_pool->params.Name;
904+
if (disjoint_pool->params.pool_trace > 2) {
905+
const char *MT = disjoint_pool->params.name;
927906
(void)MT;
928907
//std::cout << "Allocated " << std::setw(8) << size << " " << MT
929908
// << " bytes aligned at " << alignment << " from "
@@ -949,17 +928,33 @@ umf_result_t disjoint_pool_free(void *pool, void *ptr) {
949928
}
950929

951930
void *slab_ptr =
952-
(void *)ALIGN_DOWN((size_t)ptr, disjoint_pool->params.SlabMinSize);
931+
(void *)ALIGN_DOWN((size_t)ptr, disjoint_pool->params.slab_min_size);
953932

954933
// Lock the map on read
955934
utils_mutex_lock(&disjoint_pool->known_slabs_map_lock);
956935

957936
slab_t *slab =
958937
(slab_t *)critnib_get(disjoint_pool->known_slabs, (uintptr_t)slab_ptr);
959-
//auto Slabs = getKnownSlabs().equal_range(SlabPtr);
938+
939+
// check if given pointer is allocated inside any Disjoint Pool slab
960940
if (slab == NULL) {
961941
utils_mutex_unlock(&disjoint_pool->known_slabs_map_lock);
962-
umf_result_t ret = memoryProviderFree(disjoint_pool->provider, ptr);
942+
943+
// regular free
944+
umf_alloc_info_t allocInfo = {NULL, 0, NULL};
945+
umf_result_t ret = umfMemoryTrackerGetAllocInfo(ptr, &allocInfo);
946+
if (ret != UMF_RESULT_SUCCESS) {
947+
TLS_last_allocation_error = ret;
948+
return ret;
949+
}
950+
951+
size_t size = allocInfo.baseSize;
952+
umf_memory_provider_handle_t provider = disjoint_pool->provider;
953+
ret = umfMemoryProviderFree(provider, ptr, size);
954+
if (ret != UMF_RESULT_SUCCESS) {
955+
TLS_last_allocation_error = ret;
956+
}
957+
963958
return ret;
964959
}
965960

@@ -975,7 +970,7 @@ umf_result_t disjoint_pool_free(void *pool, void *ptr) {
975970
utils_mutex_unlock(&disjoint_pool->known_slabs_map_lock);
976971
bucket_t *bucket = slab->bucket;
977972

978-
if (disjoint_pool->params.PoolTrace > 1) {
973+
if (disjoint_pool->params.pool_trace > 1) {
979974
bucket->free_count++;
980975
}
981976

@@ -994,14 +989,14 @@ umf_result_t disjoint_pool_free(void *pool, void *ptr) {
994989
/*
995990
if (ret == UMF_RESULT_SUCCESS) {
996991
997-
if (impl->getParams().PoolTrace > 2) {
992+
if (impl->getParams().pool_trace > 2) {
998993
auto MT = impl->getParams().Name;
999994
std::cout << "Freed " << MT << " " << ptr << " to "
1000995
<< (ToPool ? "Pool" : "Provider")
1001996
<< ", Current total pool size "
1002997
<< impl->getLimits()->TotalSize.load()
1003998
<< ", Current pool size for " << MT << " "
1004-
<< impl->getParams().CurPoolSize << "\n";
999+
<< impl->getParams().cur_pool_size << "\n";
10051000
}
10061001
}*/
10071002

@@ -1037,7 +1032,7 @@ void disjoint_pool_finalize(void *pool) {
10371032
umf_ba_global_free(hPool);
10381033

10391034
/*
1040-
if (impl->getParams().PoolTrace > 1) {
1035+
if (impl->getParams().pool_trace > 1) {
10411036
bool TitlePrinted = false;
10421037
size_t HighBucketSize;
10431038
size_t HighPeakSlabsInUse;
@@ -1089,3 +1084,147 @@ void umfDisjointPoolSharedLimitsDestroy(
10891084
umf_disjoint_pool_shared_limits_t *limits) {
10901085
umf_ba_global_free(limits);
10911086
}
1087+
1088+
umf_result_t
1089+
umfDisjointPoolParamsCreate(umf_disjoint_pool_params_handle_t *hParams) {
1090+
static const char *DEFAULT_NAME = "disjoint_pool";
1091+
1092+
if (!hParams) {
1093+
LOG_ERR("disjoint pool params handle is NULL");
1094+
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
1095+
}
1096+
1097+
umf_disjoint_pool_params_handle_t params =
1098+
umf_ba_global_alloc(sizeof(umf_disjoint_pool_params_t));
1099+
if (params == NULL) {
1100+
LOG_ERR("cannot allocate memory for disjoint pool params");
1101+
return UMF_RESULT_ERROR_OUT_OF_HOST_MEMORY;
1102+
}
1103+
1104+
params->slab_min_size = 0;
1105+
params->max_poolable_size = 0;
1106+
params->capacity = 0;
1107+
params->min_bucket_size = UMF_DISJOINT_POOL_MIN_BUCKET_DEFAULT_SIZE;
1108+
params->cur_pool_size = 0;
1109+
params->pool_trace = 0;
1110+
params->shared_limits = NULL;
1111+
params->name = NULL;
1112+
1113+
umf_result_t ret = umfDisjointPoolParamsSetName(params, DEFAULT_NAME);
1114+
if (ret != UMF_RESULT_SUCCESS) {
1115+
umf_ba_global_free(params);
1116+
return ret;
1117+
}
1118+
1119+
*hParams = params;
1120+
1121+
return UMF_RESULT_SUCCESS;
1122+
}
1123+
1124+
umf_result_t
1125+
umfDisjointPoolParamsDestroy(umf_disjoint_pool_params_handle_t hParams) {
1126+
if (hParams) {
1127+
umf_ba_global_free(hParams->name);
1128+
umf_ba_global_free(hParams);
1129+
}
1130+
1131+
return UMF_RESULT_SUCCESS;
1132+
}
1133+
1134+
umf_result_t
1135+
umfDisjointPoolParamsSetSlabMinSize(umf_disjoint_pool_params_handle_t hParams,
1136+
size_t slabMinSize) {
1137+
if (!hParams) {
1138+
LOG_ERR("disjoint pool params handle is NULL");
1139+
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
1140+
}
1141+
1142+
hParams->slab_min_size = slabMinSize;
1143+
return UMF_RESULT_SUCCESS;
1144+
}
1145+
1146+
umf_result_t umfDisjointPoolParamsSetMaxPoolableSize(
1147+
umf_disjoint_pool_params_handle_t hParams, size_t maxPoolableSize) {
1148+
if (!hParams) {
1149+
LOG_ERR("disjoint pool params handle is NULL");
1150+
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
1151+
}
1152+
1153+
hParams->max_poolable_size = maxPoolableSize;
1154+
return UMF_RESULT_SUCCESS;
1155+
}
1156+
1157+
umf_result_t
1158+
umfDisjointPoolParamsSetCapacity(umf_disjoint_pool_params_handle_t hParams,
1159+
size_t maxCapacity) {
1160+
if (!hParams) {
1161+
LOG_ERR("disjoint pool params handle is NULL");
1162+
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
1163+
}
1164+
1165+
hParams->capacity = maxCapacity;
1166+
return UMF_RESULT_SUCCESS;
1167+
}
1168+
1169+
umf_result_t
1170+
umfDisjointPoolParamsSetMinBucketSize(umf_disjoint_pool_params_handle_t hParams,
1171+
size_t minBucketSize) {
1172+
if (!hParams) {
1173+
LOG_ERR("disjoint pool params handle is NULL");
1174+
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
1175+
}
1176+
1177+
// minBucketSize parameter must be a power of 2 and greater than 0.
1178+
if (minBucketSize == 0 || (minBucketSize & (minBucketSize - 1))) {
1179+
LOG_ERR("minBucketSize must be a power of 2 and greater than 0");
1180+
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
1181+
}
1182+
1183+
hParams->min_bucket_size = minBucketSize;
1184+
return UMF_RESULT_SUCCESS;
1185+
}
1186+
1187+
umf_result_t
1188+
umfDisjointPoolParamsSetTrace(umf_disjoint_pool_params_handle_t hParams,
1189+
int poolTrace) {
1190+
if (!hParams) {
1191+
LOG_ERR("disjoint pool params handle is NULL");
1192+
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
1193+
}
1194+
1195+
hParams->pool_trace = poolTrace;
1196+
return UMF_RESULT_SUCCESS;
1197+
}
1198+
1199+
umf_result_t umfDisjointPoolParamsSetSharedLimits(
1200+
umf_disjoint_pool_params_handle_t hParams,
1201+
umf_disjoint_pool_shared_limits_handle_t hSharedLimits) {
1202+
if (!hParams) {
1203+
LOG_ERR("disjoint pool params handle is NULL");
1204+
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
1205+
}
1206+
1207+
hParams->shared_limits = hSharedLimits;
1208+
return UMF_RESULT_SUCCESS;
1209+
}
1210+
1211+
umf_result_t
1212+
umfDisjointPoolParamsSetName(umf_disjoint_pool_params_handle_t hParams,
1213+
const char *name) {
1214+
if (!hParams) {
1215+
LOG_ERR("disjoint pool params handle is NULL");
1216+
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
1217+
}
1218+
1219+
char *newName = umf_ba_global_alloc(sizeof(char) * (strlen(name) + 1));
1220+
if (newName == NULL) {
1221+
LOG_ERR("cannot allocate memory for disjoint pool name");
1222+
return UMF_RESULT_ERROR_OUT_OF_HOST_MEMORY;
1223+
}
1224+
1225+
umf_ba_global_free(hParams->name);
1226+
hParams->name = newName;
1227+
strcpy(hParams->name, name);
1228+
1229+
return UMF_RESULT_SUCCESS;
1230+
}

0 commit comments

Comments
 (0)