Fix handling of memory limit
Default to 0, which is "disabled", just like the stack limit.
This commit is contained in:
parent
2050bc782a
commit
e5ae6cf106
3 changed files with 18 additions and 13 deletions
22
qjs.c
22
qjs.c
|
@ -199,7 +199,8 @@ static void *js_trace_malloc(JSMallocState *s, size_t size)
|
||||||
/* Do not allocate zero bytes: behavior is platform dependent */
|
/* Do not allocate zero bytes: behavior is platform dependent */
|
||||||
assert(size != 0);
|
assert(size != 0);
|
||||||
|
|
||||||
if (unlikely(s->malloc_size + size > s->malloc_limit))
|
/* When malloc_limit is 0 (unlimited), malloc_limit - 1 will be SIZE_MAX. */
|
||||||
|
if (unlikely(s->malloc_size + size > s->malloc_limit - 1))
|
||||||
return NULL;
|
return NULL;
|
||||||
ptr = malloc(size);
|
ptr = malloc(size);
|
||||||
js_trace_malloc_printf(s, "A %zd -> %p\n", size, ptr);
|
js_trace_malloc_printf(s, "A %zd -> %p\n", size, ptr);
|
||||||
|
@ -238,7 +239,8 @@ static void *js_trace_realloc(JSMallocState *s, void *ptr, size_t size)
|
||||||
free(ptr);
|
free(ptr);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
if (s->malloc_size + size - old_size > s->malloc_limit)
|
/* When malloc_limit is 0 (unlimited), malloc_limit - 1 will be SIZE_MAX. */
|
||||||
|
if (s->malloc_size + size - old_size > s->malloc_limit - 1)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
js_trace_malloc_printf(s, "R %zd %p", size, ptr);
|
js_trace_malloc_printf(s, "R %zd %p", size, ptr);
|
||||||
|
@ -295,10 +297,10 @@ int main(int argc, char **argv)
|
||||||
int module = -1;
|
int module = -1;
|
||||||
int load_std = 0;
|
int load_std = 0;
|
||||||
int dump_unhandled_promise_rejection = 0;
|
int dump_unhandled_promise_rejection = 0;
|
||||||
size_t memory_limit = 0;
|
|
||||||
char *include_list[32];
|
char *include_list[32];
|
||||||
int i, include_count = 0;
|
int i, include_count = 0;
|
||||||
size_t stack_size = 0;
|
int64_t memory_limit = -1;
|
||||||
|
int64_t stack_size = -1;
|
||||||
|
|
||||||
argv0 = (JSCFunctionListEntry)JS_PROP_STRING_DEF("argv0", argv[0],
|
argv0 = (JSCFunctionListEntry)JS_PROP_STRING_DEF("argv0", argv[0],
|
||||||
JS_PROP_C_W_E);
|
JS_PROP_C_W_E);
|
||||||
|
@ -403,7 +405,7 @@ int main(int argc, char **argv)
|
||||||
opt_arg = argv[optind++];
|
opt_arg = argv[optind++];
|
||||||
}
|
}
|
||||||
// TODO(chqrlie): accept kmg suffixes
|
// TODO(chqrlie): accept kmg suffixes
|
||||||
memory_limit = (size_t)strtod(opt_arg, NULL);
|
memory_limit = strtoull(opt_arg, NULL, 0);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if (!strcmp(longopt, "stack-size")) {
|
if (!strcmp(longopt, "stack-size")) {
|
||||||
|
@ -415,7 +417,7 @@ int main(int argc, char **argv)
|
||||||
opt_arg = argv[optind++];
|
opt_arg = argv[optind++];
|
||||||
}
|
}
|
||||||
// TODO(chqrlie): accept kmg suffixes
|
// TODO(chqrlie): accept kmg suffixes
|
||||||
stack_size = (size_t)strtod(opt_arg, NULL);
|
stack_size = strtoull(opt_arg, NULL, 0);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if (opt) {
|
if (opt) {
|
||||||
|
@ -437,10 +439,10 @@ int main(int argc, char **argv)
|
||||||
fprintf(stderr, "qjs: cannot allocate JS runtime\n");
|
fprintf(stderr, "qjs: cannot allocate JS runtime\n");
|
||||||
exit(2);
|
exit(2);
|
||||||
}
|
}
|
||||||
if (memory_limit != 0)
|
if (memory_limit >= 0)
|
||||||
JS_SetMemoryLimit(rt, memory_limit);
|
JS_SetMemoryLimit(rt, (size_t)memory_limit);
|
||||||
if (stack_size != 0)
|
if (stack_size >= 0)
|
||||||
JS_SetMaxStackSize(rt, stack_size);
|
JS_SetMaxStackSize(rt, (size_t)stack_size);
|
||||||
if (dump_flags != 0)
|
if (dump_flags != 0)
|
||||||
JS_SetDumpFlags(rt, dump_flags);
|
JS_SetDumpFlags(rt, dump_flags);
|
||||||
js_std_set_worker_new_context_func(JS_NewCustomContext);
|
js_std_set_worker_new_context_func(JS_NewCustomContext);
|
||||||
|
|
|
@ -1608,7 +1608,7 @@ JSRuntime *JS_NewRuntime2(const JSMallocFunctions *mf, void *opaque)
|
||||||
|
|
||||||
memset(&ms, 0, sizeof(ms));
|
memset(&ms, 0, sizeof(ms));
|
||||||
ms.opaque = opaque;
|
ms.opaque = opaque;
|
||||||
ms.malloc_limit = -1;
|
ms.malloc_limit = 0;
|
||||||
|
|
||||||
rt = mf->js_malloc(&ms, sizeof(JSRuntime));
|
rt = mf->js_malloc(&ms, sizeof(JSRuntime));
|
||||||
if (!rt)
|
if (!rt)
|
||||||
|
@ -1685,7 +1685,8 @@ static void *js_def_malloc(JSMallocState *s, size_t size)
|
||||||
/* Do not allocate zero bytes: behavior is platform dependent */
|
/* Do not allocate zero bytes: behavior is platform dependent */
|
||||||
assert(size != 0);
|
assert(size != 0);
|
||||||
|
|
||||||
if (unlikely(s->malloc_size + size > s->malloc_limit))
|
/* When malloc_limit is 0 (unlimited), malloc_limit - 1 will be SIZE_MAX. */
|
||||||
|
if (unlikely(s->malloc_size + size > s->malloc_limit - 1))
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
ptr = malloc(size);
|
ptr = malloc(size);
|
||||||
|
@ -1723,7 +1724,8 @@ static void *js_def_realloc(JSMallocState *s, void *ptr, size_t size)
|
||||||
free(ptr);
|
free(ptr);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
if (s->malloc_size + size - old_size > s->malloc_limit)
|
/* When malloc_limit is 0 (unlimited), malloc_limit - 1 will be SIZE_MAX. */
|
||||||
|
if (s->malloc_size + size - old_size > s->malloc_limit - 1)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
ptr = realloc(ptr, size);
|
ptr = realloc(ptr, size);
|
||||||
|
|
|
@ -291,6 +291,7 @@ typedef struct JSGCObjectHeader JSGCObjectHeader;
|
||||||
JS_EXTERN JSRuntime *JS_NewRuntime(void);
|
JS_EXTERN JSRuntime *JS_NewRuntime(void);
|
||||||
/* info lifetime must exceed that of rt */
|
/* info lifetime must exceed that of rt */
|
||||||
JS_EXTERN void JS_SetRuntimeInfo(JSRuntime *rt, const char *info);
|
JS_EXTERN void JS_SetRuntimeInfo(JSRuntime *rt, const char *info);
|
||||||
|
/* use 0 to disable memory limit */
|
||||||
JS_EXTERN void JS_SetMemoryLimit(JSRuntime *rt, size_t limit);
|
JS_EXTERN void JS_SetMemoryLimit(JSRuntime *rt, size_t limit);
|
||||||
JS_EXTERN void JS_SetDumpFlags(JSRuntime *rt, uint64_t flags);
|
JS_EXTERN void JS_SetDumpFlags(JSRuntime *rt, uint64_t flags);
|
||||||
JS_EXTERN void JS_SetGCThreshold(JSRuntime *rt, size_t gc_threshold);
|
JS_EXTERN void JS_SetGCThreshold(JSRuntime *rt, size_t gc_threshold);
|
||||||
|
|
Loading…
Reference in a new issue