q_malloc.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /* $Id: q_malloc.h 5897 2009-07-20 14:52:34Z bogdan_iancu $
  2. *
  3. * simple & fast malloc library
  4. *
  5. * Copyright (C) 2001-2003 FhG Fokus
  6. *
  7. * This file is part of opensips, a free SIP server.
  8. *
  9. * opensips 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. * opensips is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  22. *
  23. * History:
  24. * --------
  25. * 2003-05-21 on sparc64 roundto 8 even in debugging mode (so malloc'ed
  26. * long longs will be 64 bit aligned) (andrei)
  27. * 2004-07-19 support for 64 bit (2^64 mem. block) and more info
  28. * for the future de-fragmentation support (andrei)
  29. * 2004-11-10 support for > 4Gb mem. (switched to long) (andrei)
  30. */
  31. #if !defined(q_malloc_h)
  32. #define q_malloc_h
  33. #ifdef __cplusplus
  34. extern "C" {
  35. #endif
  36. #include "config.h"
  37. /* defs*/
  38. #ifdef NO_FIXED_ADDR
  39. //#define RELATIVE_ADDR //there are no help between different processes
  40. #endif // NO_FIXED_ADDR
  41. /* size we round to, must be = 2^n
  42. and also sizeof(qm_frag)+sizeof(qm_frag_end) must be multiple of ROUNDTO!*/
  43. #define ROUNDTO 16UL
  44. #define MIN_FRAG_SIZE ROUNDTO
  45. #define QM_MALLOC_OPTIMIZE_FACTOR 14UL /*used below */
  46. #define QM_MALLOC_OPTIMIZE ((unsigned long)(1UL<<QM_MALLOC_OPTIMIZE_FACTOR))
  47. /* size to optimize for,
  48. (most allocs <= this size),
  49. must be 2^k */
  50. #define QM_HASH_SIZE ((unsigned long)(QM_MALLOC_OPTIMIZE/ROUNDTO + \
  51. (sizeof(long)*8-QM_MALLOC_OPTIMIZE_FACTOR)+1))
  52. /* hash structure:
  53. * 0 .... QM_MALLOC_OPTIMIE/ROUNDTO - small buckets, size increases with
  54. * ROUNDTO from bucket to bucket
  55. * +1 .... end - size = 2^k, big buckets */
  56. struct qm_frag{
  57. unsigned long size;
  58. union{
  59. struct qm_frag* nxt_free;
  60. long is_free;
  61. }u;
  62. };
  63. struct qm_frag_end{
  64. unsigned long size;
  65. struct qm_frag* prev_free;
  66. };
  67. struct qm_frag_lnk{
  68. struct qm_frag head;
  69. struct qm_frag_end tail;
  70. unsigned long no;
  71. };
  72. struct qm_block{
  73. unsigned long size; /* total size */
  74. unsigned long used; /* alloc'ed size*/
  75. unsigned long real_used; /* used+malloc overhead*/
  76. unsigned long max_real_used;
  77. struct qm_frag* first_frag;
  78. struct qm_frag_end* last_frag_end;
  79. struct qm_frag_lnk free_hash[QM_HASH_SIZE];
  80. /*struct qm_frag_end free_lst_end;*/
  81. long lock;
  82. void *user_data[8];
  83. };
  84. /* init malloc and return a qm_block*/
  85. struct qm_block* qm_malloc_init(char* address, unsigned long size, int newcreate);
  86. void* qm_malloc(struct qm_block*, unsigned long size);
  87. void qm_free(struct qm_block*, void* p);
  88. void qm_lock(struct qm_block*);
  89. void qm_unlock(struct qm_block*);
  90. void qm_set_user_data(struct qm_block* qm, int idx, void* user_data);
  91. void* qm_get_user_data(struct qm_block* qm, int idx);
  92. #ifdef __cplusplus
  93. } // extern "C" {
  94. #endif
  95. #endif