-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathheap.c
301 lines (280 loc) · 8.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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
/*
** This file is part of the Matrix Brandy Basic VI Interpreter.
** Copyright (C) 2000-2014 David Daniels
** Copyright (C) 2018-2024 Michael McConnell and contributors
**
** Brandy is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2, or (at your option)
** any later version.
**
** Brandy is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with Brandy; see the file COPYING. If not, write to
** the Free Software Foundation, 59 Temple Place - Suite 330,
** Boston, MA 02111-1307, USA.
**
**
** This file defines the functions and so forth associated with memory
** manangement. It also contains the code to deal with memory allocation
** for strings
*/
/* Linux memory allocator based on Richard Russell's allocator
** from BBCSDL as it aims to get memory with low addresses, and within
** the first 1TB on 64-bit hardware.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "common.h"
#include "target.h"
#include "heap.h"
#include "basicdefs.h"
#include "errors.h"
#include "miscprocs.h"
#ifdef TARGET_LINUX
#ifndef __USE_LARGEFILE64
#define __USE_LARGEFILE64
#endif
#include <sys/mman.h>
#endif
#ifdef TARGET_RISCOS
#include "kernel.h"
#include "swis.h"
#endif
#if defined(TARGET_LINUX) && defined(__LP64__)
static void *mymap (size_t size)
{
FILE *fp;
char line[256] ;
void *start, *finish, *base = (void *) 0x400000 ;
DEBUGFUNCMSGIN;
fp = fopen ("/proc/self/maps", "r") ;
if (fp == NULL) {
DEBUGFUNCMSGOUT;
return NULL ;
}
while (NULL != fgets (line, 256, fp)) {
sscanf (line, "%p-%p", &start, &finish) ;
start = (void *)((size_t)start & -0x1000) ; // page align (GCC extension)
if (start >= (base + size)) {
fclose(fp);
DEBUGFUNCMSGOUT;
return base ;
}
if (finish > (void *)0xFFFFF000) {
fclose(fp);
DEBUGFUNCMSGOUT;
return NULL ;
}
base = (void *)(((size_t)finish + 0xFFF) & -0x1000) ; // page align
if (base > ((void *)0xFFFFFFFF - size)) {
fclose(fp);
DEBUGFUNCMSGOUT;
return NULL ;
}
}
fclose(fp);
DEBUGFUNCMSGOUT;
return base ;
}
#endif
/*
** 'init_heap' is called when the interpreter starts to initialise the
** heap
*/
boolean init_heap(void) {
basicvars.stringwork = malloc(MAXSTRING+4);
return basicvars.stringwork!=NIL;
}
/*
** 'init_workspace' is called to obtain the memory used to hold the Basic
** program. 'heapsize' gives the size of block. If zero, the size of the
** area used is the implementation-defined default. If returns 'true' if
** if the heap space could be allocated or 'false' if it failed
*/
boolean init_workspace(size_t heapsize) {
byte *wp = NULL;
#if defined(TARGET_LINUX) && defined(__LP64__)
void *base = NULL;
uint32 heaporig;
#endif
DEBUGFUNCMSGIN;
basicvars.misc_flags.usedmmap = 0;
if (heapsize==0)
heapsize = DEFAULTSIZE;
else if (heapsize<MINSIZE)
heapsize = MINSIZE;
else if (heapsize>0xFFFFFC00ull)
heapsize = 0xFFFFFC00ull;
else {
heapsize = ALIGN(heapsize);
}
#if defined(TARGET_LINUX) && defined(__LP64__)
heaporig = heapsize;
basicvars.misc_flags.usedmmap = 1;
#ifdef DEBUG
# ifdef MATRIX64BIT
fprintf(stderr, "heap.c:init_workspace: Requested heapsize is %ld (&%lX)\n", heapsize, heapsize);
# else
fprintf(stderr, "heap.c:init_workspace: Requested heapsize is %d (&%X)\n", heapsize, heapsize);
# endif
#endif
base = mymap(heapsize);
if (base != NULL) {
#ifdef DEBUG
# ifdef MATRIX64BIT
fprintf(stderr, "heap.c:init_workspace: Allocating at %p, size &%lX\n", base, heapsize);
# else
fprintf(stderr, "heap.c:init_workspace: Allocating at %p, size &%X\n", base, heapsize);
# endif
#endif
wp = mmap64(base, heapsize, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0) ;
#ifdef DEBUG
fprintf(stderr, "heap.c:init_workspace: mmap returns %p\n", wp);
#endif
if ((size_t)wp == -1) {
heapsize=heaporig;
wp=malloc(heapsize);
#ifdef DEBUG
fprintf(stderr, "heap.c:init_workspace: Fallback, malloc returns %p\n", wp);
#endif
basicvars.misc_flags.usedmmap = 0;
}
} else {
/* Trying to allocate via mmap didn't work, let's try malloc instead */
wp=malloc(heapsize);
basicvars.misc_flags.usedmmap = 0;
}
#else
wp = malloc(heapsize);
#endif
if (wp==NIL) {
heapsize = 0; /* Could not obtain block of requested size */
return 0;
}
basicvars.worksize = heapsize;
basicvars.workspace = wp;
basicvars.slotend = basicvars.end = basicvars.himem = wp+basicvars.worksize;
basicvars.memory = 0; /* Use as a byte array to access arbitrary points of memory */
basicvars.page = wp;
basicvars.memdump_lastaddr = (size_t)wp;
/* Under RISC OS, find out the address of the end of wimp slot */
#ifdef TARGET_RISCOS
{
_kernel_oserror *oserror;
_kernel_swi_regs regs;
oserror = _kernel_swi(OS_GetEnv, ®s, ®s);
/* OS_GetEnv returns the end of the wimp slot in R1 */
if (oserror==NIL) basicvars.slotend = CAST(regs.r[1], byte *);
}
#endif
DEBUGFUNCMSGOUT;
return wp!=NIL;
}
/*
** 'release_workspace' is called to return the Basic workspace to the operating
** system. It is used either when the program finishes or when the size of
** the workspace is being altered
*/
void release_workspace(void) {
if (basicvars.workspace!=NIL) {
#if defined(TARGET_LINUX) && defined(__LP64__)
if (basicvars.misc_flags.usedmmap)
munmap(basicvars.workspace, basicvars.worksize);
else
#endif
free(basicvars.workspace);
basicvars.workspace = NIL;
basicvars.worksize = 0;
}
}
/*
** 'release_heap' is called at the end of the interpreter's run to
** return all memory to the OS
*/
void release_heap(void) {
library *lp, *lp2;
DEBUGFUNCMSGIN;
lp = basicvars.installist; /* Free memory acquired for installed libraries */
while (lp!=NIL) {
lp2 = lp->libflink;
free(lp->libname);
free(lp);
lp = lp2;
}
release_workspace();
free(basicvars.stringwork);
if (basicvars.loadpath!=NIL) free(basicvars.loadpath);
DEBUGFUNCMSGOUT;
}
/*
** 'allocmem' is called to allocate space for variables, arrays, strings
** and so forth. The memory between 'lomem' and 'stacklimit' is available
** for this. The second parameter is a flag, set to 1 for traditional
** allocmem behaviour of reporting an error, or 0 for old condalloc
** behaviour of returning NIL upon an error to allow the calling function
** to deal with the error.
*/
void *allocmem(size_t size, boolean reporterror) {
byte *newlimit;
DEBUGFUNCMSGIN;
size = ALIGN(size);
newlimit = basicvars.stacklimit.bytesp+size;
if (newlimit>=basicvars.stacktop.bytesp) { /* Have run out of memory */
if (reporterror) {
DEBUGFUNCMSGOUT;
error(ERR_NOROOM);
return NIL;
} else {
DEBUGFUNCMSGOUT;
return NIL;
}
}
basicvars.stacklimit.bytesp = newlimit;
newlimit = basicvars.vartop;
basicvars.vartop+=size;
DEBUGFUNCMSGOUT;
return newlimit;
}
/*
** 'freemem' is called to return memory to the heap.
** Note that this version of the code can only reclaim the memory if it
** was the last item allocated. It does not deal with returning memory
** to the middle of the heap. 'returnable' should be called to ensure
** that the memory can be returned
*/
void freemem(void *where, int32 size) {
DEBUGFUNCMSGIN;
basicvars.vartop-=size;
basicvars.stacklimit.bytesp-=size;
DEBUGFUNCMSGOUT;
}
/*
** 'returnable' is called to check if the block at 'where' is the
** last item allocated on the heap and can therefore be returned
** to it
*/
#if 0 /* converted to macro in heap.h */
boolean returnable(void *where, int32 size) {
DEBUGFUNCMSGIN;
size = ALIGN(size);
DEBUGFUNCMSGOUT;
return CAST(where, byte *)+size==basicvars.vartop;
}
#endif
/*
** 'clear_heap' is used to clear the variable and free string lists
** when a 'clear' command is used, a program is edited or 'new' or
** 'old' are issued.
*/
void clear_heap(void) {
DEBUGFUNCMSGIN;
basicvars.vartop = basicvars.lomem;
basicvars.stacklimit.bytesp = basicvars.lomem+STACKBUFFER;
DEBUGFUNCMSGOUT;
}