2011-02-17 16:10:53 +08:00
|
|
|
/*
|
2014-01-28 15:16:05 +08:00
|
|
|
* Copyright (c) 2009-2014 Petri Lehtinen <petri@digip.org>
|
2012-03-21 02:55:55 +08:00
|
|
|
* Copyright (c) 2011-2012 Basile Starynkevitch <basile@starynkevitch.net>
|
2011-02-17 16:10:53 +08:00
|
|
|
*
|
|
|
|
* Jansson is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms of the MIT license. See LICENSE for details.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2012-04-30 03:09:29 +08:00
|
|
|
#include "jansson.h"
|
2011-02-17 16:10:53 +08:00
|
|
|
#include "jansson_private.h"
|
|
|
|
|
2012-04-04 09:00:29 +08:00
|
|
|
/* C89 allows these to be macros */
|
|
|
|
#undef malloc
|
|
|
|
#undef free
|
|
|
|
|
2011-02-17 16:10:53 +08:00
|
|
|
/* memory function pointers */
|
|
|
|
static json_malloc_t do_malloc = malloc;
|
|
|
|
static json_free_t do_free = free;
|
|
|
|
|
|
|
|
void *jsonp_malloc(size_t size)
|
|
|
|
{
|
|
|
|
if(!size)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return (*do_malloc)(size);
|
|
|
|
}
|
|
|
|
|
|
|
|
void jsonp_free(void *ptr)
|
|
|
|
{
|
|
|
|
if(!ptr)
|
|
|
|
return;
|
|
|
|
|
|
|
|
(*do_free)(ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
char *jsonp_strdup(const char *str)
|
|
|
|
{
|
2012-04-04 09:00:29 +08:00
|
|
|
return jsonp_strndup(str, strlen(str));
|
|
|
|
}
|
2011-02-17 16:10:53 +08:00
|
|
|
|
2012-04-04 09:00:29 +08:00
|
|
|
char *jsonp_strndup(const char *str, size_t len)
|
|
|
|
{
|
|
|
|
char *new_str;
|
2013-07-21 18:00:25 +08:00
|
|
|
|
|
|
|
new_str = jsonp_malloc(len + 1);
|
2011-02-17 16:10:53 +08:00
|
|
|
if(!new_str)
|
|
|
|
return NULL;
|
|
|
|
|
2012-04-04 09:00:29 +08:00
|
|
|
memcpy(new_str, str, len);
|
|
|
|
new_str[len] = '\0';
|
2011-02-17 16:10:53 +08:00
|
|
|
return new_str;
|
|
|
|
}
|
|
|
|
|
|
|
|
void json_set_alloc_funcs(json_malloc_t malloc_fn, json_free_t free_fn)
|
|
|
|
{
|
|
|
|
do_malloc = malloc_fn;
|
|
|
|
do_free = free_fn;
|
|
|
|
}
|
2015-12-22 00:46:32 +08:00
|
|
|
|
|
|
|
void json_get_alloc_funcs(json_malloc_t *malloc_fn, json_free_t *free_fn)
|
|
|
|
{
|
|
|
|
if (malloc_fn)
|
|
|
|
*malloc_fn = do_malloc;
|
|
|
|
if (free_fn)
|
|
|
|
*free_fn = do_free;
|
|
|
|
}
|