1/*
2 * Copyright (c) 2009-2014 Petri Lehtinen <petri@digip.org>
3 * Copyright (c) 2011-2012 Basile Starynkevitch <basile@starynkevitch.net>
4 *
5 * Jansson is free software; you can redistribute it and/or modify it
6 * under the terms of the MIT license. See LICENSE for details.
7 */
8
9#include <stdlib.h>
10#include <string.h>
11
12#include "jansson.h"
13#include "jansson_private.h"
14
15/* C89 allows these to be macros */
16#undef malloc
17#undef free
18
19/* memory function pointers */
20static json_malloc_t do_malloc = malloc;
21static json_free_t do_free = free;
22
23void *jsonp_malloc(size_t size)
24{
25    if(!size)
26        return NULL;
27
28    return (*do_malloc)(size);
29}
30
31void jsonp_free(void *ptr)
32{
33    if(!ptr)
34        return;
35
36    (*do_free)(ptr);
37}
38
39char *jsonp_strdup(const char *str)
40{
41    return jsonp_strndup(str, strlen(str));
42}
43
44char *jsonp_strndup(const char *str, size_t len)
45{
46    char *new_str;
47
48    new_str = jsonp_malloc(len + 1);
49    if(!new_str)
50        return NULL;
51
52    memcpy(new_str, str, len);
53    new_str[len] = '\0';
54    return new_str;
55}
56
57void json_set_alloc_funcs(json_malloc_t malloc_fn, json_free_t free_fn)
58{
59    do_malloc = malloc_fn;
60    do_free = free_fn;
61}
62