Skip to content

Commit 6b83551

Browse files
committed
fix: compatible with OSX compilation
1 parent deb961d commit 6b83551

13 files changed

+132
-161
lines changed

.gitmodules

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
[submodule "vendor/mimalloc"]
22
path = vendor/mimalloc
33
url = https://github.com/microsoft/mimalloc
4-
branch = v2.0.9
4+
branch = v1.7.9

qjs.c

+8-34
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,8 @@
3232
#include <errno.h>
3333
#include <fcntl.h>
3434
#include <time.h>
35-
#if defined(__APPLE__)
36-
#include <malloc/malloc.h>
37-
#elif defined(__linux__)
38-
#include <malloc.h>
39-
#endif
4035

36+
#include "mimalloc.h"
4137
#include "include/quickjs/cutils.h"
4238
#include "quickjs-libc.h"
4339

@@ -142,18 +138,7 @@ static inline unsigned long long js_trace_malloc_ptr_offset(uint8_t *ptr,
142138
/* default memory allocation functions with memory limitation */
143139
static inline size_t js_trace_malloc_usable_size(void *ptr)
144140
{
145-
#if defined(__APPLE__)
146-
return malloc_size(ptr);
147-
#elif defined(_WIN32)
148-
return _msize(ptr);
149-
#elif defined(EMSCRIPTEN)
150-
return 0;
151-
#elif defined(__linux__)
152-
return malloc_usable_size(ptr);
153-
#else
154-
/* change this to `return 0;` if compilation fails */
155-
return malloc_usable_size(ptr);
156-
#endif
141+
return mi_usable_size(ptr);
157142
}
158143

159144
static void
@@ -198,7 +183,7 @@ __attribute__((format(printf, 2, 3)))
198183

199184
static void js_trace_malloc_init(struct trace_malloc_data *s)
200185
{
201-
free(s->base = malloc(8));
186+
mi_free(s->base = mi_malloc(8));
202187
}
203188

204189
static void *js_trace_malloc(JSMallocState *s, size_t size)
@@ -210,7 +195,7 @@ static void *js_trace_malloc(JSMallocState *s, size_t size)
210195

211196
if (unlikely(s->malloc_size + size > s->malloc_limit))
212197
return NULL;
213-
ptr = malloc(size);
198+
ptr = mi_malloc(size);
214199
js_trace_malloc_printf(s, "A %zd -> %p\n", size, ptr);
215200
if (ptr) {
216201
s->malloc_count++;
@@ -227,7 +212,7 @@ static void js_trace_free(JSMallocState *s, void *ptr)
227212
js_trace_malloc_printf(s, "F %p\n", ptr);
228213
s->malloc_count--;
229214
s->malloc_size -= js_trace_malloc_usable_size(ptr) + MALLOC_OVERHEAD;
230-
free(ptr);
215+
mi_free(ptr);
231216
}
232217

233218
static void *js_trace_realloc(JSMallocState *s, void *ptr, size_t size)
@@ -244,15 +229,15 @@ static void *js_trace_realloc(JSMallocState *s, void *ptr, size_t size)
244229
js_trace_malloc_printf(s, "R %zd %p\n", size, ptr);
245230
s->malloc_count--;
246231
s->malloc_size -= old_size + MALLOC_OVERHEAD;
247-
free(ptr);
232+
mi_free(ptr);
248233
return NULL;
249234
}
250235
if (s->malloc_size + size - old_size > s->malloc_limit)
251236
return NULL;
252237

253238
js_trace_malloc_printf(s, "R %zd %p", size, ptr);
254239

255-
ptr = realloc(ptr, size);
240+
ptr = mi_realloc(ptr, size);
256241
js_trace_malloc_printf(s, " -> %p\n", ptr);
257242
if (ptr) {
258243
s->malloc_size += js_trace_malloc_usable_size(ptr) - old_size;
@@ -264,18 +249,7 @@ static const JSMallocFunctions trace_mf = {
264249
js_trace_malloc,
265250
js_trace_free,
266251
js_trace_realloc,
267-
#if defined(__APPLE__)
268-
malloc_size,
269-
#elif defined(_WIN32)
270-
(size_t (*)(const void *))_msize,
271-
#elif defined(EMSCRIPTEN)
272-
NULL,
273-
#elif defined(__linux__)
274-
(size_t (*)(const void *))malloc_usable_size,
275-
#else
276-
/* change this to `NULL,` if compilation fails */
277-
malloc_usable_size,
278-
#endif
252+
mi_usable_size,
279253
};
280254

281255
#define PROG_NAME "qjs"

qjsc.c

+7-6
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#include <sys/wait.h>
3434
#endif
3535

36+
#include "mimalloc.h"
3637
#include "include/quickjs/cutils.h"
3738
#include "quickjs-libc.h"
3839

@@ -88,15 +89,15 @@ void namelist_add(namelist_t *lp, const char *name, const char *short_name,
8889
if (lp->count == lp->size) {
8990
size_t newsize = lp->size + (lp->size >> 1) + 4;
9091
namelist_entry_t *a =
91-
realloc(lp->array, sizeof(lp->array[0]) * newsize);
92+
mi_realloc(lp->array, sizeof(lp->array[0]) * newsize);
9293
/* XXX: check for realloc failure */
9394
lp->array = a;
9495
lp->size = newsize;
9596
}
9697
e = &lp->array[lp->count++];
97-
e->name = strdup(name);
98+
e->name = mi_strdup(name);
9899
if (short_name)
99-
e->short_name = strdup(short_name);
100+
e->short_name = mi_strdup(short_name);
100101
else
101102
e->short_name = NULL;
102103
e->flags = flags;
@@ -106,10 +107,10 @@ void namelist_free(namelist_t *lp)
106107
{
107108
while (lp->count > 0) {
108109
namelist_entry_t *e = &lp->array[--lp->count];
109-
free(e->name);
110-
free(e->short_name);
110+
mi_free(e->name);
111+
mi_free(e->short_name);
111112
}
112-
free(lp->array);
113+
mi_free(lp->array);
113114
lp->array = NULL;
114115
lp->size = 0;
115116
}

quickjs-libc.c

+32-31
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ typedef sig_t sighandler_t;
6767
#include <stdatomic.h>
6868
#endif
6969

70+
#include "mimalloc.h"
7071
#include "include/quickjs/cutils.h"
7172
#include "include/quickjs/list.h"
7273
#include "quickjs-libc.h"
@@ -384,15 +385,15 @@ uint8_t *js_load_file(JSContext *ctx, size_t *pbuf_len, const char *filename)
384385
if (ctx)
385386
buf = js_malloc(ctx, buf_len + 1);
386387
else
387-
buf = malloc(buf_len + 1);
388+
buf = mi_malloc(buf_len + 1);
388389
if (!buf)
389390
goto fail;
390391
if (fread(buf, 1, buf_len, f) != buf_len) {
391392
errno = EIO;
392393
if (ctx)
393394
js_free(ctx, buf);
394395
else
395-
free(buf);
396+
mi_free(buf);
396397
fail:
397398
fclose(f);
398399
return NULL;
@@ -535,7 +536,7 @@ int js_module_set_import_meta(JSContext *ctx, JSValueConst func_val,
535536
because the corresponding module source code is not
536537
necessarily present */
537538
if (use_realpath) {
538-
char *res = realpath(module_name, buf + strlen(buf));
539+
char *res = mi_realpath(module_name, buf + strlen(buf));
539540
if (!res) {
540541
JS_ThrowTypeError(ctx, "realpath failure");
541542
JS_FreeCString(ctx, module_name);
@@ -630,13 +631,13 @@ static void setenv(const char *name, const char *value, int overwrite)
630631
size_t name_len, value_len;
631632
name_len = strlen(name);
632633
value_len = strlen(value);
633-
str = malloc(name_len + 1 + value_len + 1);
634+
str = mi_malloc(name_len + 1 + value_len + 1);
634635
memcpy(str, name, name_len);
635636
str[name_len] = '=';
636637
memcpy(str + name_len + 1, value, value_len);
637638
str[name_len + 1 + value_len] = '\0';
638639
_putenv(str);
639-
free(str);
640+
mi_free(str);
640641
}
641642

642643
static void unsetenv(const char *name)
@@ -2639,7 +2640,7 @@ static JSValue js_os_sleep(JSContext *ctx, JSValueConst this_val,
26392640
}
26402641

26412642
#if defined(_WIN32)
2642-
static char *realpath(const char *path, char *buf)
2643+
static char *mi_realpath(const char *path, char *buf)
26432644
{
26442645
if (!_fullpath(buf, path, PATH_MAX)) {
26452646
errno = ENOENT;
@@ -2661,7 +2662,7 @@ static JSValue js_os_realpath(JSContext *ctx, JSValueConst this_val,
26612662
path = JS_ToCString(ctx, argv[0]);
26622663
if (!path)
26632664
return JS_EXCEPTION;
2664-
res = realpath(path, buf);
2665+
res = mi_realpath(path, buf);
26652666
JS_FreeCString(ctx, path);
26662667
if (!res) {
26672668
buf[0] = '\0';
@@ -3153,7 +3154,7 @@ static int atomic_add_int(int *ptr, int v)
31533154
static void *js_sab_alloc(void *opaque, size_t size)
31543155
{
31553156
JSSABHeader *sab;
3156-
sab = malloc(sizeof(JSSABHeader) + size);
3157+
sab = mi_malloc(sizeof(JSSABHeader) + size);
31573158
if (!sab)
31583159
return NULL;
31593160
sab->ref_count = 1;
@@ -3168,7 +3169,7 @@ static void js_sab_free(void *opaque, void *ptr)
31683169
ref_count = atomic_add_int(&sab->ref_count, -1);
31693170
assert(ref_count >= 0);
31703171
if (ref_count == 0) {
3171-
free(sab);
3172+
mi_free(sab);
31723173
}
31733174
}
31743175

@@ -3187,7 +3188,7 @@ static JSWorkerMessagePipe *js_new_message_pipe(void)
31873188
if (pipe(pipe_fds) < 0)
31883189
return NULL;
31893190

3190-
ps = malloc(sizeof(*ps));
3191+
ps = mi_malloc(sizeof(*ps));
31913192
if (!ps) {
31923193
close(pipe_fds[0]);
31933194
close(pipe_fds[1]);
@@ -3214,9 +3215,9 @@ static void js_free_message(JSWorkerMessage *msg)
32143215
for(i = 0; i < msg->sab_tab_len; i++) {
32153216
js_sab_free(NULL, msg->sab_tab[i]);
32163217
}
3217-
free(msg->sab_tab);
3218-
free(msg->data);
3219-
free(msg);
3218+
mi_free(msg->sab_tab);
3219+
mi_free(msg->data);
3220+
mi_free(msg);
32203221
}
32213222

32223223
static void js_free_message_pipe(JSWorkerMessagePipe *ps)
@@ -3238,7 +3239,7 @@ static void js_free_message_pipe(JSWorkerMessagePipe *ps)
32383239
pthread_mutex_destroy(&ps->mutex);
32393240
close(ps->read_fd);
32403241
close(ps->write_fd);
3241-
free(ps);
3242+
mi_free(ps);
32423243
}
32433244
}
32443245

@@ -3302,9 +3303,9 @@ static void *worker_func(void *opaque)
33023303

33033304
if (!JS_RunModule(ctx, args->basename, args->filename))
33043305
js_std_dump_error(ctx);
3305-
free(args->filename);
3306-
free(args->basename);
3307-
free(args);
3306+
mi_free(args->filename);
3307+
mi_free(args->basename);
3308+
mi_free(args);
33083309

33093310
js_std_loop(ctx);
33103311

@@ -3379,12 +3380,12 @@ static JSValue js_worker_ctor(JSContext *ctx, JSValueConst new_target,
33793380
if (!filename)
33803381
goto fail;
33813382

3382-
args = malloc(sizeof(*args));
3383+
args = mi_malloc(sizeof(*args));
33833384
if (!args)
33843385
goto oom_fail;
33853386
memset(args, 0, sizeof(*args));
3386-
args->filename = strdup(filename);
3387-
args->basename = strdup(basename);
3387+
args->filename = mi_strdup(filename);
3388+
args->basename = mi_strdup(basename);
33883389

33893390
/* ports */
33903391
args->recv_pipe = js_new_message_pipe();
@@ -3417,11 +3418,11 @@ static JSValue js_worker_ctor(JSContext *ctx, JSValueConst new_target,
34173418
JS_FreeCString(ctx, basename);
34183419
JS_FreeCString(ctx, filename);
34193420
if (args) {
3420-
free(args->filename);
3421-
free(args->basename);
3421+
mi_free(args->filename);
3422+
mi_free(args->basename);
34223423
js_free_message_pipe(args->recv_pipe);
34233424
js_free_message_pipe(args->send_pipe);
3424-
free(args);
3425+
mi_free(args);
34253426
}
34263427
JS_FreeValue(ctx, obj);
34273428
return JS_EXCEPTION;
@@ -3446,20 +3447,20 @@ static JSValue js_worker_postMessage(JSContext *ctx, JSValueConst this_val,
34463447
if (!data)
34473448
return JS_EXCEPTION;
34483449

3449-
msg = malloc(sizeof(*msg));
3450+
msg = mi_malloc(sizeof(*msg));
34503451
if (!msg)
34513452
goto fail;
34523453
msg->data = NULL;
34533454
msg->sab_tab = NULL;
34543455

34553456
/* must reallocate because the allocator may be different */
3456-
msg->data = malloc(data_len);
3457+
msg->data = mi_malloc(data_len);
34573458
if (!msg->data)
34583459
goto fail;
34593460
memcpy(msg->data, data, data_len);
34603461
msg->data_len = data_len;
34613462

3462-
msg->sab_tab = malloc(sizeof(msg->sab_tab[0]) * sab_tab_len);
3463+
msg->sab_tab = mi_malloc(sizeof(msg->sab_tab[0]) * sab_tab_len);
34633464
if (!msg->sab_tab)
34643465
goto fail;
34653466
memcpy(msg->sab_tab, sab_tab, sizeof(msg->sab_tab[0]) * sab_tab_len);
@@ -3492,9 +3493,9 @@ static JSValue js_worker_postMessage(JSContext *ctx, JSValueConst this_val,
34923493
return JS_UNDEFINED;
34933494
fail:
34943495
if (msg) {
3495-
free(msg->data);
3496-
free(msg->sab_tab);
3497-
free(msg);
3496+
mi_free(msg->data);
3497+
mi_free(msg->sab_tab);
3498+
mi_free(msg);
34983499
}
34993500
js_free(ctx, data);
35003501
js_free(ctx, sab_tab);
@@ -3768,7 +3769,7 @@ void js_std_init_handlers(JSRuntime *rt)
37683769
{
37693770
JSThreadState *ts;
37703771

3771-
ts = malloc(sizeof(*ts));
3772+
ts = mi_malloc(sizeof(*ts));
37723773
if (!ts) {
37733774
fprintf(stderr, "Could not allocate memory for the worker");
37743775
exit(1);
@@ -3822,7 +3823,7 @@ void js_std_free_handlers(JSRuntime *rt)
38223823
js_free_message_pipe(ts->send_pipe);
38233824
#endif
38243825

3825-
free(ts);
3826+
mi_free(ts);
38263827
JS_SetRuntimeOpaque(rt, NULL); /* fail safe */
38273828
}
38283829

0 commit comments

Comments
 (0)