Release v4.18994 - Backdoor
[RRG-proxmark3.git] / client / deps / jansson / memory.c
blob77c51235273c9b50a1f7d3fc408a74681a332a18
1 /*
2 * Copyright (c) 2009-2016 Petri Lehtinen <petri@digip.org>
3 * Copyright (c) 2011-2012 Basile Starynkevitch <basile@starynkevitch.net>
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 */
9 #include <stdlib.h>
10 #include <string.h>
12 #include "jansson.h"
13 #include "jansson_private.h"
15 /* C89 allows these to be macros */
16 #undef malloc
17 #undef free
19 /* memory function pointers */
20 static json_malloc_t do_malloc = malloc;
21 static json_free_t do_free = free;
23 void *jsonp_malloc(size_t size) {
24 if (!size)
25 return NULL;
27 return (*do_malloc)(size);
30 void jsonp_free(void *ptr) {
31 if (!ptr)
32 return;
34 (*do_free)(ptr);
37 char *jsonp_strdup(const char *str) {
38 return jsonp_strndup(str, strlen(str));
41 char *jsonp_strndup(const char *str, size_t len) {
42 char *new_str;
44 new_str = jsonp_malloc(len + 1);
45 if (!new_str)
46 return NULL;
48 memcpy(new_str, str, len);
49 new_str[len] = '\0';
50 return new_str;
53 void json_set_alloc_funcs(json_malloc_t malloc_fn, json_free_t free_fn) {
54 do_malloc = malloc_fn;
55 do_free = free_fn;
58 void json_get_alloc_funcs(json_malloc_t *malloc_fn, json_free_t *free_fn) {
59 if (malloc_fn)
60 *malloc_fn = do_malloc;
61 if (free_fn)
62 *free_fn = do_free;