mirror of
https://github.com/rubyjs/mini_racer
synced 2023-03-27 23:21:28 -04:00
avoid malloc in single-ref case of free_context
Triggering an allocation inside a `free' callback doesn't help in low memory situations; so operate directly on the ContextInfo being freed when possible instead of creating a short-lived copy of it. For the `isolate_info->refs() > 1' case, we'll leak slightly less memory by freeing the short-lived allocation in case pthread_create(3) fails.
This commit is contained in:
parent
9a409cf792
commit
4487f11202
1 changed files with 7 additions and 7 deletions
|
@ -1474,8 +1474,6 @@ static void free_context_raw(void *arg) {
|
|||
if (isolate_info) {
|
||||
isolate_info->release();
|
||||
}
|
||||
|
||||
xfree(context_info);
|
||||
}
|
||||
|
||||
static void *free_context_thr(void* arg) {
|
||||
|
@ -1487,6 +1485,7 @@ static void *free_context_thr(void* arg) {
|
|||
}
|
||||
|
||||
free_context_raw(arg);
|
||||
xfree(arg);
|
||||
|
||||
pthread_rwlock_unlock(&exit_lock);
|
||||
|
||||
|
@ -1498,18 +1497,19 @@ static void free_context(ContextInfo* context_info) {
|
|||
|
||||
IsolateInfo* isolate_info = context_info->isolate_info;
|
||||
|
||||
ContextInfo* context_info_copy = ALLOC(ContextInfo);
|
||||
context_info_copy->isolate_info = context_info->isolate_info;
|
||||
context_info_copy->context = context_info->context;
|
||||
|
||||
if (isolate_info && isolate_info->refs() > 1) {
|
||||
pthread_t free_context_thread;
|
||||
ContextInfo* context_info_copy = ALLOC(ContextInfo);
|
||||
|
||||
context_info_copy->isolate_info = context_info->isolate_info;
|
||||
context_info_copy->context = context_info->context;
|
||||
if (pthread_create(&free_context_thread, thread_attr_p,
|
||||
free_context_thr, (void*)context_info_copy)) {
|
||||
fprintf(stderr, "WARNING failed to release memory in MiniRacer, thread to release could not be created, process will leak memory\n");
|
||||
xfree(context_info_copy);
|
||||
}
|
||||
} else {
|
||||
free_context_raw(context_info_copy);
|
||||
free_context_raw(context_info);
|
||||
}
|
||||
|
||||
context_info->context = NULL;
|
||||
|
|
Loading…
Add table
Reference in a new issue