At least don't talk about allocation sizes in the public API

This commit is contained in:
Eric Fischer 2014-02-05 23:35:35 -08:00
parent d630aa917e
commit 68e0784cc3
2 changed files with 7 additions and 15 deletions

View File

@ -13,6 +13,8 @@ static void json_error(char *s, ...) {
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
#define SIZE_FOR(i) (((i) + 31) & ~31)
static json_object *add_object(json_type type, json_object *parent) { static json_object *add_object(json_type type, json_object *parent) {
json_object *o = malloc(sizeof(struct json_object)); json_object *o = malloc(sizeof(struct json_object));
o->type = type; o->type = type;
@ -21,13 +23,11 @@ static json_object *add_object(json_type type, json_object *parent) {
o->keys = NULL; o->keys = NULL;
o->values = NULL; o->values = NULL;
o->length = 0; o->length = 0;
o->__allocated = 0;
if (parent != NULL) { if (parent != NULL) {
if (parent->type == JSON_ARRAY) { if (parent->type == JSON_ARRAY) {
if (parent->length >= parent->__allocated) { if (SIZE_FOR(parent->length + 1) != SIZE_FOR(parent->length)) {
parent->__allocated += 20; parent->array = realloc(parent->array, SIZE_FOR(parent->length + 1) * sizeof(json_object *));
parent->array = realloc(parent->array, parent->__allocated * sizeof(json_object *));
} }
parent->array[parent->length++] = o; 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"); json_error("Hash key is not a string");
} }
if (parent->length >= parent->__allocated) { if (SIZE_FOR(parent->length + 1) != SIZE_FOR(parent->length)) {
parent->__allocated += 20; parent->keys = realloc(parent->keys, SIZE_FOR(parent->length + 1) * sizeof(json_object *));
parent->keys = realloc(parent->keys, parent->__allocated * sizeof(json_object *)); parent->values = realloc(parent->values, SIZE_FOR(parent->length + 1) * sizeof(json_object *));
parent->values = realloc(parent->values, parent->__allocated * sizeof(json_object *));
} }
parent->keys[parent->length] = o; parent->keys[parent->length] = o;

View File

@ -1,9 +1,3 @@
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <stdarg.h>
typedef enum json_type { typedef enum json_type {
JSON_HASH, JSON_ARRAY, JSON_NUMBER, JSON_STRING, JSON_TRUE, JSON_FALSE, JSON_NULL, JSON_HASH, JSON_ARRAY, JSON_NUMBER, JSON_STRING, JSON_TRUE, JSON_FALSE, JSON_NULL,
} json_type; } json_type;
@ -19,7 +13,6 @@ typedef struct json_object {
struct json_object **keys; struct json_object **keys;
struct json_object **values; struct json_object **values;
int length; int length;
int __allocated;
} json_object; } json_object;
json_object *json_parse(FILE *f, json_object *current); json_object *json_parse(FILE *f, json_object *current);