memory.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. #ifndef __CDS_MEMORY_H
  26. #define __CDS_MEMORY_H
  27. /* #define TRACE_CDS_MEMORY */
  28. #ifdef __cplusplus
  29. extern "C" {
  30. #endif
  31. /** \ingroup cds
  32. * @{
  33. *
  34. * \defgroup cds_memory Memory management
  35. *
  36. * Memory operations are common for whole CDS library. Because it must work
  37. together with SER's memory management and must work without it too, there are
  38. wrapper macros for memory allocation/deallocation.
  39. *
  40. * @{ */
  41. /* typedef void*(*cds_malloc_func)(unsigned int size);
  42. typedef void(*cds_free_func)(void *ptr);
  43. extern cds_malloc_func cds_malloc;
  44. extern cds_free_func cds_free;
  45. void cds_set_memory_functions(cds_malloc_func _malloc, cds_free_func _free); */
  46. /** \def cds_malloc(s)
  47. * Function/macro for memory allocation. Which function is choosen depends on
  48. * SER and TRACE_CDS_MEMORY defines.
  49. *
  50. * When SER is defined shm_malloc is choosen, standard malloc otherwise. */
  51. /** \def cds_free(p)
  52. * Function/macro for memory deallocation. Which function is choosen depends
  53. * on SER and TRACE_CDS_MEMORY defines.
  54. *
  55. * If SER is defined shm_free is choosen, standard free otherwise. */
  56. /** \def cds_malloc_ptr
  57. * Function/macro for memory allocation when pointer to function needed. Which
  58. * function is choosen depends on SER and TRACE_CDS_MEMORY defines.
  59. *
  60. * If SER is defined shm_malloc is choosen, standard malloc otherwise. */
  61. /** \def cds_free_ptr
  62. * Function/macro for memory deallocation when pointer to function needed.
  63. * Which function is choosen depends on SER and TRACE_CDS_MEMORY defines.
  64. *
  65. * If SER is defined shm_free is choosen, standard free otherwise. */
  66. /** \def cds_malloc_pkg(s)
  67. * Function/macro for 'local' memory allocation. Which function is choosen
  68. * depends on SER and TRACE_CDS_MEMORY defines.
  69. *
  70. * When SER is defined pkg_malloc is choosen, standard malloc otherwise. */
  71. /** \def cds_free_pkg(p)
  72. * Function/macro for 'local' memory deallocation. Which function is choosen
  73. * depends on SER and TRACE_CDS_MEMORY defines.
  74. *
  75. * When SER is defined pkg_free is choosen, standard free otherwise. */
  76. #ifdef TRACE_CDS_MEMORY
  77. /** \internal Debugging variant of alloc function */
  78. void *debug_malloc(int size, const char *file, int line);
  79. /** \internal Debugging variant of free function */
  80. void debug_free(void *block, const char *file, int line);
  81. /** \internal Another debugging variant of alloc function - used when pointer
  82. * to function needed. */
  83. void *debug_malloc_ex(unsigned int size);
  84. /** \internal Another debugging variant of free function - used when pointer to
  85. * function needed. */
  86. void debug_free_ex(void *block);
  87. /* \internal Helper function for debugging - shows some debugging information about
  88. * memory allocations (currently only the number of allocated blocks). */
  89. void cds_memory_trace(char *dst, int dst_len);
  90. /** \internal Helper function which is useful for memory debugging only - initializes
  91. * internal variables for memory tracing */
  92. void cds_memory_trace_init();
  93. #define cds_malloc(s) debug_malloc(s,__FILE__, __LINE__)
  94. #define cds_free(p) debug_free(p,__FILE__, __LINE__)
  95. #define cds_free_ptr debug_free_ex
  96. #define cds_malloc_ptr debug_malloc_ex
  97. #define cds_malloc_pkg(s) debug_malloc(s,__FILE__, __LINE__)
  98. #define cds_free_pkg(p) debug_free(p,__FILE__, __LINE__)
  99. #else /* !TRACE */
  100. #ifdef SER
  101. #include <mem/mem.h>
  102. #include <mem/shm_mem.h>
  103. void* shm_malloc_x(unsigned int size);
  104. void shm_free_x(void *ptr);
  105. #define cds_malloc(s) shm_malloc(s)
  106. #define cds_free(p) shm_free(p)
  107. #define cds_malloc_ptr shm_malloc_x
  108. #define cds_free_ptr shm_free_x
  109. #define cds_malloc_pkg(s) pkg_malloc(s)
  110. #define cds_free_pkg(p) pkg_free(p)
  111. #else /* !SER */
  112. #include <stdlib.h>
  113. #define cds_malloc(s) malloc(s)
  114. #define cds_free(p) free(p)
  115. #define cds_malloc_ptr malloc
  116. #define cds_free_ptr free
  117. #define cds_malloc_pkg(s) malloc(s)
  118. #define cds_free_pkg(p) free(p)
  119. #endif /* !SER */
  120. #endif /* !TRACE_CDS_MEMORY */
  121. #ifdef __cplusplus
  122. }
  123. #endif
  124. /** @}
  125. * @} */
  126. #endif