q_malloc.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /* $Id$
  2. *
  3. * simple & fast malloc library
  4. *
  5. * Copyright (C) 2001-2003 FhG Fokus
  6. *
  7. * This file is part of ser, a free SIP server.
  8. *
  9. * ser is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version
  13. *
  14. * For a license to use the ser software under conditions
  15. * other than those described here, or to purchase support for this
  16. * software, please contact iptel.org by e-mail at the following addresses:
  17. * [email protected]
  18. *
  19. * ser is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU General Public License
  25. * along with this program; if not, write to the Free Software
  26. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  27. */
  28. /*
  29. * History:
  30. * --------
  31. * 2003-05-21 on sparc64 roundto 8 even in debugging mode (so malloc'ed
  32. * long longs will be 64 bit aligned) (andrei)
  33. * 2004-07-19 support for 64 bit (2^64 mem. block) and more info
  34. * for the future de-fragmentation support (andrei)
  35. * 2004-11-10 support for > 4Gb mem. (switched to long) (andrei)
  36. */
  37. #if !defined(q_malloc_h) && !defined(VQ_MALLOC) && !defined(F_MALLOC)
  38. #define q_malloc_h
  39. #include "meminfo.h"
  40. /* defs*/
  41. #ifdef DBG_QM_MALLOC
  42. #if defined(__CPU_sparc64) || defined(__CPU_sparc)
  43. /* tricky, on sun in 32 bits mode long long must be 64 bits aligned
  44. * but long can be 32 bits aligned => malloc should return long long
  45. * aligned memory */
  46. #define ROUNDTO sizeof(long long)
  47. #else
  48. #define ROUNDTO sizeof(void*) /* minimum possible ROUNDTO ->heavy
  49. debugging*/
  50. #endif
  51. #else /* DBG_QM_MALLOC */
  52. #define ROUNDTO 16UL /* size we round to, must be = 2^n and also
  53. sizeof(qm_frag)+sizeof(qm_frag_end)
  54. must be multiple of ROUNDTO!
  55. */
  56. #endif
  57. #define MIN_FRAG_SIZE ROUNDTO
  58. #define QM_MALLOC_OPTIMIZE_FACTOR 14UL /*used below */
  59. #define QM_MALLOC_OPTIMIZE ((unsigned long)(1UL<<QM_MALLOC_OPTIMIZE_FACTOR))
  60. /* size to optimize for,
  61. (most allocs <= this size),
  62. must be 2^k */
  63. #define QM_HASH_SIZE ((unsigned long)(QM_MALLOC_OPTIMIZE/ROUNDTO + \
  64. (sizeof(long)*8-QM_MALLOC_OPTIMIZE_FACTOR)+1))
  65. /* hash structure:
  66. * 0 .... QM_MALLOC_OPTIMIE/ROUNDTO - small buckets, size increases with
  67. * ROUNDTO from bucket to bucket
  68. * +1 .... end - size = 2^k, big buckets */
  69. struct qm_frag{
  70. unsigned long size;
  71. union{
  72. struct qm_frag* nxt_free;
  73. long is_free;
  74. }u;
  75. #ifdef DBG_QM_MALLOC
  76. const char* file;
  77. const char* func;
  78. unsigned long line;
  79. unsigned long check;
  80. #endif
  81. };
  82. struct qm_frag_end{
  83. #ifdef DBG_QM_MALLOC
  84. unsigned long check1;
  85. unsigned long check2;
  86. unsigned long reserved1;
  87. unsigned long reserved2;
  88. #endif
  89. unsigned long size;
  90. struct qm_frag* prev_free;
  91. };
  92. struct qm_frag_lnk{
  93. struct qm_frag head;
  94. struct qm_frag_end tail;
  95. unsigned long no;
  96. };
  97. struct qm_block{
  98. unsigned long size; /* total size */
  99. unsigned long used; /* alloc'ed size*/
  100. unsigned long real_used; /* used+malloc overhead*/
  101. unsigned long max_real_used;
  102. struct qm_frag* first_frag;
  103. struct qm_frag_end* last_frag_end;
  104. struct qm_frag_lnk free_hash[QM_HASH_SIZE];
  105. /*struct qm_frag_end free_lst_end;*/
  106. };
  107. struct qm_block* qm_malloc_init(char* address, unsigned long size);
  108. #ifdef DBG_QM_MALLOC
  109. void* qm_malloc(struct qm_block*, unsigned long size, const char* file,
  110. const char* func, unsigned int line);
  111. #else
  112. void* qm_malloc(struct qm_block*, unsigned long size);
  113. #endif
  114. #ifdef DBG_QM_MALLOC
  115. void qm_free(struct qm_block*, void* p, const char* file, const char* func,
  116. unsigned int line);
  117. #else
  118. void qm_free(struct qm_block*, void* p);
  119. #endif
  120. #ifdef DBG_QM_MALLOC
  121. void* qm_realloc(struct qm_block*, void* p, unsigned long size,
  122. const char* file, const char* func, unsigned int line);
  123. #else
  124. void* qm_realloc(struct qm_block*, void* p, unsigned long size);
  125. #endif
  126. void qm_status(struct qm_block*);
  127. void qm_check(struct qm_block*);
  128. void qm_info(struct qm_block*, struct mem_info*);
  129. unsigned long qm_available(struct qm_block* qm);
  130. #endif