Skip to content

Commit 6c576c1

Browse files
authored
Merge pull request #1254 from ldorau/Simplify_and_improve_trackingAllocationSplit
Simplify and improve `trackingAllocationSplit()`
2 parents 6102b8f + 6cd4759 commit 6c576c1

File tree

1 file changed

+10
-28
lines changed

1 file changed

+10
-28
lines changed

src/provider/provider_tracking.c

+10-28
Original file line numberDiff line numberDiff line change
@@ -506,16 +506,6 @@ static umf_result_t trackingAllocationSplit(void *hProvider, void *ptr,
506506
tracker_alloc_info_t *parent_value = NULL;
507507
uintptr_t parent_key = 0;
508508

509-
tracker_alloc_info_t *splitValue =
510-
umf_ba_alloc(provider->hTracker->alloc_info_allocator);
511-
if (!splitValue) {
512-
return UMF_RESULT_ERROR_OUT_OF_HOST_MEMORY;
513-
}
514-
515-
splitValue->pool = provider->pool;
516-
splitValue->size = firstSize;
517-
splitValue->n_children = 0;
518-
519509
int r = utils_mutex_lock(&provider->hTracker->splitMergeMutex);
520510
if (r) {
521511
goto err_lock;
@@ -547,17 +537,11 @@ static umf_result_t trackingAllocationSplit(void *hProvider, void *ptr,
547537
goto err;
548538
}
549539

550-
assert(level < MAX_LEVELS_OF_ALLOC_SEGMENT_MAP);
551-
int cret =
552-
critnib_insert(provider->hTracker->alloc_segments_map[level],
553-
(uintptr_t)ptr, (void *)splitValue, 1 /* update */);
554-
// this cannot fail since we know the element exists (nothing to allocate)
555-
assert(cret == 0);
556-
(void)cret;
557-
558540
void *highPtr = (void *)(((uintptr_t)ptr) + firstSize);
559541
size_t secondSize = totalSize - firstSize;
560542

543+
assert(level < MAX_LEVELS_OF_ALLOC_SEGMENT_MAP);
544+
561545
// We'll have a duplicate entry for the range [highPtr, highValue->size] but this is fine,
562546
// the value is the same anyway and we forbid removing that range concurrently
563547
ret = umfMemoryTrackerAddAtLevel(provider->hTracker, level, provider->pool,
@@ -567,21 +551,20 @@ static umf_result_t trackingAllocationSplit(void *hProvider, void *ptr,
567551
LOG_ERR("failed to add the split region to the tracker, ptr=%p, "
568552
"size=%zu, ret=%d",
569553
highPtr, secondSize, ret);
554+
570555
// revert the split
571-
assert(level < MAX_LEVELS_OF_ALLOC_SEGMENT_MAP);
572-
cret = critnib_insert(provider->hTracker->alloc_segments_map[level],
573-
(uintptr_t)ptr, (void *)value, 1 /* update */);
574-
// this cannot fail since we know the element exists (nothing to allocate)
575-
assert(cret == 0);
576-
(void)cret;
556+
(void)umfMemoryProviderAllocationMerge(provider->hUpstream, ptr,
557+
highPtr, totalSize);
558+
577559
// TODO: what now? should we rollback the split? This can only happen due to ENOMEM
578560
// so it's unlikely but probably the best solution would be to try to preallocate everything
579561
// (value and critnib nodes) before calling umfMemoryProviderAllocationSplit.
580562
goto err;
581563
}
582564

583-
// free the original value
584-
umf_ba_free(provider->hTracker->alloc_info_allocator, value);
565+
// update the size of the first part
566+
utils_atomic_store_release_u64((uint64_t *)&value->size, firstSize);
567+
585568
utils_mutex_unlock(&provider->hTracker->splitMergeMutex);
586569

587570
LOG_DEBUG(
@@ -592,9 +575,8 @@ static umf_result_t trackingAllocationSplit(void *hProvider, void *ptr,
592575

593576
err:
594577
utils_mutex_unlock(&provider->hTracker->splitMergeMutex);
595-
err_lock:
596-
umf_ba_free(provider->hTracker->alloc_info_allocator, splitValue);
597578

579+
err_lock:
598580
LOG_ERR(
599581
"failed to split memory region: ptr=%p, totalSize=%zu, firstSize=%zu",
600582
ptr, totalSize, firstSize);

0 commit comments

Comments
 (0)