summaryrefslogtreecommitdiff
path: root/src/internal.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/internal.h')
-rw-r--r--src/internal.h21
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;