memory.c 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. /*
  2. * Copyright (c) 2009-2013 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. #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. size_t len;
  31. len = strlen(str);
  32. if(len == (size_t)-1)
  33. return NULL;
  34. new_str = jsonp_malloc(len + 1);
  35. if(!new_str)
  36. return NULL;
  37. memcpy(new_str, str, len + 1);
  38. return new_str;
  39. }
  40. void json_set_alloc_funcs(json_malloc_t malloc_fn, json_free_t free_fn)
  41. {
  42. do_malloc = malloc_fn;
  43. do_free = free_fn;
  44. }