Skip to content

Commit 8fbd438

Browse files
committed
core: use the JS allocator for all bindings
1 parent 59b17af commit 8fbd438

File tree

9 files changed

+38
-34
lines changed

9 files changed

+38
-34
lines changed

src/fswatch.c

+5-5
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ static void uv__fsevent_close_cb(uv_handle_t *handle) {
4444
if (fw) {
4545
fw->closed = 1;
4646
if (fw->finalized)
47-
free(fw);
47+
js_free(fw->ctx, fw);
4848
}
4949
}
5050

@@ -59,7 +59,7 @@ static void tjs_fswatch_finalizer(JSRuntime *rt, JSValue val) {
5959
JS_FreeValueRT(rt, fw->callback);
6060
fw->finalized = 1;
6161
if (fw->closed)
62-
free(fw);
62+
js_free_rt(rt, fw);
6363
else
6464
maybe_close(fw);
6565
}
@@ -167,7 +167,7 @@ static JSValue tjs_fs_watch(JSContext *ctx, JSValueConst this_val, int argc, JSV
167167
return JS_EXCEPTION;
168168
}
169169

170-
TJSFsWatch *fw = calloc(1, sizeof(*fw));
170+
TJSFsWatch *fw = js_mallocz(ctx, sizeof(*fw));
171171
if (!fw) {
172172
JS_FreeCString(ctx, path);
173173
JS_FreeValue(ctx, obj);
@@ -178,15 +178,15 @@ static JSValue tjs_fs_watch(JSContext *ctx, JSValueConst this_val, int argc, JSV
178178
if (r != 0) {
179179
JS_FreeCString(ctx, path);
180180
JS_FreeValue(ctx, obj);
181-
free(fw);
181+
js_free(ctx, fw);
182182
return JS_ThrowInternalError(ctx, "couldn't initialize handle");
183183
}
184184

185185
r = uv_fs_event_start(&fw->handle, uv__fs_event_cb, path, UV_FS_EVENT_RECURSIVE);
186186
if (r != 0) {
187187
JS_FreeCString(ctx, path);
188188
JS_FreeValue(ctx, obj);
189-
free(fw);
189+
js_free(ctx, fw);
190190
return tjs_throw_errno(ctx, r);
191191
}
192192

src/process.c

+4-4
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ static void uv__close_cb(uv_handle_t *handle) {
5050
CHECK_NOT_NULL(p);
5151
p->closed = true;
5252
if (p->finalized)
53-
free(p);
53+
js_free(p->ctx, p);
5454
}
5555

5656
static void maybe_close(TJSProcess *p) {
@@ -67,7 +67,7 @@ static void tjs_process_finalizer(JSRuntime *rt, JSValue val) {
6767
JS_FreeValueRT(rt, p->stdio[2]);
6868
p->finalized = true;
6969
if (p->closed)
70-
free(p);
70+
js_free_rt(rt, p);
7171
else
7272
maybe_close(p);
7373
}
@@ -178,7 +178,7 @@ static JSValue tjs_spawn(JSContext *ctx, JSValueConst this_val, int argc, JSValu
178178
if (JS_IsException(obj))
179179
return obj;
180180

181-
TJSProcess *p = calloc(1, sizeof(*p));
181+
TJSProcess *p = js_mallocz(ctx, sizeof(*p));
182182
if (!p) {
183183
JS_FreeValue(ctx, obj);
184184
return JS_EXCEPTION;
@@ -441,7 +441,7 @@ static JSValue tjs_spawn(JSContext *ctx, JSValueConst this_val, int argc, JSValu
441441
JS_FreeValue(ctx, p->stdio[0]);
442442
JS_FreeValue(ctx, p->stdio[1]);
443443
JS_FreeValue(ctx, p->stdio[2]);
444-
free(p);
444+
js_free(ctx, p);
445445
JS_FreeValue(ctx, obj);
446446
ret = JS_EXCEPTION;
447447
cleanup:

src/signals.c

+5-5
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ static void uv__signal_close_cb(uv_handle_t *handle) {
4343
if (sh) {
4444
sh->closed = 1;
4545
if (sh->finalized)
46-
free(sh);
46+
js_free(sh->ctx, sh);
4747
}
4848
}
4949

@@ -58,7 +58,7 @@ static void tjs_signal_handler_finalizer(JSRuntime *rt, JSValue val) {
5858
JS_FreeValueRT(rt, sh->func);
5959
sh->finalized = 1;
6060
if (sh->closed)
61-
free(sh);
61+
js_free_rt(rt, sh);
6262
else
6363
maybe_close(sh);
6464
}
@@ -96,7 +96,7 @@ static JSValue tjs_signal(JSContext *ctx, JSValueConst this_val, int argc, JSVal
9696
if (JS_IsException(obj))
9797
return obj;
9898

99-
TJSSignalHandler *sh = calloc(1, sizeof(*sh));
99+
TJSSignalHandler *sh = js_mallocz(ctx, sizeof(*sh));
100100
if (!sh) {
101101
JS_FreeValue(ctx, obj);
102102
return JS_EXCEPTION;
@@ -105,14 +105,14 @@ static JSValue tjs_signal(JSContext *ctx, JSValueConst this_val, int argc, JSVal
105105
int r = uv_signal_init(tjs_get_loop(ctx), &sh->handle);
106106
if (r != 0) {
107107
JS_FreeValue(ctx, obj);
108-
free(sh);
108+
js_free(ctx, sh);
109109
return JS_ThrowInternalError(ctx, "couldn't initialize Signal handle");
110110
}
111111

112112
r = uv_signal_start(&sh->handle, uv__signal_cb, sig_num);
113113
if (r != 0) {
114114
JS_FreeValue(ctx, obj);
115-
free(sh);
115+
js_free(ctx, sh);
116116
return tjs_throw_errno(ctx, r);
117117
}
118118
uv_unref((uv_handle_t *) &sh->handle);

src/streams.c

+10-8
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ static JSValue tjs_new_tcp(JSContext *ctx, int af);
3636

3737
typedef struct {
3838
JSContext *ctx;
39+
JSRuntime *rt;
3940
int closed;
4041
int finalized;
4142
union {
@@ -82,7 +83,7 @@ static void uv__stream_close_cb(uv_handle_t *handle) {
8283
CHECK_NOT_NULL(s);
8384
s->closed = 1;
8485
if (s->finalized)
85-
free(s);
86+
js_free_rt(s->rt, s);
8687
}
8788

8889
static void maybe_close(TJSStream *s) {
@@ -399,6 +400,7 @@ static JSValue tjs_stream_set_blocking(JSContext *ctx, TJSStream *s, int argc, J
399400

400401
static JSValue tjs_init_stream(JSContext *ctx, JSValue obj, TJSStream *s) {
401402
s->ctx = ctx;
403+
s->rt = JS_GetRuntime(ctx);
402404
s->closed = 0;
403405
s->finalized = 0;
404406

@@ -422,7 +424,7 @@ static void tjs_stream_finalizer(JSRuntime *rt, TJSStream *s) {
422424
JS_FreeValueRT(rt, s->read.b.tarray);
423425
s->finalized = 1;
424426
if (s->closed)
425-
free(s);
427+
js_free_rt(rt, s);
426428
else
427429
maybe_close(s);
428430
}
@@ -466,7 +468,7 @@ static JSValue tjs_new_tcp(JSContext *ctx, int af) {
466468
if (JS_IsException(obj))
467469
return obj;
468470

469-
s = calloc(1, sizeof(*s));
471+
s = js_mallocz(ctx, sizeof(*s));
470472
if (!s) {
471473
JS_FreeValue(ctx, obj);
472474
return JS_EXCEPTION;
@@ -475,7 +477,7 @@ static JSValue tjs_new_tcp(JSContext *ctx, int af) {
475477
r = uv_tcp_init_ex(tjs_get_loop(ctx), &s->h.tcp, af);
476478
if (r != 0) {
477479
JS_FreeValue(ctx, obj);
478-
free(s);
480+
js_free(ctx, s);
479481
return JS_ThrowInternalError(ctx, "couldn't initialize TCP handle");
480482
}
481483

@@ -644,7 +646,7 @@ static JSValue tjs_tty_constructor(JSContext *ctx, JSValueConst new_target, int
644646
if (JS_IsException(obj))
645647
return obj;
646648

647-
s = calloc(1, sizeof(*s));
649+
s = js_mallocz(ctx, sizeof(*s));
648650
if (!s) {
649651
JS_FreeValue(ctx, obj);
650652
return JS_EXCEPTION;
@@ -653,7 +655,7 @@ static JSValue tjs_tty_constructor(JSContext *ctx, JSValueConst new_target, int
653655
r = uv_tty_init(tjs_get_loop(ctx), &s->h.tty, fd, readable);
654656
if (r != 0) {
655657
JS_FreeValue(ctx, obj);
656-
free(s);
658+
js_free(ctx, s);
657659
return JS_ThrowInternalError(ctx, "couldn't initialize TTY handle");
658660
}
659661

@@ -726,7 +728,7 @@ JSValue tjs_new_pipe(JSContext *ctx) {
726728
if (JS_IsException(obj))
727729
return obj;
728730

729-
s = calloc(1, sizeof(*s));
731+
s = js_mallocz(ctx, sizeof(*s));
730732
if (!s) {
731733
JS_FreeValue(ctx, obj);
732734
return JS_EXCEPTION;
@@ -735,7 +737,7 @@ JSValue tjs_new_pipe(JSContext *ctx) {
735737
r = uv_pipe_init(tjs_get_loop(ctx), &s->h.pipe, 0);
736738
if (r != 0) {
737739
JS_FreeValue(ctx, obj);
738-
free(s);
740+
js_free(ctx, s);
739741
return JS_ThrowInternalError(ctx, "couldn't initialize Pipe handle");
740742
}
741743

src/timers.c

+4-2
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828

2929
typedef struct {
3030
JSContext *ctx;
31+
JSRuntime *rt;
3132
uv_timer_t handle;
3233
int interval;
3334
JSValue func;
@@ -63,7 +64,7 @@ static void call_timer(TJSTimer *th) {
6364
static void uv__timer_close(uv_handle_t *handle) {
6465
TJSTimer *th = handle->data;
6566
CHECK_NOT_NULL(th);
66-
free(th);
67+
js_free_rt(th->rt, th);
6768
}
6869

6970
static void uv__timer_cb(uv_timer_t *handle) {
@@ -129,13 +130,14 @@ static JSValue tjs_setTimeout(JSContext *ctx, JSValueConst this_val, int argc, J
129130
nargs = 0;
130131
}
131132

132-
th = calloc(1, sizeof(*th) + nargs * sizeof(JSValue));
133+
th = js_mallocz(ctx, sizeof(*th) + nargs * sizeof(JSValue));
133134
if (!th) {
134135
JS_FreeValue(ctx, obj);
135136
return JS_EXCEPTION;
136137
}
137138

138139
th->ctx = ctx;
140+
th->rt = JS_GetRuntime(ctx);
139141
CHECK_EQ(uv_timer_init(tjs_get_loop(ctx), &th->handle), 0);
140142
th->handle.data = th;
141143
th->interval = magic;

src/udp.c

+4-4
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ static void uv__udp_close_cb(uv_handle_t *handle) {
5656
CHECK_NOT_NULL(u);
5757
u->closed = 1;
5858
if (u->finalized)
59-
free(u);
59+
js_free(u->ctx, u);
6060
}
6161

6262
static void maybe_close(TJSUdp *u) {
@@ -71,7 +71,7 @@ static void tjs_udp_finalizer(JSRuntime *rt, JSValue val) {
7171
JS_FreeValueRT(rt, u->read.b.tarray);
7272
u->finalized = 1;
7373
if (u->closed)
74-
free(u);
74+
js_free_rt(rt, u);
7575
else
7676
maybe_close(u);
7777
}
@@ -282,7 +282,7 @@ static JSValue tjs_new_udp(JSContext *ctx, int af) {
282282
if (JS_IsException(obj))
283283
return obj;
284284

285-
u = calloc(1, sizeof(*u));
285+
u = js_mallocz(ctx, sizeof(*u));
286286
if (!u) {
287287
JS_FreeValue(ctx, obj);
288288
return JS_EXCEPTION;
@@ -291,7 +291,7 @@ static JSValue tjs_new_udp(JSContext *ctx, int af) {
291291
r = uv_udp_init_ex(tjs_get_loop(ctx), &u->udp, af);
292292
if (r != 0) {
293293
JS_FreeValue(ctx, obj);
294-
free(u);
294+
js_free(ctx, u);
295295
return JS_ThrowInternalError(ctx, "couldn't initialize UDP handle");
296296
}
297297

src/worker.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ static void worker_entry(void *arg) {
131131
static void uv__close_cb(uv_handle_t *handle) {
132132
TJSWorker *w = handle->data;
133133
CHECK_NOT_NULL(w);
134-
free(w);
134+
js_free(w->ctx, w);
135135
}
136136

137137
static void tjs_worker_finalizer(JSRuntime *rt, JSValue val) {
@@ -227,7 +227,7 @@ static JSValue tjs_new_worker(JSContext *ctx, uv_os_sock_t channel_fd, bool is_m
227227
if (JS_IsException(obj))
228228
return obj;
229229

230-
TJSWorker *w = calloc(1, sizeof(*w));
230+
TJSWorker *w = js_mallocz(ctx, sizeof(*w));
231231
if (!w) {
232232
JS_FreeValue(ctx, obj);
233233
return JS_EXCEPTION;

src/ws.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ static void tjs_ws_finalizer(JSRuntime *rt, JSValue val) {
5050
}
5151
for (int i = 0; i < WS_EVENT_MAX; i++)
5252
JS_FreeValueRT(rt, w->events[i]);
53-
free(w);
53+
js_free_rt(rt, w);
5454
}
5555
}
5656

@@ -140,7 +140,7 @@ static JSValue tjs_ws_constructor(JSContext *ctx, JSValueConst new_target, int a
140140
if (JS_IsException(obj))
141141
return obj;
142142

143-
TJSWs *w = calloc(1, sizeof(*w));
143+
TJSWs *w = js_mallocz(ctx, sizeof(*w));
144144
if (!w) {
145145
JS_FreeValue(ctx, obj);
146146
return JS_EXCEPTION;

src/xhr.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ static void tjs_xhr_finalizer(JSRuntime *rt, JSValue val) {
107107
JS_FreeValueRT(rt, x->result.response_text);
108108
dbuf_free(&x->result.hbuf);
109109
dbuf_free(&x->result.bbuf);
110-
free(x);
110+
js_free_rt(rt, x);
111111
}
112112
}
113113

@@ -301,7 +301,7 @@ static JSValue tjs_xhr_constructor(JSContext *ctx, JSValueConst new_target, int
301301
if (JS_IsException(obj))
302302
return obj;
303303

304-
TJSXhr *x = calloc(1, sizeof(*x));
304+
TJSXhr *x = js_mallocz(ctx, sizeof(*x));
305305
if (!x) {
306306
JS_FreeValue(ctx, obj);
307307
return JS_EXCEPTION;

0 commit comments

Comments
 (0)