q_malloc.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. */
  27. /**
  28. * \file
  29. * \brief Simple & fast malloc library
  30. * \ingroup mem
  31. */
  32. #if !defined(q_malloc_h) && !defined(F_MALLOC)
  33. #define q_malloc_h
  34. #include "meminfo.h"
  35. /* defs*/
  36. #ifdef DBG_QM_MALLOC
  37. #if defined(__CPU_sparc64) || defined(__CPU_sparc)
  38. /* tricky, on sun in 32 bits mode long long must be 64 bits aligned
  39. * but long can be 32 bits aligned => malloc should return long long
  40. * aligned memory */
  41. #define ROUNDTO sizeof(long long)
  42. #else
  43. #define ROUNDTO sizeof(void*) /* minimum possible ROUNDTO ->heavy
  44. debugging*/
  45. #endif
  46. #else /* DBG_QM_MALLOC */
  47. #define ROUNDTO 16UL /* size we round to, must be = 2^n and also
  48. sizeof(qm_frag)+sizeof(qm_frag_end)
  49. must be multiple of ROUNDTO!
  50. */
  51. #endif
  52. #define MIN_FRAG_SIZE ROUNDTO
  53. #define QM_MALLOC_OPTIMIZE_FACTOR 14UL /*used below */
  54. #define QM_MALLOC_OPTIMIZE ((unsigned long)(1UL<<QM_MALLOC_OPTIMIZE_FACTOR))
  55. /* size to optimize for,
  56. (most allocs <= this size),
  57. must be 2^k */
  58. #define QM_HASH_SIZE ((unsigned long)(QM_MALLOC_OPTIMIZE/ROUNDTO + \
  59. (sizeof(long)*8-QM_MALLOC_OPTIMIZE_FACTOR)+1))
  60. /* hash structure:
  61. * 0 .... QM_MALLOC_OPTIMIE/ROUNDTO - small buckets, size increases with
  62. * ROUNDTO from bucket to bucket
  63. * +1 .... end - size = 2^k, big buckets */
  64. struct qm_frag{
  65. unsigned long size;
  66. union{
  67. struct qm_frag* nxt_free;
  68. long is_free;
  69. }u;
  70. #ifdef DBG_QM_MALLOC
  71. const char* file;
  72. const char* func;
  73. unsigned long line;
  74. unsigned long check;
  75. #endif
  76. };
  77. struct qm_frag_end{
  78. #ifdef DBG_QM_MALLOC
  79. unsigned long check1;
  80. unsigned long check2;
  81. unsigned long reserved1;
  82. unsigned long reserved2;
  83. #endif
  84. unsigned long size;
  85. struct qm_frag* prev_free;
  86. };
  87. struct qm_frag_lnk{
  88. struct qm_frag head;
  89. struct qm_frag_end tail;
  90. unsigned long no;
  91. };
  92. /**
  93. * \brief Block of memory for Q_MALLOC memory manager
  94. * \see mem_info
  95. */
  96. struct qm_block{
  97. unsigned long size; /* total size */
  98. unsigned long used; /* alloc'ed size*/
  99. unsigned long real_used; /* used+malloc overhead*/
  100. unsigned long max_real_used;
  101. struct qm_frag* first_frag;
  102. struct qm_frag_end* last_frag_end;
  103. struct qm_frag_lnk free_hash[QM_HASH_SIZE];
  104. /*struct qm_frag_end free_lst_end;*/
  105. };
  106. struct qm_block* qm_malloc_init(char* address, unsigned long size);
  107. #ifdef DBG_QM_MALLOC
  108. void* qm_malloc(struct qm_block*, unsigned long size, const char* file,
  109. const char* func, unsigned int line);
  110. #else
  111. void* qm_malloc(struct qm_block*, unsigned long size);
  112. #endif
  113. #ifdef DBG_QM_MALLOC
  114. void qm_free(struct qm_block*, void* p, const char* file, const char* func,
  115. unsigned int line);
  116. #else
  117. void qm_free(struct qm_block*, void* p);
  118. #endif
  119. #ifdef DBG_QM_MALLOC
  120. void* qm_realloc(struct qm_block*, void* p, unsigned long size,
  121. const char* file, const char* func, unsigned int line);
  122. #else
  123. void* qm_realloc(struct qm_block*, void* p, unsigned long size);
  124. #endif
  125. void qm_status(struct qm_block*);
  126. void qm_check(struct qm_block*);
  127. void qm_info(struct qm_block*, struct mem_info*);
  128. unsigned long qm_available(struct qm_block* qm);
  129. #ifdef DBG_QM_MALLOC
  130. void qm_sums(struct qm_block* qm);
  131. #else
  132. #define qm_sums(v) do{}while(0)
  133. #endif /*DBQ_QM_MALLOC */
  134. #endif