jansson 2.0
[jansson.git] / src / memory.c
1 /*
2  * Copyright (c) 2009-2011 Petri Lehtinen <petri@digip.org>
3  * Copyright (c) 2011 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 /* memory function pointers */
16 static json_malloc_t do_malloc = malloc;
17 static json_free_t do_free = free;
18
19 void *jsonp_malloc(size_t size)
20 {
21     if(!size)
22         return NULL;
23
24     return (*do_malloc)(size);
25 }
26
27 void jsonp_free(void *ptr)
28 {
29     if(!ptr)
30         return;
31
32     (*do_free)(ptr);
33 }
34
35 char *jsonp_strdup(const char *str)
36 {
37     char *new_str;
38
39     new_str = jsonp_malloc(strlen(str) + 1);
40     if(!new_str)
41         return NULL;
42
43     strcpy(new_str, str);
44     return new_str;
45 }
46
47 void json_set_alloc_funcs(json_malloc_t malloc_fn, json_free_t free_fn)
48 {
49     do_malloc = malloc_fn;
50     do_free = free_fn;
51 }