-
-
Notifications
You must be signed in to change notification settings - Fork 45
/
heap.c
250 lines (222 loc) · 6.32 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
/*
* system specific memory routines
*
* Copyright 2020 by Gray Watson
*
* This file is part of the dmalloc package.
*
* Permission to use, copy, modify, and distribute this software for
* any purpose and without fee is hereby granted, provided that the
* above copyright notice and this permission notice appear in all
* copies, and that the name of Gray Watson not be used in advertising
* or publicity pertaining to distribution of the document or software
* without specific, written prior permission.
*
* Gray Watson makes no representations about the suitability of the
* software described herein for any purpose. It is provided "as is"
* without express or implied warranty.
*
* The author may be contacted via https://dmalloc.com/
*/
/*
* These are the system/machine specific routines for allocating space on the
* heap as well as reporting the current position of the heap.
*/
#if HAVE_SYS_TYPES_H
# include <sys/types.h>
#endif
#if HAVE_SYS_MMAN_H
# include <sys/mman.h> /* for mmap stuff */
#endif
#define DMALLOC_DISABLE
#include "conf.h"
#include "dmalloc.h"
#include "append.h"
#include "chunk.h"
#include "compat.h"
#include "debug_tok.h"
#include "error.h"
#include "error_val.h"
#include "heap.h"
#include "dmalloc_loc.h"
#define SBRK_ERROR ((char *)-1) /* sbrk error code */
/* exported variables */
void *_dmalloc_heap_low = NULL; /* base of our heap */
void *_dmalloc_heap_high = NULL; /* end of our heap */
/****************************** local functions ******************************/
/*
* static void *heap_extend
*
* Get more bytes from the system functions. Returns a valid pointer or NULL on error.
*
* ARGUMENTS:
*
* incr -> Number of bytes we need.
*/
static void *heap_extend(const int incr)
{
void *ret = SBRK_ERROR;
char *high;
#if INTERNAL_MEMORY_SPACE
{
static char block_o_bytes[INTERNAL_MEMORY_SPACE];
static char *bounds_p = block_o_bytes + sizeof(block_o_bytes);
static char *block_p = block_o_bytes;
if (block_p + incr > bounds_p) {
ret = SBRK_ERROR;
}
else {
ret = block_p;
block_p += incr;
}
}
#else
#if HAVE_MMAP && USE_MMAP
#if MAP_ANON
/* if we have and can use mmap, then do so */
ret = mmap(0L, incr, PROT_READ | PROT_WRITE | PROT_EXEC,
MAP_PRIVATE | MAP_ANON, -1 /* no fd */, 0 /* no offset */);
#else
#endif
if (ret == MAP_FAILED) {
ret = SBRK_ERROR;
}
#else
#if HAVE_SBRK
ret = sbrk(incr);
#endif /* if HAVE_SBRK */
#endif /* if not HAVE_MMAP && USE_MMAP */
#endif /* if not INTERNAL_MEMORY_SPACE */
if (ret == SBRK_ERROR) {
if (BIT_IS_SET(_dmalloc_flags, DMALLOC_DEBUG_CATCH_NULL)) {
loc_dprintf(STDERR,
"\r\ndmalloc: critical error: could not extend heap %u more bytes\r\n", incr);
_dmalloc_die(0);
}
dmalloc_errno = DMALLOC_ERROR_ALLOC_FAILED;
dmalloc_error("heap_extend");
}
if (_dmalloc_heap_low == NULL || (char *)ret < (char *)_dmalloc_heap_low) {
_dmalloc_heap_low = ret;
}
high = (char *)ret + incr;
if (high > (char *)_dmalloc_heap_high) {
_dmalloc_heap_high = high;
}
if (BIT_IS_SET(_dmalloc_flags, DMALLOC_DEBUG_LOG_ADMIN)) {
dmalloc_message("extended heap space by %d bytes returned %p [%p, %p]",
incr, ret, _dmalloc_heap_low, _dmalloc_heap_high);
}
return ret;
}
/*
* static void heap_release
*
* Release a memory chunk back to the sytem.
*
* ARGUMENTS:
*
* addr -> Previously used memory.
* size -> Size of memory.
*/
static void heap_release(void *addr, const int size)
{
#if INTERNAL_MEMORY_SPACE
/* no-op */
#else
#if HAVE_MUNMAP && USE_MMAP
if (munmap(addr, size) == 0) {
if (BIT_IS_SET(_dmalloc_flags, DMALLOC_DEBUG_LOG_ADMIN)) {
dmalloc_message("releasing heap memory %p, size %d", addr, size);
}
} else {
dmalloc_message("munmap failed to release heap memory %p, size %d",
addr, size);
}
#else
/* no-op */
#endif /* if not HAVE_MMAP && USE_MMAP */
#endif /* if not INTERNAL_MEMORY_SPACE */
}
/**************************** exported functions *****************************/
/*
* int _heap_startup
*
* Initialize heap pointers.
*
* Returns 1 on success or 0 on failure.
*/
int _dmalloc_heap_startup(void)
{
return 1;
}
/*
* void *_dmalloc_heap_alloc
*
* Function to get memory bytes from the heap.
*
* Returns a valid pointer on success or NULL on failure.
*
* ARGUMENTS:
*
* size -> Number of bytes we need.
*/
void *_dmalloc_heap_alloc(const unsigned int size)
{
void *heap_new, *heap_diff;
long diff_size;
if (size == 0) {
dmalloc_errno = DMALLOC_ERROR_BAD_SIZE;
dmalloc_error("_dmalloc_heap_alloc");
return HEAP_ALLOC_ERROR;
}
/* extend the heap by our size */
heap_new = heap_extend(size);
if (heap_new == SBRK_ERROR) {
return HEAP_ALLOC_ERROR;
}
/* calculate bytes needed to align to block boundary */
diff_size = (PNT_ARITH_TYPE)heap_new % BLOCK_SIZE;
if (diff_size == 0) {
/* if we are already aligned then we are all set */
return heap_new;
}
diff_size = BLOCK_SIZE - diff_size;
/* shift the heap a bit to account for non block alignment */
heap_diff = heap_extend(diff_size);
if (heap_diff == SBRK_ERROR) {
heap_release(heap_new, size);
return HEAP_ALLOC_ERROR;
}
/* if heap-diff went down then probably using sbrk and stack grows down */
if ((char *)heap_diff + diff_size == (char *)heap_new) {
return heap_diff;
}
else if ((char *)heap_new + size == (char *)heap_diff) {
/* shift up our heap to align with the block */
return (char *)heap_new + diff_size;
}
/*
* We got non-contiguous memory regions. This may indicate that
* mmap does not return page-size chunks or that we didn't determine
* the page size right. In any case we do the unfortunate thing to
* allocate size + BLOCK_SIZE and then we are guaranteed a page-size
* aligned chunk in there while wasting a page-size of extra memory.
* Blah.
*/
heap_release(heap_new, size);
heap_release(heap_diff, diff_size);
int new_size = size + BLOCK_SIZE;
heap_new = heap_extend(new_size);
if (heap_new == SBRK_ERROR) {
return HEAP_ALLOC_ERROR;
}
dmalloc_message("WARNING: had to extend heap by %d more bytes to get page aligned %p",
new_size, heap_new);
diff_size = (PNT_ARITH_TYPE)heap_new % BLOCK_SIZE;
if (diff_size == 0) {
return heap_new;
} else {
return heap_new + diff_size;
}
}