1 //////////////////////////////////////////////////////////////////////////////
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)
7 // See http://www.boost.org/libs/container for documentation.
9 //////////////////////////////////////////////////////////////////////////////
10 #ifndef BOOST_CONTAINER_ALLOC_LIB_EXT_H
11 #define BOOST_CONTAINER_ALLOC_LIB_EXT_H
16 #pragma warning (push)
17 #pragma warning (disable : 4127)
24 /*!An forward iterator to traverse the elements of a memory chain container.*/
25 typedef struct multialloc_node_impl
27 struct multialloc_node_impl
*next_node_ptr
;
28 } boost_cont_memchain_node
;
31 /*!An forward iterator to traverse the elements of a memory chain container.*/
32 typedef struct multialloc_it_impl
34 boost_cont_memchain_node
*node_ptr
;
35 } boost_cont_memchain_it
;
37 /*!Memory chain: A container holding memory portions allocated by boost_cont_multialloc_nodes
38 and boost_cont_multialloc_arrays functions.*/
39 typedef struct boost_cont_memchain_impl
42 boost_cont_memchain_node root_node
;
43 boost_cont_memchain_node
*last_node_ptr
;
44 } boost_cont_memchain
;
46 /*!Advances the iterator one position so that it points to the next element in the memory chain*/
47 #define BOOST_CONTAINER_MEMIT_NEXT(IT) (IT.node_ptr = IT.node_ptr->next_node_ptr)
49 /*!Returns the address of the memory chain currently pointed by the iterator*/
50 #define BOOST_CONTAINER_MEMIT_ADDR(IT) ((void*)IT.node_ptr)
52 /*!Initializer for an iterator pointing to the position before the first element*/
53 #define BOOST_CONTAINER_MEMCHAIN_BEFORE_BEGIN_IT(PMEMCHAIN) { &((PMEMCHAIN)->root_node) }
55 /*!Initializer for an iterator pointing to the first element*/
56 #define BOOST_CONTAINER_MEMCHAIN_BEGIN_IT(PMEMCHAIN) {(PMEMCHAIN)->root_node.next_node_ptr }
58 /*!Initializer for an iterator pointing to the last element*/
59 #define BOOST_CONTAINER_MEMCHAIN_LAST_IT(PMEMCHAIN) {(PMEMCHAIN)->last_node_ptr }
61 /*!Initializer for an iterator pointing to one past the last element (end iterator)*/
62 #define BOOST_CONTAINER_MEMCHAIN_END_IT(PMEMCHAIN) {(boost_cont_memchain_node *)0 }
64 /*!True if IT is the end iterator, false otherwise*/
65 #define BOOST_CONTAINER_MEMCHAIN_IS_END_IT(PMEMCHAIN, IT) (!(IT).node_ptr)
67 /*!The address of the first memory portion hold by the memory chain*/
68 #define BOOST_CONTAINER_MEMCHAIN_FIRSTMEM(PMEMCHAIN)((void*)((PMEMCHAIN)->root_node.next_node_ptr))
70 /*!The address of the last memory portion hold by the memory chain*/
71 #define BOOST_CONTAINER_MEMCHAIN_LASTMEM(PMEMCHAIN) ((void*)((PMEMCHAIN)->last_node_ptr))
73 /*!The number of memory portions hold by the memory chain*/
74 #define BOOST_CONTAINER_MEMCHAIN_SIZE(PMEMCHAIN) ((PMEMCHAIN)->num_mem)
76 /*!Initializes the memory chain from the first memory portion, the last memory
77 portion and number of portions obtained from another memory chain*/
78 #define BOOST_CONTAINER_MEMCHAIN_INIT_FROM(PMEMCHAIN, FIRST, LAST, NUM)\
79 (PMEMCHAIN)->last_node_ptr = (boost_cont_memchain_node *)(LAST), \
80 (PMEMCHAIN)->root_node.next_node_ptr = (boost_cont_memchain_node *)(FIRST), \
81 (PMEMCHAIN)->num_mem = (NUM);\
84 /*!Default initializes a memory chain. Postconditions: begin iterator is end iterator,
85 the number of portions is zero.*/
86 #define BOOST_CONTAINER_MEMCHAIN_INIT(PMEMCHAIN)\
87 ((PMEMCHAIN)->root_node.next_node_ptr = 0, (PMEMCHAIN)->last_node_ptr = &((PMEMCHAIN)->root_node), (PMEMCHAIN)->num_mem = 0)\
90 /*!True if the memory chain is empty (holds no memory portions*/
91 #define BOOST_CONTAINER_MEMCHAIN_EMPTY(PMEMCHAIN)\
92 ((PMEMCHAIN)->num_mem == 0)\
95 /*!Inserts a new memory portions in the front of the chain*/
96 #define BOOST_CONTAINER_MEMCHAIN_PUSH_BACK(PMEMCHAIN, MEM)\
98 boost_cont_memchain *____chain____ = (PMEMCHAIN);\
99 boost_cont_memchain_node *____tmp_mem____ = (boost_cont_memchain_node *)(MEM);\
100 ____chain____->last_node_ptr->next_node_ptr = ____tmp_mem____;\
101 ____tmp_mem____->next_node_ptr = 0;\
102 ____chain____->last_node_ptr = ____tmp_mem____;\
103 ++____chain____->num_mem;\
107 /*!Inserts a new memory portions in the back of the chain*/
108 #define BOOST_CONTAINER_MEMCHAIN_PUSH_FRONT(PMEMCHAIN, MEM)\
110 boost_cont_memchain *____chain____ = (PMEMCHAIN);\
111 boost_cont_memchain_node *____tmp_mem____ = (boost_cont_memchain_node *)(MEM);\
112 boost_cont_memchain *____root____ = &((PMEMCHAIN)->root_node);\
113 if(!____chain____->root_node.next_node_ptr){\
114 ____chain____->last_node_ptr = ____tmp_mem____;\
116 boost_cont_memchain_node *____old_first____ = ____root____->next_node_ptr;\
117 ____tmp_mem____->next_node_ptr = ____old_first____;\
118 ____root____->next_node_ptr = ____tmp_mem____;\
119 ++____chain____->num_mem;\
123 /*!Erases the memory portion after the portion pointed by BEFORE_IT from the memory chain*/
124 /*!Precondition: BEFORE_IT must be a valid iterator of the memory chain and it can't be the end iterator*/
125 #define BOOST_CONTAINER_MEMCHAIN_ERASE_AFTER(PMEMCHAIN, BEFORE_IT)\
127 boost_cont_memchain *____chain____ = (PMEMCHAIN);\
128 boost_cont_memchain_node *____prev_node____ = (BEFORE_IT).node_ptr;\
129 boost_cont_memchain_node *____erase_node____ = ____prev_node____->next_node_ptr;\
130 if(____chain____->last_node_ptr == ____erase_node____){\
131 ____chain____->last_node_ptr = &____chain____->root_node;\
133 ____prev_node____->next_node_ptr = ____erase_node____->next_node_ptr;\
134 --____chain____->num_mem;\
138 /*!Erases the first portion from the memory chain.
139 Precondition: the memory chain must not be empty*/
140 #define BOOST_CONTAINER_MEMCHAIN_POP_FRONT(PMEMCHAIN)\
142 boost_cont_memchain *____chain____ = (PMEMCHAIN);\
143 boost_cont_memchain_node *____prev_node____ = &____chain____->root_node;\
144 boost_cont_memchain_node *____erase_node____ = ____prev_node____->next_node_ptr;\
145 if(____chain____->last_node_ptr == ____erase_node____){\
146 ____chain____->last_node_ptr = &____chain____->root_node;\
148 ____prev_node____->next_node_ptr = ____erase_node____->next_node_ptr;\
149 --____chain____->num_mem;\
153 /*!Joins two memory chains inserting the portions of the second chain at the back of the first chain*/
155 #define BOOST_CONTAINER_MEMCHAIN_SPLICE_BACK(PMEMCHAIN, PMEMCHAIN2)\
157 boost_cont_memchain *____chain____ = (PMEMCHAIN);\
158 boost_cont_memchain *____chain2____ = (PMEMCHAIN2);\
159 if(!____chain2____->root_node.next_node_ptr){\
162 else if(!____chain____->first_mem){\
163 ____chain____->first_mem = ____chain2____->first_mem;\
164 ____chain____->last_node_ptr = ____chain2____->last_node_ptr;\
165 ____chain____->num_mem = ____chain2____->num_mem;\
166 BOOST_CONTAINER_MEMCHAIN_INIT(*____chain2____);\
169 ____chain____->last_node_ptr->next_node_ptr = ____chain2____->first_mem;\
170 ____chain____->last_node_ptr = ____chain2____->last_node_ptr;\
171 ____chain____->num_mem += ____chain2____->num_mem;\
176 /*!Joins two memory chains inserting the portions of the second chain at the back of the first chain*/
177 #define BOOST_CONTAINER_MEMCHAIN_INCORPORATE_AFTER(PMEMCHAIN, BEFORE_IT, FIRST, BEFORELAST, NUM)\
179 boost_cont_memchain *____chain____ = (PMEMCHAIN);\
180 boost_cont_memchain_node *____pnode____ = (BEFORE_IT).node_ptr;\
181 boost_cont_memchain_node *____next____ = ____pnode____->next_node_ptr;\
182 boost_cont_memchain_node *____first____ = (boost_cont_memchain_node *)(FIRST);\
183 boost_cont_memchain_node *____blast____ = (boost_cont_memchain_node *)(BEFORELAST);\
184 size_t ____num____ = (NUM);\
188 if(____pnode____ == ____chain____->last_node_ptr){\
189 ____chain____->last_node_ptr = ____blast____;\
191 ____pnode____->next_node_ptr = ____first____;\
192 ____blast____->next_node_ptr = ____next____;\
193 ____chain____->num_mem += ____num____;\
197 /*!Indicates the all elements allocated by boost_cont_multialloc_nodes or boost_cont_multialloc_arrays
198 must be contiguous.*/
199 #define DL_MULTIALLOC_ALL_CONTIGUOUS ((size_t)(-1))
201 /*!Indicates the number of contiguous elements allocated by boost_cont_multialloc_nodes or boost_cont_multialloc_arrays
202 should be selected by those functions.*/
203 #define DL_MULTIALLOC_DEFAULT_CONTIGUOUS ((size_t)(0))
205 typedef struct boost_cont_malloc_stats_impl
207 size_t max_system_bytes
;
210 } boost_cont_malloc_stats_t
;
212 typedef unsigned int allocation_type
;
216 // constants for allocation commands
217 BOOST_CONTAINER_ALLOCATE_NEW
= 0X01,
218 BOOST_CONTAINER_EXPAND_FWD
= 0X02,
219 BOOST_CONTAINER_EXPAND_BWD
= 0X04,
220 BOOST_CONTAINER_SHRINK_IN_PLACE
= 0X08,
221 BOOST_CONTAINER_NOTHROW_ALLOCATION
= 0X10,
222 // BOOST_CONTAINER_ZERO_MEMORY = 0X20,
223 BOOST_CONTAINER_TRY_SHRINK_IN_PLACE
= 0X40,
224 BOOST_CONTAINER_EXPAND_BOTH
= BOOST_CONTAINER_EXPAND_FWD
| BOOST_CONTAINER_EXPAND_BWD
,
225 BOOST_CONTAINER_EXPAND_OR_NEW
= BOOST_CONTAINER_ALLOCATE_NEW
| BOOST_CONTAINER_EXPAND_BOTH
228 //#define BOOST_CONTAINERDLMALLOC__FOOTERS
229 #ifndef BOOST_CONTAINERDLMALLOC__FOOTERS
230 enum { BOOST_CONTAINER_ALLOCATION_PAYLOAD
= sizeof(size_t) };
232 enum { BOOST_CONTAINER_ALLOCATION_PAYLOAD
= sizeof(size_t)*2 };
235 typedef struct boost_cont_command_ret_impl
239 }boost_cont_command_ret_t
;
241 size_t boost_cont_size(const void *p
);
243 void* boost_cont_malloc(size_t bytes
);
245 void boost_cont_free(void* mem
);
247 void* boost_cont_memalign(size_t bytes
, size_t alignment
);
249 int boost_cont_multialloc_nodes
250 (size_t n_elements
, size_t elem_size
, size_t contiguous_elements
, boost_cont_memchain
*pchain
);
252 int boost_cont_multialloc_arrays
253 (size_t n_elements
, const size_t *sizes
, size_t sizeof_element
, size_t contiguous_elements
, boost_cont_memchain
*pchain
);
255 void boost_cont_multidealloc(boost_cont_memchain
*pchain
);
257 size_t boost_cont_footprint();
259 size_t boost_cont_allocated_memory();
261 size_t boost_cont_chunksize(const void *p
);
263 int boost_cont_all_deallocated();
265 boost_cont_malloc_stats_t
boost_cont_malloc_stats();
267 size_t boost_cont_in_use_memory();
269 int boost_cont_trim(size_t pad
);
271 int boost_cont_mallopt(int parameter_number
, int parameter_value
);
274 (void* oldmem
, size_t minbytes
, size_t maxbytes
, size_t *received
);
276 int boost_cont_shrink
277 (void* oldmem
, size_t minbytes
, size_t maxbytes
, size_t *received
, int do_commit
);
279 void* boost_cont_alloc
280 (size_t minbytes
, size_t preferred_bytes
, size_t *received_bytes
);
282 int boost_cont_malloc_check();
284 boost_cont_command_ret_t boost_cont_allocation_command
285 ( allocation_type command
286 , size_t sizeof_object
287 , size_t limit_objects
288 , size_t preferred_objects
289 , size_t *received_objects
293 void *boost_cont_sync_create();
295 void boost_cont_sync_destroy(void *sync
);
297 int boost_cont_sync_lock(void *sync
);
299 void boost_cont_sync_unlock(void *sync
);
301 int boost_cont_global_sync_lock();
303 void boost_cont_global_sync_unlock();
310 #pragma warning (pop)
314 #endif //#define BOOST_CONTAINERDLMALLOC__EXT_H