Path: blob/master/compat/jansson/memory.c
1294 views
/*1* Copyright (c) 2009-2013 Petri Lehtinen <[email protected]>2* Copyright (c) 2011-2012 Basile Starynkevitch <[email protected]>3*4* Jansson is free software; you can redistribute it and/or modify it5* under the terms of the MIT license. See LICENSE for details.6*/78#include <stdlib.h>9#include <string.h>1011#include "jansson.h"12#include "jansson_private.h"1314/* memory function pointers */15static json_malloc_t do_malloc = malloc;16static json_free_t do_free = free;1718void *jsonp_malloc(size_t size)19{20if(!size)21return NULL;2223return (*do_malloc)(size);24}2526void jsonp_free(void *ptr)27{28if(!ptr)29return;3031(*do_free)(ptr);32}3334char *jsonp_strdup(const char *str)35{36char *new_str;37size_t len;3839len = strlen(str);40if(len == (size_t)-1)41return NULL;4243new_str = jsonp_malloc(len + 1);44if(!new_str)45return NULL;4647memcpy(new_str, str, len + 1);48return new_str;49}5051void json_set_alloc_funcs(json_malloc_t malloc_fn, json_free_t free_fn)52{53do_malloc = malloc_fn;54do_free = free_fn;55}565758