ngtcp2_mem.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. * ngtcp2
  3. *
  4. * Copyright (c) 2017 ngtcp2 contributors
  5. * Copyright (c) 2014 nghttp2 contributors
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining
  8. * a copy of this software and associated documentation files (the
  9. * "Software"), to deal in the Software without restriction, including
  10. * without limitation the rights to use, copy, modify, merge, publish,
  11. * distribute, sublicense, and/or sell copies of the Software, and to
  12. * permit persons to whom the Software is furnished to do so, subject to
  13. * the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be
  16. * included in all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  19. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  20. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  21. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  22. * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  23. * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  24. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  25. */
  26. #include "ngtcp2_mem.h"
  27. #include <stdio.h>
  28. static void *default_malloc(size_t size, void *user_data) {
  29. (void)user_data;
  30. return malloc(size);
  31. }
  32. static void default_free(void *ptr, void *user_data) {
  33. (void)user_data;
  34. free(ptr);
  35. }
  36. static void *default_calloc(size_t nmemb, size_t size, void *user_data) {
  37. (void)user_data;
  38. return calloc(nmemb, size);
  39. }
  40. static void *default_realloc(void *ptr, size_t size, void *user_data) {
  41. (void)user_data;
  42. return realloc(ptr, size);
  43. }
  44. static const ngtcp2_mem mem_default = {NULL, default_malloc, default_free,
  45. default_calloc, default_realloc};
  46. const ngtcp2_mem *ngtcp2_mem_default(void) { return &mem_default; }
  47. #ifndef MEMDEBUG
  48. void *ngtcp2_mem_malloc(const ngtcp2_mem *mem, size_t size) {
  49. return mem->malloc(size, mem->user_data);
  50. }
  51. void ngtcp2_mem_free(const ngtcp2_mem *mem, void *ptr) {
  52. mem->free(ptr, mem->user_data);
  53. }
  54. void *ngtcp2_mem_calloc(const ngtcp2_mem *mem, size_t nmemb, size_t size) {
  55. return mem->calloc(nmemb, size, mem->user_data);
  56. }
  57. void *ngtcp2_mem_realloc(const ngtcp2_mem *mem, void *ptr, size_t size) {
  58. return mem->realloc(ptr, size, mem->user_data);
  59. }
  60. #else /* defined(MEMDEBUG) */
  61. void *ngtcp2_mem_malloc_debug(const ngtcp2_mem *mem, size_t size,
  62. const char *func, const char *file, size_t line) {
  63. void *nptr = mem->malloc(size, mem->user_data);
  64. fprintf(stderr, "malloc %p size=%zu in %s at %s:%zu\n", nptr, size, func,
  65. file, line);
  66. return nptr;
  67. }
  68. void ngtcp2_mem_free_debug(const ngtcp2_mem *mem, void *ptr, const char *func,
  69. const char *file, size_t line) {
  70. fprintf(stderr, "free ptr=%p in %s at %s:%zu\n", ptr, func, file, line);
  71. mem->free(ptr, mem->user_data);
  72. }
  73. void *ngtcp2_mem_calloc_debug(const ngtcp2_mem *mem, size_t nmemb, size_t size,
  74. const char *func, const char *file, size_t line) {
  75. void *nptr = mem->calloc(nmemb, size, mem->user_data);
  76. fprintf(stderr, "calloc %p nmemb=%zu size=%zu in %s at %s:%zu\n", nptr, nmemb,
  77. size, func, file, line);
  78. return nptr;
  79. }
  80. void *ngtcp2_mem_realloc_debug(const ngtcp2_mem *mem, void *ptr, size_t size,
  81. const char *func, const char *file,
  82. size_t line) {
  83. void *nptr = mem->realloc(ptr, size, mem->user_data);
  84. fprintf(stderr, "realloc %p ptr=%p size=%zu in %s at %s:%zu\n", nptr, ptr,
  85. size, func, file, line);
  86. return nptr;
  87. }
  88. #endif /* defined(MEMDEBUG) */