Skip to content

Assert if tracking provider is not empty #153

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 53 additions & 1 deletion src/provider/provider_tracking.c
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,50 @@ static umf_result_t trackingInitialize(void *params, void **ret) {
return UMF_RESULT_SUCCESS;
}

static void trackingFinalize(void *provider) { free(provider); }
#ifndef NDEBUG
static void check_if_tracker_is_empty(umf_memory_tracker_handle_t hTracker,
umf_memory_pool_handle_t pool) {
uintptr_t rkey;
void *rvalue;
size_t n_items = 0;
uintptr_t last_key = 0;

while (1 == critnib_find((critnib *)hTracker->map, last_key, FIND_G, &rkey,
&rvalue)) {
tracker_value_t *value = (tracker_value_t *)rvalue;
if (value->pool == pool || pool == NULL) {
n_items++;
}

last_key = rkey;
}

if (n_items) {
if (pool) {
fprintf(stderr,
"ASSERT: tracking provider of pool %p is not empty! (%zu "
"items left)\n",
(void *)pool, n_items);
} else {
fprintf(
stderr,
"ASSERT: tracking provider is not empty! (%zu items left)\n",
n_items);
}
assert(n_items == 0);
}
}
#endif /* NDEBUG */

static void trackingFinalize(void *provider) {
#ifndef NDEBUG
umf_tracking_memory_provider_t *p =
(umf_tracking_memory_provider_t *)provider;
check_if_tracker_is_empty(p->hTracker, p->pool);
#endif /* NDEBUG */

free(provider);
}

static void trackingGetLastError(void *provider, const char **msg,
int32_t *pError) {
Expand Down Expand Up @@ -404,3 +447,12 @@ void umfTrackingMemoryProviderGetUpstreamProvider(
(umf_tracking_memory_provider_t *)hTrackingProvider;
*hUpstream = p->hUpstream;
}

void umfTrackingMemoryProviderFini(void *tracker) {
umf_memory_tracker_handle_t hTracker = (umf_memory_tracker_handle_t)tracker;
#ifndef NDEBUG
check_if_tracker_is_empty(hTracker, NULL);
#endif /* NDEBUG */

umfMemoryTrackerDestroy(hTracker);
}
2 changes: 2 additions & 0 deletions src/provider/provider_tracking.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ umf_result_t umfTrackingMemoryProviderCreate(
// Initialize critnib for a UMF static library build on Windows
void umfTrackingMemoryProviderInit(void);

void umfTrackingMemoryProviderFini(void *tracker);

void umfTrackingMemoryProviderGetUpstreamProvider(
umf_memory_provider_handle_t hTrackingProvider,
umf_memory_provider_handle_t *hUpstream);
Expand Down
2 changes: 1 addition & 1 deletion src/provider/provider_tracking_linux.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ void __attribute__((constructor)) createLibTracker(void) {
TRACKER = umfMemoryTrackerCreate();
}
void __attribute__((destructor)) deleteLibTracker(void) {
umfMemoryTrackerDestroy(TRACKER);
umfTrackingMemoryProviderFini(TRACKER);
}

void umfTrackingMemoryProviderInit(void) {
Expand Down
6 changes: 3 additions & 3 deletions src/provider/provider_tracking_windows.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@

static umf_memory_tracker_handle_t TRACKER = NULL;

static void providerFini(void) { umfTrackingMemoryProviderFini(TRACKER); }

#if defined(UMF_SHARED_LIBRARY)
BOOL APIENTRY DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved) {
if (fdwReason == DLL_PROCESS_DETACH) {
umfMemoryTrackerDestroy(TRACKER);
providerFini();
} else if (fdwReason == DLL_PROCESS_ATTACH) {
TRACKER = umfMemoryTrackerCreate();
}
Expand All @@ -30,8 +32,6 @@ void umfTrackingMemoryProviderInit(void) {
#else
INIT_ONCE init_once_flag = INIT_ONCE_STATIC_INIT;

static void providerFini(void) { umfMemoryTrackerDestroy(TRACKER); }

BOOL CALLBACK providerInit(PINIT_ONCE InitOnce, PVOID Parameter,
PVOID *lpContext) {
TRACKER = umfMemoryTrackerCreate();
Expand Down