Skip to content

Commit b395576

Browse files
authored
Merge pull request #457 from vinser52/svinogra_ipc_fix
Fix umfMemoryTrackerGetPool function and minor refactoring
2 parents e5063d9 + ae10a01 commit b395576

File tree

5 files changed

+52
-27
lines changed

5 files changed

+52
-27
lines changed

src/ipc.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,16 +47,16 @@ umf_result_t umfGetIPCHandle(const void *ptr, umf_ipc_handle_t *umfIPCHandle,
4747
return UMF_RESULT_ERROR_OUT_OF_HOST_MEMORY;
4848
}
4949

50-
ret =
51-
umfMemoryProviderGetIPCHandle(provider, allocInfo.base, allocInfo.size,
52-
(void *)ipcData->providerIpcData);
50+
ret = umfMemoryProviderGetIPCHandle(provider, allocInfo.base,
51+
allocInfo.baseSize,
52+
(void *)ipcData->providerIpcData);
5353
if (ret != UMF_RESULT_SUCCESS) {
5454
LOG_ERR("umfGetIPCHandle: failed to get IPC handle.");
5555
umf_ba_global_free(ipcData);
5656
return ret;
5757
}
5858

59-
ipcData->size = allocInfo.size;
59+
ipcData->baseSize = allocInfo.baseSize;
6060
ipcData->offset = (uintptr_t)ptr - (uintptr_t)allocInfo.base;
6161

6262
*umfIPCHandle = ipcData;
@@ -114,5 +114,5 @@ umf_result_t umfCloseIPCHandle(void *ptr) {
114114
umf_memory_provider_handle_t hProvider = allocInfo.pool->provider;
115115

116116
return umfMemoryProviderCloseIPCHandle(hProvider, allocInfo.base,
117-
allocInfo.size);
117+
allocInfo.baseSize);
118118
}

src/ipc_internal.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ extern "C" {
2121
// providerIpcData is a Flexible Array Member because its size varies
2222
// depending on the provider.
2323
typedef struct umf_ipc_data_t {
24-
size_t size; // size of base allocation
24+
size_t baseSize; // size of base (coarse-grain) allocation
2525
uint64_t offset;
2626
char providerIpcData[];
2727
} umf_ipc_data_t;

src/provider/provider_tracking.c

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -80,34 +80,30 @@ static umf_result_t umfMemoryTrackerRemove(umf_memory_tracker_handle_t hTracker,
8080
}
8181

8282
umf_memory_pool_handle_t umfMemoryTrackerGetPool(const void *ptr) {
83-
assert(ptr);
84-
85-
if (TRACKER == NULL) {
86-
LOG_ERR("tracker is not created");
87-
return NULL;
88-
}
89-
90-
if (TRACKER->map == NULL) {
91-
LOG_ERR("tracker's map is not created");
92-
return NULL;
93-
}
94-
95-
uintptr_t rkey;
96-
tracker_value_t *rvalue;
97-
int found = critnib_find(TRACKER->map, (uintptr_t)ptr, FIND_LE,
98-
(void *)&rkey, (void **)&rvalue);
99-
if (!found) {
83+
umf_alloc_info_t allocInfo = {0};
84+
umf_result_t ret = umfMemoryTrackerGetAllocInfo(ptr, &allocInfo);
85+
if (ret != UMF_RESULT_SUCCESS) {
10086
return NULL;
10187
}
10288

103-
return (rkey + rvalue->size >= (uintptr_t)ptr) ? rvalue->pool : NULL;
89+
return allocInfo.pool;
10490
}
10591

10692
umf_result_t umfMemoryTrackerGetAllocInfo(const void *ptr,
10793
umf_alloc_info_t *pAllocInfo) {
10894
assert(ptr);
10995
assert(pAllocInfo);
11096

97+
if (TRACKER == NULL) {
98+
LOG_ERR("tracker is not created");
99+
return UMF_RESULT_ERROR_NOT_SUPPORTED;
100+
}
101+
102+
if (TRACKER->map == NULL) {
103+
LOG_ERR("tracker's map is not created");
104+
return UMF_RESULT_ERROR_NOT_SUPPORTED;
105+
}
106+
111107
uintptr_t rkey;
112108
tracker_value_t *rvalue;
113109
int found = critnib_find(TRACKER->map, (uintptr_t)ptr, FIND_LE,
@@ -117,7 +113,7 @@ umf_result_t umfMemoryTrackerGetAllocInfo(const void *ptr,
117113
}
118114

119115
pAllocInfo->base = (void *)rkey;
120-
pAllocInfo->size = rvalue->size;
116+
pAllocInfo->baseSize = rvalue->size;
121117
pAllocInfo->pool = rvalue->pool;
122118

123119
return UMF_RESULT_SUCCESS;
@@ -577,7 +573,7 @@ static size_t getDataSizeFromIpcHandle(const void *providerIpcData) {
577573
// the Flexible Array Member of umf_ipc_data_t.
578574
umf_ipc_data_t *ipcUmfData =
579575
(umf_ipc_data_t *)((uint8_t *)providerIpcData - sizeof(umf_ipc_data_t));
580-
return ipcUmfData->size;
576+
return ipcUmfData->baseSize;
581577
}
582578

583579
static umf_result_t trackingOpenIpcHandle(void *provider, void *providerIpcData,

src/provider/provider_tracking.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ umf_memory_pool_handle_t umfMemoryTrackerGetPool(const void *ptr);
4242

4343
typedef struct umf_alloc_info_t {
4444
void *base;
45-
size_t size;
45+
size_t baseSize;
4646
umf_memory_pool_handle_t pool;
4747
} umf_alloc_info_t;
4848

test/memoryPoolAPI.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,35 @@ TEST_F(test, retrieveMemoryProvider) {
150150
ASSERT_EQ(retProvider, provider);
151151
}
152152

153+
#ifdef UMF_ENABLE_POOL_TRACKING_TESTS
154+
TEST_F(test, BasicPoolByPtrTest) {
155+
constexpr size_t SIZE = 4096 * 1024;
156+
157+
umf_memory_provider_handle_t provider;
158+
umf_result_t ret =
159+
umfMemoryProviderCreate(&MALLOC_PROVIDER_OPS, NULL, &provider);
160+
ASSERT_EQ(ret, UMF_RESULT_SUCCESS);
161+
auto pool =
162+
wrapPoolUnique(createPoolChecked(umfProxyPoolOps(), provider, nullptr,
163+
UMF_POOL_CREATE_FLAG_OWN_PROVIDER));
164+
auto expected_pool = pool.get();
165+
char *ptr = (char *)umfPoolMalloc(expected_pool, SIZE);
166+
ASSERT_NE(ptr, nullptr);
167+
168+
auto ret_pool = umfPoolByPtr(ptr);
169+
EXPECT_EQ(ret_pool, expected_pool);
170+
171+
ret_pool = umfPoolByPtr(ptr + SIZE);
172+
EXPECT_EQ(ret_pool, nullptr);
173+
174+
ret_pool = umfPoolByPtr(ptr + SIZE - 1);
175+
EXPECT_EQ(ret_pool, expected_pool);
176+
177+
ret = umfFree(ptr);
178+
ASSERT_EQ(ret, UMF_RESULT_SUCCESS);
179+
}
180+
#endif /* UMF_ENABLE_POOL_TRACKING_TESTS */
181+
153182
INSTANTIATE_TEST_SUITE_P(
154183
mallocPoolTest, umfPoolTest,
155184
::testing::Values(poolCreateExtParams{&MALLOC_POOL_OPS, nullptr,

0 commit comments

Comments
 (0)