-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathgemdosmem.c
259 lines (208 loc) · 6.48 KB
/
gemdosmem.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
/*
* TOSEMU - an emulated environment for TOS applications
* Copyright (C) 2014 Johan Thelin <[email protected]>
*
* This program 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
* of the License, or (at your option) any later version.
*
* This program 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 this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
#include "gemdosmem_p.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "cpu.h"
#include "gemdos_p.h"
/* Memory management functions ***********************************************/
/* The memory areas are stored in a sorted order by base */
struct mem_area;
struct mem_area {
uint32_t base, len;
struct mem_area *next;
};
struct mem_area *mem_list;
static uint32_t mem_allocatable_top;
static struct mem_area * find_mem_area(uint32_t base, struct mem_area **prevptr)
{
struct mem_area *ptr = mem_list;
if (prevptr)
*prevptr = 0;
while (ptr)
{
if (ptr->base == base)
break;
if (prevptr)
*prevptr = ptr;
ptr = ptr->next;
}
return ptr;
}
uint32_t GEMDOS_Mshrink()
{
struct mem_area *ma;
uint32_t newsiz = peek_u32(8);
uint32_t block = peek_u32(4);
FUNC_TRACE_ENTER_ARGS {
printf(" ns: 0x%x, b: 0x%x\n", newsiz, block);
}
ma = find_mem_area(block, 0);
if (!ma)
return GEMDOS_EIMBA;
if (ma->len < newsiz)
return GEMDOS_EGSBF;
ma->len = newsiz;
return 0;
}
uint32_t GEMDOS_Malloc()
{
/* This is the tricky mem function, stay safe if changing it.
*
* - Managed memory starts from 0x800 to mem_allocatable_top.
* - Memory between 0x800 - 0x900 cannot be deallocated [1].
* - Memory can be allocated from 0x900 to the end of managed memory.
* - mem_area structures are sorted by base address and never overlap.
*
* [1] Mfree and Mshrink does not care.
*
* The algorithm works like this:
*
* - Iterate current mem_area structures, look for gaps of the right size.
* - If no gaps, look for space at the end of allocated memory.
* - When memory is found:
* - Create a new mem_area
* - Insert in the correct place in the mem_list linked mem_list
* - Return new base
* - Else:
* - Return error
*
* TODO room for improvement, to avoid fragmentation, use the smallest gap
* suitable when allocating, right now the first suitable gap is used.
*/
struct mem_area *prev, *ptr, *n;
uint32_t prev_top, max_free;
int32_t newsiz = peek_s32(2);
if (newsiz == -1)
{
/* Simply locate largest gap */
max_free = 0;
prev = mem_list;
if (prev)
ptr = mem_list->next;
else
ptr = 0;
while (ptr)
{
prev_top = prev->base + prev->len;
if (max_free < ptr->base - prev_top)
max_free = ptr->base - prev_top;
prev = ptr;
ptr = ptr->next;
}
/* Look at the gap at the end */
if (prev)
prev_top = prev->base + prev->len;
else
prev_top = 0x900;
if (max_free < mem_allocatable_top - prev_top)
max_free = mem_allocatable_top - prev_top;
return max_free;
}
else
{
prev = mem_list;
if (prev)
ptr = mem_list->next;
else
ptr = 0;
while (ptr)
{
prev_top = prev->base + prev->len;
if (ptr->base - prev_top > newsiz)
{
/* Large enough gap found */
/* Allocate new area */
n = malloc(sizeof(struct mem_area));
memset(n, 0, sizeof(struct mem_area));
/* Set base and len */
n->base = prev_top;
n->len = newsiz;
/* Insert into list */
n->next = ptr;
prev->next = n;
/* Return new base */
return n->base;
}
prev = ptr;
ptr = ptr->next;
}
if (prev)
prev_top = prev->base + prev->len;
else
prev_top = 0x900;
if (newsiz < mem_allocatable_top - prev_top)
{
/* Large enough gap found at the end (which can be the start) */
/* Allocate new area */
n = malloc(sizeof(struct mem_area));
memset(n, 0, sizeof(struct mem_area));
/* Set base and len */
n->base = prev_top;
n->len = newsiz;
/* Insert into list */
if (prev)
prev->next = n;
else
mem_list = n;
/* Return new base */
return n->base;
}
return 0; /* NULL pointer, indicating no new memory allocated */
}
}
uint32_t GEMDOS_Mfree()
{
struct mem_area *ma, *prev;
uint32_t block = peek_u32(2);
FUNC_TRACE_ENTER_ARGS {
printf(" 0x%x\n", block);
}
ma = find_mem_area(block, &prev);
if (!ma)
return GEMDOS_EIMBA;
if (prev)
prev->next = ma->next;
else
mem_list = ma->next;
free(ma);
return 0;
}
void gemdos_mem_init(struct tos_environment *te)
{
struct mem_area *ma = malloc(sizeof(struct mem_area));
memset(ma, 0, sizeof(struct mem_area));
/* The initial area is by convention and relates to the binary loading and
* base page setup from tossystem */
ma->base = 0x800;
ma->len = te->size + 0x100; /* Size + basepage */
mem_allocatable_top = ma->len;
mem_list = ma;
}
void gemdos_mem_free()
{
while (mem_list)
{
struct mem_area *n = mem_list->next;
free(mem_list);
mem_list = n;
}
}