Skip to content

Commit 92660e2

Browse files
committed
Postpone freeing a tracker entry until it is really removed from tracker
Postpone freeing a tracker entry until it is really removed from tracker. Ref: #1233 Signed-off-by: Lukasz Dorau <[email protected]>
1 parent 9b16ed7 commit 92660e2

File tree

1 file changed

+31
-9
lines changed

1 file changed

+31
-9
lines changed

src/provider/provider_tracking.c

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,19 @@ static umf_result_t umfMemoryTrackerAdd(umf_memory_tracker_handle_t hTracker,
201201

202202
utils_atomic_load_acquire_u64((uint64_t *)&rvalue->size, &rsize);
203203

204+
// size == 0 means that the entry was removed
205+
if (rsize == 0) {
206+
// restart the search
207+
parent_value = NULL;
208+
rvalue = NULL;
209+
parent_key = 0;
210+
rkey = 0;
211+
rsize = 0;
212+
level = 0;
213+
found = 0;
214+
continue;
215+
}
216+
204217
if ((uintptr_t)ptr < rkey + rsize) {
205218
if (level == MAX_LEVELS_OF_ALLOC_SEGMENT_MAP - 1) {
206219
// TODO: we need to support an arbitrary amount of layers in the future
@@ -254,14 +267,17 @@ static umf_result_t umfMemoryTrackerRemove(umf_memory_tracker_handle_t hTracker,
254267
return UMF_RESULT_ERROR_UNKNOWN;
255268
}
256269

257-
assert(level < MAX_LEVELS_OF_ALLOC_SEGMENT_MAP);
258-
value = critnib_remove(hTracker->alloc_segments_map[level], (uintptr_t)ptr);
259-
assert(value);
260-
261270
LOG_DEBUG("memory region removed: tracker=%p, level=%i, pool=%p, ptr=%p, "
262271
"size=%zu",
263272
(void *)hTracker, level, (void *)value->pool, ptr, value->size);
264273

274+
// size == 0 means that the entry was removed
275+
utils_atomic_store_release_u64((uint64_t *)&value->size, 0);
276+
277+
assert(level < MAX_LEVELS_OF_ALLOC_SEGMENT_MAP);
278+
value = critnib_remove(hTracker->alloc_segments_map[level], (uintptr_t)ptr);
279+
assert(value);
280+
265281
if (parent_value) {
266282
LOG_DEBUG(
267283
"child #%zu removed from memory region: tracker=%p, level=%i, "
@@ -271,8 +287,6 @@ static umf_result_t umfMemoryTrackerRemove(umf_memory_tracker_handle_t hTracker,
271287
parent_value->n_children--;
272288
}
273289

274-
umf_ba_free(hTracker->alloc_info_allocator, value);
275-
276290
return UMF_RESULT_SUCCESS;
277291
}
278292

@@ -652,6 +666,9 @@ static umf_result_t trackingAllocationMerge(void *hProvider, void *lowPtr,
652666
// we only need to update the size of the first part
653667
utils_atomic_store_release_u64((uint64_t *)&lowValue->size, totalSize);
654668

669+
// size == 0 means that the entry was removed
670+
utils_atomic_store_release_u64((uint64_t *)&highValue->size, 0);
671+
655672
void *erasedhighValue = critnib_remove(
656673
provider->hTracker->alloc_segments_map[highLevel], (uintptr_t)highPtr);
657674
assert(erasedhighValue == highValue);
@@ -664,8 +681,6 @@ static umf_result_t trackingAllocationMerge(void *hProvider, void *lowPtr,
664681
lowLevel, lowPtr, lowValue->n_children, highPtr,
665682
highValue->n_children, totalSize);
666683

667-
umf_ba_free(provider->hTracker->alloc_info_allocator, highValue);
668-
669684
return UMF_RESULT_SUCCESS;
670685

671686
err_fatal:
@@ -1156,6 +1171,12 @@ void umfTrackingMemoryProviderGetUpstreamProvider(
11561171
*hUpstream = p->hUpstream;
11571172
}
11581173

1174+
static void free_leaf(void *leaf_allocator, void *ptr) {
1175+
if (ptr) {
1176+
umf_ba_free(leaf_allocator, ptr);
1177+
}
1178+
}
1179+
11591180
umf_memory_tracker_handle_t umfMemoryTrackerCreate(void) {
11601181
umf_memory_tracker_handle_t handle =
11611182
umf_ba_global_alloc(sizeof(struct umf_memory_tracker_t));
@@ -1180,7 +1201,8 @@ umf_memory_tracker_handle_t umfMemoryTrackerCreate(void) {
11801201

11811202
int i;
11821203
for (i = 0; i < MAX_LEVELS_OF_ALLOC_SEGMENT_MAP; i++) {
1183-
handle->alloc_segments_map[i] = critnib_new(NULL, NULL);
1204+
handle->alloc_segments_map[i] =
1205+
critnib_new(alloc_info_allocator, free_leaf);
11841206
if (!handle->alloc_segments_map[i]) {
11851207
goto err_destroy_alloc_segments_map;
11861208
}

0 commit comments

Comments
 (0)