f_malloc.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /*
  2. * Copyright (C) 2001-2003 FhG Fokus
  3. *
  4. * This file is part of sip-router, a free SIP server.
  5. *
  6. * Permission to use, copy, modify, and distribute this software for any
  7. * purpose with or without fee is hereby granted, provided that the above
  8. * copyright notice and this permission notice appear in all copies.
  9. *
  10. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  11. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  12. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  13. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  14. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  15. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  16. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  17. */
  18. /*
  19. * History:
  20. * --------
  21. * 2003-05-21 on sparc64 roundto 8 even in debugging mode (so malloc'ed
  22. * long longs will be 64 bit aligned) (andrei)
  23. * 2004-07-19 support for 64 bit (2^64 mem. block) and more info
  24. * for the future de-fragmentation support (andrei)
  25. * 2004-11-10 support for > 4Gb mem., switched to long (andrei)
  26. * 2007-06-23 added hash bitmap (andrei)
  27. */
  28. /**
  29. * \file
  30. * \brief Simple, very fast, malloc library
  31. * \ingroup mem
  32. */
  33. #if !defined(f_malloc_h)
  34. #define f_malloc_h
  35. #ifdef DBG_QM_MALLOC
  36. #ifndef DBG_F_MALLOC
  37. #define DBG_F_MALLOC
  38. #endif /* DBG_F_MALLOC */
  39. #endif /* DBG_QM_MALLOC */
  40. #include "meminfo.h"
  41. /**
  42. * Use a bitmap to quickly find free fragments, should speed up
  43. * especially startup (non-warmed-up malloc)
  44. */
  45. #define F_MALLOC_HASH_BITMAP
  46. #ifdef DBG_F_MALLOC
  47. #if defined(__CPU_sparc64) || defined(__CPU_sparc)
  48. /* tricky, on sun in 32 bits mode long long must be 64 bits aligned
  49. * but long can be 32 bits aligned => malloc should return long long
  50. * aligned memory */
  51. #define ROUNDTO sizeof(long long)
  52. #else
  53. #define ROUNDTO sizeof(void*) /* size we round to, must be = 2^n, and
  54. sizeof(fm_frag) must be multiple of ROUNDTO !*/
  55. #endif
  56. #else /* DBG_F_MALLOC */
  57. #define ROUNDTO 8UL
  58. #endif
  59. #define MIN_FRAG_SIZE ROUNDTO
  60. #define F_MALLOC_OPTIMIZE_FACTOR 14UL /* used below */
  61. /** Size to optimize for, (most allocs <= this size), must be 2^k */
  62. #define F_MALLOC_OPTIMIZE (1UL<<F_MALLOC_OPTIMIZE_FACTOR)
  63. #define F_HASH_SIZE (F_MALLOC_OPTIMIZE/ROUNDTO + \
  64. (sizeof(long)*8-F_MALLOC_OPTIMIZE_FACTOR)+1)
  65. #ifdef F_MALLOC_HASH_BITMAP
  66. typedef unsigned long fm_hash_bitmap_t;
  67. #define FM_HASH_BMP_BITS (sizeof(fm_hash_bitmap_t)*8)
  68. #define FM_HASH_BMP_SIZE \
  69. ((F_HASH_SIZE+FM_HASH_BMP_BITS-1)/FM_HASH_BMP_BITS)
  70. #endif
  71. /**
  72. * \name Hash structure
  73. * - 0 .... F_MALLOC_OPTIMIZE/ROUNDTO - small buckets, size increases with
  74. * ROUNDTO from bucket to bucket
  75. * - +1 .... end - size = 2^k, big buckets
  76. */
  77. struct fm_frag{
  78. unsigned long size;
  79. union{
  80. struct fm_frag* nxt_free;
  81. long reserved;
  82. }u;
  83. #ifdef DBG_F_MALLOC
  84. const char* file;
  85. const char* func;
  86. unsigned long line;
  87. unsigned long check;
  88. #endif
  89. };
  90. struct fm_frag_lnk{
  91. struct fm_frag* first;
  92. unsigned long no;
  93. };
  94. /**
  95. * \brief Block of memory for F_MALLOC memory manager
  96. * \see mem_info
  97. */
  98. struct fm_block{
  99. unsigned long size; /** total size */
  100. #if defined(DBG_F_MALLOC) || defined(MALLOC_STATS)
  101. unsigned long used; /** allocated size*/
  102. unsigned long real_used; /** used + malloc overhead */
  103. unsigned long max_real_used;
  104. #endif
  105. struct fm_frag* first_frag;
  106. struct fm_frag* last_frag;
  107. #ifdef F_MALLOC_HASH_BITMAP
  108. fm_hash_bitmap_t free_bitmap[FM_HASH_BMP_SIZE];
  109. #endif
  110. struct fm_frag_lnk free_hash[F_HASH_SIZE];
  111. };
  112. /**
  113. * \brief Initialize memory manager malloc
  114. * \param address start address for memory block
  115. * \param size Size of allocation
  116. * \return return the fm_block
  117. */
  118. struct fm_block* fm_malloc_init(char* address, unsigned long size);
  119. /**
  120. * \brief Main memory manager allocation function
  121. * \param qm memory block
  122. * \param size memory allocation size
  123. * \return address of allocated memory
  124. */
  125. #ifdef DBG_F_MALLOC
  126. void* fm_malloc(struct fm_block* qm, unsigned long size,
  127. const char* file, const char* func, unsigned int line);
  128. #else
  129. void* fm_malloc(struct fm_block* qm, unsigned long size);
  130. #endif
  131. /**
  132. * \brief Main memory manager free function
  133. *
  134. * Main memory manager free function, provide functionality necessary for pkg_free
  135. * \param qm memory block
  136. * \param p freed memory
  137. */
  138. #ifdef DBG_F_MALLOC
  139. void fm_free(struct fm_block* qm, void* p, const char* file, const char* func,
  140. unsigned int line);
  141. #else
  142. void fm_free(struct fm_block* qm, void* p);
  143. #endif
  144. /**
  145. * \brief Main memory manager realloc function
  146. *
  147. * Main memory manager realloc function, provide functionality for pkg_realloc
  148. * \param qm memory block
  149. * \param p reallocated memory block
  150. * \param size
  151. * \return reallocated memory block
  152. */
  153. #ifdef DBG_F_MALLOC
  154. void* fm_realloc(struct fm_block* qm, void* p, unsigned long size,
  155. const char* file, const char* func, unsigned int line);
  156. #else
  157. void* fm_realloc(struct fm_block* qm, void* p, unsigned long size);
  158. #endif
  159. /**
  160. * \brief Report internal memory manager status
  161. * \param qm memory block
  162. */
  163. void fm_status(struct fm_block* qm);
  164. /**
  165. * \brief Fills a malloc info structure with info about the block
  166. *
  167. * Fills a malloc info structure with info about the block, if a
  168. * parameter is not supported, it will be filled with 0
  169. * \param qm memory block
  170. * \param info memory information
  171. */
  172. void fm_info(struct fm_block* qm, struct mem_info* info);
  173. /**
  174. * \brief Helper function for available memory report
  175. * \param qm memory block
  176. * \return Returns how much free memory is available, on error (not compiled
  177. * with bookkeeping code) returns (unsigned long)(-1)
  178. */
  179. unsigned long fm_available(struct fm_block* qm);
  180. /**
  181. * \brief Debugging helper, summary and logs all allocated memory blocks
  182. * \param qm memory block
  183. */
  184. #ifdef DBG_F_MALLOC
  185. void fm_sums(struct fm_block* qm);
  186. #else
  187. #define fm_sums(qm) do{}while(0)
  188. #endif /* DBG_F_MALLOC */
  189. #endif