| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
#define glulx_malloc malloc |
| 7 |
#define glulx_free free |
| 8 |
|
| 9 |
#ifndef TRUE |
| 10 |
#define TRUE 1 |
| 11 |
#endif |
| 12 |
#ifndef FALSE |
| 13 |
#define FALSE 0 |
| 14 |
#endif |
| 15 |
|
| 16 |
#include "glk.h" |
| 17 |
#include "git.h" |
| 18 |
|
| 19 |
typedef struct heapblock_struct { |
| 20 |
glui32 addr; |
| 21 |
glui32 len; |
| 22 |
int isfree; |
| 23 |
struct heapblock_struct *next; |
| 24 |
struct heapblock_struct *prev; |
| 25 |
} heapblock_t; |
| 26 |
|
| 27 |
static glui32 heap_start = 0; |
| 28 |
static int alloc_count = 0; |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
static heapblock_t *heap_head = NULL; |
| 46 |
static heapblock_t *heap_tail = NULL; |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
void heap_clear() |
| 53 |
{ |
| 54 |
while (heap_head) { |
| 55 |
heapblock_t *blo = heap_head; |
| 56 |
heap_head = blo->next; |
| 57 |
blo->next = NULL; |
| 58 |
blo->prev = NULL; |
| 59 |
glulx_free(blo); |
| 60 |
} |
| 61 |
heap_tail = NULL; |
| 62 |
|
| 63 |
if (heap_start) { |
| 64 |
glui32 res = resizeMemory(heap_start, 1); |
| 65 |
if (res) |
| 66 |
fatalError("Unable to revert memory size when deactivating heap."); |
| 67 |
} |
| 68 |
|
| 69 |
heap_start = 0; |
| 70 |
alloc_count = 0; |
| 71 |
|
| 72 |
} |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
int heap_is_active() { |
| 78 |
return (heap_start != 0); |
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
glui32 heap_get_start() { |
| 85 |
return heap_start; |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
glui32 heap_alloc(glui32 len) |
| 93 |
{ |
| 94 |
heapblock_t *blo, *newblo; |
| 95 |
|
| 96 |
if (len <= 0) |
| 97 |
fatalError("Heap allocation length must be positive."); |
| 98 |
|
| 99 |
blo = heap_head; |
| 100 |
while (blo) { |
| 101 |
if (blo->isfree && blo->len >= len) |
| 102 |
break; |
| 103 |
|
| 104 |
if (!blo->isfree) { |
| 105 |
blo = blo->next; |
| 106 |
continue; |
| 107 |
} |
| 108 |
|
| 109 |
if (!blo->next || !blo->next->isfree) { |
| 110 |
blo = blo->next; |
| 111 |
continue; |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
newblo = blo->next; |
| 118 |
blo->len += newblo->len; |
| 119 |
if (newblo->next) { |
| 120 |
blo->next = newblo->next; |
| 121 |
newblo->next->prev = blo; |
| 122 |
} |
| 123 |
else { |
| 124 |
blo->next = NULL; |
| 125 |
heap_tail = blo; |
| 126 |
} |
| 127 |
newblo->next = NULL; |
| 128 |
newblo->prev = NULL; |
| 129 |
glulx_free(newblo); |
| 130 |
newblo = NULL; |
| 131 |
continue; |
| 132 |
} |
| 133 |
|
| 134 |
if (!blo) { |
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
glui32 res; |
| 139 |
glui32 extension; |
| 140 |
glui32 oldendmem = gEndMem; |
| 141 |
|
| 142 |
extension = 0; |
| 143 |
if (heap_start) |
| 144 |
extension = gEndMem - heap_start; |
| 145 |
if (extension < len) |
| 146 |
extension = len; |
| 147 |
if (extension < 256) |
| 148 |
extension = 256; |
| 149 |
|
| 150 |
extension = (extension + 0xFF) & (~(glui32)0xFF); |
| 151 |
|
| 152 |
res = resizeMemory(gEndMem+extension, 1); |
| 153 |
if (res) |
| 154 |
return 0; |
| 155 |
|
| 156 |
|
| 157 |
if (heap_start == 0) |
| 158 |
heap_start = oldendmem; |
| 159 |
|
| 160 |
if (heap_tail && heap_tail->isfree) { |
| 161 |
|
| 162 |
blo = heap_tail; |
| 163 |
blo->len += extension; |
| 164 |
} |
| 165 |
else { |
| 166 |
|
| 167 |
newblo = glulx_malloc(sizeof(heapblock_t)); |
| 168 |
if (!newblo) |
| 169 |
fatalError("Unable to allocate record for heap block."); |
| 170 |
newblo->addr = oldendmem; |
| 171 |
newblo->len = extension; |
| 172 |
newblo->isfree = TRUE; |
| 173 |
newblo->next = NULL; |
| 174 |
newblo->prev = NULL; |
| 175 |
|
| 176 |
if (!heap_tail) { |
| 177 |
heap_head = newblo; |
| 178 |
heap_tail = newblo; |
| 179 |
} |
| 180 |
else { |
| 181 |
blo = heap_tail; |
| 182 |
heap_tail = newblo; |
| 183 |
blo->next = newblo; |
| 184 |
newblo->prev = blo; |
| 185 |
} |
| 186 |
|
| 187 |
blo = newblo; |
| 188 |
newblo = NULL; |
| 189 |
} |
| 190 |
|
| 191 |
|
| 192 |
} |
| 193 |
|
| 194 |
|
| 195 |
if (!blo || !blo->isfree || blo->len < len) |
| 196 |
return 0; |
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
if (blo->len == len) { |
| 201 |
blo->isfree = FALSE; |
| 202 |
} |
| 203 |
else { |
| 204 |
newblo = glulx_malloc(sizeof(heapblock_t)); |
| 205 |
if (!newblo) |
| 206 |
fatalError("Unable to allocate record for heap block."); |
| 207 |
newblo->isfree = TRUE; |
| 208 |
newblo->addr = blo->addr + len; |
| 209 |
newblo->len = blo->len - len; |
| 210 |
blo->len = len; |
| 211 |
blo->isfree = FALSE; |
| 212 |
newblo->next = blo->next; |
| 213 |
if (newblo->next) |
| 214 |
newblo->next->prev = newblo; |
| 215 |
newblo->prev = blo; |
| 216 |
blo->next = newblo; |
| 217 |
if (heap_tail == blo) |
| 218 |
heap_tail = newblo; |
| 219 |
} |
| 220 |
|
| 221 |
alloc_count++; |
| 222 |
|
| 223 |
return blo->addr; |
| 224 |
} |
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
void heap_free(glui32 addr) |
| 230 |
{ |
| 231 |
heapblock_t *blo; |
| 232 |
|
| 233 |
for (blo = heap_head; blo; blo = blo->next) { |
| 234 |
if (blo->addr == addr) |
| 235 |
break; |
| 236 |
}; |
| 237 |
if (!blo || blo->isfree) |
| 238 |
fatalError("Attempt to free unallocated address from heap."); |
| 239 |
|
| 240 |
blo->isfree = TRUE; |
| 241 |
alloc_count--; |
| 242 |
if (alloc_count <= 0) { |
| 243 |
heap_clear(); |
| 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 |
int heap_get_summary(glui32 *valcount, glui32 **summary) |
| 269 |
{ |
| 270 |
glui32 *arr, len, pos; |
| 271 |
heapblock_t *blo; |
| 272 |
|
| 273 |
*valcount = 0; |
| 274 |
*summary = NULL; |
| 275 |
|
| 276 |
if (heap_start == 0) |
| 277 |
return 0; |
| 278 |
|
| 279 |
len = 2 + 2*alloc_count; |
| 280 |
arr = glulx_malloc(len * sizeof(glui32)); |
| 281 |
if (!arr) |
| 282 |
return 1; |
| 283 |
|
| 284 |
pos = 0; |
| 285 |
arr[pos++] = heap_start; |
| 286 |
arr[pos++] = alloc_count; |
| 287 |
|
| 288 |
for (blo = heap_head; blo; blo = blo->next) { |
| 289 |
if (blo->isfree) |
| 290 |
continue; |
| 291 |
arr[pos++] = blo->addr; |
| 292 |
arr[pos++] = blo->len; |
| 293 |
} |
| 294 |
|
| 295 |
if (pos != len) |
| 296 |
fatalError("Wrong number of active blocks in heap"); |
| 297 |
|
| 298 |
*valcount = len; |
| 299 |
*summary = arr; |
| 300 |
return 0; |
| 301 |
} |
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
int heap_apply_summary(glui32 valcount, glui32 *summary) |
| 313 |
{ |
| 314 |
glui32 lx, jx, lastend; |
| 315 |
|
| 316 |
if (heap_start) |
| 317 |
fatalError("Heap active when heap_apply_summary called"); |
| 318 |
|
| 319 |
if (valcount == 0 || summary == NULL) |
| 320 |
return 0; |
| 321 |
if (valcount == 2 && summary[0] == 0 && summary[1] == 0) |
| 322 |
return 0; |
| 323 |
|
| 324 |
lx = 0; |
| 325 |
heap_start = summary[lx++]; |
| 326 |
alloc_count = summary[lx++]; |
| 327 |
|
| 328 |
for (jx=lx; jx+2<valcount; jx+=2) { |
| 329 |
if (summary[jx] >= summary[jx+2]) |
| 330 |
fatalError("Heap block summary is out of order."); |
| 331 |
} |
| 332 |
|
| 333 |
lastend = heap_start; |
| 334 |
|
| 335 |
while (lx < valcount || lastend < gEndMem) { |
| 336 |
heapblock_t *blo; |
| 337 |
|
| 338 |
blo = glulx_malloc(sizeof(heapblock_t)); |
| 339 |
if (!blo) |
| 340 |
fatalError("Unable to allocate record for heap block."); |
| 341 |
|
| 342 |
if (lx >= valcount) { |
| 343 |
blo->addr = lastend; |
| 344 |
blo->len = gEndMem - lastend; |
| 345 |
blo->isfree = TRUE; |
| 346 |
} |
| 347 |
else { |
| 348 |
if (lastend < summary[lx]) { |
| 349 |
blo->addr = lastend; |
| 350 |
blo->len = summary[lx] - lastend; |
| 351 |
blo->isfree = TRUE; |
| 352 |
} |
| 353 |
else { |
| 354 |
blo->addr = summary[lx++]; |
| 355 |
blo->len = summary[lx++]; |
| 356 |
blo->isfree = FALSE; |
| 357 |
} |
| 358 |
} |
| 359 |
|
| 360 |
blo->prev = NULL; |
| 361 |
blo->next = NULL; |
| 362 |
|
| 363 |
if (!heap_head) { |
| 364 |
heap_head = blo; |
| 365 |
heap_tail = blo; |
| 366 |
} |
| 367 |
else { |
| 368 |
heap_tail->next = blo; |
| 369 |
blo->prev = heap_tail; |
| 370 |
heap_tail = blo; |
| 371 |
} |
| 372 |
|
| 373 |
lastend = blo->addr + blo->len; |
| 374 |
} |
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
return 0; |
| 379 |
} |
| 380 |
|