diff --git a/include/rjp.h b/include/rjp.h index 889a5c8..c9a2dff 100644 --- a/include/rjp.h +++ b/include/rjp.h @@ -94,7 +94,8 @@ void rjp_free_value(RJP_value* root); //initialize an empty json handle (outputting to string would result in '{}') RJP_value* rjp_init_json(void); -char* rjp_alloc(size_t nbytes); +void* rjp_alloc(size_t nbytes); +void* rjp_calloc(size_t num, size_t nbytes); void rjp_free(void* dest); //add a member to a json object diff --git a/src/input.c b/src/input.c index 6e8daf6..3885aaa 100644 --- a/src/input.c +++ b/src/input.c @@ -58,10 +58,10 @@ static int _rjp__is_whitespace(char c){ void _rjp__add_element(RJP_array* j){ ++j->num_elements; if(!j->elements){ - j->elements = calloc(1, sizeof(RJP_array_element)); + j->elements = rjp_calloc(1, sizeof(RJP_array_element)); j->last = j->elements; }else{ - j->last->next = calloc(1, sizeof(RJP_array_element)); + j->last->next = rjp_calloc(1, sizeof(RJP_array_element)); j->last = j->last->next; } } @@ -69,13 +69,13 @@ void _rjp__add_element(RJP_array* j){ void _rjp__add_member(RJP_object* j, char* str, size_t len){ ++j->num_members; if(!j->members){ - j->members = calloc(1, sizeof(RJP_object_member)); + j->members = rjp_calloc(1, sizeof(RJP_object_member)); j->last = j->members; }else{ - j->last->next = calloc(1, sizeof(RJP_object_member)); + j->last->next = rjp_calloc(1, sizeof(RJP_object_member)); j->last = j->last->next; } - j->last->name.value = malloc(len + 1); + j->last->name.value = rjp_alloc(len + 1); strncpy(j->last->name.value, str, len); j->last->name.value[len] = 0; j->last->name.length = len; @@ -83,10 +83,10 @@ void _rjp__add_member(RJP_object* j, char* str, size_t len){ void _rjp__add_member_no_alloc(RJP_object* j, char* str, size_t len){ ++j->num_members; if(!j->members){ - j->members = calloc(1, sizeof(RJP_object_member)); + j->members = rjp_calloc(1, sizeof(RJP_object_member)); j->last = j->members; }else{ - j->last->next = calloc(1, sizeof(RJP_object_member)); + j->last->next = rjp_calloc(1, sizeof(RJP_object_member)); j->last = j->last->next; } j->last->name.value = str; @@ -104,7 +104,7 @@ static RJP_value* _rjp__add_element_to_array(RJP_value* curr, RJP_state* state, //Assign object characteristics to previously allocated RJP_value static RJP_value* _rjp__add_object(RJP_value* curr, RJP_state* state){ if(!curr){ - curr = calloc(1, sizeof(RJP_value)); + curr = rjp_calloc(1, sizeof(RJP_value)); curr->type = json_object; return curr; } @@ -351,7 +351,7 @@ RJP_value* rjp_parse(const char* str){ char* new_string = _rjp__parse_string(root, str, &vallen, &row, &column); if(!new_string){ if(vallen == 0){ - new_string = calloc(1, 1); + new_string = rjp_calloc(1, 1); }else{ return NULL; } diff --git a/src/output.c b/src/output.c index b180192..7271f80 100644 --- a/src/output.c +++ b/src/output.c @@ -6,13 +6,16 @@ #include //sprintf RJP_value* rjp_init_json(void){ - RJP_value* ret = calloc(1, sizeof(RJP_value)); + RJP_value* ret = rjp_calloc(1, sizeof(RJP_value)); ret->type = json_object; return ret; } -char* rjp_alloc(size_t nbytes){ +void* rjp_alloc(size_t nbytes){ return malloc(nbytes); } +void* rjp_calloc(size_t num, size_t nbytes){ + return calloc(num, nbytes); +} void rjp_free(void* data){ free(data); } @@ -173,7 +176,7 @@ char* rjp_to_json(RJP_value* root){ size_t len = _rjp__object_strlen(root); if(!len) return NULL; - char* tmp = malloc(len + 1); + char* tmp = rjp_alloc(len + 1); tmp[len] = 0; size_t pos = 1; diff --git a/src/strings.c b/src/strings.c index 3cd514f..b1d6269 100644 --- a/src/strings.c +++ b/src/strings.c @@ -25,7 +25,7 @@ char* _rjp__parse_string(RJP_value* root, const char* str, int* len, int* row, i if(*len == 0){ return NULL; } - new_string = malloc(*len + 1); + new_string = rjp_alloc(*len + 1); new_string[*len] = 0; for(int i = 0;*str != '"';++i,++str){ if(*str == '\\'){