memory.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. * Copyright (C) 2005 iptelorg GmbH
  3. *
  4. * This file is part of ser, a free SIP server.
  5. *
  6. * ser is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version
  10. *
  11. * For a license to use the ser software under conditions
  12. * other than those described here, or to purchase support for this
  13. * software, please contact iptel.org by e-mail at the following addresses:
  14. * [email protected]
  15. *
  16. * ser is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  24. */
  25. /* Common Data Structures - functions for memory allocation and deallocation
  26. * and may be other memory management. */
  27. #include <stdio.h>
  28. #include <stdlib.h>
  29. #include <cds/memory.h>
  30. #include <cds/sync.h>
  31. #include <cds/logger.h>
  32. #ifdef TRACE_CDS_MEMORY
  33. cds_mutex_t *mem_mutex = NULL;
  34. int *allocated_cnt = NULL;
  35. char *debug_file = "/tmp/mem.log";
  36. #define write_debug(s,args...) if (1) { \
  37. FILE *f = fopen(debug_file, "a"); \
  38. if (f) { \
  39. fprintf(f,s,##args); \
  40. fclose(f); \
  41. } \
  42. TRACE_LOG(s,##args); \
  43. }
  44. void *debug_malloc(int size, const char *file, int line)
  45. {
  46. void *m = NULL;
  47. if (allocated_cnt && mem_mutex) {
  48. cds_mutex_lock(mem_mutex);
  49. (*allocated_cnt)++;
  50. cds_mutex_unlock(mem_mutex);
  51. }
  52. #ifdef SER
  53. m = shm_malloc(size);
  54. #else
  55. m = malloc(size);
  56. #endif
  57. write_debug("ALLOC %p size %u from %s(%d)\n", m, size, file, line);
  58. /* LOG(L_INFO, "%p\n", m); */
  59. return m;
  60. }
  61. void debug_free(void *block, const char *file, int line)
  62. {
  63. if (allocated_cnt && mem_mutex) {
  64. cds_mutex_lock(mem_mutex);
  65. (*allocated_cnt)--;
  66. cds_mutex_unlock(mem_mutex);
  67. }
  68. #ifdef SER
  69. shm_free(block);
  70. #else
  71. free(block);
  72. #endif
  73. write_debug("FREE %p from %s(%d)\n", block, file, line);
  74. }
  75. void *debug_malloc_ex(unsigned int size)
  76. {
  77. return debug_malloc(size, "<none>", 0);
  78. }
  79. void debug_free_ex(void *block)
  80. {
  81. debug_free(block, "<none>", 0);
  82. }
  83. void cds_memory_trace_init()
  84. {
  85. cds_mutex_init(mem_mutex);
  86. allocated_cnt = cds_malloc(sizeof(int));
  87. *allocated_cnt = 0;
  88. }
  89. void cds_memory_trace(char *dst, int dst_len)
  90. {
  91. if (allocated_cnt && mem_mutex) {
  92. cds_mutex_lock(mem_mutex);
  93. snprintf(dst, dst_len, "There are allocated: %d memory blocks\n", *allocated_cnt);
  94. cds_mutex_unlock(mem_mutex);
  95. }
  96. }
  97. #else /* ! CDS_TRACE_MEMORY */
  98. #ifdef SER
  99. void* shm_malloc_x(unsigned int size)
  100. {
  101. return shm_malloc(size);
  102. }
  103. void shm_free_x(void *ptr)
  104. {
  105. shm_free(ptr);
  106. }
  107. #endif
  108. #endif