diff options
author | Anna (navi) Figueiredo Gomes <navi@vlhl.dev> | 2024-04-12 12:18:03 +0200 |
---|---|---|
committer | Anna (navi) Figueiredo Gomes <navi@vlhl.dev> | 2024-04-12 12:18:03 +0200 |
commit | 1dd3e1c8d7deb0fa6fe6208b2dd8d236d1f3fc2e (patch) | |
tree | 92c27ac0a3bf86bfe5f00214e691c3c388dcab27 /src/internal.h | |
parent | 46bc58545b95ee1cba1292dcaeb46c60533e0184 (diff) |
libjson: new api
a lot... changed...
Signed-off-by: Anna (navi) Figueiredo Gomes <navi@vlhl.dev>
Diffstat (limited to 'src/internal.h')
-rw-r--r-- | src/internal.h | 21 |
1 files changed, 14 insertions, 7 deletions
diff --git a/src/internal.h b/src/internal.h index f1e08dc..812688f 100644 --- a/src/internal.h +++ b/src/internal.h @@ -33,16 +33,23 @@ inline static bool init_children(struct json *obj) { return true; } +inline static bool ensure_size(struct json *dest, size_t to_add) { + struct json_children *chld = dest->children; + while (chld->nitems + to_add >= chld->maxitems) { + chld->maxitems *= 2; + chld = realloc(chld, sizeof(*chld) + (sizeof(*chld->items) * chld->maxitems)); + if (!chld) + return false; + dest->children = chld; + } + return true; +} inline static void add_children(struct json *dest, struct json *src) { + if (!ensure_size(dest, 1)) + return; + struct json_children *children = dest->children; - if (children->nitems + 1 >= children->maxitems) { - children->maxitems += 5; - children = realloc(children, sizeof(*children) + (sizeof(*children->items) * children->maxitems)); - if (!children) - return; - dest->children = children; - } src->parent = dest; src->index = children->nitems; |