nghttp3_mem.c 4.1 KB

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