sf_malloc.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /* $Id$
  2. *
  3. * shared memory, multi-process safe, pool based version of f_malloc
  4. *
  5. * Copyright (C) 2007 iptelorg GmbH
  6. *
  7. * Permission to use, copy, modify, and distribute this software for any
  8. * purpose with or without fee is hereby granted, provided that the above
  9. * copyright notice and this permission notice appear in all copies.
  10. *
  11. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  12. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  13. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  14. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  15. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  16. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  17. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  18. */
  19. /*
  20. * History:
  21. * --------
  22. * 2003-05-21 on sparc64 roundto 8 even in debugging mode (so malloc'ed
  23. * long longs will be 64 bit aligned) (andrei)
  24. * 2004-07-19 support for 64 bit (2^64 mem. block) and more info
  25. * for the future de-fragmentation support (andrei)
  26. * 2004-11-10 support for > 4Gb mem., switched to long (andrei)
  27. * 2007-06-09 forked from the f_malloc code (andrei)
  28. */
  29. #if !defined(sf_malloc_h)
  30. #define sf_malloc_h
  31. #include "meminfo.h"
  32. #include "../lock_ops.h"
  33. #include "../atomic_ops.h"
  34. #include "../compiler_opt.h"
  35. /* defs*/
  36. #ifdef GEN_LOCK_T_UNLIMITED
  37. #define SFM_LOCK_PER_BUCKET
  38. #else
  39. #define SFM_ONE_LOCK
  40. #endif
  41. #ifdef DBG_SF_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 SF_ROUNDTO sizeof(long long)
  47. #else
  48. #define SF_ROUNDTO sizeof(void*) /* size we round to, must be = 2^n, and
  49. sizeof(sfm_frag) must be multiple of SF_ROUNDTO !*/
  50. #endif
  51. #else /* DBG_SF_MALLOC */
  52. #define SF_ROUNDTO 8UL
  53. #endif
  54. #define SF_MIN_FRAG_SIZE SF_ROUNDTO
  55. #define SFM_POOLS_NO 4U /* the more the better, but higher initial
  56. mem. consumption */
  57. #define SF_MALLOC_OPTIMIZE_FACTOR 14UL /*used below */
  58. #define SF_MALLOC_OPTIMIZE (1UL<<SF_MALLOC_OPTIMIZE_FACTOR)
  59. /* size to optimize for,
  60. (most allocs <= this size),
  61. must be 2^k */
  62. #define SF_HASH_POOL_SIZE (SF_MALLOC_OPTIMIZE/SF_ROUNDTO + 1)
  63. #define SF_POOL_MAX_SIZE SF_MALLOC_OPTIMIZE
  64. #define SF_HASH_SIZE (SF_MALLOC_OPTIMIZE/SF_ROUNDTO + \
  65. (sizeof(long)*8-SF_MALLOC_OPTIMIZE_FACTOR)+1)
  66. /* hash structure:
  67. * 0 .... SF_MALLOC_OPTIMIZE/SF_ROUNDTO - small buckets, size increases with
  68. * SF_ROUNDTO from bucket to bucket
  69. * +1 .... end - size = 2^k, big buckets */
  70. struct sfm_frag{
  71. union{
  72. struct sfm_frag* nxt_free;
  73. long reserved;
  74. }u;
  75. unsigned long size;
  76. unsigned long id; /* TODO better optimize the size */
  77. /* pad to SF_ROUNDTO multiple */
  78. char _pad[((3*sizeof(long)+SF_ROUNDTO-1)&~(SF_ROUNDTO-1))-3*sizeof(long)];
  79. #ifdef DBG_SF_MALLOC
  80. const char* file;
  81. const char* func;
  82. unsigned long line;
  83. unsigned long check;
  84. #endif
  85. };
  86. struct sfm_frag_lnk{
  87. struct sfm_frag* first;
  88. #ifdef SFM_LOCK_PER_BUCKET
  89. gen_lock_t lock;
  90. #endif
  91. unsigned long no;
  92. };
  93. struct sfm_pool_head{
  94. struct sfm_frag* first;
  95. #ifdef SFM_LOCK_PER_BUCKET
  96. gen_lock_t lock;
  97. #endif
  98. unsigned long no;
  99. unsigned long misses;
  100. };
  101. struct sfm_pool{
  102. #ifdef SFM_ONE_LOCK
  103. gen_lock_t lock;
  104. #endif
  105. unsigned long missed;
  106. unsigned long hits; /* debugging only TODO: remove */
  107. unsigned long bitmap;
  108. struct sfm_pool_head pool_hash[SF_HASH_POOL_SIZE];
  109. };
  110. struct sfm_block{
  111. #ifdef SFM_ONE_LOCK
  112. gen_lock_t lock;
  113. #endif
  114. atomic_t crt_id; /* current pool */
  115. unsigned long size; /* total size */
  116. /* stats are kept now per bucket */
  117. struct sfm_frag* first_frag;
  118. struct sfm_frag* last_frag;
  119. unsigned long bitmap; /* only up to SF_MALLOC_OPTIMIZE */
  120. struct sfm_frag_lnk free_hash[SF_HASH_SIZE];
  121. struct sfm_pool pool[SFM_POOLS_NO];
  122. int is_init;
  123. gen_lock_t get_and_split;
  124. char _pad[256];
  125. };
  126. struct sfm_block* sfm_malloc_init(char* address, unsigned long size);
  127. void sfm_malloc_destroy(struct sfm_block* qm);
  128. int sfm_pool_reset();
  129. #ifdef DBG_SF_MALLOC
  130. void* sfm_malloc(struct sfm_block*, unsigned long size,
  131. const char* file, const char* func, unsigned int line);
  132. #else
  133. void* sfm_malloc(struct sfm_block*, unsigned long size);
  134. #endif
  135. #ifdef DBG_SF_MALLOC
  136. void sfm_free(struct sfm_block*, void* p, const char* file, const char* func,
  137. unsigned int line);
  138. #else
  139. void sfm_free(struct sfm_block*, void* p);
  140. #endif
  141. #ifdef DBG_SF_MALLOC
  142. void* sfm_realloc(struct sfm_block*, void* p, unsigned long size,
  143. const char* file, const char* func, unsigned int line);
  144. #else
  145. void* sfm_realloc(struct sfm_block*, void* p, unsigned long size);
  146. #endif
  147. void sfm_status(struct sfm_block*);
  148. void sfm_info(struct sfm_block*, struct mem_info*);
  149. unsigned long sfm_available(struct sfm_block*);
  150. #endif