alloc_lib.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. //////////////////////////////////////////////////////////////////////////////
  2. //
  3. // (C) Copyright Ion Gaztanaga 2005-2013. Distributed under the Boost
  4. // Software License, Version 1.0. (See accompanying file
  5. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. //
  7. // See http://www.boost.org/libs/container for documentation.
  8. //
  9. //////////////////////////////////////////////////////////////////////////////
  10. #ifndef BOOST_CONTAINER_ALLOC_LIB_EXT_H
  11. #define BOOST_CONTAINER_ALLOC_LIB_EXT_H
  12. #include <stddef.h>
  13. #ifdef _MSC_VER
  14. #pragma warning (push)
  15. #pragma warning (disable : 4127)
  16. #endif
  17. #ifdef __cplusplus
  18. extern "C" {
  19. #endif
  20. /*!An forward iterator to traverse the elements of a memory chain container.*/
  21. typedef struct multialloc_node_impl
  22. {
  23. struct multialloc_node_impl *next_node_ptr;
  24. } boost_cont_memchain_node;
  25. /*!An forward iterator to traverse the elements of a memory chain container.*/
  26. typedef struct multialloc_it_impl
  27. {
  28. boost_cont_memchain_node *node_ptr;
  29. } boost_cont_memchain_it;
  30. /*!Memory chain: A container holding memory portions allocated by boost_cont_multialloc_nodes
  31. and boost_cont_multialloc_arrays functions.*/
  32. typedef struct boost_cont_memchain_impl
  33. {
  34. size_t num_mem;
  35. boost_cont_memchain_node root_node;
  36. boost_cont_memchain_node *last_node_ptr;
  37. } boost_cont_memchain;
  38. /*!Advances the iterator one position so that it points to the next element in the memory chain*/
  39. #define BOOST_CONTAINER_MEMIT_NEXT(IT) (IT.node_ptr = IT.node_ptr->next_node_ptr)
  40. /*!Returns the address of the memory chain currently pointed by the iterator*/
  41. #define BOOST_CONTAINER_MEMIT_ADDR(IT) ((void*)IT.node_ptr)
  42. /*!Initializer for an iterator pointing to the position before the first element*/
  43. #define BOOST_CONTAINER_MEMCHAIN_BEFORE_BEGIN_IT(PMEMCHAIN) { &((PMEMCHAIN)->root_node) }
  44. /*!Initializer for an iterator pointing to the first element*/
  45. #define BOOST_CONTAINER_MEMCHAIN_BEGIN_IT(PMEMCHAIN) {(PMEMCHAIN)->root_node.next_node_ptr }
  46. /*!Initializer for an iterator pointing to the last element*/
  47. #define BOOST_CONTAINER_MEMCHAIN_LAST_IT(PMEMCHAIN) {(PMEMCHAIN)->last_node_ptr }
  48. /*!Initializer for an iterator pointing to one past the last element (end iterator)*/
  49. #define BOOST_CONTAINER_MEMCHAIN_END_IT(PMEMCHAIN) {(boost_cont_memchain_node *)0 }
  50. /*!True if IT is the end iterator, false otherwise*/
  51. #define BOOST_CONTAINER_MEMCHAIN_IS_END_IT(PMEMCHAIN, IT) (!(IT).node_ptr)
  52. /*!The address of the first memory portion hold by the memory chain*/
  53. #define BOOST_CONTAINER_MEMCHAIN_FIRSTMEM(PMEMCHAIN)((void*)((PMEMCHAIN)->root_node.next_node_ptr))
  54. /*!The address of the last memory portion hold by the memory chain*/
  55. #define BOOST_CONTAINER_MEMCHAIN_LASTMEM(PMEMCHAIN) ((void*)((PMEMCHAIN)->last_node_ptr))
  56. /*!The number of memory portions hold by the memory chain*/
  57. #define BOOST_CONTAINER_MEMCHAIN_SIZE(PMEMCHAIN) ((PMEMCHAIN)->num_mem)
  58. /*!Initializes the memory chain from the first memory portion, the last memory
  59. portion and number of portions obtained from another memory chain*/
  60. #define BOOST_CONTAINER_MEMCHAIN_INIT_FROM(PMEMCHAIN, FIRST, LAST, NUM)\
  61. (PMEMCHAIN)->last_node_ptr = (boost_cont_memchain_node *)(LAST), \
  62. (PMEMCHAIN)->root_node.next_node_ptr = (boost_cont_memchain_node *)(FIRST), \
  63. (PMEMCHAIN)->num_mem = (NUM);\
  64. /**/
  65. /*!Default initializes a memory chain. Postconditions: begin iterator is end iterator,
  66. the number of portions is zero.*/
  67. #define BOOST_CONTAINER_MEMCHAIN_INIT(PMEMCHAIN)\
  68. ((PMEMCHAIN)->root_node.next_node_ptr = 0, (PMEMCHAIN)->last_node_ptr = &((PMEMCHAIN)->root_node), (PMEMCHAIN)->num_mem = 0)\
  69. /**/
  70. /*!True if the memory chain is empty (holds no memory portions*/
  71. #define BOOST_CONTAINER_MEMCHAIN_EMPTY(PMEMCHAIN)\
  72. ((PMEMCHAIN)->num_mem == 0)\
  73. /**/
  74. /*!Inserts a new memory portions in the front of the chain*/
  75. #define BOOST_CONTAINER_MEMCHAIN_PUSH_BACK(PMEMCHAIN, MEM)\
  76. do{\
  77. boost_cont_memchain *____chain____ = (PMEMCHAIN);\
  78. boost_cont_memchain_node *____tmp_mem____ = (boost_cont_memchain_node *)(MEM);\
  79. ____chain____->last_node_ptr->next_node_ptr = ____tmp_mem____;\
  80. ____tmp_mem____->next_node_ptr = 0;\
  81. ____chain____->last_node_ptr = ____tmp_mem____;\
  82. ++____chain____->num_mem;\
  83. }while(0)\
  84. /**/
  85. /*!Inserts a new memory portions in the back of the chain*/
  86. #define BOOST_CONTAINER_MEMCHAIN_PUSH_FRONT(PMEMCHAIN, MEM)\
  87. do{\
  88. boost_cont_memchain *____chain____ = (PMEMCHAIN);\
  89. boost_cont_memchain_node *____tmp_mem____ = (boost_cont_memchain_node *)(MEM);\
  90. boost_cont_memchain *____root____ = &((PMEMCHAIN)->root_node);\
  91. if(!____chain____->root_node.next_node_ptr){\
  92. ____chain____->last_node_ptr = ____tmp_mem____;\
  93. }\
  94. boost_cont_memchain_node *____old_first____ = ____root____->next_node_ptr;\
  95. ____tmp_mem____->next_node_ptr = ____old_first____;\
  96. ____root____->next_node_ptr = ____tmp_mem____;\
  97. ++____chain____->num_mem;\
  98. }while(0)\
  99. /**/
  100. /*!Erases the memory portion after the portion pointed by BEFORE_IT from the memory chain*/
  101. /*!Precondition: BEFORE_IT must be a valid iterator of the memory chain and it can't be the end iterator*/
  102. #define BOOST_CONTAINER_MEMCHAIN_ERASE_AFTER(PMEMCHAIN, BEFORE_IT)\
  103. do{\
  104. boost_cont_memchain *____chain____ = (PMEMCHAIN);\
  105. boost_cont_memchain_node *____prev_node____ = (BEFORE_IT).node_ptr;\
  106. boost_cont_memchain_node *____erase_node____ = ____prev_node____->next_node_ptr;\
  107. if(____chain____->last_node_ptr == ____erase_node____){\
  108. ____chain____->last_node_ptr = &____chain____->root_node;\
  109. }\
  110. ____prev_node____->next_node_ptr = ____erase_node____->next_node_ptr;\
  111. --____chain____->num_mem;\
  112. }while(0)\
  113. /**/
  114. /*!Erases the first portion from the memory chain.
  115. Precondition: the memory chain must not be empty*/
  116. #define BOOST_CONTAINER_MEMCHAIN_POP_FRONT(PMEMCHAIN)\
  117. do{\
  118. boost_cont_memchain *____chain____ = (PMEMCHAIN);\
  119. boost_cont_memchain_node *____prev_node____ = &____chain____->root_node;\
  120. boost_cont_memchain_node *____erase_node____ = ____prev_node____->next_node_ptr;\
  121. if(____chain____->last_node_ptr == ____erase_node____){\
  122. ____chain____->last_node_ptr = &____chain____->root_node;\
  123. }\
  124. ____prev_node____->next_node_ptr = ____erase_node____->next_node_ptr;\
  125. --____chain____->num_mem;\
  126. }while(0)\
  127. /**/
  128. /*!Joins two memory chains inserting the portions of the second chain at the back of the first chain*/
  129. /*
  130. #define BOOST_CONTAINER_MEMCHAIN_SPLICE_BACK(PMEMCHAIN, PMEMCHAIN2)\
  131. do{\
  132. boost_cont_memchain *____chain____ = (PMEMCHAIN);\
  133. boost_cont_memchain *____chain2____ = (PMEMCHAIN2);\
  134. if(!____chain2____->root_node.next_node_ptr){\
  135. break;\
  136. }\
  137. else if(!____chain____->first_mem){\
  138. ____chain____->first_mem = ____chain2____->first_mem;\
  139. ____chain____->last_node_ptr = ____chain2____->last_node_ptr;\
  140. ____chain____->num_mem = ____chain2____->num_mem;\
  141. BOOST_CONTAINER_MEMCHAIN_INIT(*____chain2____);\
  142. }\
  143. else{\
  144. ____chain____->last_node_ptr->next_node_ptr = ____chain2____->first_mem;\
  145. ____chain____->last_node_ptr = ____chain2____->last_node_ptr;\
  146. ____chain____->num_mem += ____chain2____->num_mem;\
  147. }\
  148. }while(0)\*/
  149. /**/
  150. /*!Joins two memory chains inserting the portions of the second chain at the back of the first chain*/
  151. #define BOOST_CONTAINER_MEMCHAIN_INCORPORATE_AFTER(PMEMCHAIN, BEFORE_IT, FIRST, BEFORELAST, NUM)\
  152. do{\
  153. boost_cont_memchain *____chain____ = (PMEMCHAIN);\
  154. boost_cont_memchain_node *____pnode____ = (BEFORE_IT).node_ptr;\
  155. boost_cont_memchain_node *____next____ = ____pnode____->next_node_ptr;\
  156. boost_cont_memchain_node *____first____ = (boost_cont_memchain_node *)(FIRST);\
  157. boost_cont_memchain_node *____blast____ = (boost_cont_memchain_node *)(BEFORELAST);\
  158. size_t ____num____ = (NUM);\
  159. if(!____num____){\
  160. break;\
  161. }\
  162. if(____pnode____ == ____chain____->last_node_ptr){\
  163. ____chain____->last_node_ptr = ____blast____;\
  164. }\
  165. ____pnode____->next_node_ptr = ____first____;\
  166. ____blast____->next_node_ptr = ____next____;\
  167. ____chain____->num_mem += ____num____;\
  168. }while(0)\
  169. /**/
  170. /*!Indicates the all elements allocated by boost_cont_multialloc_nodes or boost_cont_multialloc_arrays
  171. must be contiguous.*/
  172. #define DL_MULTIALLOC_ALL_CONTIGUOUS ((size_t)(-1))
  173. /*!Indicates the number of contiguous elements allocated by boost_cont_multialloc_nodes or boost_cont_multialloc_arrays
  174. should be selected by those functions.*/
  175. #define DL_MULTIALLOC_DEFAULT_CONTIGUOUS ((size_t)(0))
  176. typedef struct boost_cont_malloc_stats_impl
  177. {
  178. size_t max_system_bytes;
  179. size_t system_bytes;
  180. size_t in_use_bytes;
  181. } boost_cont_malloc_stats_t;
  182. typedef unsigned int allocation_type;
  183. enum
  184. {
  185. // constants for allocation commands
  186. BOOST_CONTAINER_ALLOCATE_NEW = 0X01,
  187. BOOST_CONTAINER_EXPAND_FWD = 0X02,
  188. BOOST_CONTAINER_EXPAND_BWD = 0X04,
  189. BOOST_CONTAINER_SHRINK_IN_PLACE = 0X08,
  190. BOOST_CONTAINER_NOTHROW_ALLOCATION = 0X10,
  191. // BOOST_CONTAINER_ZERO_MEMORY = 0X20,
  192. BOOST_CONTAINER_TRY_SHRINK_IN_PLACE = 0X40,
  193. BOOST_CONTAINER_EXPAND_BOTH = BOOST_CONTAINER_EXPAND_FWD | BOOST_CONTAINER_EXPAND_BWD,
  194. BOOST_CONTAINER_EXPAND_OR_NEW = BOOST_CONTAINER_ALLOCATE_NEW | BOOST_CONTAINER_EXPAND_BOTH
  195. };
  196. //#define BOOST_CONTAINERDLMALLOC__FOOTERS
  197. #ifndef BOOST_CONTAINERDLMALLOC__FOOTERS
  198. enum { BOOST_CONTAINER_ALLOCATION_PAYLOAD = sizeof(size_t) };
  199. #else
  200. enum { BOOST_CONTAINER_ALLOCATION_PAYLOAD = sizeof(size_t)*2 };
  201. #endif
  202. typedef struct boost_cont_command_ret_impl
  203. {
  204. void *first;
  205. int second;
  206. }boost_cont_command_ret_t;
  207. size_t boost_cont_size(const void *p);
  208. void* boost_cont_malloc(size_t bytes);
  209. void boost_cont_free(void* mem);
  210. void* boost_cont_memalign(size_t bytes, size_t alignment);
  211. int boost_cont_multialloc_nodes
  212. (size_t n_elements, size_t elem_size, size_t contiguous_elements, boost_cont_memchain *pchain);
  213. int boost_cont_multialloc_arrays
  214. (size_t n_elements, const size_t *sizes, size_t sizeof_element, size_t contiguous_elements, boost_cont_memchain *pchain);
  215. void boost_cont_multidealloc(boost_cont_memchain *pchain);
  216. size_t boost_cont_footprint();
  217. size_t boost_cont_allocated_memory();
  218. size_t boost_cont_chunksize(const void *p);
  219. int boost_cont_all_deallocated();
  220. boost_cont_malloc_stats_t boost_cont_malloc_stats();
  221. size_t boost_cont_in_use_memory();
  222. int boost_cont_trim(size_t pad);
  223. int boost_cont_mallopt(int parameter_number, int parameter_value);
  224. int boost_cont_grow
  225. (void* oldmem, size_t minbytes, size_t maxbytes, size_t *received);
  226. int boost_cont_shrink
  227. (void* oldmem, size_t minbytes, size_t maxbytes, size_t *received, int do_commit);
  228. void* boost_cont_alloc
  229. (size_t minbytes, size_t preferred_bytes, size_t *received_bytes);
  230. int boost_cont_malloc_check();
  231. boost_cont_command_ret_t boost_cont_allocation_command
  232. ( allocation_type command
  233. , size_t sizeof_object
  234. , size_t limit_objects
  235. , size_t preferred_objects
  236. , size_t *received_objects
  237. , void *reuse_ptr
  238. );
  239. void *boost_cont_sync_create();
  240. void boost_cont_sync_destroy(void *sync);
  241. int boost_cont_sync_lock(void *sync);
  242. void boost_cont_sync_unlock(void *sync);
  243. int boost_cont_global_sync_lock();
  244. void boost_cont_global_sync_unlock();
  245. #ifdef __cplusplus
  246. } //extern "C" {
  247. #endif
  248. #ifdef _MSC_VER
  249. #pragma warning (pop)
  250. #endif
  251. #endif //#define BOOST_CONTAINERDLMALLOC__EXT_H