Scippy

SCIP

Solving Constraint Integer Programs

memory.h
Go to the documentation of this file.
1 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2 /* */
3 /* This file is part of the library */
4 /* BMS --- Block Memory Shell */
5 /* */
6 /* Copyright (C) 2002-2014 Konrad-Zuse-Zentrum */
7 /* fuer Informationstechnik Berlin */
8 /* */
9 /* BMS is distributed under the terms of the ZIB Academic License. */
10 /* */
11 /* You should have received a copy of the ZIB Academic License */
12 /* along with BMS; see the file COPYING. If not email to achterberg@zib.de. */
13 /* */
14 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
15 
16 /**@file memory.h
17  * @brief memory allocation routines
18  * @author Tobias Achterberg
19  */
20 
21 /*---+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+----9----+----0----+----1----+----2*/
22 
23 #ifndef __BMS_MEMORY_H__
24 #define __BMS_MEMORY_H__
25 
26 #include <limits.h>
27 #include <stdlib.h>
28 
29 /* special thanks to Daniel Junglas for following template and macros */
30 #ifdef __cplusplus
31 
32 template<typename T> T* docast(T*, void *v) { return reinterpret_cast<T*>(v); }
33 
34 extern "C" {
35 
36 #define ASSIGN(pointerstarstar, voidstarfunction) (*(pointerstarstar) = docast(*(pointerstarstar), (voidstarfunction)))
37 
38 #else
39 
40 #define ASSIGN(pointerstarstar, voidstarfunction) (*(pointerstarstar) = (voidstarfunction))
41 
42 #endif
43 
44 /*
45  * Define the macro EXTERN depending if the OS is Windows or not
46  */
47 #ifndef EXTERN
48 
49 #if defined(_WIN32) || defined(_WIN64)
50 #define EXTERN __declspec(dllexport)
51 #else
52 #define EXTERN extern
53 #endif
54 
55 #endif
56 
57 
58 
59 /*************************************************************************************
60  * Standard Memory Management
61  *
62  * In debug mode, these methods extend malloc() and free() by logging all currently
63  * allocated memory elements in an allocation list. This can be used as a simple leak
64  * detection.
65  *************************************************************************************/
66 
67 /* Check for integer overflow in allocation size */
68 #ifdef NDEBUG
69 #define BMSallocMemoryArray(ptr,num) ASSIGN((ptr), BMSallocMemory_call( (num)*sizeof(**(ptr)), \
70  __FILE__, __LINE__ ))
71 #define BMSreallocMemoryArray(ptr,num) ASSIGN((ptr), BMSreallocMemory_call( *(ptr), (num)*sizeof(**(ptr)), \
72  __FILE__, __LINE__ ))
73 #define BMSallocMemoryArrayCPP(num,size) BMSallocMemory_call( (size_t)((num)*(size)), __FILE__, __LINE__ )
74 #define BMSallocClearMemoryArray(ptr,num) ASSIGN((ptr), BMSallocClearMemory_call((size_t)(num), sizeof(**(ptr)), __FILE__, __LINE__ ))
75 #else
76 #define BMSallocMemoryArray(ptr,num) ( ( ((size_t)(num)) > (UINT_MAX / sizeof(**(ptr))) ) \
77  ? ( ASSIGN((ptr), NULL) ) \
78  : ( ASSIGN((ptr), BMSallocMemory_call( (num)*sizeof(**(ptr)), \
79  __FILE__, __LINE__ )) ) )
80 #define BMSreallocMemoryArray(ptr,num) ( ( ((size_t)(num)) > (UINT_MAX / sizeof(**(ptr))) ) \
81  ? ( ASSIGN((ptr), NULL) ) \
82  : ( ASSIGN((ptr), BMSreallocMemory_call( *(ptr), (num)*sizeof(**(ptr)), \
83  __FILE__, __LINE__ )) ) )
84 #define BMSallocMemoryArrayCPP(num,size) ( ( ((size_t)(num)) > (UINT_MAX / size) ) \
85  ? ( ASSIGN((ptr), NULL) ) \
86  :( BMSallocMemory_call( (size_t)((num)*(size)), __FILE__, __LINE__ ) ) )
87 #define BMSallocClearMemoryArray(ptr,num) ( ( ((size_t)(num)) > (UINT_MAX / sizeof(**(ptr))) ) \
88  ? ( ASSIGN((ptr), NULL) ) \
89  : ( ASSIGN((ptr), BMSallocClearMemory_call((size_t)(num), sizeof(**(ptr)), __FILE__, __LINE__ )) ) )
90 #endif
91 
92 #define BMSallocMemory(ptr) ASSIGN((ptr), BMSallocMemory_call( sizeof(**(ptr)), __FILE__, __LINE__ ))
93 #define BMSallocMemorySize(ptr,size) ASSIGN((ptr), BMSallocMemory_call( (size_t)(size), __FILE__, __LINE__ ))
94 #define BMSallocMemoryCPP(size) BMSallocMemory_call( (size_t)(size), __FILE__, __LINE__ )
95 #define BMSreallocMemorySize(ptr,size) ASSIGN((ptr), BMSreallocMemory_call( *(ptr), (size_t)(size), \
96  __FILE__, __LINE__ ))
97 #define BMSclearMemory(ptr) BMSclearMemory_call( (void*)(ptr), sizeof(*(ptr)) )
98 #define BMSclearMemoryArray(ptr, num) BMSclearMemory_call( (void*)(ptr), (num)*sizeof(*(ptr)) )
99 #define BMSclearMemorySize(ptr, size) BMSclearMemory_call( (void*)(ptr), (size_t)(size) )
100 
101 #define BMScopyMemory(ptr, source) BMScopyMemory_call( (void*)(ptr), (const void*)(source), sizeof(*(ptr)) )
102 #define BMScopyMemoryArray(ptr, source, num) BMScopyMemory_call( (void*)(ptr), (const void*)(source), (num)*sizeof(*(ptr)) )
103 #define BMScopyMemorySize(ptr, source, size) BMScopyMemory_call( (void*)(ptr), (const void*)(source), (size_t)(size) )
104 
105 #define BMSmoveMemory(ptr, source) BMSmoveMemory_call( (void*)(ptr), (const void*)(source), sizeof(*(ptr)) )
106 #define BMSmoveMemoryArray(ptr, source, num) BMSmoveMemory_call( (void*)(ptr), (const void*)(source), (num) * sizeof(*(ptr)) )
107 #define BMSmoveMemorySize(ptr, source, size) BMSmoveMemory_call( (void*)(ptr), (const void*)(source), (size_t)(size) )
108 
109 #define BMSduplicateMemory(ptr, source) ASSIGN((ptr), BMSduplicateMemory_call( (const void*)(source), \
110  sizeof(**(ptr)), __FILE__, __LINE__ ))
111 #define BMSduplicateMemoryArray(ptr, source, num) \
112  ASSIGN((ptr), BMSduplicateMemory_call( (const void*)(source), \
113  (num)*sizeof(**(ptr)), __FILE__, __LINE__ ))
114 #define BMSduplicateMemorySize(ptr, source, size) \
115  ASSIGN((ptr), BMSduplicateMemory_call( (const void*)(source), \
116  (size_t)(size), __FILE__, __LINE__ ))
117 #define BMSfreeMemory(ptr) { BMSfreeMemory_call( (void*)(*(ptr)), __FILE__, __LINE__ ); \
118  *(ptr) = NULL; }
119 #define BMSfreeMemoryNull(ptr) { if( *(ptr) != NULL ) BMSfreeMemory( (ptr) ); }
120 #define BMSfreeMemoryArray(ptr) { BMSfreeMemory_call( (void*)(*(ptr)), __FILE__, __LINE__ ); \
121  *(ptr) = NULL; }
122 #define BMSfreeMemoryArrayNull(ptr) { if( *(ptr) != NULL ) BMSfreeMemoryArray( (ptr) ); }
123 #define BMSfreeMemorySize(ptr) { BMSfreeMemory_call( (void*)(*(ptr)), __FILE__, __LINE__ ); \
124  *(ptr) = NULL; }
125 #define BMSfreeMemorySizeNull(ptr) { if( *(ptr) != NULL ) BMSfreeMemorySize( (ptr) ); }
126 
127 #ifndef NDEBUG
128 #define BMSgetPointerSize(ptr) BMSgetPointerSize_call(ptr)
129 #define BMSdisplayMemory() BMSdisplayMemory_call()
130 #define BMScheckEmptyMemory() BMScheckEmptyMemory_call()
131 #define BMSgetMemoryUsed() BMSgetMemoryUsed_call()
132 #else
133 #define BMSgetPointerSize(ptr) 0
134 #define BMSdisplayMemory() /**/
135 #define BMScheckEmptyMemory() /**/
136 #define BMSgetMemoryUsed() 0LL
137 #endif
138 
139 /** allocates memory and initializes it with 0; returns NULL if memory allocation failed */
140 extern
142  size_t num, /**< number of memory element to allocate */
143  size_t size, /**< size of memory element to allocate */
144  const char* filename, /**< source file where the allocation is performed */
145  int line /**< line number in source file where the allocation is performed */
146  );
147 
148 /** allocates memory; returns NULL if memory allocation failed */
149 extern
150 void* BMSallocMemory_call(
151  size_t size, /**< size of memory element to allocate */
152  const char* filename, /**< source file where the allocation is performed */
153  int line /**< line number in source file where the allocation is performed */
154  );
155 
156 /** allocates memory; returns NULL if memory allocation failed */
157 extern
159  void* ptr, /**< pointer to memory to reallocate */
160  size_t size, /**< new size of memory element */
161  const char* filename, /**< source file where the reallocation is performed */
162  int line /**< line number in source file where the reallocation is performed */
163  );
164 
165 /** clears a memory element (i.e. fills it with zeros) */
166 extern
168  void* ptr, /**< pointer to memory element */
169  size_t size /**< size of memory element */
170  );
171 
172 /** copies the contents of one memory element into another memory element */
173 extern
174 void BMScopyMemory_call(
175  void* ptr, /**< pointer to target memory element */
176  const void* source, /**< pointer to source memory element */
177  size_t size /**< size of memory element to copy */
178  );
179 
180 /** moves the contents of one memory element into another memory element, should be used if both elements overlap,
181  * otherwise BMScopyMemory is faster
182  */
183 extern
184 void BMSmoveMemory_call(
185  void* ptr, /**< pointer to target memory element */
186  const void* source, /**< pointer to source memory element */
187  size_t size /**< size of memory element to copy */
188  );
189 
190 /** allocates memory and copies the contents of the given memory element into the new memory element */
191 extern
193  const void* source, /**< pointer to source memory element */
194  size_t size, /**< size of memory element to copy */
195  const char* filename, /**< source file where the duplication is performed */
196  int line /**< line number in source file where the duplication is performed */
197  );
198 
199 /** frees an allocated memory element */
200 extern
201 void BMSfreeMemory_call(
202  void* ptr, /**< pointer to memory element */
203  const char* filename, /**< source file where the deallocation is performed */
204  int line /**< line number in source file where the deallocation is performed */
205  );
206 
207 /** returns the size of an allocated memory element */
208 extern
210  const void* ptr /**< pointer to allocated memory */
211  );
212 
213 /** outputs information about currently allocated memory to the screen */
214 extern
216  void
217  );
218 
219 /** displays a warning message on the screen, if allocated memory exists */
220 extern
222  void
223  );
224 
225 /** returns total number of allocated bytes */
226 extern
227 long long BMSgetMemoryUsed_call(
228  void
229  );
230 
231 
232 
233 
234 /********************************************************************
235  * Chunk Memory Management
236  *
237  * Efficient memory management for multiple objects of the same size
238  ********************************************************************/
239 
240 typedef struct BMS_ChkMem BMS_CHKMEM; /**< collection of memory chunks of the same element size */
241 
242 
243 #ifndef BMS_NOBLOCKMEM
244 
245 #define BMScreateChunkMemory(sz,isz,gbf) BMScreateChunkMemory_call( (sz), (isz), (gbf), __FILE__, __LINE__ )
246 #define BMSclearChunkMemory(mem) BMSclearChunkMemory_call( (mem), __FILE__, __LINE__ )
247 #define BMSclearChunkMemoryNull(mem) { if( (mem) != NULL ) BMSclearChunkMemory( (mem) ); }
248 #define BMSdestroyChunkMemory(mem) BMSdestroyChunkMemory_call( (mem), __FILE__, __LINE__ )
249 #define BMSdestroyChunkMemoryNull(mem) { if( *(mem) != NULL ) BMSdestroyChunkMemory( (mem) ); }
250 
251 #define BMSallocChunkMemory(mem,ptr) ASSIGN((ptr), BMSallocChunkMemory_call((mem), sizeof(**(ptr)), \
252  __FILE__, __LINE__))
253 #define BMSduplicateChunkMemory(mem, ptr, source) \
254  ASSIGN((ptr), BMSduplicateChunkMemory_call((mem), (const void*)(source), \
255  sizeof(**(ptr)), __FILE__, __LINE__ ))
256 #define BMSfreeChunkMemory(mem,ptr) { BMSfreeChunkMemory_call( (mem), (void*)(*(ptr)), sizeof(**(ptr)), \
257  __FILE__, __LINE__ ); \
258  *(ptr) = NULL; }
259 #define BMSfreeChunkMemoryNull(mem,ptr) { if( *(ptr) != NULL ) BMSfreeChunkMemory( (mem), (ptr) ); }
260 #define BMSgarbagecollectChunkMemory(mem) BMSgarbagecollectChunkMemory_call(mem)
261 #define BMSgetChunkMemoryUsed(mem) BMSgetChunkMemoryUsed_call(mem)
262 
263 #else
264 
265 /* block memory management mapped to standard memory management */
266 
267 #define BMScreateChunkMemory(sz,isz,gbf) (void*)(0x01) /* dummy to not return a NULL pointer */
268 #define BMSclearChunkMemory(mem) /**/
269 #define BMSclearChunkMemoryNull(mem) /**/
270 #define BMSdestroyChunkMemory(mem) /**/
271 #define BMSdestroyChunkMemoryNull(mem) /**/
272 #define BMSallocChunkMemory(mem,ptr) BMSallocMemory(ptr)
273 #define BMSduplicateChunkMemory(mem, ptr, source) BMSduplicateMemory(ptr,source)
274 #define BMSfreeChunkMemory(mem,ptr) BMSfreeMemory(ptr)
275 #define BMSfreeChunkMemoryNull(mem,ptr) BMSfreeMemoryNull(ptr)
276 #define BMSgarbagecollectChunkMemory(mem) /**/
277 #define BMSgetChunkMemoryUsed(mem) 0LL
278 
279 #endif
280 
281 
282 /** aligns the given byte size corresponding to the minimal alignment for chunk and block memory */
283 extern
284 void BMSalignMemsize(
285  size_t* size /**< pointer to the size to align */
286  );
287 
288 /** checks whether the given size meets the alignment conditions for chunk and block memory */
289 extern
290 int BMSisAligned(
291  size_t size /**< size to check for alignment */
292  );
293 
294 /** creates a new chunk block data structure */
295 extern
297  size_t size, /**< element size of the chunk block */
298  int initchunksize, /**< number of elements in the first chunk of the chunk block */
299  int garbagefactor, /**< garbage collector is called, if at least garbagefactor * avg. chunksize
300  * elements are free (-1: disable garbage collection) */
301  const char* filename, /**< source file of the function call */
302  int line /**< line number in source file of the function call */
303  );
304 
305 /** clears a chunk block data structure */
306 extern
308  BMS_CHKMEM* chkmem, /**< chunk block */
309  const char* filename, /**< source file of the function call */
310  int line /**< line number in source file of the function call */
311  );
312 
313 /** destroys and frees a chunk block data structure */
314 extern
316  BMS_CHKMEM** chkmem, /**< pointer to chunk block */
317  const char* filename, /**< source file of the function call */
318  int line /**< line number in source file of the function call */
319  );
320 
321 /** allocates a memory element of the given chunk block */
322 extern
324  BMS_CHKMEM* chkmem, /**< chunk block */
325  size_t size, /**< size of memory element to allocate (only needed for sanity check) */
326  const char* filename, /**< source file of the function call */
327  int line /**< line number in source file of the function call */
328  );
329 
330 /** duplicates a given memory element by allocating a new element of the same chunk block and copying the data */
331 extern
333  BMS_CHKMEM* chkmem, /**< chunk block */
334  const void* source, /**< source memory element */
335  size_t size, /**< size of memory element to allocate (only needed for sanity check) */
336  const char* filename, /**< source file of the function call */
337  int line /**< line number in source file of the function call */
338  );
339 
340 /** frees a memory element of the given chunk block */
341 extern
343  BMS_CHKMEM* chkmem, /**< chunk block */
344  void* ptr, /**< memory element to free */
345  size_t size, /**< size of memory element to allocate (only needed for sanity check) */
346  const char* filename, /**< source file of the function call */
347  int line /**< line number in source file of the function call */
348  );
349 
350 /** calls garbage collection of chunk block and frees chunks without allocated memory elements */
351 extern
353  BMS_CHKMEM* chkmem /**< chunk block */
354  );
355 
356 /** returns the number of allocated bytes in the chunk block */
357 extern
359  const BMS_CHKMEM* chkmem /**< chunk block */
360  );
361 
362 
363 
364 
365 /***********************************************************
366  * Block Memory Management
367  *
368  * Efficient memory management for objects of varying sizes
369  ***********************************************************/
370 
371 typedef struct BMS_BlkMem BMS_BLKMEM; /**< block memory: collection of chunk blocks */
372 
373 
374 #ifndef BMS_NOBLOCKMEM
375 
376 /* block memory methods for faster memory access */
377 
378 /* Check for integer overflow in allocation size */
379 #ifdef NDEBUG
380 #define BMSallocBlockMemoryArray(mem,ptr,num) ASSIGN((ptr), BMSallocBlockMemory_call((mem), (num)*sizeof(**(ptr)), \
381  __FILE__, __LINE__))
382 #define BMSreallocBlockMemoryArray(mem,ptr,oldnum,newnum) \
383  ASSIGN((ptr), BMSreallocBlockMemory_call((mem), (void*)(*(ptr)), \
384  (oldnum)*sizeof(**(ptr)), (newnum)*sizeof(**(ptr)), __FILE__, __LINE__))
385 #else
386 #define BMSallocBlockMemoryArray(mem,ptr,num) ( ( ((size_t)(num)) > UINT_MAX / sizeof(**(ptr)) ) \
387  ? ( ASSIGN((ptr), NULL) ) \
388  : ( ASSIGN((ptr), BMSallocBlockMemory_call((mem), (num)*sizeof(**(ptr)),\
389  __FILE__, __LINE__)) ) )
390 #define BMSreallocBlockMemoryArray(mem,ptr,oldnum,newnum) \
391  ( ( ((size_t)(newnum)) > UINT_MAX / sizeof(**(ptr)) ) \
392  ? ( ASSIGN((ptr), NULL) ) \
393  : ( ASSIGN((ptr), BMSreallocBlockMemory_call((mem), (void*)(*(ptr)), \
394  (oldnum)*sizeof(**(ptr)), (newnum)*sizeof(**(ptr)), __FILE__, \
395  __LINE__)) ) )
396 #endif
397 
398 #define BMScreateBlockMemory(csz,gbf) BMScreateBlockMemory_call( (csz), (gbf), __FILE__, __LINE__ )
399 #define BMSclearBlockMemory(mem) BMSclearBlockMemory_call( (mem), __FILE__, __LINE__ )
400 #define BMSclearBlockMemoryNull(mem) { if( (mem) != NULL ) BMSclearBlockMemory( (mem) ); }
401 #define BMSdestroyBlockMemory(mem) BMSdestroyBlockMemory_call( (mem), __FILE__, __LINE__ )
402 #define BMSdestroyBlockMemoryNull(mem) { if( *(mem) != NULL ) BMSdestroyBlockMemory( (mem) ); }
403 
404 #define BMSallocBlockMemory(mem,ptr) ASSIGN((ptr), BMSallocBlockMemory_call((mem), sizeof(**(ptr)), \
405  __FILE__, __LINE__))
406 #define BMSallocBlockMemorySize(mem,ptr,size) ASSIGN((ptr), BMSallocBlockMemory_call((mem), (size_t)(size), \
407  __FILE__, __LINE__))
408 #define BMSreallocBlockMemorySize(mem,ptr,oldsize,newsize) \
409  ASSIGN((ptr), BMSreallocBlockMemory_call((mem), (void*)(*(ptr)), \
410  (size_t)(oldsize), (size_t)(newsize), __FILE__, __LINE__))
411 #define BMSduplicateBlockMemory(mem, ptr, source) \
412  ASSIGN((ptr), BMSduplicateBlockMemory_call((mem), (const void*)(source), \
413  sizeof(**(ptr)), __FILE__, __LINE__ ))
414 #define BMSduplicateBlockMemoryArray(mem, ptr, source, num) \
415  ASSIGN((ptr), BMSduplicateBlockMemory_call( (mem), (const void*)(source), \
416  (num)*sizeof(**(ptr)), __FILE__, __LINE__ ))
417 #define BMSfreeBlockMemory(mem,ptr) { BMSfreeBlockMemory_call( (mem), (void*)(*(ptr)), sizeof(**(ptr)), \
418  __FILE__, __LINE__ ); \
419  *(ptr) = NULL; }
420 #define BMSfreeBlockMemoryNull(mem,ptr) { if( *(ptr) != NULL ) BMSfreeBlockMemory( (mem), (ptr) ); }
421 #define BMSfreeBlockMemoryArray(mem,ptr,num) { BMSfreeBlockMemory_call( (mem), (void*)(*(ptr)), (num)*sizeof(**(ptr)), \
422  __FILE__, __LINE__ ); \
423  *(ptr) = NULL; }
424 #define BMSfreeBlockMemoryArrayNull(mem,ptr,num) { if( *(ptr) != NULL ) BMSfreeBlockMemoryArray( (mem), (ptr), (num) ); }
425 #define BMSfreeBlockMemorySize(mem,ptr,size) { BMSfreeBlockMemory_call( (mem), (void*)(*(ptr)), (size_t)(size), \
426  __FILE__, __LINE__ ); \
427  *(ptr) = NULL; }
428 #define BMSfreeBlockMemorySizeNull(mem,ptr,size) { if( *(ptr) != NULL ) BMSfreeBlockMemorySize( (mem), (ptr), (size) ); }
429 #define BMSgarbagecollectBlockMemory(mem) BMSgarbagecollectBlockMemory_call(mem)
430 #define BMSgetBlockMemoryUsed(mem) BMSgetBlockMemoryUsed_call(mem)
431 #define BMSgetBlockPointerSize(mem,ptr) BMSgetBlockPointerSize_call((mem), (ptr))
432 #define BMSdisplayBlockMemory(mem) BMSdisplayBlockMemory_call(mem)
433 #define BMSblockMemoryCheckEmpty(mem) BMScheckEmptyBlockMemory_call(mem)
434 
435 #else
436 
437 /* block memory management mapped to standard memory management */
438 
439 #define BMScreateBlockMemory(csz,gbf) (void*)(0x01) /* dummy to not return a NULL pointer */
440 #define BMSclearBlockMemory(mem) /**/
441 #define BMSclearBlockMemoryNull(mem) /**/
442 #define BMSdestroyBlockMemory(mem) /**/
443 #define BMSdestroyBlockMemoryNull(mem) /**/
444 #define BMSallocBlockMemory(mem,ptr) BMSallocMemory(ptr)
445 #define BMSallocBlockMemoryArray(mem,ptr,num) BMSallocMemoryArray(ptr,num)
446 #define BMSallocBlockMemorySize(mem,ptr,size) BMSallocMemorySize(ptr,size)
447 #define BMSreallocBlockMemoryArray(mem,ptr,oldnum,newnum) BMSreallocMemoryArray(ptr,newnum)
448 #define BMSreallocBlockMemorySize(mem,ptr,oldsize,newsize) BMSreallocMemorySize(ptr,newsize)
449 #define BMSduplicateBlockMemory(mem, ptr, source) BMSduplicateMemory(ptr,source)
450 #define BMSduplicateBlockMemoryArray(mem, ptr, source, num) BMSduplicateMemoryArray(ptr,source,num)
451 #define BMSfreeBlockMemory(mem,ptr) BMSfreeMemory(ptr)
452 #define BMSfreeBlockMemoryNull(mem,ptr) BMSfreeMemoryNull(ptr)
453 #define BMSfreeBlockMemoryArray(mem,ptr,num) BMSfreeMemoryArray(ptr)
454 #define BMSfreeBlockMemoryArrayNull(mem,ptr,num) BMSfreeMemoryArrayNull(ptr)
455 #define BMSfreeBlockMemorySize(mem,ptr,size) BMSfreeMemory(ptr)
456 #define BMSfreeBlockMemorySizeNull(mem,ptr,size) BMSfreeMemoryNull(ptr)
457 #define BMSgarbagecollectBlockMemory(mem) /**/
458 #define BMSgetBlockMemoryUsed(mem) 0LL
459 #define BMSgetBlockPointerSize(mem,ptr) 0
460 #define BMSdisplayBlockMemory(mem) /**/
461 #define BMSblockMemoryCheckEmpty(mem) /**/
462 
463 #endif
464 
465 
466 /** creates a block memory allocation data structure */
467 extern
469  int initchunksize, /**< number of elements in the first chunk of each chunk block */
470  int garbagefactor, /**< garbage collector is called, if at least garbagefactor * avg. chunksize
471  * elements are free (-1: disable garbage collection) */
472  const char* filename, /**< source file of the function call */
473  int line /**< line number in source file of the function call */
474  );
475 
476 /** frees all chunk blocks in the block memory */
477 extern
479  BMS_BLKMEM* blkmem, /**< block memory */
480  const char* filename, /**< source file of the function call */
481  int line /**< line number in source file of the function call */
482  );
483 
484 /** clears and deletes block memory */
485 extern
487  BMS_BLKMEM** blkmem, /**< pointer to block memory */
488  const char* filename, /**< source file of the function call */
489  int line /**< line number in source file of the function call */
490  );
491 
492 /** allocates memory in the block memory pool */
494  BMS_BLKMEM* blkmem, /**< block memory */
495  size_t size, /**< size of memory element to allocate */
496  const char* filename, /**< source file of the function call */
497  int line /**< line number in source file of the function call */
498  );
499 
500 /** resizes memory element in the block memory pool, and copies the data */
501 extern
503  BMS_BLKMEM* blkmem, /**< block memory */
504  void* ptr, /**< memory element to reallocated */
505  size_t oldsize, /**< old size of memory element */
506  size_t newsize, /**< new size of memory element */
507  const char* filename, /**< source file of the function call */
508  int line /**< line number in source file of the function call */
509  );
510 
511 /** duplicates memory element in the block memory pool, and copies the data */
512 extern
514  BMS_BLKMEM* blkmem, /**< block memory */
515  const void* source, /**< memory element to duplicate */
516  size_t size, /**< size of memory elements */
517  const char* filename, /**< source file of the function call */
518  int line /**< line number in source file of the function call */
519  );
520 
521 /** frees memory element in the block memory pool */
522 extern
524  BMS_BLKMEM* blkmem, /**< block memory */
525  void* ptr, /**< memory element to free */
526  size_t size, /**< size of memory element */
527  const char* filename, /**< source file of the function call */
528  int line /**< line number in source file of the function call */
529  );
530 
531 /** calls garbage collection of block memory, frees chunks without allocated memory elements, and frees
532  * chunk blocks without any chunks
533  */
534 extern
536  BMS_BLKMEM* blkmem /**< block memory */
537  );
538 
539 /** returns the number of allocated bytes in the block memory */
540 extern
542  const BMS_BLKMEM* blkmem /**< block memory */
543  );
544 
545 /** returns the size of the given memory element; returns 0, if the element is not member of the block memory */
546 extern
548  const BMS_BLKMEM* blkmem, /**< block memory */
549  const void* ptr /**< memory element */
550  );
551 
552 /** outputs allocation diagnostics of block memory */
553 extern
555  const BMS_BLKMEM* blkmem /**< block memory */
556  );
557 
558 /** outputs warning messages, if there are allocated elements in the block memory */
559 extern
561  const BMS_BLKMEM* blkmem /**< block memory */
562  );
563 
564 #ifdef __cplusplus
565 }
566 #endif
567 
568 #endif
569