From 68e0784cc36448449e9876ef3c9859d668d3b8a4 Mon Sep 17 00:00:00 2001 From: Eric Fischer Date: Wed, 5 Feb 2014 23:35:35 -0800 Subject: [PATCH] At least don't talk about allocation sizes in the public API --- jsonpull.c | 15 +++++++-------- jsonpull.h | 7 ------- 2 files changed, 7 insertions(+), 15 deletions(-) diff --git a/jsonpull.c b/jsonpull.c index 80433e2..cbf0371 100644 --- a/jsonpull.c +++ b/jsonpull.c @@ -13,6 +13,8 @@ static void json_error(char *s, ...) { exit(EXIT_FAILURE); } +#define SIZE_FOR(i) (((i) + 31) & ~31) + static json_object *add_object(json_type type, json_object *parent) { json_object *o = malloc(sizeof(struct json_object)); o->type = type; @@ -21,13 +23,11 @@ static json_object *add_object(json_type type, json_object *parent) { o->keys = NULL; o->values = NULL; o->length = 0; - o->__allocated = 0; if (parent != NULL) { if (parent->type == JSON_ARRAY) { - if (parent->length >= parent->__allocated) { - parent->__allocated += 20; - parent->array = realloc(parent->array, parent->__allocated * sizeof(json_object *)); + if (SIZE_FOR(parent->length + 1) != SIZE_FOR(parent->length)) { + parent->array = realloc(parent->array, SIZE_FOR(parent->length + 1) * sizeof(json_object *)); } parent->array[parent->length++] = o; @@ -44,10 +44,9 @@ static json_object *add_object(json_type type, json_object *parent) { json_error("Hash key is not a string"); } - if (parent->length >= parent->__allocated) { - parent->__allocated += 20; - parent->keys = realloc(parent->keys, parent->__allocated * sizeof(json_object *)); - parent->values = realloc(parent->values, parent->__allocated * sizeof(json_object *)); + if (SIZE_FOR(parent->length + 1) != SIZE_FOR(parent->length)) { + parent->keys = realloc(parent->keys, SIZE_FOR(parent->length + 1) * sizeof(json_object *)); + parent->values = realloc(parent->values, SIZE_FOR(parent->length + 1) * sizeof(json_object *)); } parent->keys[parent->length] = o; diff --git a/jsonpull.h b/jsonpull.h index 08f3650..3adfe8f 100644 --- a/jsonpull.h +++ b/jsonpull.h @@ -1,9 +1,3 @@ -#include -#include -#include -#include -#include - typedef enum json_type { JSON_HASH, JSON_ARRAY, JSON_NUMBER, JSON_STRING, JSON_TRUE, JSON_FALSE, JSON_NULL, } json_type; @@ -19,7 +13,6 @@ typedef struct json_object { struct json_object **keys; struct json_object **values; int length; - int __allocated; } json_object; json_object *json_parse(FILE *f, json_object *current);