From 1dd3e1c8d7deb0fa6fe6208b2dd8d236d1f3fc2e Mon Sep 17 00:00:00 2001 From: "Anna (navi) Figueiredo Gomes" Date: Fri, 12 Apr 2024 12:18:03 +0200 Subject: libjson: new api a lot... changed... Signed-off-by: Anna (navi) Figueiredo Gomes --- src/internal.h | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) (limited to 'src/internal.h') 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; -- cgit v1.2.3