memory.c 980 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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. #include <stdlib.h>
  9. #include <string.h>
  10. #include <jansson.h>
  11. #include "jansson_private.h"
  12. /* memory function pointers */
  13. static json_malloc_t do_malloc = malloc;
  14. static json_free_t do_free = free;
  15. void *jsonp_malloc(size_t size)
  16. {
  17. if(!size)
  18. return NULL;
  19. return (*do_malloc)(size);
  20. }
  21. void jsonp_free(void *ptr)
  22. {
  23. if(!ptr)
  24. return;
  25. (*do_free)(ptr);
  26. }
  27. char *jsonp_strdup(const char *str)
  28. {
  29. char *new_str;
  30. new_str = jsonp_malloc(strlen(str) + 1);
  31. if(!new_str)
  32. return NULL;
  33. strcpy(new_str, str);
  34. return new_str;
  35. }
  36. void json_set_alloc_funcs(json_malloc_t malloc_fn, json_free_t free_fn)
  37. {
  38. do_malloc = malloc_fn;
  39. do_free = free_fn;
  40. }