@@ -67,6 +67,7 @@ typedef sig_t sighandler_t;
67
67
#include <stdatomic.h>
68
68
#endif
69
69
70
+ #include "mimalloc.h"
70
71
#include "include/quickjs/cutils.h"
71
72
#include "include/quickjs/list.h"
72
73
#include "quickjs-libc.h"
@@ -384,15 +385,15 @@ uint8_t *js_load_file(JSContext *ctx, size_t *pbuf_len, const char *filename)
384
385
if (ctx )
385
386
buf = js_malloc (ctx , buf_len + 1 );
386
387
else
387
- buf = malloc (buf_len + 1 );
388
+ buf = mi_malloc (buf_len + 1 );
388
389
if (!buf )
389
390
goto fail ;
390
391
if (fread (buf , 1 , buf_len , f ) != buf_len ) {
391
392
errno = EIO ;
392
393
if (ctx )
393
394
js_free (ctx , buf );
394
395
else
395
- free (buf );
396
+ mi_free (buf );
396
397
fail :
397
398
fclose (f );
398
399
return NULL ;
@@ -535,7 +536,7 @@ int js_module_set_import_meta(JSContext *ctx, JSValueConst func_val,
535
536
because the corresponding module source code is not
536
537
necessarily present */
537
538
if (use_realpath ) {
538
- char * res = realpath (module_name , buf + strlen (buf ));
539
+ char * res = mi_realpath (module_name , buf + strlen (buf ));
539
540
if (!res ) {
540
541
JS_ThrowTypeError (ctx , "realpath failure" );
541
542
JS_FreeCString (ctx , module_name );
@@ -630,13 +631,13 @@ static void setenv(const char *name, const char *value, int overwrite)
630
631
size_t name_len , value_len ;
631
632
name_len = strlen (name );
632
633
value_len = strlen (value );
633
- str = malloc (name_len + 1 + value_len + 1 );
634
+ str = mi_malloc (name_len + 1 + value_len + 1 );
634
635
memcpy (str , name , name_len );
635
636
str [name_len ] = '=' ;
636
637
memcpy (str + name_len + 1 , value , value_len );
637
638
str [name_len + 1 + value_len ] = '\0' ;
638
639
_putenv (str );
639
- free (str );
640
+ mi_free (str );
640
641
}
641
642
642
643
static void unsetenv (const char * name )
@@ -2639,7 +2640,7 @@ static JSValue js_os_sleep(JSContext *ctx, JSValueConst this_val,
2639
2640
}
2640
2641
2641
2642
#if defined(_WIN32 )
2642
- static char * realpath (const char * path , char * buf )
2643
+ static char * mi_realpath (const char * path , char * buf )
2643
2644
{
2644
2645
if (!_fullpath (buf , path , PATH_MAX )) {
2645
2646
errno = ENOENT ;
@@ -2661,7 +2662,7 @@ static JSValue js_os_realpath(JSContext *ctx, JSValueConst this_val,
2661
2662
path = JS_ToCString (ctx , argv [0 ]);
2662
2663
if (!path )
2663
2664
return JS_EXCEPTION ;
2664
- res = realpath (path , buf );
2665
+ res = mi_realpath (path , buf );
2665
2666
JS_FreeCString (ctx , path );
2666
2667
if (!res ) {
2667
2668
buf [0 ] = '\0' ;
@@ -3153,7 +3154,7 @@ static int atomic_add_int(int *ptr, int v)
3153
3154
static void * js_sab_alloc (void * opaque , size_t size )
3154
3155
{
3155
3156
JSSABHeader * sab ;
3156
- sab = malloc (sizeof (JSSABHeader ) + size );
3157
+ sab = mi_malloc (sizeof (JSSABHeader ) + size );
3157
3158
if (!sab )
3158
3159
return NULL ;
3159
3160
sab -> ref_count = 1 ;
@@ -3168,7 +3169,7 @@ static void js_sab_free(void *opaque, void *ptr)
3168
3169
ref_count = atomic_add_int (& sab -> ref_count , -1 );
3169
3170
assert (ref_count >= 0 );
3170
3171
if (ref_count == 0 ) {
3171
- free (sab );
3172
+ mi_free (sab );
3172
3173
}
3173
3174
}
3174
3175
@@ -3187,7 +3188,7 @@ static JSWorkerMessagePipe *js_new_message_pipe(void)
3187
3188
if (pipe (pipe_fds ) < 0 )
3188
3189
return NULL ;
3189
3190
3190
- ps = malloc (sizeof (* ps ));
3191
+ ps = mi_malloc (sizeof (* ps ));
3191
3192
if (!ps ) {
3192
3193
close (pipe_fds [0 ]);
3193
3194
close (pipe_fds [1 ]);
@@ -3214,9 +3215,9 @@ static void js_free_message(JSWorkerMessage *msg)
3214
3215
for (i = 0 ; i < msg -> sab_tab_len ; i ++ ) {
3215
3216
js_sab_free (NULL , msg -> sab_tab [i ]);
3216
3217
}
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 );
3220
3221
}
3221
3222
3222
3223
static void js_free_message_pipe (JSWorkerMessagePipe * ps )
@@ -3238,7 +3239,7 @@ static void js_free_message_pipe(JSWorkerMessagePipe *ps)
3238
3239
pthread_mutex_destroy (& ps -> mutex );
3239
3240
close (ps -> read_fd );
3240
3241
close (ps -> write_fd );
3241
- free (ps );
3242
+ mi_free (ps );
3242
3243
}
3243
3244
}
3244
3245
@@ -3302,9 +3303,9 @@ static void *worker_func(void *opaque)
3302
3303
3303
3304
if (!JS_RunModule (ctx , args -> basename , args -> filename ))
3304
3305
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 );
3308
3309
3309
3310
js_std_loop (ctx );
3310
3311
@@ -3379,12 +3380,12 @@ static JSValue js_worker_ctor(JSContext *ctx, JSValueConst new_target,
3379
3380
if (!filename )
3380
3381
goto fail ;
3381
3382
3382
- args = malloc (sizeof (* args ));
3383
+ args = mi_malloc (sizeof (* args ));
3383
3384
if (!args )
3384
3385
goto oom_fail ;
3385
3386
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 );
3388
3389
3389
3390
/* ports */
3390
3391
args -> recv_pipe = js_new_message_pipe ();
@@ -3417,11 +3418,11 @@ static JSValue js_worker_ctor(JSContext *ctx, JSValueConst new_target,
3417
3418
JS_FreeCString (ctx , basename );
3418
3419
JS_FreeCString (ctx , filename );
3419
3420
if (args ) {
3420
- free (args -> filename );
3421
- free (args -> basename );
3421
+ mi_free (args -> filename );
3422
+ mi_free (args -> basename );
3422
3423
js_free_message_pipe (args -> recv_pipe );
3423
3424
js_free_message_pipe (args -> send_pipe );
3424
- free (args );
3425
+ mi_free (args );
3425
3426
}
3426
3427
JS_FreeValue (ctx , obj );
3427
3428
return JS_EXCEPTION ;
@@ -3446,20 +3447,20 @@ static JSValue js_worker_postMessage(JSContext *ctx, JSValueConst this_val,
3446
3447
if (!data )
3447
3448
return JS_EXCEPTION ;
3448
3449
3449
- msg = malloc (sizeof (* msg ));
3450
+ msg = mi_malloc (sizeof (* msg ));
3450
3451
if (!msg )
3451
3452
goto fail ;
3452
3453
msg -> data = NULL ;
3453
3454
msg -> sab_tab = NULL ;
3454
3455
3455
3456
/* must reallocate because the allocator may be different */
3456
- msg -> data = malloc (data_len );
3457
+ msg -> data = mi_malloc (data_len );
3457
3458
if (!msg -> data )
3458
3459
goto fail ;
3459
3460
memcpy (msg -> data , data , data_len );
3460
3461
msg -> data_len = data_len ;
3461
3462
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 );
3463
3464
if (!msg -> sab_tab )
3464
3465
goto fail ;
3465
3466
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,
3492
3493
return JS_UNDEFINED ;
3493
3494
fail :
3494
3495
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 );
3498
3499
}
3499
3500
js_free (ctx , data );
3500
3501
js_free (ctx , sab_tab );
@@ -3768,7 +3769,7 @@ void js_std_init_handlers(JSRuntime *rt)
3768
3769
{
3769
3770
JSThreadState * ts ;
3770
3771
3771
- ts = malloc (sizeof (* ts ));
3772
+ ts = mi_malloc (sizeof (* ts ));
3772
3773
if (!ts ) {
3773
3774
fprintf (stderr , "Could not allocate memory for the worker" );
3774
3775
exit (1 );
@@ -3822,7 +3823,7 @@ void js_std_free_handlers(JSRuntime *rt)
3822
3823
js_free_message_pipe (ts -> send_pipe );
3823
3824
#endif
3824
3825
3825
- free (ts );
3826
+ mi_free (ts );
3826
3827
JS_SetRuntimeOpaque (rt , NULL ); /* fail safe */
3827
3828
}
3828
3829
0 commit comments