Skip to content

Commit

Permalink
parsing and constructing json tests with oom
Browse files Browse the repository at this point in the history
  • Loading branch information
rheatley-pervasid authored and zpl-zak committed Jan 20, 2023
1 parent 94304c2 commit ef96f39
Show file tree
Hide file tree
Showing 7 changed files with 181 additions and 75 deletions.
11 changes: 6 additions & 5 deletions code/header/adt.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ typedef enum zpl_adt_error {
ZPL_ADT_ERROR_INTERNAL,
ZPL_ADT_ERROR_ALREADY_CONVERTED,
ZPL_ADT_ERROR_INVALID_TYPE,
ZPL_ADT_ERROR_OUT_OF_MEMORY,
} zpl_adt_error;

typedef struct zpl_adt_node {
Expand Down Expand Up @@ -218,7 +219,7 @@ ZPL_DEF void zpl_adt_remove_node(zpl_adt_node *node);
* @param backing
* @return
*/
ZPL_DEF void zpl_adt_set_obj(zpl_adt_node *obj, char const *name, zpl_allocator backing);
ZPL_DEF zpl_b8 zpl_adt_set_obj(zpl_adt_node *obj, char const *name, zpl_allocator backing);

/**
* @brief Initialise a node as an array
Expand All @@ -228,7 +229,7 @@ ZPL_DEF void zpl_adt_set_obj(zpl_adt_node *obj, char const *name, zpl_allocator
* @param backing
* @return
*/
ZPL_DEF void zpl_adt_set_arr(zpl_adt_node *obj, char const *name, zpl_allocator backing);
ZPL_DEF zpl_b8 zpl_adt_set_arr(zpl_adt_node *obj, char const *name, zpl_allocator backing);

/**
* @brief Initialise a node as a string
Expand All @@ -238,7 +239,7 @@ ZPL_DEF void zpl_adt_set_arr(zpl_adt_node *obj, char const *name, zpl_allocator
* @param value
* @return
*/
ZPL_DEF void zpl_adt_set_str(zpl_adt_node *obj, char const *name, char const *value);
ZPL_DEF zpl_b8 zpl_adt_set_str(zpl_adt_node *obj, char const *name, char const *value);

/**
* @brief Initialise a node as a float
Expand All @@ -248,7 +249,7 @@ ZPL_DEF void zpl_adt_set_str(zpl_adt_node *obj, char const *name, char const *va
* @param value
* @return
*/
ZPL_DEF void zpl_adt_set_flt(zpl_adt_node *obj, char const *name, zpl_f64 value);
ZPL_DEF zpl_b8 zpl_adt_set_flt(zpl_adt_node *obj, char const *name, zpl_f64 value);

/**
* @brief Initialise a node as a signed integer
Expand All @@ -258,7 +259,7 @@ ZPL_DEF void zpl_adt_set_flt(zpl_adt_node *obj, char const *name, zpl_f64 value)
* @param value
* @return
*/
ZPL_DEF void zpl_adt_set_int(zpl_adt_node *obj, char const *name, zpl_i64 value);
ZPL_DEF zpl_b8 zpl_adt_set_int(zpl_adt_node *obj, char const *name, zpl_i64 value);

/**
* @brief Append a new node to a container as an object
Expand Down
4 changes: 2 additions & 2 deletions code/header/core/file_stream.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ typedef enum {
* @param file
* @param allocator
*/
ZPL_DEF void zpl_file_stream_new(zpl_file* file, zpl_allocator allocator);
ZPL_DEF zpl_b8 zpl_file_stream_new(zpl_file* file, zpl_allocator allocator);

/**
* Opens a memory stream over an existing buffer
Expand All @@ -35,7 +35,7 @@ ZPL_DEF void zpl_file_stream_new(zpl_file* file, zpl_allocator allocator);
* @param size Buffer's size
* @param flags
*/
ZPL_DEF void zpl_file_stream_open(zpl_file* file, zpl_allocator allocator, zpl_u8 *buffer, zpl_isize size, zpl_file_stream_flags flags);
ZPL_DEF zpl_b8 zpl_file_stream_open(zpl_file* file, zpl_allocator allocator, zpl_u8 *buffer, zpl_isize size, zpl_file_stream_flags flags);

/**
* Retrieves the stream's underlying buffer and buffer size.
Expand Down
1 change: 1 addition & 0 deletions code/header/parsers/json.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ typedef enum zpl_json_error {
ZPL_JSON_ERROR_UNKNOWN_KEYWORD,
ZPL_JSON_ERROR_ARRAY_LEFT_OPEN,
ZPL_JSON_ERROR_OBJECT_END_PAIR_MISMATCHED,
ZPL_JSON_ERROR_OUT_OF_MEMORY,
} zpl_json_error;

typedef zpl_adt_node zpl_json_object;
Expand Down
35 changes: 25 additions & 10 deletions code/source/adt.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ zpl_u8 zpl_adt_make_branch(zpl_adt_node *node, zpl_allocator backing, char const
node->type = type;
node->name = name;
node->parent = parent;
zpl_array_init(node->nodes, backing);
if (!zpl_array_init(node->nodes, backing))
return ZPL_ADT_ERROR_OUT_OF_MEMORY;
return 0;
}

Expand Down Expand Up @@ -240,23 +241,26 @@ zpl_adt_node *zpl_adt_alloc(zpl_adt_node *parent) {
}


void zpl_adt_set_obj(zpl_adt_node *obj, char const *name, zpl_allocator backing) {
zpl_adt_make_branch(obj, backing, name, 0);
zpl_b8 zpl_adt_set_obj(zpl_adt_node *obj, char const *name, zpl_allocator backing) {
return zpl_adt_make_branch(obj, backing, name, 0);
}
void zpl_adt_set_arr(zpl_adt_node *obj, char const *name, zpl_allocator backing) {
zpl_adt_make_branch(obj, backing, name, 1);
zpl_b8 zpl_adt_set_arr(zpl_adt_node *obj, char const *name, zpl_allocator backing) {
return zpl_adt_make_branch(obj, backing, name, 1);
}
void zpl_adt_set_str(zpl_adt_node *obj, char const *name, char const *value) {
zpl_b8 zpl_adt_set_str(zpl_adt_node *obj, char const *name, char const *value) {
zpl_adt_make_leaf(obj, name, ZPL_ADT_TYPE_STRING);
obj->string = value;
return true;
}
void zpl_adt_set_flt(zpl_adt_node *obj, char const *name, zpl_f64 value) {
zpl_b8 zpl_adt_set_flt(zpl_adt_node *obj, char const *name, zpl_f64 value) {
zpl_adt_make_leaf(obj, name, ZPL_ADT_TYPE_REAL);
obj->real = value;
return true;
}
void zpl_adt_set_int(zpl_adt_node *obj, char const *name, zpl_i64 value) {
zpl_b8 zpl_adt_set_int(zpl_adt_node *obj, char const *name, zpl_i64 value) {
zpl_adt_make_leaf(obj, name, ZPL_ADT_TYPE_INTEGER);
obj->integer = value;
return true;
}

zpl_adt_node *zpl_adt_move_node_at(zpl_adt_node *node, zpl_adt_node *new_parent, zpl_isize index) {
Expand Down Expand Up @@ -304,26 +308,37 @@ void zpl_adt_remove_node(zpl_adt_node *node) {

zpl_adt_node *zpl_adt_append_obj(zpl_adt_node *parent, char const *name) {
zpl_adt_node *o = zpl_adt_alloc(parent);
zpl_adt_set_obj(o, name, ZPL_ARRAY_HEADER(parent->nodes)->allocator);
if (!o) return NULL;
if (zpl_adt_set_obj(o, name, ZPL_ARRAY_HEADER(parent->nodes)->allocator)) {
zpl_adt_remove_node(o);
return NULL;
}
return o;
}
zpl_adt_node *zpl_adt_append_arr(zpl_adt_node *parent, char const *name) {
zpl_adt_node *o = zpl_adt_alloc(parent);
zpl_adt_set_arr(o, name, ZPL_ARRAY_HEADER(parent->nodes)->allocator);
if (!o) return NULL;
if (zpl_adt_set_arr(o, name, ZPL_ARRAY_HEADER(parent->nodes)->allocator)) {
zpl_adt_remove_node(o);
return NULL;
}
return o;
}
zpl_adt_node *zpl_adt_append_str(zpl_adt_node *parent, char const *name, char const *value) {
zpl_adt_node *o = zpl_adt_alloc(parent);
if (!o) return NULL;
zpl_adt_set_str(o, name, value);
return o;
}
zpl_adt_node *zpl_adt_append_flt(zpl_adt_node *parent, char const *name, zpl_f64 value) {
zpl_adt_node *o = zpl_adt_alloc(parent);
if (!o) return NULL;
zpl_adt_set_flt(o, name, value);
return o;
}
zpl_adt_node *zpl_adt_append_int(zpl_adt_node *parent, char const *name, zpl_i64 value) {
zpl_adt_node *o = zpl_adt_alloc(parent);
if (!o) return NULL;
zpl_adt_set_int(o, name, value);
return o;
}
Expand Down
14 changes: 9 additions & 5 deletions code/source/core/file_stream.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,31 +36,34 @@ ZPL_IMPL_INLINE zpl__memory_fd *zpl__file_stream_from_fd(zpl_file_descriptor fd)
return d;
}

void zpl_file_stream_new(zpl_file* file, zpl_allocator allocator) {
zpl_b8 zpl_file_stream_new(zpl_file* file, zpl_allocator allocator) {
ZPL_ASSERT_NOT_NULL(file);
zpl__memory_fd *d = (zpl__memory_fd*)zpl_alloc(allocator, zpl_size_of(zpl__memory_fd));
if (!d) return false;
zpl_zero_item(file);
d->magic = ZPL__FILE_STREAM_FD_MAGIC;
d->alloc = allocator;
d->flags = ZPL_FILE_STREAM_CLONE_WRITABLE;
d->cap = 0;
zpl_array_init(d->buf, allocator);
if (!zpl_array_init(d->buf, allocator)) return false;
file->ops = zpl_memory_file_operations;
file->fd = zpl__file_stream_fd_make(d);
file->dir = NULL;
file->last_write_time = 0;
file->filename = NULL;
file->is_temp = true;
return true;
}
void zpl_file_stream_open(zpl_file* file, zpl_allocator allocator, zpl_u8 *buffer, zpl_isize size, zpl_file_stream_flags flags) {
zpl_b8 zpl_file_stream_open(zpl_file* file, zpl_allocator allocator, zpl_u8 *buffer, zpl_isize size, zpl_file_stream_flags flags) {
ZPL_ASSERT_NOT_NULL(file);
zpl__memory_fd *d = (zpl__memory_fd*)zpl_alloc(allocator, zpl_size_of(zpl__memory_fd));
if (!d) return false;
zpl_zero_item(file);
d->magic = ZPL__FILE_STREAM_FD_MAGIC;
d->alloc = allocator;
d->flags = flags;
if (d->flags & ZPL_FILE_STREAM_CLONE_WRITABLE) {
zpl_array_init_reserve(d->buf, allocator, size);
if (!zpl_array_init_reserve(d->buf, allocator, size)) return false;
zpl_memcopy(d->buf, buffer, size);
d->cap = zpl_array_count(d->buf) = size;
} else {
Expand All @@ -73,6 +76,7 @@ void zpl_file_stream_open(zpl_file* file, zpl_allocator allocator, zpl_u8 *buffe
file->last_write_time = 0;
file->filename = NULL;
file->is_temp = true;
return true;
}

zpl_u8 *zpl_file_stream_buf(zpl_file* file, zpl_isize *size) {
Expand Down Expand Up @@ -114,7 +118,7 @@ zpl_internal ZPL_FILE_WRITE_AT_PROC(zpl__memory_file_write) {
zpl_isize new_cap = buflen+extralen;
if (d->flags & ZPL_FILE_STREAM_CLONE_WRITABLE) {
if(zpl_array_capacity(d->buf) < new_cap) {
zpl_array_grow(d->buf, (zpl_i64)(new_cap));
if (!zpl_array_grow(d->buf, (zpl_i64)(new_cap))) return false;
}
}
zpl_memcopy(d->buf + offset, buffer, rwlen);
Expand Down
Loading

0 comments on commit ef96f39

Please sign in to comment.