Debian Codegraph @

dune/uggrid/low/heaps.cc

raw
1 // SPDX-FileCopyrightText: Copyright © DUNE Project contributors, see file LICENSE.md in module root
2 // SPDX-License-Identifier: LGPL-2.1-or-later
3 // -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
4 // vi: set et ts=4 sw=2 sts=2:
5 /****************************************************************************/
6 /* */
7 /* File: heaps.c */
8 /* */
9 /* Purpose: low-level memory management for ug 2.0 */
10 /* */
11 /* Author: Peter Bastian/Henrik Rentz-Reichert */
12 /* Institut fuer Computeranwendungen III */
13 /* Universitaet Stuttgart */
14 /* Pfaffenwaldring 27 */
15 /* 70569 Stuttgart */
16 /* email: [email protected] */
17 /* */
18 /* History: 29.01.92 begin, ug version 2.0 */
19 /* 02.02.95 begin, ug version 3.0 */
20 /* */
21 /* Revision: 04.09.95 */
22 /* */
23 /****************************************************************************/
24
25 /****************************************************************************/
26 /* */
27 /* include files in the order */
28 /* system include files */
29 /* application include files */
30 /* */
31 /****************************************************************************/
32
33 #include <config.h>
34
35 #include <cstdlib>
36 #include <cstddef>
37 #include <cstring>
38 #include <cassert>
39 #include <cstdio>
40
41 #include "ugtypes.h"
42 #include "architecture.h"
43 #include "heaps.h"
44 #include "misc.h"
45 #include "debug.h"
46 #include <dune/uggrid/ugdevices.h>
47
48 #include "namespace.h"
49 USING_UG_NAMESPACE
50
51 /****************************************************************************/
52 /* */
53 /* defines in the following order */
54 /* */
55 /* compile time constants defining static data size (i.e. arrays) */
56 /* other constants */
57 /* macros */
58 /* */
59 /****************************************************************************/
60
61 /****************************************************************************/
62 /** \brief Install a new heap structure
63
64 \param type - type of heap
65 \param size - size of new heap in bytes
66 \param buffer - 4-aligned memory for the heap
67
68 This function installs a new heap structure.
69 Valid 'type' is either 'SIMPLE_HEAP' or 'GENERAL_HEAP'.
70 The allocation of memory starts at the address given by
71 '*buffer' and is of 'size' bytes.
72
73 \return <ul>
74 <li> pointer to HEAP </li>
75 <li> NULL if not enough space available </li>
76 </ul>
77 */
78 /****************************************************************************/
79
80 HEAP *NS_PREFIX NewHeap (enum HeapType type, MEM size, void *buffer)
81 {
82 HEAP *theHeap;
83
84 /* check size */
85 if (buffer==NULL) return(NULL);
86 if (size<MIN_HEAP_SIZE) return(NULL);
87
88 /* initialize heap structure */
89 theHeap = (HEAP *) buffer;
90 theHeap->type = type;
91 theHeap->size = size;
92 theHeap->markKey = 0;
93
94 /* No constructor is ever called for theHeap. Consequently, no constructor
95 * has been called for its member markedMemory, either. Here we force this
96 * constructor call using placement new. */
97 new(theHeap->markedMemory) std::vector<void*>[MARK_STACK_SIZE+1];
98
99 /* return heap structure */
100 return(theHeap);
101 }
102
103 /****************************************************************************/
104 /** \brief Clean up and deallocate a heap structure
105
106 \param theHeap The heap to be deallocated
107
108 This method properly cleans up a HEAP data structure, and then frees the
109 memory.
110 */
111 /****************************************************************************/
112
113
114
115 void NS_PREFIX DisposeHeap (HEAP *theHeap)
116 {
117 if (theHeap != NULL) {
118 /* When using the system heap, the HEAP data structure contains an array of
119 * std::vectors, which have all be created using placement new. Therefore,
120 * before freeing the memory of a HEAP we have to call the destructors of
121 * these std::vectors explicitly. Otherwise we get memory leaks.
122 */
123 using namespace std;
124 for (INT i=0; i<MARK_STACK_SIZE; i++)
125 theHeap->markedMemory[i].~vector<void*>();
126
127 free(theHeap);
128 }
129 }
130
131 /****************************************************************************/
132 /** \brief Allocate memory from heap
133
134 \param theHeap - heap structure which manages memory allocation
135 \param n - number of bytes to allocate
136
137 this function now only forwards to `malloc`.
138 */
139 void *NS_PREFIX GetMem (HEAP *theHeap, MEM n)
140 {
141 return malloc(n);
142 }
143
144 /****************************************************************************/
145 /** \brief Allocate memory and register it for rollback
146
147 \param theHeap - heap structure which manages memory allocation
148 \param n - number of bytes to allocate
149 \param key - key with which we can identify the rollback record
150
151 this function allocates memory on the heap and tags it with the `key`.
152 */
153 void *NS_PREFIX GetTmpMem (HEAP *theHeap, MEM n, INT key)
154 {
155 if (theHeap->type==SIMPLE_HEAP)
156 {
157 ASSERT(key == theHeap->markKey);
158 /* we have to keep track of allocated memory, in order to do a proper rollback */
159 void* ptr = GetMem(theHeap,n);
160 theHeap->markedMemory[key].push_back(ptr);
161 return theHeap->markedMemory[key].back();
162 }
163 /* no key for GENERAL_HEAP */
164 return (GetMem(theHeap,n));
165 }
166
167 /****************************************************************************/
168 /** \brief Free memory previously allocated from that heap
169
170 \param theHeap - heap structure which manages memory allocation
171 \param buffer - memory area previously allocated
172
173 This function creates free memory previously allocated from that heap.
174 */
175 /****************************************************************************/
176
177 void NS_PREFIX DisposeMem (HEAP *theHeap, void *buffer)
178 {
179 free(buffer);
180 }
181
182 /****************************************************************************/
183 /** \copydoc Allocate memory from heap
184
185 \param theHeap - heap structure which manages memory allocation
186 \param size - number of bytes to allocate
187
188 \note the allocated memory is initialized to zero
189
190 \return pointer to an object of the requested size
191 */
192 /****************************************************************************/
193
194 void *NS_PREFIX GetFreelistMemory (HEAP *theHeap, INT size)
195 {
196 void* obj = malloc(size);
197
198 if (obj != NULL)
199 memset(obj,0,size);
200
201 return(obj);
202 }
203
204 /****************************************************************************/
205 /** \brief Mark heap position for future release
206
207 \param theHeap - heap to mark
208
209 This function marks heap position for future release. Only valid in
210 the 'SIMPLE_HEAP' type.
211
212 \return <ul>
213 <li> 0 if OK </li>
214 <li> 1 if mark stack full or wrong heap type </li>
215 </ul>
216 */
217 /****************************************************************************/
218
219 INT NS_PREFIX MarkTmpMem (HEAP *theHeap, INT *key)
220 {
221 assert(theHeap->type==SIMPLE_HEAP);
222 if (theHeap->type!=SIMPLE_HEAP) return(1);
223
224 if(theHeap->markKey >= MARK_STACK_SIZE)
225 return 1;
226 theHeap->markKey++;
227 *key = theHeap->markKey;
228 return 0;
229 }
230
231 /****************************************************************************/
232 /** \brief Release to next stack position
233
234 \param theHeap - heap to release
235
236 This function releases to the next stack position. Only valid in the
237 'SIMPLE_HEAP' type.
238
239 \return <ul>
240 <li> 0 if OK </li>
241 <li> 1 if mark stack empty or wrong heap type. </li>
242 </ul>
243 */
244 /****************************************************************************/
245
246 INT NS_PREFIX ReleaseTmpMem (HEAP *theHeap, INT key)
247 {
248 if (theHeap->type!=SIMPLE_HEAP) return 1;
249
250 if (theHeap->markKey == 0) return 0;
251 if (key > theHeap->markKey) return 1;
252
253 /* Free all memory associated to 'key' */
254 for (void* ptr : theHeap->markedMemory[key])
255 free(ptr);
256 theHeap->markedMemory[key].resize(0);
257
258 if (key < theHeap->markKey) return 2;
259 while (theHeap->markKey > 0 && theHeap->markedMemory[theHeap->markKey].size() == 0)
260 theHeap->markKey--;
261
262 return 0;
263 }