Skip to content

Commit 7e4d37a

Browse files
committed
Replace trackerShrinkEntry() with trackerRemoveEntry()
Replace trackerShrinkEntry() with trackerRemoveEntry() and trackerGrowEntry() with trackerAddEntry(). Signed-off-by: Lukasz Dorau <[email protected]>
1 parent d6d8f60 commit 7e4d37a

File tree

4 files changed

+59
-151
lines changed

4 files changed

+59
-151
lines changed

src/provider/provider_fixed_memory.c

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ typedef struct fixed_memory_provider_t {
3535
coarse_t *coarse; // coarse library handle
3636

3737
// used only when the UMF_FIXED_FLAG_CREATE_FROM_POOL_PTR flag is set
38-
size_t ptr_orig_size; // original size of the memory region in the tracker
3938
umf_memory_provider_handle_t
4039
trackingProvider; // tracking provider of the original memory pool
4140
} fixed_memory_provider_t;
@@ -92,7 +91,6 @@ static umf_result_t fixed_allocation_merge_cb(void *provider, void *lowPtr,
9291

9392
static umf_result_t fixed_initialize(void *params, void **provider) {
9493
umf_result_t ret;
95-
size_t ptr_orig_size = 0;
9694
umf_memory_provider_handle_t trackingProvider = NULL;
9795

9896
if (params == NULL) {
@@ -116,12 +114,11 @@ static umf_result_t fixed_initialize(void *params, void **provider) {
116114
return ret;
117115
}
118116

119-
ret = trackerShrinkEntry(trackingProvider, in_params->ptr,
120-
in_params->size, &ptr_orig_size);
117+
ret = trackerRemoveEntry(trackingProvider, in_params->ptr,
118+
in_params->size);
121119
if (ret != UMF_RESULT_SUCCESS) {
122-
LOG_ERR(
123-
"cannot shrink the allocation %p in the tracker to size %zu",
124-
in_params->ptr, in_params->size);
120+
LOG_ERR("cannot remove the allocation %p from the tracker",
121+
in_params->ptr);
125122
return ret;
126123
}
127124
}
@@ -158,7 +155,6 @@ static umf_result_t fixed_initialize(void *params, void **provider) {
158155
fixed_provider->base = in_params->ptr;
159156
fixed_provider->size = in_params->size;
160157
fixed_provider->flags = in_params->flags;
161-
fixed_provider->ptr_orig_size = ptr_orig_size;
162158
fixed_provider->trackingProvider = trackingProvider;
163159

164160
// add the entire memory as a single block
@@ -184,16 +180,13 @@ static void fixed_finalize(void *provider) {
184180
fixed_memory_provider_t *fixed_provider = provider;
185181
coarse_delete(fixed_provider->coarse);
186182

187-
if (fixed_provider->trackingProvider &&
188-
(fixed_provider->ptr_orig_size >= fixed_provider->size)) {
189-
umf_result_t ret = trackerGrowEntry(
190-
fixed_provider->trackingProvider, fixed_provider->base,
191-
fixed_provider->size, fixed_provider->ptr_orig_size);
183+
if (fixed_provider->trackingProvider) {
184+
umf_result_t ret =
185+
trackerAddEntry(fixed_provider->trackingProvider,
186+
fixed_provider->base, fixed_provider->size);
192187
if (ret != UMF_RESULT_SUCCESS) {
193-
LOG_ERR("cannot grow the allocation %p in the tracker (from size "
194-
"%zu to size %zu)",
195-
fixed_provider->base, fixed_provider->size,
196-
fixed_provider->ptr_orig_size);
188+
LOG_ERR("cannot add the allocation %p to the tracker",
189+
fixed_provider->base);
197190
}
198191
}
199192

src/provider/provider_tracking.c

Lines changed: 44 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -267,58 +267,6 @@ static umf_result_t trackingAllocationSplit(void *hProvider, void *ptr,
267267
return ret;
268268
}
269269

270-
// shrink (or remove) an entry in the tracker and return the totalSize of the original entry
271-
umf_result_t trackerShrinkEntry(void *hProvider, void *ptr, size_t shrinkSize,
272-
size_t *totalSize) {
273-
umf_result_t ret = UMF_RESULT_SUCCESS;
274-
umf_tracking_memory_provider_t *provider =
275-
(umf_tracking_memory_provider_t *)hProvider;
276-
277-
int r = utils_mutex_lock(&provider->hTracker->splitMergeMutex);
278-
if (r) {
279-
return UMF_RESULT_ERROR_UNKNOWN;
280-
}
281-
282-
tracker_alloc_info_t *value = (tracker_alloc_info_t *)critnib_get(
283-
provider->hTracker->alloc_segments_map, (uintptr_t)ptr);
284-
if (!value) {
285-
LOG_ERR("region for shrinking is not found in the tracker");
286-
ret = UMF_RESULT_ERROR_INVALID_ARGUMENT;
287-
goto err;
288-
}
289-
if (shrinkSize > value->size) {
290-
LOG_ERR("requested size %zu to shrink exceeds the tracked size %zu",
291-
shrinkSize, value->size);
292-
ret = UMF_RESULT_ERROR_INVALID_ARGUMENT;
293-
goto err;
294-
}
295-
296-
if (shrinkSize < value->size) {
297-
void *highPtr = (void *)(((uintptr_t)ptr) + shrinkSize);
298-
size_t secondSize = value->size - shrinkSize;
299-
ret = umfMemoryTrackerAdd(provider->hTracker, provider->pool, highPtr,
300-
secondSize);
301-
if (ret != UMF_RESULT_SUCCESS) {
302-
LOG_ERR("failed to add the new shrunk region to the tracker, ptr = "
303-
"%p, size = %zu, ret = %d",
304-
highPtr, secondSize, ret);
305-
goto err;
306-
}
307-
}
308-
309-
*totalSize = value->size;
310-
311-
void *erasedValue =
312-
critnib_remove(provider->hTracker->alloc_segments_map, (uintptr_t)ptr);
313-
assert(erasedValue == value);
314-
umf_ba_free(provider->hTracker->alloc_info_allocator, erasedValue);
315-
316-
err:
317-
utils_mutex_unlock(&provider->hTracker->splitMergeMutex);
318-
319-
return ret;
320-
}
321-
322270
static umf_result_t trackingAllocationMerge(void *hProvider, void *lowPtr,
323271
void *highPtr, size_t totalSize) {
324272
umf_result_t ret = UMF_RESULT_ERROR_UNKNOWN;
@@ -405,65 +353,6 @@ static umf_result_t trackingAllocationMerge(void *hProvider, void *lowPtr,
405353
return ret;
406354
}
407355

408-
// grow (or add) an entry in the tracker to its original size
409-
umf_result_t trackerGrowEntry(void *hProvider, void *ptr, size_t growSize,
410-
size_t origSize) {
411-
umf_result_t ret = UMF_RESULT_SUCCESS;
412-
umf_tracking_memory_provider_t *provider =
413-
(umf_tracking_memory_provider_t *)hProvider;
414-
415-
if (growSize > origSize) {
416-
LOG_ERR("Invalid grow size %zu (larger than the original size %zu)",
417-
growSize, origSize);
418-
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
419-
}
420-
421-
int r = utils_mutex_lock(&provider->hTracker->splitMergeMutex);
422-
if (r) {
423-
return UMF_RESULT_ERROR_UNKNOWN;
424-
}
425-
426-
void *highPtr = (void *)(((uintptr_t)ptr) + growSize);
427-
tracker_alloc_info_t *highValue = NULL;
428-
429-
if (growSize < origSize) {
430-
highValue = (tracker_alloc_info_t *)critnib_get(
431-
provider->hTracker->alloc_segments_map, (uintptr_t)highPtr);
432-
if (!highValue) {
433-
LOG_ERR("cannot find the tracker entry to grow %p", highPtr);
434-
ret = UMF_RESULT_ERROR_INVALID_ARGUMENT;
435-
goto err;
436-
}
437-
if (growSize + highValue->size != origSize) {
438-
LOG_ERR("Grow size plus the current size does not equal the "
439-
"original size");
440-
ret = UMF_RESULT_ERROR_INVALID_ARGUMENT;
441-
goto err;
442-
}
443-
}
444-
445-
ret =
446-
umfMemoryTrackerAdd(provider->hTracker, provider->pool, ptr, origSize);
447-
if (ret != UMF_RESULT_SUCCESS) {
448-
LOG_ERR("failed to add the new grown region to the tracker, ptr = %p, "
449-
"size = %zu, ret = %d",
450-
ptr, origSize, ret);
451-
goto err;
452-
}
453-
454-
if (growSize < origSize) {
455-
void *erasedhighValue = critnib_remove(
456-
provider->hTracker->alloc_segments_map, (uintptr_t)highPtr);
457-
assert(erasedhighValue == highValue);
458-
umf_ba_free(provider->hTracker->alloc_info_allocator, erasedhighValue);
459-
}
460-
461-
err:
462-
utils_mutex_unlock(&provider->hTracker->splitMergeMutex);
463-
464-
return ret;
465-
}
466-
467356
static umf_result_t trackingFree(void *hProvider, void *ptr, size_t size) {
468357
umf_result_t ret;
469358
umf_result_t ret_remove = UMF_RESULT_ERROR_UNKNOWN;
@@ -980,3 +869,47 @@ void umfMemoryTrackerDestroy(umf_memory_tracker_handle_t handle) {
980869
handle->alloc_info_allocator = NULL;
981870
umf_ba_global_free(handle);
982871
}
872+
873+
// add an entry to the tracker
874+
umf_result_t trackerAddEntry(void *hProvider, void *ptr, size_t size) {
875+
umf_tracking_memory_provider_t *provider =
876+
(umf_tracking_memory_provider_t *)hProvider;
877+
return umfMemoryTrackerAdd(provider->hTracker, provider->pool, ptr, size);
878+
}
879+
880+
// remove an entry from the tracker
881+
umf_result_t trackerRemoveEntry(void *hProvider, void *ptr, size_t size) {
882+
umf_result_t ret = UMF_RESULT_SUCCESS;
883+
umf_tracking_memory_provider_t *provider =
884+
(umf_tracking_memory_provider_t *)hProvider;
885+
886+
int r = utils_mutex_lock(&provider->hTracker->splitMergeMutex);
887+
if (r) {
888+
return UMF_RESULT_ERROR_UNKNOWN;
889+
}
890+
891+
tracker_alloc_info_t *value = (tracker_alloc_info_t *)critnib_get(
892+
provider->hTracker->alloc_segments_map, (uintptr_t)ptr);
893+
if (!value) {
894+
LOG_ERR("region for removing was not found in the tracker");
895+
ret = UMF_RESULT_ERROR_INVALID_ARGUMENT;
896+
goto err;
897+
}
898+
if (size != value->size) {
899+
LOG_ERR(
900+
"requested size %zu to remove does not match the tracked size %zu",
901+
size, value->size);
902+
ret = UMF_RESULT_ERROR_INVALID_ARGUMENT;
903+
goto err;
904+
}
905+
906+
void *erasedValue =
907+
critnib_remove(provider->hTracker->alloc_segments_map, (uintptr_t)ptr);
908+
assert(erasedValue == value);
909+
umf_ba_free(provider->hTracker->alloc_info_allocator, erasedValue);
910+
911+
err:
912+
utils_mutex_unlock(&provider->hTracker->splitMergeMutex);
913+
914+
return ret;
915+
}

src/provider/provider_tracking.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,9 @@ void umfTrackingMemoryProviderGetUpstreamProvider(
5555
umf_memory_provider_handle_t hTrackingProvider,
5656
umf_memory_provider_handle_t *hUpstream);
5757

58-
umf_result_t trackerShrinkEntry(void *hProvider, void *ptr, size_t shrinkSize,
59-
size_t *totalSize);
58+
umf_result_t trackerRemoveEntry(void *hProvider, void *ptr, size_t size);
6059

61-
umf_result_t trackerGrowEntry(void *hProvider, void *ptr, size_t growSize,
62-
size_t origSize);
60+
umf_result_t trackerAddEntry(void *hProvider, void *ptr, size_t size);
6361

6462
#ifdef __cplusplus
6563
}

test/provider_fixed_memory.cpp

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -556,12 +556,11 @@ TEST_P(FixedProviderTest, pool_from_ptr_whole_size_success) {
556556
umfPoolDestroy(proxyFixedPool);
557557
}
558558

559-
TEST_P(FixedProviderTest, pool_from_ptr_half_size_success) {
559+
TEST_P(FixedProviderTest, pool_from_ptr_negative_half_size) {
560560
umf_result_t umf_result;
561561
size_t size_of_first_alloc;
562562
size_t size_of_pool_from_ptr;
563563
void *ptr_for_pool = nullptr;
564-
void *ptr = nullptr;
565564

566565
umf_memory_pool_handle_t proxyFixedPool = nullptr;
567566
umf_result = umfPoolCreate(umfProxyPoolOps(), provider.get(), nullptr, 0,
@@ -587,24 +586,9 @@ TEST_P(FixedProviderTest, pool_from_ptr_half_size_success) {
587586
umf_memory_provider_handle_t providerFromPtr = nullptr;
588587
umf_result = umfMemoryProviderCreate(umfFixedMemoryProviderOps(), params,
589588
&providerFromPtr);
590-
ASSERT_EQ(umf_result, UMF_RESULT_SUCCESS);
591-
ASSERT_NE(providerFromPtr, nullptr);
592-
593-
umf_memory_pool_handle_t poolFromPtr = nullptr;
594-
umf_result = umfPoolCreate(umfProxyPoolOps(), providerFromPtr, nullptr, 0,
595-
&poolFromPtr);
596-
ASSERT_EQ(umf_result, UMF_RESULT_SUCCESS);
597-
598-
ptr = umfPoolMalloc(poolFromPtr, size_of_pool_from_ptr);
599-
ASSERT_NE(ptr, nullptr);
600-
601-
memset(ptr, 0xFF, size_of_pool_from_ptr);
602-
603-
umf_result = umfPoolFree(poolFromPtr, ptr);
604-
ASSERT_EQ(umf_result, UMF_RESULT_SUCCESS);
589+
ASSERT_EQ(umf_result, UMF_RESULT_ERROR_INVALID_ARGUMENT);
590+
ASSERT_EQ(providerFromPtr, nullptr);
605591

606-
umfPoolDestroy(poolFromPtr);
607-
umfMemoryProviderDestroy(providerFromPtr);
608592
umfFixedMemoryProviderParamsDestroy(params);
609593

610594
umf_result = umfPoolFree(proxyFixedPool, ptr_for_pool);

0 commit comments

Comments
 (0)