forked from LLNL/cardioid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathheap.c
274 lines (229 loc) · 6.89 KB
/
heap.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
/* $Id$ */
#include "heap.h"
#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#include <libgen.h>
#include "mpiUtils.h"
#include "ddcMalloc.h"
#include "object.h"
#define MAX_HEAP_BLOCKS 64
extern FILE *ddcfile;
// Is there any reason we shouldn't quad align all of the heap
// allocations on every platform? I can't think of any.
#define QUAD_WORD_ALIGN_HEAP
static const int alignOffset=16;
typedef struct heapBlock_st
{
char* blockStart;
size_t blockSize;
char location[40];
} HEAP_BLOCK;
static void* heapBuffer=NULL;
static size_t _heapCapacity = 0;
static size_t _heapClaimed = 0;
static int _nBlocks=0;
static HEAP_BLOCK _blocks[MAX_HEAP_BLOCKS];
static int _openBlock = -1;
static size_t _heapHighWaterMark = 0;
static void blockOpenError(const char* file, int line);
static void tooManyBlocksError(const char* file, int line);
static void heapTooSmallError(size_t request, const char* file, int line);
static void heapFreeOpenBlockError(const char* file, int line);
static void heapFreeOrderError(const char* file, int line);
static void heapAlreadyAllocatedWarning(void);
static void heapAlreadyClosedError(const char* file, int line);
HEAP* heap_init(void* parent, char* name)
{
HEAP* heapObj = (HEAP*)object_initialize(name, "HEAP", sizeof(HEAP));
heapObj->parent=parent;
int size;
object_get((OBJECT *) heapObj, "size", &size, INT, 1, "1");
/* The size in bytes can be large, so make sure to compute as size_t */ {
size_t l_size = size;
l_size *= (size_t) (1024*1024);
heap_allocate(l_size);
}
_nBlocks = 0;
_openBlock = -1;
for (unsigned ii=0; ii<MAX_HEAP_BLOCKS; ++ii)
{
_blocks[ii].blockStart = NULL;
_blocks[ii].blockSize = 0;
_blocks[ii].location[0] = '\0';
}
return heapObj;
}
void heap_start(int size)
{
/* The size in bytes can be large, so make sure to compute as size_t */ {
size_t l_size = size;
l_size *= (size_t) (1024*1024);
heap_allocate(l_size);
}
_nBlocks = 0;
_openBlock = -1;
for (unsigned ii=0; ii<MAX_HEAP_BLOCKS; ++ii)
{
_blocks[ii].blockStart = NULL;
_blocks[ii].blockSize = 0;
_blocks[ii].location[0] = '\0';
}
}
void* _heapGet(unsigned* blockNumber, char* file, int line)
{
if (_openBlock != -1)
blockOpenError(file, line);
_openBlock = _nBlocks;
*blockNumber= _nBlocks;
//fprintf(ddcfile,"ddt %d: Opening block %d (%s:%d)\n", getRank(-1), _nBlocks, file, line); fflush(ddcfile);
void* blockStart = ((char*)heapBuffer)+_heapClaimed;
_blocks[_nBlocks].blockStart = blockStart;
_blocks[_nBlocks].blockSize = _heapCapacity-_heapClaimed;
snprintf(_blocks[_nBlocks].location, 39, "%s:%d", basename(file), line);
++_nBlocks;
if (_nBlocks >= MAX_HEAP_BLOCKS)
tooManyBlocksError(file, line);
return blockStart;
}
void slave_heap_init(char *data) { heap_init(NULL,data); }
void _heapTestAvailable(unsigned blockNumber, size_t size, const char* file, int line)
{
size_t heapNeeded = _heapClaimed + size;
if (heapNeeded > _heapCapacity) heapTooSmallError(size, file, line);
}
size_t heapAvailable(void) {
return _heapCapacity - _heapClaimed - 64 /* In case 32 bytes are lost on either side for alignment reasons */;
}
void _heapEndBlock(unsigned blockNumber, size_t size, const char* file, int line)
{
//fprintf(ddcfile,"ddt %d: Ending block %d (%s:%d), size=%d\n", getRank(-1), blockNumber, file, line, size); fflush(ddcfile);
if (_openBlock != (int)blockNumber) heapAlreadyClosedError(file, line);
_openBlock = -1;
#ifdef QUAD_WORD_ALIGN_HEAP
size += (alignOffset - (size%alignOffset) ) % alignOffset;
#endif
assert((int)blockNumber == _nBlocks-1);
_heapClaimed += size;
_blocks[_nBlocks-1].blockSize = size;
if (_heapHighWaterMark < _heapClaimed) _heapHighWaterMark = _heapClaimed;
if (_heapHighWaterMark > _heapCapacity) heapTooSmallError(size, file, line);
}
void _heapFree(unsigned blockNumber, const char* file, int line)
{
//fprintf(ddcfile,"ddt %d: Free block %d (%s:%d) size=%d, claimed=%d\n", getRank(-1), blockNumber, file, line, _blocks[_nBlocks-1].blockSize, _heapClaimed); fflush(ddcfile);
if (_openBlock != -1)
heapFreeOpenBlockError(file, line);
if ((int)blockNumber != _nBlocks-1)
heapFreeOrderError(file, line);
if (heapBuffer == NULL || _nBlocks == 0)
return ;
--_nBlocks;
assert(_nBlocks == (int)blockNumber);
_heapClaimed -= _blocks[_nBlocks].blockSize;
_blocks[_nBlocks].blockStart = NULL;
_blocks[_nBlocks].blockSize = 0;
_blocks[_nBlocks].location[0] = '\0';
}
void heap_allocate(size_t n)
{
if (heapBuffer != NULL)
{
heapAlreadyAllocatedWarning();
return;
}
_heapHighWaterMark = 0;
_heapClaimed = 0;
_heapCapacity = n;
#ifdef QUAD_WORD_ALIGN_HEAP
_heapCapacity += (alignOffset - (_heapCapacity % alignOffset) ) % alignOffset;
ddcMallocAligned((void **)&heapBuffer, alignOffset, _heapCapacity);
#else
heapBuffer = ddcMalloc(n);
#endif
_nBlocks=0;
}
size_t heapSize(void)
{
return _heapCapacity;
}
size_t heapBlockSize(unsigned block)
{
if ((int)block >= _nBlocks)
return 0;
return _blocks[block].blockSize;
}
void heapSummary(FILE *file)
{
fprintf(file, "Scratch heap capacity: %f Mbytes, high water mark: %f Mbytes \n",
_heapCapacity/(1024*1024.0), _heapHighWaterMark/(1024*1024.0));
}
#ifdef WITH_PIO
void heapSummary_pio(PFILE *file)
{
Pprintf(file, "Scratch heap capacity: %f Mbytes, high water mark: %f Mbytes \n",
_heapCapacity/(1024*1024.0), _heapHighWaterMark/(1024*1024.0));
}
#endif
void heap_deallocate(void)
{
ddcFree(heapBuffer);
}
void blockOpenError(const char* file, int line)
{
printf("Scratch Heap ERROR:\n"
" heapGet called with block already open.\n"
" current call from %s:%d\n"
" open block created from %s\n",
file, line, _blocks[_nBlocks-1].location);
abortAll(-1);
}
void tooManyBlocksError(const char* file, int line)
{
printf("Scratch Heap ERROR:\n"
" Too many blocks. _nBlocks = %d\n"
" current call from %s:%d\n",
_nBlocks, file, line);
abortAll(-1);
}
void heapTooSmallError(size_t request, const char* file, int line)
{
printf("Scratch Heap ERROR:\n"
" heap too small\n"
" current call from %s:%d\n"
" _heapCapacity = %f Mbytes\n"
" _heapClaimed = %f Mbytes\n"
" request = %f Mbytes\n",
file, line, _heapCapacity/(1024*1024.0),
_heapClaimed/(1024*1024.0),
request/(1024*1024.0));
abortAll(-1);
}
void heapFreeOpenBlockError(const char* file, int line)
{
printf("Scratch Heap ERROR:\n"
" Attempt to free open block from %s:%d\n",
file, line);
abortAll(-1);
}
void heapFreeOrderError(const char* file, int line)
{
printf("Scratch Heap ERROR:\n"
" Attempt to free blocks out of order %s:%d\n",
file, line);
abortAll(-1);
}
void heapAlreadyAllocatedWarning(void)
{
}
void heapAlreadyClosedError(const char* file, int line)
{
printf("Scratch Heap ERROR:\n"
" Attempt to call heapEndBlock on a block that is already closed\n"
" %s:%d\n",
file, line);
abortAll(-1);
}
/* Local Variables: */
/* tab-width: 3 */
/* End: */