mirror of
https://github.com/janet-lang/janet
synced 2024-11-25 09:47:17 +00:00
Remove array_init and array_deinit
These functions made it very easy to create memory leaks, and are better replaced with functions in vector.h or simply using non-stack allocated arrays.
This commit is contained in:
parent
b082c8123e
commit
efb2ab06cb
@ -28,8 +28,9 @@
|
|||||||
|
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
/* Initializes an array */
|
/* Creates a new array */
|
||||||
JanetArray *janet_array_init(JanetArray *array, int32_t capacity) {
|
JanetArray *janet_array(int32_t capacity) {
|
||||||
|
JanetArray *array = janet_gcalloc(JANET_MEMORY_ARRAY, sizeof(JanetArray));
|
||||||
Janet *data = NULL;
|
Janet *data = NULL;
|
||||||
if (capacity > 0) {
|
if (capacity > 0) {
|
||||||
data = (Janet *) malloc(sizeof(Janet) * capacity);
|
data = (Janet *) malloc(sizeof(Janet) * capacity);
|
||||||
@ -43,16 +44,6 @@ JanetArray *janet_array_init(JanetArray *array, int32_t capacity) {
|
|||||||
return array;
|
return array;
|
||||||
}
|
}
|
||||||
|
|
||||||
void janet_array_deinit(JanetArray *array) {
|
|
||||||
free(array->data);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Creates a new array */
|
|
||||||
JanetArray *janet_array(int32_t capacity) {
|
|
||||||
JanetArray *array = janet_gcalloc(JANET_MEMORY_ARRAY, sizeof(JanetArray));
|
|
||||||
return janet_array_init(array, capacity);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Creates a new array from n elements. */
|
/* Creates a new array from n elements. */
|
||||||
JanetArray *janet_array_n(const Janet *elements, int32_t n) {
|
JanetArray *janet_array_n(const Janet *elements, int32_t n) {
|
||||||
JanetArray *array = janet_gcalloc(JANET_MEMORY_ARRAY, sizeof(JanetArray));
|
JanetArray *array = janet_gcalloc(JANET_MEMORY_ARRAY, sizeof(JanetArray));
|
||||||
|
@ -262,7 +262,7 @@ static void janet_deinit_block(JanetGCObject *mem) {
|
|||||||
janet_symbol_deinit(((JanetStringHead *) mem)->data);
|
janet_symbol_deinit(((JanetStringHead *) mem)->data);
|
||||||
break;
|
break;
|
||||||
case JANET_MEMORY_ARRAY:
|
case JANET_MEMORY_ARRAY:
|
||||||
janet_array_deinit((JanetArray *) mem);
|
free(((JanetArray *) mem)->data);
|
||||||
break;
|
break;
|
||||||
case JANET_MEMORY_TABLE:
|
case JANET_MEMORY_TABLE:
|
||||||
janet_table_deinit((JanetTable *) mem);
|
janet_table_deinit((JanetTable *) mem);
|
||||||
|
@ -535,7 +535,6 @@ void janet_marshal(
|
|||||||
st.rreg = rreg;
|
st.rreg = rreg;
|
||||||
janet_table_init(&st.seen, 0);
|
janet_table_init(&st.seen, 0);
|
||||||
marshal_one(&st, x, flags);
|
marshal_one(&st, x, flags);
|
||||||
/* Clean up. See comment in janet_unmarshal about autoreleasing memory on panics.*/
|
|
||||||
janet_table_deinit(&st.seen);
|
janet_table_deinit(&st.seen);
|
||||||
janet_v_free(st.seen_envs);
|
janet_v_free(st.seen_envs);
|
||||||
janet_v_free(st.seen_defs);
|
janet_v_free(st.seen_defs);
|
||||||
@ -543,7 +542,7 @@ void janet_marshal(
|
|||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
jmp_buf err;
|
jmp_buf err;
|
||||||
JanetArray lookup;
|
Janet *lookup;
|
||||||
JanetTable *reg;
|
JanetTable *reg;
|
||||||
JanetFuncEnv **lookup_envs;
|
JanetFuncEnv **lookup_envs;
|
||||||
JanetFuncDef **lookup_defs;
|
JanetFuncDef **lookup_defs;
|
||||||
@ -849,7 +848,7 @@ static const uint8_t *unmarshal_one_fiber(
|
|||||||
fiber->env = NULL;
|
fiber->env = NULL;
|
||||||
|
|
||||||
/* Push fiber to seen stack */
|
/* Push fiber to seen stack */
|
||||||
janet_array_push(&st->lookup, janet_wrap_fiber(fiber));
|
janet_v_push(st->lookup, janet_wrap_fiber(fiber));
|
||||||
|
|
||||||
/* Set frame later so fiber can be GCed at anytime if unmarshalling fails */
|
/* Set frame later so fiber can be GCed at anytime if unmarshalling fails */
|
||||||
int32_t frame = 0;
|
int32_t frame = 0;
|
||||||
@ -1071,7 +1070,7 @@ static const uint8_t *unmarshal_one(
|
|||||||
memcpy(&u.bytes, data + 1, sizeof(double));
|
memcpy(&u.bytes, data + 1, sizeof(double));
|
||||||
#endif
|
#endif
|
||||||
*out = janet_wrap_number(u.d);
|
*out = janet_wrap_number(u.d);
|
||||||
janet_array_push(&st->lookup, *out);
|
janet_v_push(st->lookup, *out);
|
||||||
return data + 9;
|
return data + 9;
|
||||||
}
|
}
|
||||||
case LB_STRING:
|
case LB_STRING:
|
||||||
@ -1104,7 +1103,7 @@ static const uint8_t *unmarshal_one(
|
|||||||
memcpy(buffer->data, data, len);
|
memcpy(buffer->data, data, len);
|
||||||
*out = janet_wrap_buffer(buffer);
|
*out = janet_wrap_buffer(buffer);
|
||||||
}
|
}
|
||||||
janet_array_push(&st->lookup, *out);
|
janet_v_push(st->lookup, *out);
|
||||||
return data + len;
|
return data + len;
|
||||||
}
|
}
|
||||||
case LB_FIBER: {
|
case LB_FIBER: {
|
||||||
@ -1121,7 +1120,7 @@ static const uint8_t *unmarshal_one(
|
|||||||
def->environments_length * sizeof(JanetFuncEnv));
|
def->environments_length * sizeof(JanetFuncEnv));
|
||||||
func->def = def;
|
func->def = def;
|
||||||
*out = janet_wrap_function(func);
|
*out = janet_wrap_function(func);
|
||||||
janet_array_push(&st->lookup, *out);
|
janet_v_push(st->lookup, *out);
|
||||||
for (int32_t i = 0; i < def->environments_length; i++) {
|
for (int32_t i = 0; i < def->environments_length; i++) {
|
||||||
data = unmarshal_one_env(st, data, &(func->envs[i]), flags + 1);
|
data = unmarshal_one_env(st, data, &(func->envs[i]), flags + 1);
|
||||||
}
|
}
|
||||||
@ -1146,7 +1145,7 @@ static const uint8_t *unmarshal_one(
|
|||||||
JanetArray *array = janet_array(len);
|
JanetArray *array = janet_array(len);
|
||||||
array->count = len;
|
array->count = len;
|
||||||
*out = janet_wrap_array(array);
|
*out = janet_wrap_array(array);
|
||||||
janet_array_push(&st->lookup, *out);
|
janet_v_push(st->lookup, *out);
|
||||||
for (int32_t i = 0; i < len; i++) {
|
for (int32_t i = 0; i < len; i++) {
|
||||||
data = unmarshal_one(st, data, array->data + i, flags + 1);
|
data = unmarshal_one(st, data, array->data + i, flags + 1);
|
||||||
}
|
}
|
||||||
@ -1159,7 +1158,7 @@ static const uint8_t *unmarshal_one(
|
|||||||
data = unmarshal_one(st, data, tup + i, flags + 1);
|
data = unmarshal_one(st, data, tup + i, flags + 1);
|
||||||
}
|
}
|
||||||
*out = janet_wrap_tuple(janet_tuple_end(tup));
|
*out = janet_wrap_tuple(janet_tuple_end(tup));
|
||||||
janet_array_push(&st->lookup, *out);
|
janet_v_push(st->lookup, *out);
|
||||||
} else if (lead == LB_STRUCT) {
|
} else if (lead == LB_STRUCT) {
|
||||||
/* Struct */
|
/* Struct */
|
||||||
JanetKV *struct_ = janet_struct_begin(len);
|
JanetKV *struct_ = janet_struct_begin(len);
|
||||||
@ -1170,16 +1169,16 @@ static const uint8_t *unmarshal_one(
|
|||||||
janet_struct_put(struct_, key, value);
|
janet_struct_put(struct_, key, value);
|
||||||
}
|
}
|
||||||
*out = janet_wrap_struct(janet_struct_end(struct_));
|
*out = janet_wrap_struct(janet_struct_end(struct_));
|
||||||
janet_array_push(&st->lookup, *out);
|
janet_v_push(st->lookup, *out);
|
||||||
} else if (lead == LB_REFERENCE) {
|
} else if (lead == LB_REFERENCE) {
|
||||||
if (len < 0 || len >= st->lookup.count)
|
if (len < 0 || len >= janet_v_count(st->lookup))
|
||||||
janet_panicf("invalid reference %d", len);
|
janet_panicf("invalid reference %d", len);
|
||||||
*out = st->lookup.data[len];
|
*out = st->lookup[len];
|
||||||
} else {
|
} else {
|
||||||
/* Table */
|
/* Table */
|
||||||
JanetTable *t = janet_table(len);
|
JanetTable *t = janet_table(len);
|
||||||
*out = janet_wrap_table(t);
|
*out = janet_wrap_table(t);
|
||||||
janet_array_push(&st->lookup, *out);
|
janet_v_push(st->lookup, *out);
|
||||||
if (lead == LB_TABLE_PROTO) {
|
if (lead == LB_TABLE_PROTO) {
|
||||||
Janet proto;
|
Janet proto;
|
||||||
data = unmarshal_one(st, data, &proto, flags + 1);
|
data = unmarshal_one(st, data, &proto, flags + 1);
|
||||||
@ -1216,17 +1215,14 @@ Janet janet_unmarshal(
|
|||||||
st.end = bytes + len;
|
st.end = bytes + len;
|
||||||
st.lookup_defs = NULL;
|
st.lookup_defs = NULL;
|
||||||
st.lookup_envs = NULL;
|
st.lookup_envs = NULL;
|
||||||
|
st.lookup = NULL;
|
||||||
st.reg = reg;
|
st.reg = reg;
|
||||||
janet_array_init(&st.lookup, 0);
|
|
||||||
Janet out;
|
Janet out;
|
||||||
const uint8_t *nextbytes = unmarshal_one(&st, bytes, &out, flags);
|
const uint8_t *nextbytes = unmarshal_one(&st, bytes, &out, flags);
|
||||||
if (next) *next = nextbytes;
|
if (next) *next = nextbytes;
|
||||||
/* Clean up - this should be auto released on panics, TODO. We should
|
|
||||||
* change the vector implementation to track allocations for auto release, and
|
|
||||||
* make st.lookup auto release as well, or move to heap. */
|
|
||||||
janet_array_deinit(&st.lookup);
|
|
||||||
janet_v_free(st.lookup_defs);
|
janet_v_free(st.lookup_defs);
|
||||||
janet_v_free(st.lookup_envs);
|
janet_v_free(st.lookup_envs);
|
||||||
|
janet_v_free(st.lookup);
|
||||||
return out;
|
return out;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1105,8 +1105,6 @@ JANET_API void janet_debug_find(
|
|||||||
/* Array functions */
|
/* Array functions */
|
||||||
JANET_API JanetArray *janet_array(int32_t capacity);
|
JANET_API JanetArray *janet_array(int32_t capacity);
|
||||||
JANET_API JanetArray *janet_array_n(const Janet *elements, int32_t n);
|
JANET_API JanetArray *janet_array_n(const Janet *elements, int32_t n);
|
||||||
JANET_API JanetArray *janet_array_init(JanetArray *array, int32_t capacity);
|
|
||||||
JANET_API void janet_array_deinit(JanetArray *array);
|
|
||||||
JANET_API void janet_array_ensure(JanetArray *array, int32_t capacity, int32_t growth);
|
JANET_API void janet_array_ensure(JanetArray *array, int32_t capacity, int32_t growth);
|
||||||
JANET_API void janet_array_setcount(JanetArray *array, int32_t count);
|
JANET_API void janet_array_setcount(JanetArray *array, int32_t count);
|
||||||
JANET_API void janet_array_push(JanetArray *array, Janet x);
|
JANET_API void janet_array_push(JanetArray *array, Janet x);
|
||||||
|
Loading…
Reference in New Issue
Block a user