7
7
*
8
8
*/
9
9
10
- #include "provider_tracking.h"
10
+ #include <assert.h>
11
+ #include <errno.h>
12
+ #include <stdio.h>
13
+ #include <stdlib.h>
14
+ #include <string.h>
15
+
16
+ #include <umf/memory_pool.h>
17
+ #include <umf/memory_provider.h>
18
+ #include <umf/memory_provider_ops.h>
19
+
11
20
#include "base_alloc_global.h"
12
21
#include "critnib.h"
13
22
#include "ipc_internal.h"
23
+ #include "libumf.h"
24
+ #include "memory_pool_internal.h"
25
+ #include "provider_tracking.h"
14
26
#include "utils_common.h"
15
27
#include "utils_concurrency.h"
16
28
#include "utils_log.h"
17
29
18
- #include <umf/memory_pool.h>
19
- #include <umf/memory_provider.h>
20
- #include <umf/memory_provider_ops.h>
21
-
22
- #include <assert.h>
23
- #include <errno.h>
24
- #include <stdio.h>
25
- #include <stdlib.h>
26
- #include <string.h>
27
-
28
30
typedef struct tracker_value_t {
29
31
umf_memory_pool_handle_t pool ;
30
32
size_t size ;
@@ -82,9 +84,8 @@ static umf_result_t umfMemoryTrackerRemove(umf_memory_tracker_handle_t hTracker,
82
84
}
83
85
84
86
tracker_value_t * v = value ;
85
-
86
- LOG_DEBUG ("memory region removed: tracker=%p, ptr=%p, size=%zu" ,
87
- (void * )hTracker , ptr , v -> size );
87
+ LOG_DEBUG ("memory region removed: tracker=%p, ptr=%p, pool=%p, size=%zu" ,
88
+ (void * )hTracker , ptr , (void * )v -> pool , v -> size );
88
89
89
90
umf_ba_free (hTracker -> tracker_allocator , value );
90
91
@@ -167,22 +168,25 @@ static umf_result_t trackingAlloc(void *hProvider, size_t size,
167
168
return ret ;
168
169
}
169
170
171
+ LOG_DEBUG ("allocated %p, provider: %p, size: %zu" , * ptr ,
172
+ (void * )p -> hUpstream , size );
173
+
170
174
// check if the allocation was already added to the tracker
171
175
tracker_value_t * value =
172
176
(tracker_value_t * )critnib_get (p -> hTracker -> map , * (uintptr_t * )ptr );
173
177
if (value ) {
174
178
assert (value -> pool != p -> pool );
179
+ LOG_ERR ("ptr already exists in the tracker ptr=%p, old size=%zu, new "
180
+ "size=%zu, old pool %p, new pool %p, tracker %p" ,
181
+ * ptr , value -> size , size , (void * )value -> pool , (void * )p -> pool ,
182
+ (void * )p -> hTracker );
175
183
176
- LOG_DEBUG ("ptr already exists in the tracker (added by Proxy Lib) - "
177
- "updating value, ptr=%p, size=%zu, old pool: %p, new pool %p" ,
178
- * ptr , size , (void * )value -> pool , (void * )p -> pool );
179
-
180
- // the allocation was made by the ProxyLib so we only update the tracker
181
184
value -> pool = p -> pool ;
185
+ value -> size = size ;
182
186
int crit_ret = critnib_insert (p -> hTracker -> map , * (uintptr_t * )ptr ,
183
187
value , 1 /* update */ );
184
188
185
- // this cannot fail since we know the element exists and there is
189
+ // this cannot fail since we know the element exists and there is
186
190
// nothing to allocate
187
191
assert (crit_ret == 0 );
188
192
(void )crit_ret ;
@@ -221,6 +225,12 @@ static umf_result_t trackingAllocationSplit(void *hProvider, void *ptr,
221
225
goto err_lock ;
222
226
}
223
227
228
+ void * highPtr = (void * )(((uintptr_t )ptr ) + firstSize );
229
+ size_t secondSize = totalSize - firstSize ;
230
+
231
+ LOG_DEBUG ("trying to split (%p, %zu) to (%p, %zu) and (%p, %zu)" , ptr ,
232
+ totalSize , ptr , firstSize , highPtr , secondSize );
233
+
224
234
tracker_value_t * value =
225
235
(tracker_value_t * )critnib_get (provider -> hTracker -> map , (uintptr_t )ptr );
226
236
if (!value ) {
@@ -242,9 +252,6 @@ static umf_result_t trackingAllocationSplit(void *hProvider, void *ptr,
242
252
goto err ;
243
253
}
244
254
245
- void * highPtr = (void * )(((uintptr_t )ptr ) + firstSize );
246
- size_t secondSize = totalSize - firstSize ;
247
-
248
255
// We'll have a duplicate entry for the range [highPtr, highValue->size] but this is fine,
249
256
// the value is the same anyway and we forbid removing that range concurrently
250
257
ret = umfMemoryTrackerAdd (provider -> hTracker , provider -> pool , highPtr ,
@@ -259,6 +266,9 @@ static umf_result_t trackingAllocationSplit(void *hProvider, void *ptr,
259
266
goto err ;
260
267
}
261
268
269
+ LOG_DEBUG ("update split region ptr=%p, pool=%p size=%zu" , ptr ,
270
+ (void * )splitValue -> pool , splitValue -> size );
271
+
262
272
int cret = critnib_insert (provider -> hTracker -> map , (uintptr_t )ptr ,
263
273
(void * )splitValue , 1 /* update */ );
264
274
// this cannot fail since we know the element exists (nothing to allocate)
@@ -302,22 +312,26 @@ static umf_result_t trackingAllocationMerge(void *hProvider, void *lowPtr,
302
312
tracker_value_t * lowValue = (tracker_value_t * )critnib_get (
303
313
provider -> hTracker -> map , (uintptr_t )lowPtr );
304
314
if (!lowValue ) {
305
- LOG_ERR ("no left value" );
315
+ LOG_ERR ("no left value (%p) found in tracker!" , lowPtr );
306
316
ret = UMF_RESULT_ERROR_INVALID_ARGUMENT ;
307
317
goto err ;
308
318
}
319
+
309
320
tracker_value_t * highValue = (tracker_value_t * )critnib_get (
310
321
provider -> hTracker -> map , (uintptr_t )highPtr );
311
322
if (!highValue ) {
312
- LOG_ERR ("no right value" );
323
+ LOG_ERR ("no right value (%p) found in tracker!" , highPtr );
313
324
ret = UMF_RESULT_ERROR_INVALID_ARGUMENT ;
314
325
goto err ;
315
326
}
327
+
316
328
if (lowValue -> pool != highValue -> pool ) {
317
- LOG_ERR ("pool mismatch" );
329
+ LOG_ERR ("pool mismatch: %p vs %p" , (void * )lowValue -> pool ,
330
+ (void * )highValue -> pool );
318
331
ret = UMF_RESULT_ERROR_INVALID_ARGUMENT ;
319
332
goto err ;
320
333
}
334
+
321
335
if (lowValue -> size + highValue -> size != totalSize ) {
322
336
LOG_ERR ("lowValue->size + highValue->size != totalSize" );
323
337
ret = UMF_RESULT_ERROR_INVALID_ARGUMENT ;
@@ -354,6 +368,8 @@ static umf_result_t trackingAllocationMerge(void *hProvider, void *lowPtr,
354
368
355
369
err :
356
370
utils_mutex_unlock (& provider -> hTracker -> splitMergeMutex );
371
+ assert (0 );
372
+
357
373
err_lock :
358
374
umf_ba_free (provider -> hTracker -> tracker_allocator , mergedValue );
359
375
return ret ;
@@ -375,7 +391,7 @@ static umf_result_t trackingFree(void *hProvider, void *ptr, size_t size) {
375
391
// DO NOT return an error here, because the tracking provider
376
392
// cannot change behaviour of the upstream provider.
377
393
LOG_ERR ("failed to remove the region from the tracker, ptr=%p, "
378
- "size=%zu, ret = %d" ,
394
+ "size=%zu, ret= %d" ,
379
395
ptr , size , ret_remove );
380
396
}
381
397
}
0 commit comments