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-2016 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 scip@zib.de. */
13 /* */
14 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
15 
16 /**@file memory.h
17  * @brief memory allocation routines
18  * @author Tobias Achterberg
19  * @author Gerald Gamrath
20  * @author Marc Pfetsch
21  */
22 
23 /*---+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+----9----+----0----+----1----+----2*/
24 
25 #ifndef __BMS_MEMORY_H__
26 #define __BMS_MEMORY_H__
27 
28 #include <limits.h>
29 #include <stdlib.h>
30 #include <stddef.h>
31 
32 /* special thanks to Daniel Junglas for following template and macros */
33 #ifdef __cplusplus
34 
35 template<typename T> T* docast(T*, void *v) { return reinterpret_cast<T*>(v); }
36 
37 extern "C" {
38 
39 #define ASSIGN(pointerstarstar, voidstarfunction) (*(pointerstarstar) = docast(*(pointerstarstar), (voidstarfunction)))
40 
41 #else
42 
43 #define ASSIGN(pointerstarstar, voidstarfunction) (*(pointerstarstar) = (voidstarfunction))
44 
45 #endif
46 
47 /*
48  * Define the macro EXTERN depending if the OS is Windows or not
49  */
50 #ifndef EXTERN
51 
52 #if defined(_WIN32) || defined(_WIN64)
53 #define EXTERN __declspec(dllexport)
54 #else
55 #define EXTERN extern
56 #endif
57 
58 #endif
59 
60 
61 
62 /*************************************************************************************
63  * Standard Memory Management
64  *
65  * In debug mode, these methods extend malloc() and free() by logging all currently
66  * allocated memory elements in an allocation list. This can be used as a simple leak
67  * detection.
68  *************************************************************************************/
69 
70 /* Note: values that are passed as a size_t parameter are first converted to ptrdiff_t to be sure that negative numbers
71  * are extended to the larger size. Then they are converted to size_t. Thus, negative numbers are converted to very
72  * large size_t values. This is then checked within the functions. */
73 
74 #define BMSallocMemory(ptr) ASSIGN((ptr), BMSallocMemory_call( sizeof(**(ptr)), __FILE__, __LINE__ ))
75 #define BMSallocMemorySize(ptr,size) ASSIGN((ptr), BMSallocMemory_call( (size_t)(ptrdiff_t)(size), __FILE__, __LINE__ ))
76 #define BMSallocMemoryCPP(size) BMSallocMemory_call( (size_t)(ptrdiff_t)(size), __FILE__, __LINE__ )
77 #define BMSallocClearMemorySize(ptr,size) ASSIGN((ptr), BMSallocClearMemory_call((size_t)(1), (size_t)(ptrdiff_t)(size), __FILE__, __LINE__ ))
78 #define BMSallocMemoryArray(ptr,num) ASSIGN((ptr), BMSallocMemoryArray_call((size_t)(ptrdiff_t)(num), sizeof(**(ptr)), __FILE__, __LINE__ ))
79 #define BMSallocMemoryArrayCPP(num,size) BMSallocMemoryArray_call( (size_t)(ptrdiff_t)(num), (size_t)(ptrdiff_t)(size), __FILE__, __LINE__ )
80 #define BMSallocClearMemoryArray(ptr,num) ASSIGN((ptr), BMSallocClearMemory_call((size_t)(ptrdiff_t)(num), sizeof(**(ptr)), __FILE__, __LINE__ ))
81 #define BMSreallocMemorySize(ptr,size) ASSIGN((ptr), BMSreallocMemory_call((void*)(*(ptr)), (size_t)(ptrdiff_t)(size), __FILE__, __LINE__ ))
82 #define BMSreallocMemoryArray(ptr,num) ASSIGN((ptr), BMSreallocMemoryArray_call( *(ptr), (size_t)(ptrdiff_t)(num), sizeof(**(ptr)), __FILE__, __LINE__ ))
83 
84 #define BMSclearMemory(ptr) BMSclearMemory_call( (void*)(ptr), sizeof(*(ptr)) )
85 #define BMSclearMemoryArray(ptr, num) BMSclearMemory_call( (void*)(ptr), (size_t)(ptrdiff_t)(num)*sizeof(*(ptr)) )
86 #define BMSclearMemorySize(ptr, size) BMSclearMemory_call( (void*)(ptr), (size_t)(ptrdiff_t)(size) )
87 
88 #define BMScopyMemory(ptr, source) BMScopyMemory_call( (void*)(ptr), (const void*)(source), sizeof(*(ptr)) )
89 #define BMScopyMemoryArray(ptr, source, num) BMScopyMemory_call( (void*)(ptr), (const void*)(source), (size_t)(ptrdiff_t)(num)*sizeof(*(ptr)) )
90 #define BMScopyMemorySize(ptr, source, size) BMScopyMemory_call( (void*)(ptr), (const void*)(source), (size_t)(ptrdiff_t)(size) )
91 
92 #define BMSmoveMemory(ptr, source) BMSmoveMemory_call( (void*)(ptr), (const void*)(source), sizeof(*(ptr)) )
93 #define BMSmoveMemoryArray(ptr, source, num) BMSmoveMemory_call( (void*)(ptr), (const void*)(source), (size_t)(ptrdiff_t)(num) * sizeof(*(ptr)) )
94 #define BMSmoveMemorySize(ptr, source, size) BMSmoveMemory_call( (void*)(ptr), (const void*)(source), (size_t)(ptrdiff_t)(size) )
95 
96 #define BMSduplicateMemory(ptr, source) ASSIGN((ptr), BMSduplicateMemory_call( (const void*)(source), sizeof(**(ptr)), __FILE__, __LINE__ ))
97 #define BMSduplicateMemorySize(ptr, source, size) ASSIGN((ptr), BMSduplicateMemory_call( (const void*)(source), (size_t)(ptrdiff_t)(size), __FILE__, __LINE__ ))
98 #define BMSduplicateMemoryArray(ptr, source, num) ASSIGN((ptr), BMSduplicateMemoryArray_call( (const void*)(source), (size_t)(ptrdiff_t)(num), \
99  sizeof(**(ptr)), __FILE__, __LINE__ ))
100 #define BMSfreeMemory(ptr) BMSfreeMemory_call( (void**)(ptr), __FILE__, __LINE__ )
101 #define BMSfreeMemoryNull(ptr) BMSfreeMemoryNull_call( (void**)(ptr), __FILE__, __LINE__ )
102 #define BMSfreeMemoryArray(ptr) BMSfreeMemory_call( (void**)(ptr), __FILE__, __LINE__ )
103 #define BMSfreeMemoryArrayNull(ptr) BMSfreeMemoryNull_call( (void**)(ptr), __FILE__, __LINE__ )
104 #define BMSfreeMemorySize(ptr) BMSfreeMemory_call( (void**)(ptr), __FILE__, __LINE__ )
105 #define BMSfreeMemorySizeNull(ptr) BMSfreeMemoryNull_call( (void**)(ptr), __FILE__, __LINE__ )
106 
107 #ifndef NDEBUG
108 #define BMSgetPointerSize(ptr) BMSgetPointerSize_call(ptr)
109 #define BMSdisplayMemory() BMSdisplayMemory_call()
110 #define BMScheckEmptyMemory() BMScheckEmptyMemory_call()
111 #define BMSgetMemoryUsed() BMSgetMemoryUsed_call()
112 #else
113 #define BMSgetPointerSize(ptr) 0
114 #define BMSdisplayMemory() /**/
115 #define BMScheckEmptyMemory() /**/
116 #define BMSgetMemoryUsed() 0LL
117 #endif
118 
119 /** allocates array and initializes it with 0; returns NULL if memory allocation failed */
120 extern
122  size_t num, /**< number of memory element to allocate */
123  size_t typesize, /**< size of memory element to allocate */
124  const char* filename, /**< source file where the allocation is performed */
125  int line /**< line number in source file where the allocation is performed */
126  );
127 
128 /** allocates memory; returns NULL if memory allocation failed */
129 extern
130 void* BMSallocMemory_call(
131  size_t size, /**< size of memory element to allocate */
132  const char* filename, /**< source file where the allocation is performed */
133  int line /**< line number in source file where the allocation is performed */
134  );
135 
136 /** allocates array; returns NULL if memory allocation failed */
137 extern
139  size_t num, /**< number of components of array to allocate */
140  size_t typesize, /**< size of each component */
141  const char* filename, /**< source file where the allocation is performed */
142  int line /**< line number in source file where the allocation is performed */
143  );
144 
145 /** allocates memory; returns NULL if memory allocation failed */
146 extern
148  void* ptr, /**< pointer to memory to reallocate */
149  size_t size, /**< new size of memory element */
150  const char* filename, /**< source file where the reallocation is performed */
151  int line /**< line number in source file where the reallocation is performed */
152  );
153 
154 /** reallocates array; returns NULL if memory allocation failed */
155 extern
157  void* ptr, /**< pointer to memory to reallocate */
158  size_t num, /**< number of components of array to allocate */
159  size_t typesize, /**< size of each component */
160  const char* filename, /**< source file where the reallocation is performed */
161  int line /**< line number in source file where the reallocation is performed */
162  );
163 
164 /** clears a memory element (i.e. fills it with zeros) */
165 extern
167  void* ptr, /**< pointer to memory element */
168  size_t size /**< size of memory element */
169  );
170 
171 /** copies the contents of one memory element into another memory element */
172 extern
173 void BMScopyMemory_call(
174  void* ptr, /**< pointer to target memory element */
175  const void* source, /**< pointer to source memory element */
176  size_t size /**< size of memory element to copy */
177  );
178 
179 /** moves the contents of one memory element into another memory element, should be used if both elements overlap,
180  * otherwise BMScopyMemory is faster
181  */
182 extern
183 void BMSmoveMemory_call(
184  void* ptr, /**< pointer to target memory element */
185  const void* source, /**< pointer to source memory element */
186  size_t size /**< size of memory element to copy */
187  );
188 
189 /** allocates memory and copies the contents of the given memory element into the new memory element */
190 extern
192  const void* source, /**< pointer to source memory element */
193  size_t size, /**< size of memory element to copy */
194  const char* filename, /**< source file where the duplication is performed */
195  int line /**< line number in source file where the duplication is performed */
196  );
197 
198 /** allocates array and copies the contents of the given source array into the new array */
199 extern
201  const void* source, /**< pointer to source memory element */
202  size_t num, /**< number of components of array to allocate */
203  size_t typesize, /**< size of each component */
204  const char* filename, /**< source file where the duplication is performed */
205  int line /**< line number in source file where the duplication is performed */
206  );
207 
208 /** frees an allocated memory element and sets pointer to NULL */
209 extern
210 void BMSfreeMemory_call(
211  void** ptr, /**< pointer to pointer to memory element */
212  const char* filename, /**< source file where the deallocation is performed */
213  int line /**< line number in source file where the deallocation is performed */
214  );
215 
216 /** frees an allocated memory element if pointer is not NULL and sets pointer to NULL */
217 extern
219  void** ptr, /**< pointer to pointer to memory element */
220  const char* filename, /**< source file where the deallocation is performed */
221  int line /**< line number in source file where the deallocation is performed */
222  );
223 
224 /** returns the size of an allocated memory element */
225 extern
227  const void* ptr /**< pointer to allocated memory */
228  );
229 
230 /** outputs information about currently allocated memory to the screen */
231 extern
233  void
234  );
235 
236 /** displays a warning message on the screen, if allocated memory exists */
237 extern
239  void
240  );
241 
242 /** returns total number of allocated bytes */
243 extern
244 long long BMSgetMemoryUsed_call(
245  void
246  );
247 
248 
249 
250 
251 /********************************************************************
252  * Chunk Memory Management
253  *
254  * Efficient memory management for multiple objects of the same size
255  ********************************************************************/
256 
257 typedef struct BMS_ChkMem BMS_CHKMEM; /**< collection of memory chunks of the same element size */
258 
259 
260 #ifndef BMS_NOBLOCKMEM
261 
262 #define BMScreateChunkMemory(sz,isz,gbf) BMScreateChunkMemory_call( (sz), (isz), (gbf), __FILE__, __LINE__ )
263 #define BMSclearChunkMemory(mem) BMSclearChunkMemory_call( (mem), __FILE__, __LINE__ )
264 #define BMSdestroyChunkMemory(mem) BMSdestroyChunkMemory_call( (mem), __FILE__, __LINE__ )
265 
266 #define BMSallocChunkMemory(mem,ptr) ASSIGN((ptr), BMSallocChunkMemory_call((mem), sizeof(**(ptr)), __FILE__, __LINE__))
267 #define BMSduplicateChunkMemory(mem, ptr, source) ASSIGN((ptr), BMSduplicateChunkMemory_call((mem), (const void*)(source), \
268  sizeof(**(ptr)), __FILE__, __LINE__ ))
269 #define BMSfreeChunkMemory(mem,ptr) BMSfreeChunkMemory_call( (mem), (void**)(ptr), sizeof(**(ptr)), __FILE__, __LINE__ )
270 #define BMSfreeChunkMemoryNull(mem,ptr) BMSfreeChunkMemoryNull_call( (mem), (void**)(ptr) )
271 #define BMSgarbagecollectChunkMemory(mem) BMSgarbagecollectChunkMemory_call(mem)
272 #define BMSgetChunkMemoryUsed(mem) BMSgetChunkMemoryUsed_call(mem)
273 
274 #else
275 
276 /* block memory management mapped to standard memory management */
277 
278 #define BMScreateChunkMemory(sz,isz,gbf) (void*)(0x01) /* dummy to not return a NULL pointer */
279 #define BMSclearChunkMemory(mem) /**/
280 #define BMSclearChunkMemoryNull(mem) /**/
281 #define BMSdestroyChunkMemory(mem) /**/
282 #define BMSdestroyChunkMemoryNull(mem) /**/
283 #define BMSallocChunkMemory(mem,ptr) BMSallocMemory(ptr)
284 #define BMSduplicateChunkMemory(mem, ptr, source) BMSduplicateMemory(ptr,source)
285 #define BMSfreeChunkMemory(mem,ptr) BMSfreeMemory(ptr)
286 #define BMSfreeChunkMemoryNull(mem,ptr) BMSfreeMemoryNull(ptr)
287 #define BMSgarbagecollectChunkMemory(mem) /**/
288 #define BMSgetChunkMemoryUsed(mem) 0LL
289 
290 #endif
291 
292 
293 /** aligns the given byte size corresponding to the minimal alignment for chunk and block memory */
294 extern
295 void BMSalignMemsize(
296  size_t* size /**< pointer to the size to align */
297  );
298 
299 /** checks whether the given size meets the alignment conditions for chunk and block memory */
300 extern
301 int BMSisAligned(
302  size_t size /**< size to check for alignment */
303  );
304 
305 /** creates a new chunk block data structure */
306 extern
308  size_t size, /**< element size of the chunk block */
309  int initchunksize, /**< number of elements in the first chunk of the chunk block */
310  int garbagefactor, /**< garbage collector is called, if at least garbagefactor * avg. chunksize
311  * elements are free (-1: disable garbage collection) */
312  const char* filename, /**< source file of the function call */
313  int line /**< line number in source file of the function call */
314  );
315 
316 /** clears a chunk block data structure */
317 extern
319  BMS_CHKMEM* chkmem, /**< chunk block */
320  const char* filename, /**< source file of the function call */
321  int line /**< line number in source file of the function call */
322  );
323 
324 /** destroys and frees a chunk block data structure */
325 extern
327  BMS_CHKMEM** chkmem, /**< pointer to chunk block */
328  const char* filename, /**< source file of the function call */
329  int line /**< line number in source file of the function call */
330  );
331 
332 /** allocates a memory element of the given chunk block */
333 extern
335  BMS_CHKMEM* chkmem, /**< chunk block */
336  size_t size, /**< size of memory element to allocate (only needed for sanity check) */
337  const char* filename, /**< source file of the function call */
338  int line /**< line number in source file of the function call */
339  );
340 
341 /** duplicates a given memory element by allocating a new element of the same chunk block and copying the data */
342 extern
344  BMS_CHKMEM* chkmem, /**< chunk block */
345  const void* source, /**< source memory element */
346  size_t size, /**< size of memory element to allocate (only needed for sanity check) */
347  const char* filename, /**< source file of the function call */
348  int line /**< line number in source file of the function call */
349  );
350 
351 /** frees a memory element of the given chunk block and sets pointer to NULL */
352 extern
354  BMS_CHKMEM* chkmem, /**< chunk block */
355  void** ptr, /**< pointer to pointer to memory element to free */
356  size_t size, /**< size of memory element to allocate (only needed for sanity check) */
357  const char* filename, /**< source file of the function call */
358  int line /**< line number in source file of the function call */
359  );
360 
361 /** frees a memory element of the given chunk block if pointer is not NULL and sets pointer to NULL */
362 extern
364  BMS_CHKMEM* chkmem, /**< chunk block */
365  void** ptr, /**< pointer to pointer to memory element to free */
366  size_t size, /**< size of memory element to allocate (only needed for sanity check) */
367  const char* filename, /**< source file of the function call */
368  int line /**< line number in source file of the function call */
369  );
370 
371 /** calls garbage collection of chunk block and frees chunks without allocated memory elements */
372 extern
374  BMS_CHKMEM* chkmem /**< chunk block */
375  );
376 
377 /** returns the number of allocated bytes in the chunk block */
378 extern
380  const BMS_CHKMEM* chkmem /**< chunk block */
381  );
382 
383 
384 
385 
386 /***********************************************************
387  * Block Memory Management
388  *
389  * Efficient memory management for objects of varying sizes
390  ***********************************************************/
391 
392 typedef struct BMS_BlkMem BMS_BLKMEM; /**< block memory: collection of chunk blocks */
393 
394 #ifndef BMS_NOBLOCKMEM
395 
396 /* block memory methods for faster memory access */
397 
398 /* Note: values that are passed as a size_t parameter are first converted to ptrdiff_t to be sure that negative numbers
399  * are extended to the larger size. Then they are converted to size_t. Thus, negative numbers are converted to very
400  * large size_t values. This is then checked within the functions. */
401 
402 #define BMScreateBlockMemory(csz,gbf) BMScreateBlockMemory_call( (csz), (gbf), __FILE__, __LINE__ )
403 #define BMSclearBlockMemory(mem) BMSclearBlockMemory_call( (mem), __FILE__, __LINE__ )
404 #define BMSdestroyBlockMemory(mem) BMSdestroyBlockMemory_call( (mem), __FILE__, __LINE__ )
405 
406 #define BMSallocBlockMemory(mem,ptr) ASSIGN((ptr), BMSallocBlockMemory_call((mem), sizeof(**(ptr)), __FILE__, __LINE__))
407 #define BMSallocBlockMemorySize(mem,ptr,size) ASSIGN((ptr), BMSallocBlockMemory_call((mem), (size_t)(ptrdiff_t)(size), __FILE__, __LINE__))
408 #define BMSallocBlockMemoryArray(mem,ptr,num) ASSIGN((ptr), BMSallocBlockMemoryArray_call((mem), (size_t)(ptrdiff_t)(num), sizeof(**(ptr)), __FILE__, __LINE__))
409 #define BMSallocClearBlockMemoryArray(mem,ptr,num) ASSIGN((ptr), BMSallocClearBlockMemoryArray_call((mem), (size_t)(ptrdiff_t)(num), sizeof(**(ptr)), __FILE__, __LINE__))
410 #define BMSreallocBlockMemorySize(mem,ptr,oldsize,newsize) ASSIGN((ptr), BMSreallocBlockMemory_call((mem), (void*)(*(ptr)), \
411  (size_t)(ptrdiff_t)(oldsize), (size_t)(ptrdiff_t)(newsize), __FILE__, __LINE__))
412 #define BMSreallocBlockMemoryArray(mem,ptr,oldnum,newnum) ASSIGN((ptr), BMSreallocBlockMemoryArray_call((mem), (void*)(*(ptr)), \
413  (size_t)(ptrdiff_t)(oldnum), (size_t)(ptrdiff_t)(newnum), sizeof(**(ptr)), __FILE__, __LINE__))
414 #define BMSduplicateBlockMemory(mem, ptr, source) ASSIGN((ptr), BMSduplicateBlockMemory_call((mem), (const void*)(source), \
415  sizeof(**(ptr)), __FILE__, __LINE__ ))
416 #define BMSduplicateBlockMemoryArray(mem, ptr, source, num) ASSIGN((ptr), BMSduplicateBlockMemoryArray_call( (mem), (const void*)(source), \
417  (size_t)(ptrdiff_t)(num), sizeof(**(ptr)), __FILE__, __LINE__ ))
418 
419 #define BMSfreeBlockMemory(mem,ptr) BMSfreeBlockMemory_call( (mem), (void**)(ptr), sizeof(**(ptr)), __FILE__, __LINE__ )
420 #define BMSfreeBlockMemoryNull(mem,ptr) BMSfreeBlockMemoryNull_call( (mem), (void**)(ptr), sizeof(**(ptr)), __FILE__, __LINE__ )
421 #define BMSfreeBlockMemoryArray(mem,ptr,num) BMSfreeBlockMemory_call( (mem), (void**)(ptr), (num)*sizeof(**(ptr)), __FILE__, __LINE__ )
422 #define BMSfreeBlockMemoryArrayNull(mem,ptr,num) BMSfreeBlockMemoryNull_call( (mem), (void**)(ptr), (num)*sizeof(**(ptr)), __FILE__, __LINE__ )
423 #define BMSfreeBlockMemorySize(mem,ptr,size) BMSfreeBlockMemory_call( (mem), (void**)(ptr), (size_t)(ptrdiff_t)(size), __FILE__, __LINE__ )
424 #define BMSfreeBlockMemorySizeNull(mem,ptr,size) BMSfreeBlockMemory_call( (mem), (void**)(ptr), (size_t)(ptrdiff_t)(size), __FILE__, __LINE__ )
425 
426 #define BMSgarbagecollectBlockMemory(mem) BMSgarbagecollectBlockMemory_call(mem)
427 #define BMSgetBlockMemoryUsed(mem) BMSgetBlockMemoryUsed_call(mem)
428 #define BMSgetBlockPointerSize(mem,ptr) BMSgetBlockPointerSize_call((mem), (ptr))
429 #define BMSdisplayBlockMemory(mem) BMSdisplayBlockMemory_call(mem)
430 #define BMSblockMemoryCheckEmpty(mem) BMScheckEmptyBlockMemory_call(mem)
431 
432 #else
433 
434 /* block memory management mapped to standard memory management */
435 
436 #define BMScreateBlockMemory(csz,gbf) (void*)(0x01) /* dummy to not return a NULL pointer */
437 #define BMSclearBlockMemory(mem) /**/
438 #define BMSclearBlockMemoryNull(mem) /**/
439 #define BMSdestroyBlockMemory(mem) /**/
440 #define BMSdestroyBlockMemoryNull(mem) /**/
441 #define BMSallocBlockMemory(mem,ptr) BMSallocMemory(ptr)
442 #define BMSallocBlockMemoryArray(mem,ptr,num) BMSallocMemoryArray(ptr,num)
443 #define BMSallocClearBlockMemoryArray(mem,ptr,num) BMSallocClearMemoryArray(ptr,num)
444 #define BMSallocBlockMemorySize(mem,ptr,size) BMSallocMemorySize(ptr,size)
445 #define BMSreallocBlockMemoryArray(mem,ptr,oldnum,newnum) BMSreallocMemoryArray(ptr,newnum)
446 #define BMSreallocBlockMemorySize(mem,ptr,oldsize,newsize) BMSreallocMemorySize(ptr,newsize)
447 #define BMSduplicateBlockMemory(mem, ptr, source) BMSduplicateMemory(ptr,source)
448 #define BMSduplicateBlockMemoryArray(mem, ptr, source, num) BMSduplicateMemoryArray(ptr,source,num)
449 #define BMSfreeBlockMemory(mem,ptr) BMSfreeMemory(ptr)
450 #define BMSfreeBlockMemoryNull(mem,ptr) BMSfreeMemoryNull(ptr)
451 #define BMSfreeBlockMemoryArray(mem,ptr,num) BMSfreeMemoryArray(ptr)
452 #define BMSfreeBlockMemoryArrayNull(mem,ptr,num) BMSfreeMemoryArrayNull(ptr)
453 #define BMSfreeBlockMemorySize(mem,ptr,size) BMSfreeMemory(ptr)
454 #define BMSfreeBlockMemorySizeNull(mem,ptr,size) BMSfreeMemoryNull(ptr)
455 #define BMSgarbagecollectBlockMemory(mem) /**/
456 #define BMSgetBlockMemoryUsed(mem) 0LL
457 #define BMSgetBlockPointerSize(mem,ptr) 0
458 #define BMSdisplayBlockMemory(mem) /**/
459 #define BMSblockMemoryCheckEmpty(mem) /**/
460 
461 #endif
462 
463 
464 /** creates a block memory allocation data structure */
465 extern
467  int initchunksize, /**< number of elements in the first chunk of each chunk block */
468  int garbagefactor, /**< garbage collector is called, if at least garbagefactor * avg. chunksize
469  * elements are free (-1: disable garbage collection) */
470  const char* filename, /**< source file of the function call */
471  int line /**< line number in source file of the function call */
472  );
473 
474 /** frees all chunk blocks in the block memory */
475 extern
477  BMS_BLKMEM* blkmem, /**< block memory */
478  const char* filename, /**< source file of the function call */
479  int line /**< line number in source file of the function call */
480  );
481 
482 /** clears and deletes block memory */
483 extern
485  BMS_BLKMEM** blkmem, /**< pointer to block memory */
486  const char* filename, /**< source file of the function call */
487  int line /**< line number in source file of the function call */
488  );
489 
490 /** allocates memory in the block memory pool */
491 extern
493  BMS_BLKMEM* blkmem, /**< block memory */
494  size_t size, /**< size of memory element to allocate */
495  const char* filename, /**< source file of the function call */
496  int line /**< line number in source file of the function call */
497  );
498 
499 /** allocates array in the block memory pool */
500 extern
502  BMS_BLKMEM* blkmem, /**< block memory */
503  size_t num, /**< size of array to be allocated */
504  size_t typesize, /**< size of each component */
505  const char* filename, /**< source file of the function call */
506  int line /**< line number in source file of the function call */
507  );
508 
509 /** allocates array in the block memory pool and clears it */
510 extern
512  BMS_BLKMEM* blkmem, /**< block memory */
513  size_t num, /**< size of array to be allocated */
514  size_t typesize, /**< size of each component */
515  const char* filename, /**< source file of the function call */
516  int line /**< line number in source file of the function call */
517  );
518 
519 /** resizes memory element in the block memory pool and copies the data */
520 extern
522  BMS_BLKMEM* blkmem, /**< block memory */
523  void* ptr, /**< memory element to reallocated */
524  size_t oldsize, /**< old size of memory element */
525  size_t newsize, /**< new size of memory element */
526  const char* filename, /**< source file of the function call */
527  int line /**< line number in source file of the function call */
528  );
529 
530 /** resizes array in the block memory pool and copies the data */
531 extern
533  BMS_BLKMEM* blkmem, /**< block memory */
534  void* ptr, /**< memory element to reallocated */
535  size_t oldnum, /**< old size of array */
536  size_t newnum, /**< new size of array */
537  size_t typesize, /**< size of each component */
538  const char* filename, /**< source file of the function call */
539  int line /**< line number in source file of the function call */
540  );
541 
542 /** duplicates memory element in the block memory pool and copies the data */
543 extern
545  BMS_BLKMEM* blkmem, /**< block memory */
546  const void* source, /**< memory element to duplicate */
547  size_t size, /**< size of memory elements */
548  const char* filename, /**< source file of the function call */
549  int line /**< line number in source file of the function call */
550  );
551 
552 /** duplicates array in the block memory pool and copies the data */
553 extern
555  BMS_BLKMEM* blkmem, /**< block memory */
556  const void* source, /**< memory element to duplicate */
557  size_t num, /**< size of array to be duplicated */
558  size_t typesize, /**< size of each component */
559  const char* filename, /**< source file of the function call */
560  int line /**< line number in source file of the function call */
561  );
562 
563 /** frees memory element in the block memory pool and sets pointer to NULL */
564 extern
566  BMS_BLKMEM* blkmem, /**< block memory */
567  void** ptr, /**< pointer to pointer to memory element to free */
568  size_t size, /**< size of memory element */
569  const char* filename, /**< source file of the function call */
570  int line /**< line number in source file of the function call */
571  );
572 
573 /** frees memory element in the block memory pool if pointer is not NULL and sets pointer to NULL */
574 extern
576  BMS_BLKMEM* blkmem, /**< block memory */
577  void** ptr, /**< pointer to pointer to memory element to free */
578  size_t size, /**< size of memory element */
579  const char* filename, /**< source file of the function call */
580  int line /**< line number in source file of the function call */
581  );
582 
583 /** calls garbage collection of block memory, frees chunks without allocated memory elements, and frees
584  * chunk blocks without any chunks
585  */
586 extern
588  BMS_BLKMEM* blkmem /**< block memory */
589  );
590 
591 /** returns the number of allocated bytes in the block memory */
592 extern
594  const BMS_BLKMEM* blkmem /**< block memory */
595  );
596 
597 /** returns the size of the given memory element; returns 0, if the element is not member of the block memory */
598 extern
600  const BMS_BLKMEM* blkmem, /**< block memory */
601  const void* ptr /**< memory element */
602  );
603 
604 /** outputs allocation diagnostics of block memory */
605 extern
607  const BMS_BLKMEM* blkmem /**< block memory */
608  );
609 
610 /** outputs warning messages, if there are allocated elements in the block memory */
611 extern
613  const BMS_BLKMEM* blkmem /**< block memory */
614  );
615 
616 
617 
618 
619 
620 /***********************************************************
621  * Buffer Memory Management
622  *
623  * Efficient memory management for temporary objects
624  ***********************************************************/
625 
626 typedef struct BMS_BufMem BMS_BUFMEM; /**< buffer memory for temporary objects */
627 
628 /* Note: values that are passed as a size_t parameter are first converted to ptrdiff_t to be sure that negative numbers
629  * are extended to the larger size. Then they are converted to size_t. Thus, negative numbers are converted to very
630  * large size_t values. This is then checked within the functions. */
631 
632 #define BMSallocBufferMemory(mem,ptr) ASSIGN((ptr), BMSallocBufferMemory_call((mem), sizeof(**(ptr)), __FILE__, __LINE__))
633 #define BMSallocBufferMemorySize(mem,ptr,size) ASSIGN((ptr), BMSallocBufferMemory_call((mem), (size_t)(ptrdiff_t)(size), __FILE__, __LINE__))
634 #define BMSreallocBufferMemorySize(mem,ptr,size) \
635  ASSIGN((ptr), BMSreallocBufferMemory_call((mem), (void*)(*(ptr)), (size_t)(ptrdiff_t)(size), __FILE__, __LINE__))
636 #define BMSallocBufferMemoryArray(mem,ptr,num) ASSIGN((ptr), BMSallocBufferMemoryArray_call((mem), (size_t)(ptrdiff_t)(num), sizeof(**(ptr)), __FILE__, __LINE__))
637 #define BMSallocClearBufferMemoryArray(mem,ptr,num) ASSIGN((ptr), BMSallocClearBufferMemoryArray_call((mem), (size_t)(ptrdiff_t)(num), sizeof(**(ptr)), __FILE__, __LINE__))
638 #define BMSreallocBufferMemoryArray(mem,ptr,num) ASSIGN((ptr), BMSreallocBufferMemoryArray_call((mem), (void*)(*(ptr)), (size_t)(ptrdiff_t)(num), \
639  sizeof(**(ptr)), __FILE__, __LINE__))
640 #define BMSduplicateBufferMemory(mem,ptr,source,size) \
641  ASSIGN((ptr), BMSduplicateBufferMemory_call((mem), (const void*)(source), (size_t)(ptrdiff_t)(size), __FILE__, __LINE__))
642 #define BMSduplicateBufferMemoryArray(mem,ptr,source,num) ASSIGN((ptr), BMSduplicateBufferMemoryArray_call((mem), \
643  (const void*)(source), (size_t)(ptrdiff_t)(num), sizeof(**(ptr)), __FILE__, __LINE__))
644 
645 #define BMSfreeBufferMemory(mem,ptr) BMSfreeBufferMemory_call((mem), (void**)(ptr), __FILE__, __LINE__)
646 #define BMSfreeBufferMemoryNull(mem,ptr) BMSfreeBufferMemoryNull_call((mem), (void**)(ptr), __FILE__, __LINE__)
647 #define BMSfreeBufferMemoryArray(mem,ptr) BMSfreeBufferMemory_call((mem), (void**)(ptr), __FILE__, __LINE__)
648 #define BMSfreeBufferMemoryArrayNull(mem,ptr) BMSfreeBufferMemoryNull_call((mem), (void**)(ptr), __FILE__, __LINE__)
649 #define BMSfreeBufferMemorySize(mem,ptr) BMSfreeBufferMemory_call((mem), (void**)(ptr), __FILE__, __LINE__);
650 #define BMSfreeBufferMemorySizeNull(mem,ptr) BMSfreeBufferMemoryNull_call((mem), (void**)(ptr), __FILE__, __LINE__)
651 
652 #define BMScreateBufferMemory(fac,init,clean) BMScreateBufferMemory_call((fac), (init), (clean), __FILE__, __LINE__)
653 #define BMSdestroyBufferMemory(mem) BMSdestroyBufferMemory_call((mem), __FILE__, __LINE__)
654 
655 
656 /** creates memory buffer storage */
657 extern
659  double arraygrowfac, /**< memory growing factor for dynamically allocated arrays */
660  int arraygrowinit, /**< initial size of dynamically allocated arrays */
661  unsigned int clean, /**< should the memory blocks in the buffer be initialized to zero? */
662  const char* filename, /**< source file of the function call */
663  int line /**< line number in source file of the function call */
664  );
665 
666 /** destroys buffer memory */
667 extern
669  BMS_BUFMEM** buffer, /**< pointer to memory buffer storage */
670  const char* filename, /**< source file of the function call */
671  int line /**< line number in source file of the function call */
672  );
673 
674 /** set arraygrowfac */
675 extern
677  BMS_BUFMEM* buffer, /**< pointer to memory buffer storage */
678  double arraygrowfac /**< memory growing factor for dynamically allocated arrays */
679  );
680 
681 /** set arraygrowinit */
682 extern
684  BMS_BUFMEM* buffer, /**< pointer to memory buffer storage */
685  int arraygrowinit /**< initial size of dynamically allocated arrays */
686  );
687 
688 /** allocates the next unused buffer */
689 extern
691  BMS_BUFMEM* buffer, /**< memory buffer storage */
692  size_t size, /**< minimal required size of the buffer */
693  const char* filename, /**< source file of the function call */
694  int line /**< line number in source file of the function call */
695  );
696 
697 /** allocates the next unused buffer array */
698 extern
700  BMS_BUFMEM* buffer, /**< memory buffer storage */
701  size_t num, /**< size of array to be allocated */
702  size_t typesize, /**< size of components */
703  const char* filename, /**< source file of the function call */
704  int line /**< line number in source file of the function call */
705  );
706 
707 /** allocates the next unused buffer and clears it */
708 extern
710  BMS_BUFMEM* buffer, /**< memory buffer storage */
711  size_t num, /**< size of array to be allocated */
712  size_t typesize, /**< size of components */
713  const char* filename, /**< source file of the function call */
714  int line /**< line number in source file of the function call */
715  );
716 
717 /** reallocates the buffer to at least the given size */
718 extern
720  BMS_BUFMEM* buffer, /**< memory buffer storage */
721  void* ptr, /**< pointer to the allocated memory buffer */
722  size_t size, /**< minimal required size of the buffer */
723  const char* filename, /**< source file of the function call */
724  int line /**< line number in source file of the function call */
725  );
726 
727 /** reallocates an array in the buffer to at least the given size */
728 extern
730  BMS_BUFMEM* buffer, /**< memory buffer storage */
731  void* ptr, /**< pointer to the allocated memory buffer */
732  size_t num, /**< size of array to be allocated */
733  size_t typesize, /**< size of components */
734  const char* filename, /**< source file of the function call */
735  int line /**< line number in source file of the function call */
736  );
737 
738 /** allocates the next unused buffer and copies the given memory into the buffer */
739 extern
741  BMS_BUFMEM* buffer, /**< memory buffer storage */
742  const void* source, /**< memory block to copy into the buffer */
743  size_t size, /**< minimal required size of the buffer */
744  const char* filename, /**< source file of the function call */
745  int line /**< line number in source file of the function call */
746  );
747 
748 /** allocates an array in the next unused buffer and copies the given memory into the buffer */
749 extern
751  BMS_BUFMEM* buffer, /**< memory buffer storage */
752  const void* source, /**< memory block to copy into the buffer */
753  size_t num, /**< size of array to be allocated */
754  size_t typesize, /**< size of components */
755  const char* filename, /**< source file of the function call */
756  int line /**< line number in source file of the function call */
757  );
758 
759 /** frees a buffer and sets pointer to NULL */
760 extern
762  BMS_BUFMEM* buffer, /**< memory buffer storage */
763  void** ptr, /**< pointer to pointer to the allocated memory buffer */
764  const char* filename, /**< source file of the function call */
765  int line /**< line number in source file of the function call */
766  );
767 
768 /** frees a buffer if pointer is not NULL and sets pointer to NULL */
769 extern
771  BMS_BUFMEM* buffer, /**< memory buffer storage */
772  void** ptr, /**< pointer to pointer to the allocated memory buffer */
773  const char* filename, /**< source file of the function call */
774  int line /**< line number in source file of the function call */
775  );
776 
777 /** gets number of used buffers */
778 extern
780  BMS_BUFMEM* buffer /**< memory buffer storage */
781  );
782 
783 /** returns the number of allocated bytes in the buffer memory */
784 extern
785 long long BMSgetBufferMemoryUsed(
786  const BMS_BUFMEM* bufmem /**< buffer memory */
787  );
788 
789 /** outputs statistics about currently allocated buffers to the screen */
790 extern
792  BMS_BUFMEM* buffer /**< memory buffer storage */
793  );
794 
795 
796 #ifdef __cplusplus
797 }
798 #endif
799 
800 #endif
void * BMSreallocBufferMemory_call(BMS_BUFMEM *buffer, void *ptr, size_t size, const char *filename, int line)
Definition: memory.c:2832
void BMSfreeBufferMemoryNull_call(BMS_BUFMEM *buffer, void **ptr, const char *filename, int line)
Definition: memory.c:3011
void BMSdestroyChunkMemory_call(BMS_CHKMEM **chkmem, const char *filename, int line)
Definition: memory.c:1504
void * BMSallocBlockMemory_call(BMS_BLKMEM *blkmem, size_t size, const char *filename, int line)
Definition: memory.c:1887
struct BMS_ChkMem BMS_CHKMEM
Definition: memory.h:257
BMS_CHKMEM * BMScreateChunkMemory_call(size_t size, int initchunksize, int garbagefactor, const char *filename, int line)
Definition: memory.c:1462
void BMSfreeMemoryNull_call(void **ptr, const char *filename, int line)
Definition: memory.c:616
void * BMSallocChunkMemory_call(BMS_CHKMEM *chkmem, size_t size, const char *filename, int line)
Definition: memory.c:1524
void * BMSallocBlockMemoryArray_call(BMS_BLKMEM *blkmem, size_t num, size_t typesize, const char *filename, int line)
Definition: memory.c:1907
void * BMSallocClearBlockMemoryArray_call(BMS_BLKMEM *blkmem, size_t num, size_t typesize, const char *filename, int line)
Definition: memory.c:1928
void BMSclearBlockMemory_call(BMS_BLKMEM *blkmem, const char *filename, int line)
Definition: memory.c:1776
double arraygrowfac
Definition: memory.c:2424
void * BMSallocBufferMemoryArray_call(BMS_BUFMEM *buffer, size_t num, size_t typesize, const char *filename, int line)
Definition: memory.c:2720
size_t * size
Definition: memory.c:2418
int BMSisAligned(size_t size)
Definition: memory.c:724
void BMSdestroyBlockMemory_call(BMS_BLKMEM **blkmem, const char *filename, int line)
Definition: memory.c:1809
void * BMSreallocBlockMemoryArray_call(BMS_BLKMEM *blkmem, void *ptr, size_t oldnum, size_t newnum, size_t typesize, const char *filename, int line)
Definition: memory.c:1986
size_t BMSgetBlockPointerSize_call(const BMS_BLKMEM *blkmem, const void *ptr)
Definition: memory.c:2194
void * BMSduplicateChunkMemory_call(BMS_CHKMEM *chkmem, const void *source, size_t size, const char *filename, int line)
Definition: memory.c:1551
void BMSfreeChunkMemory_call(BMS_CHKMEM *chkmem, void **ptr, size_t size, const char *filename, int line)
Definition: memory.c:1573
void * BMSallocBufferMemory_call(BMS_BUFMEM *buffer, size_t size, const char *filename, int line)
Definition: memory.c:2700
void BMSsetBufferMemoryArraygrowinit(BMS_BUFMEM *buffer, int arraygrowinit)
Definition: memory.c:2511
void * BMSallocClearBufferMemoryArray_call(BMS_BUFMEM *buffer, size_t num, size_t typesize, const char *filename, int line)
Definition: memory.c:2741
void * BMSduplicateMemory_call(const void *source, size_t size, const char *filename, int line)
Definition: memory.c:554
void * BMSreallocBlockMemory_call(BMS_BLKMEM *blkmem, void *ptr, size_t oldsize, size_t newsize, const char *filename, int line)
Definition: memory.c:1946
void * BMSreallocMemoryArray_call(void *ptr, size_t num, size_t typesize, const char *filename, int line)
Definition: memory.c:466
void * BMSallocMemoryArray_call(size_t num, size_t typesize, const char *filename, int line)
Definition: memory.c:386
void BMSdestroyBufferMemory_call(BMS_BUFMEM **buffer, const char *filename, int line)
Definition: memory.c:2466
void BMSdisplayMemory_call(void)
Definition: memory.c:281
BMS_BLKMEM * BMScreateBlockMemory_call(int initchunksize, int garbagefactor, const char *filename, int line)
Definition: memory.c:1746
void BMSclearMemory_call(void *ptr, size_t size)
Definition: memory.c:509
void BMSfreeBufferMemory_call(BMS_BUFMEM *buffer, void **ptr, const char *filename, int line)
Definition: memory.c:2988
void * BMSduplicateBlockMemory_call(BMS_BLKMEM *blkmem, const void *source, size_t size, const char *filename, int line)
Definition: memory.c:2025
void BMScheckEmptyMemory_call(void)
Definition: memory.c:291
void BMSgarbagecollectChunkMemory_call(BMS_CHKMEM *chkmem)
Definition: memory.c:1626
void * BMSreallocMemory_call(void *ptr, size_t size, const char *filename, int line)
Definition: memory.c:426
long long BMSgetBlockMemoryUsed_call(const BMS_BLKMEM *blkmem)
Definition: memory.c:2184
void BMScheckEmptyBlockMemory_call(const BMS_BLKMEM *blkmem)
Definition: memory.c:2337
void * BMSduplicateBlockMemoryArray_call(BMS_BLKMEM *blkmem, const void *source, size_t num, size_t typesize, const char *filename, int line)
Definition: memory.c:2045
void BMSfreeBlockMemoryNull_call(BMS_BLKMEM *blkmem, void **ptr, size_t size, const char *filename, int line)
Definition: memory.c:2132
long long BMSgetChunkMemoryUsed_call(const BMS_CHKMEM *chkmem)
Definition: memory.c:1636
void BMSgarbagecollectBlockMemory_call(BMS_BLKMEM *blkmem)
Definition: memory.c:2153
void BMSprintBufferMemory(BMS_BUFMEM *buffer)
Definition: memory.c:3059
void * BMSallocMemory_call(size_t size, const char *filename, int line)
Definition: memory.c:350
unsigned int arraygrowinit
Definition: memory.c:2425
void BMSfreeMemory_call(void **ptr, const char *filename, int line)
Definition: memory.c:593
BMS_BUFMEM * BMScreateBufferMemory_call(double arraygrowfac, int arraygrowinit, unsigned int clean, const char *filename, int line)
Definition: memory.c:2430
void * BMSallocClearMemory_call(size_t num, size_t typesize, const char *filename, int line)
Definition: memory.c:311
void BMSsetBufferMemoryArraygrowfac(BMS_BUFMEM *buffer, double arraygrowfac)
Definition: memory.c:2499
void * BMSduplicateBufferMemory_call(BMS_BUFMEM *buffer, const void *source, size_t size, const char *filename, int line)
Definition: memory.c:2875
void BMSmoveMemory_call(void *ptr, const void *source, size_t size)
Definition: memory.c:539
void * BMSreallocBufferMemoryArray_call(BMS_BUFMEM *buffer, void *ptr, size_t num, size_t typesize, const char *filename, int line)
Definition: memory.c:2853
void BMSfreeChunkMemoryNull_call(BMS_CHKMEM *chkmem, void **ptr, size_t size, const char *filename, int line)
Definition: memory.c:1602
size_t BMSgetNUsedBufferMemory(BMS_BUFMEM *buffer)
Definition: memory.c:3031
void BMSalignMemsize(size_t *size)
Definition: memory.c:715
void BMScopyMemory_call(void *ptr, const void *source, size_t size)
Definition: memory.c:522
void BMSfreeBlockMemory_call(BMS_BLKMEM *blkmem, void **ptr, size_t size, const char *filename, int line)
Definition: memory.c:2110
void BMSdisplayBlockMemory_call(const BMS_BLKMEM *blkmem)
Definition: memory.c:2214
void BMSclearChunkMemory_call(BMS_CHKMEM *chkmem, const char *filename, int line)
Definition: memory.c:1486
size_t BMSgetPointerSize_call(const void *ptr)
Definition: memory.c:273
struct BMS_BlkMem BMS_BLKMEM
Definition: memory.h:392
unsigned int clean
Definition: memory.c:2421
long long BMSgetMemoryUsed_call(void)
Definition: memory.c:301
void * BMSduplicateMemoryArray_call(const void *source, size_t num, size_t typesize, const char *filename, int line)
Definition: memory.c:573
long long BMSgetBufferMemoryUsed(const BMS_BUFMEM *bufmem)
Definition: memory.c:3041
void * BMSduplicateBufferMemoryArray_call(BMS_BUFMEM *buffer, const void *source, size_t num, size_t typesize, const char *filename, int line)
Definition: memory.c:2898