Skip to content
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

Fix leaks in test/correctness/memoize.cpp #7705

Merged
merged 17 commits into from
Aug 5, 2023
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
29 changes: 22 additions & 7 deletions test/correctness/memoize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,18 +59,22 @@ extern "C" HALIDE_EXPORT_SYMBOL int computed_eviction_key(int a) {
}
HalideExtern_1(int, computed_eviction_key, int);

void simple_free(JITUserContext *user_context, void *ptr) {
free(ptr);
}
void *(*default_malloc)(JITUserContext *, size_t);
void (*default_free)(JITUserContext *, void *);

void *flakey_malloc(JITUserContext * /* user_context */, size_t x) {
// A flaky allocator that wraps the built-in runtime one.
void *flaky_malloc(JITUserContext *user_context, size_t x) {
if ((rand() % 4) == 0) {
return nullptr;
} else {
return malloc(x);
return default_malloc(user_context, x);
}
}

void simple_free(JITUserContext *user_context, void *ptr) {
return default_free(user_context, ptr);
}

bool error_occured = false;
void record_error(JITUserContext *user_context, const char *msg) {
error_occured = true;
Expand Down Expand Up @@ -584,6 +588,15 @@ int main(int argc, char **argv) {
return 0;
} else {
// Test out of memory handling.

// Get the runtime's malloc and free. We need to use the ones
// in the runtime to ensure the matching free is called when
// we release all the runtimes at the end.
JITUserContext ctx;
Internal::JITSharedRuntime::populate_jit_handlers(&ctx, JITHandlers{});
default_malloc = ctx.handlers.custom_malloc;
default_free = ctx.handlers.custom_free;

Param<float> val;

Func count_calls;
Expand All @@ -600,7 +613,7 @@ int main(int argc, char **argv) {

Pipeline pipe(g);
pipe.jit_handlers().custom_error = record_error;
pipe.jit_handlers().custom_malloc = flakey_malloc;
pipe.jit_handlers().custom_malloc = flaky_malloc;
pipe.jit_handlers().custom_free = simple_free;

int total_errors = 0;
Expand Down Expand Up @@ -644,7 +657,8 @@ int main(int argc, char **argv) {
}
}

printf("In 100 attempts with flakey malloc, %d errors and %d full completions occured.\n", total_errors, completed);
printf("In 100 attempts with flaky malloc, %d errors and %d full completions occured.\n",
total_errors, completed);
}

{
Expand Down Expand Up @@ -733,6 +747,7 @@ int main(int argc, char **argv) {

assert(call_count == 8);
}
Internal::JITSharedRuntime::release_all();

printf("Success!\n");
return 0;
Expand Down
2 changes: 2 additions & 0 deletions test/correctness/memoize_cloned.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ int main(int argc, char **argv) {
return 1;
}

Internal::JITSharedRuntime::release_all();

printf("Success!\n");
return 0;
}