memory_mosq.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /*
  2. Copyright (c) 2009-2020 Roger Light <roger@atchoo.org>
  3. All rights reserved. This program and the accompanying materials
  4. are made available under the terms of the Eclipse Public License 2.0
  5. and Eclipse Distribution License v1.0 which accompany this distribution.
  6. The Eclipse Public License is available at
  7. https://www.eclipse.org/legal/epl-2.0/
  8. and the Eclipse Distribution License is available at
  9. http://www.eclipse.org/org/documents/edl-v10.php.
  10. SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
  11. Contributors:
  12. Roger Light - initial implementation and documentation.
  13. */
  14. #include "config.h"
  15. #include <stdlib.h>
  16. #include <string.h>
  17. #include "memory_mosq.h"
  18. #ifdef REAL_WITH_MEMORY_TRACKING
  19. # if defined(__APPLE__)
  20. # include <malloc/malloc.h>
  21. # define malloc_usable_size malloc_size
  22. # elif defined(__FreeBSD__)
  23. # include <malloc_np.h>
  24. # else
  25. # include <malloc.h>
  26. # endif
  27. #endif
  28. #ifdef REAL_WITH_MEMORY_TRACKING
  29. static unsigned long memcount = 0;
  30. static unsigned long max_memcount = 0;
  31. #endif
  32. #ifdef WITH_BROKER
  33. static size_t mem_limit = 0;
  34. void memory__set_limit(size_t lim)
  35. {
  36. mem_limit = lim;
  37. }
  38. #endif
  39. void *mosquitto__calloc(size_t nmemb, size_t size)
  40. {
  41. void *mem;
  42. #ifdef REAL_WITH_MEMORY_TRACKING
  43. if(mem_limit && memcount + size > mem_limit){
  44. return NULL;
  45. }
  46. #endif
  47. mem = calloc(nmemb, size);
  48. #ifdef REAL_WITH_MEMORY_TRACKING
  49. if(mem){
  50. memcount += malloc_usable_size(mem);
  51. if(memcount > max_memcount){
  52. max_memcount = memcount;
  53. }
  54. }
  55. #endif
  56. return mem;
  57. }
  58. void mosquitto__free(void *mem)
  59. {
  60. #ifdef REAL_WITH_MEMORY_TRACKING
  61. if(!mem){
  62. return;
  63. }
  64. memcount -= malloc_usable_size(mem);
  65. #endif
  66. free(mem);
  67. }
  68. void *mosquitto__malloc(size_t size)
  69. {
  70. void *mem;
  71. #ifdef REAL_WITH_MEMORY_TRACKING
  72. if(mem_limit && memcount + size > mem_limit){
  73. return NULL;
  74. }
  75. #endif
  76. mem = malloc(size);
  77. #ifdef REAL_WITH_MEMORY_TRACKING
  78. if(mem){
  79. memcount += malloc_usable_size(mem);
  80. if(memcount > max_memcount){
  81. max_memcount = memcount;
  82. }
  83. }
  84. #endif
  85. return mem;
  86. }
  87. #ifdef REAL_WITH_MEMORY_TRACKING
  88. unsigned long mosquitto__memory_used(void)
  89. {
  90. return memcount;
  91. }
  92. unsigned long mosquitto__max_memory_used(void)
  93. {
  94. return max_memcount;
  95. }
  96. #endif
  97. void *mosquitto__realloc(void *ptr, size_t size)
  98. {
  99. void *mem;
  100. #ifdef REAL_WITH_MEMORY_TRACKING
  101. if(mem_limit && memcount + size > mem_limit){
  102. return NULL;
  103. }
  104. if(ptr){
  105. memcount -= malloc_usable_size(ptr);
  106. }
  107. #endif
  108. mem = realloc(ptr, size);
  109. #ifdef REAL_WITH_MEMORY_TRACKING
  110. if(mem){
  111. memcount += malloc_usable_size(mem);
  112. if(memcount > max_memcount){
  113. max_memcount = memcount;
  114. }
  115. }
  116. #endif
  117. return mem;
  118. }
  119. char *mosquitto__strdup(const char *s)
  120. {
  121. char *str;
  122. #ifdef REAL_WITH_MEMORY_TRACKING
  123. if(mem_limit && memcount + strlen(s) > mem_limit){
  124. return NULL;
  125. }
  126. #endif
  127. str = strdup(s);
  128. #ifdef REAL_WITH_MEMORY_TRACKING
  129. if(str){
  130. memcount += malloc_usable_size(str);
  131. if(memcount > max_memcount){
  132. max_memcount = memcount;
  133. }
  134. }
  135. #endif
  136. return str;
  137. }