Skip to content
Myr

jv_invalid() shouldn't allocate

Before adoption: this commit came in with the history the project adopted, so no landing records it.

jv_invalid() should behave like jv_invalid_with_msg(jv_null()), and neither should allocate memory, because neither ought to need to.

authored by Nicolas Williams · committed by Nicolas Williams · parent eb45372 · Browse files · 1 file+11 added−4 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+11 added−4 removed

  • other:+11 added−4 removed

1 file+11 added−4 removed

modified:jv.c+11 added-4 removed
59 unmodified lines
60 return "<unknown>";61}6263static const jv JV_NULL = {JV_KIND_NULL, 0, 0, 0, {0}};64static const jv JV_INVALID = {JV_KIND_INVALID, 0, 0, 0, {0}};65static const jv JV_FALSE = {JV_KIND_FALSE, 0, 0, 0, {0}};66static const jv JV_TRUE = {JV_KIND_TRUE, 0, 0, 0, {0}};6768jv jv_true() {
21 unmodified lines
90 jv errmsg;91} jvp_invalid;9293jv jv_invalid_with_msg(jv err) {94 if (jv_get_kind(err) == JV_KIND_NULL)95 return JV_INVALID;96 jvp_invalid* i = jv_mem_alloc(sizeof(jvp_invalid));97 i->refcnt = JV_REFCNT_INIT;98 i->errmsg = err;99
1 unmodified line
101 return x;102}103104jv jv_invalid() {102 return jv_invalid_with_msg(jv_null());105 return JV_INVALID;106}107108jv jv_invalid_get_msg(jv inv) {109 assert(jv_get_kind(inv) == JV_KIND_INVALID);110 jv x;111 if (inv.u.ptr == 0)112 x = jv_null();113 else107 jv x = jv_copy(((jvp_invalid*)inv.u.ptr)->errmsg);114 x = jv_copy(((jvp_invalid*)inv.u.ptr)->errmsg);115 jv_free(inv);116 return x;117}118
5 unmodified lines
124}125126static void jvp_invalid_free(jv x) {127 assert(jv_get_kind(x) == JV_KIND_INVALID);121 if (jvp_refcnt_dec(x.u.ptr)) {128 if (x.u.ptr != 0 && jvp_refcnt_dec(x.u.ptr)) {129 jv_free(((jvp_invalid*)x.u.ptr)->errmsg);130 jv_mem_free(x.u.ptr);131 }132}
1090 unmodified lines
1223jv jv_copy(jv j) {1224 if (jv_get_kind(j) == JV_KIND_ARRAY || 1225 jv_get_kind(j) == JV_KIND_STRING || 1226 jv_get_kind(j) == JV_KIND_OBJECT ||1220 jv_get_kind(j) == JV_KIND_INVALID) {1227 (jv_get_kind(j) == JV_KIND_INVALID && j.u.ptr != 0)) {1228 jvp_refcnt_inc(j.u.ptr);1229 }1230 return j;1231}
76 unmodified lines