| 1 |
/* |
| 2 |
* Ikarus Scheme -- A compiler for R6RS Scheme. |
| 3 |
* Copyright (C) 2006,2007,2008 Abdulaziz Ghuloum |
| 4 |
* |
| 5 |
* This program is free software: you can redistribute it and/or modify |
| 6 |
* it under the terms of the GNU General Public License version 3 as |
| 7 |
* published by the Free Software Foundation. |
| 8 |
* |
| 9 |
* This program is distributed in the hope that it will be useful, but |
| 10 |
* WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 |
* General Public License for more details. |
| 13 |
* |
| 14 |
* You should have received a copy of the GNU General Public License |
| 15 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 |
*/ |
| 17 |
|
| 18 |
|
| 19 |
#include "ikarus-data.h" |
| 20 |
#include <stdlib.h> |
| 21 |
#include <stdio.h> |
| 22 |
#include <stdint.h> |
| 23 |
#include <unistd.h> |
| 24 |
#include <string.h> |
| 25 |
#include <strings.h> |
| 26 |
#include <sys/mman.h> |
| 27 |
#include <sys/types.h> |
| 28 |
#include <sys/time.h> |
| 29 |
#include <assert.h> |
| 30 |
#include <errno.h> |
| 31 |
#include <sys/time.h> |
| 32 |
|
| 33 |
#define forward_ptr ((ikptr)-1) |
| 34 |
#define minimum_heap_size (pagesize * 1024 * 4) |
| 35 |
#define maximum_heap_size (pagesize * 1024 * 8) |
| 36 |
#define minimum_stack_size (pagesize * 128) |
| 37 |
|
| 38 |
#define accounting 0 |
| 39 |
|
| 40 |
#if accounting |
| 41 |
static int pair_count = 0; |
| 42 |
static int symbol_count = 0; |
| 43 |
static int closure_count = 0; |
| 44 |
static int vector_count = 0; |
| 45 |
static int record_count = 0; |
| 46 |
static int continuation_count = 0; |
| 47 |
static int string_count = 0; |
| 48 |
static int htable_count = 0; |
| 49 |
#endif |
| 50 |
|
| 51 |
typedef struct qupages_t{ |
| 52 |
ikptr p; /* pointer to the scan start */ |
| 53 |
ikptr q; /* pointer to the scan end */ |
| 54 |
struct qupages_t* next; |
| 55 |
} qupages_t; |
| 56 |
|
| 57 |
|
| 58 |
typedef struct{ |
| 59 |
ikptr ap; |
| 60 |
ikptr aq; |
| 61 |
ikptr ep; |
| 62 |
ikptr base; |
| 63 |
} meta_t; |
| 64 |
|
| 65 |
|
| 66 |
#define meta_ptrs 0 |
| 67 |
#define meta_code 1 |
| 68 |
#define meta_data 2 |
| 69 |
#define meta_weak 3 |
| 70 |
#define meta_pair 4 |
| 71 |
#define meta_symbol 5 |
| 72 |
#define meta_count 6 |
| 73 |
|
| 74 |
static int extension_amount[meta_count] = { |
| 75 |
1 * pagesize, |
| 76 |
1 * pagesize, |
| 77 |
1 * pagesize, |
| 78 |
1 * pagesize, |
| 79 |
1 * pagesize, |
| 80 |
1 * pagesize, |
| 81 |
}; |
| 82 |
|
| 83 |
static unsigned int meta_mt[meta_count] = { |
| 84 |
pointers_mt, |
| 85 |
code_mt, |
| 86 |
data_mt, |
| 87 |
weak_pairs_mt, |
| 88 |
pointers_mt, |
| 89 |
symbols_mt |
| 90 |
}; |
| 91 |
|
| 92 |
typedef struct gc_t{ |
| 93 |
meta_t meta [meta_count]; |
| 94 |
qupages_t* queues [meta_count]; |
| 95 |
ikpcb* pcb; |
| 96 |
unsigned int* segment_vector; |
| 97 |
int collect_gen; |
| 98 |
int collect_gen_tag; |
| 99 |
ikptr tconc_ap; |
| 100 |
ikptr tconc_ep; |
| 101 |
ikptr tconc_base; |
| 102 |
ikpages* tconc_queue; |
| 103 |
ik_ptr_page* forward_list; |
| 104 |
} gc_t; |
| 105 |
|
| 106 |
static void handle_guardians(gc_t* gc); |
| 107 |
static void gc_finalize_guardians(gc_t* gc); |
| 108 |
|
| 109 |
static unsigned int |
| 110 |
next_gen_tag[generation_count] = { |
| 111 |
(4 << meta_dirty_shift) | 1 | new_gen_tag, |
| 112 |
(2 << meta_dirty_shift) | 2 | new_gen_tag, |
| 113 |
(1 << meta_dirty_shift) | 3 | new_gen_tag, |
| 114 |
(0 << meta_dirty_shift) | 4 | new_gen_tag, |
| 115 |
(0 << meta_dirty_shift) | 4 | new_gen_tag |
| 116 |
}; |
| 117 |
|
| 118 |
static ikptr |
| 119 |
meta_alloc_extending(long int size, gc_t* gc, int meta_id){ |
| 120 |
long int mapsize = align_to_next_page(size); |
| 121 |
if(mapsize < extension_amount[meta_id]){ |
| 122 |
mapsize = extension_amount[meta_id]; |
| 123 |
} |
| 124 |
meta_t* meta = &gc->meta[meta_id]; |
| 125 |
if((meta_id != meta_data) && meta->base){ |
| 126 |
qupages_t* p = ik_malloc(sizeof(qupages_t)); |
| 127 |
ikptr aq = meta->aq; |
| 128 |
ikptr ap = meta->ap; |
| 129 |
ikptr ep = meta->ep; |
| 130 |
p->p = aq; |
| 131 |
p->q = ap; |
| 132 |
p->next = gc->queues[meta_id]; |
| 133 |
gc->queues[meta_id] = p; |
| 134 |
ikptr x = ap; |
| 135 |
while(x < ep){ |
| 136 |
ref(x, 0) = 0; |
| 137 |
x += wordsize; |
| 138 |
} |
| 139 |
} |
| 140 |
ikptr mem = ik_mmap_typed( |
| 141 |
mapsize, |
| 142 |
meta_mt[meta_id] | gc->collect_gen_tag, |
| 143 |
gc->pcb); |
| 144 |
gc->segment_vector = gc->pcb->segment_vector; |
| 145 |
meta->ap = mem + size; |
| 146 |
meta->aq = mem; |
| 147 |
meta->ep = mem + mapsize; |
| 148 |
meta->base = mem; |
| 149 |
return mem; |
| 150 |
} |
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
static inline ikptr |
| 156 |
meta_alloc(long int size, gc_t* gc, int meta_id){ |
| 157 |
assert(size == align(size)); |
| 158 |
meta_t* meta = &gc->meta[meta_id]; |
| 159 |
ikptr ap = meta->ap; |
| 160 |
ikptr ep = meta->ep; |
| 161 |
ikptr nap = ap + size; |
| 162 |
if(nap > ep){ |
| 163 |
return meta_alloc_extending(size, gc, meta_id); |
| 164 |
} else { |
| 165 |
meta->ap = nap; |
| 166 |
return ap; |
| 167 |
} |
| 168 |
} |
| 169 |
|
| 170 |
static inline ikptr |
| 171 |
gc_alloc_new_ptr(int size, gc_t* gc){ |
| 172 |
assert(size == align(size)); |
| 173 |
return meta_alloc(size, gc, meta_ptrs); |
| 174 |
} |
| 175 |
|
| 176 |
static inline ikptr |
| 177 |
gc_alloc_new_large_ptr(int size, gc_t* gc){ |
| 178 |
int memreq = align_to_next_page(size); |
| 179 |
ikptr mem = |
| 180 |
ik_mmap_typed(memreq, |
| 181 |
pointers_mt | large_object_tag | gc->collect_gen_tag, |
| 182 |
gc->pcb); |
| 183 |
gc->segment_vector = gc->pcb->segment_vector; |
| 184 |
qupages_t* p = ik_malloc(sizeof(qupages_t)); |
| 185 |
p->p = mem; |
| 186 |
p->q = mem+size; |
| 187 |
bzero((char*)(long)(mem+size), memreq-size); |
| 188 |
p->next = gc->queues[meta_ptrs]; |
| 189 |
gc->queues[meta_ptrs] = p; |
| 190 |
return mem; |
| 191 |
} |
| 192 |
|
| 193 |
|
| 194 |
static inline void |
| 195 |
enqueue_large_ptr(ikptr mem, int size, gc_t* gc){ |
| 196 |
long int i = page_index(mem); |
| 197 |
long int j = page_index(mem+size-1); |
| 198 |
while(i<=j){ |
| 199 |
gc->segment_vector[i] = |
| 200 |
pointers_mt | large_object_tag | gc->collect_gen_tag; |
| 201 |
i++; |
| 202 |
} |
| 203 |
qupages_t* p = ik_malloc(sizeof(qupages_t)); |
| 204 |
p->p = mem; |
| 205 |
p->q = mem+size; |
| 206 |
p->next = gc->queues[meta_ptrs]; |
| 207 |
gc->queues[meta_ptrs] = p; |
| 208 |
} |
| 209 |
|
| 210 |
|
| 211 |
static inline ikptr |
| 212 |
gc_alloc_new_symbol_record(gc_t* gc){ |
| 213 |
assert(symbol_record_size == align(symbol_record_size)); |
| 214 |
return meta_alloc(symbol_record_size, gc, meta_symbol); |
| 215 |
} |
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
static inline ikptr |
| 221 |
gc_alloc_new_pair(gc_t* gc){ |
| 222 |
return meta_alloc(pair_size, gc, meta_pair); |
| 223 |
} |
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
static inline ikptr |
| 228 |
gc_alloc_new_weak_pair(gc_t* gc){ |
| 229 |
meta_t* meta = &gc->meta[meta_weak]; |
| 230 |
ikptr ap = meta->ap; |
| 231 |
ikptr ep = meta->ep; |
| 232 |
ikptr nap = ap + pair_size; |
| 233 |
if(nap > ep){ |
| 234 |
ikptr mem = ik_mmap_typed( |
| 235 |
pagesize, |
| 236 |
meta_mt[meta_weak] | gc->collect_gen_tag, |
| 237 |
gc->pcb); |
| 238 |
gc->segment_vector = gc->pcb->segment_vector; |
| 239 |
meta->ap = mem + pair_size; |
| 240 |
meta->aq = mem; |
| 241 |
meta->ep = mem + pagesize; |
| 242 |
meta->base = mem; |
| 243 |
return mem; |
| 244 |
} else { |
| 245 |
meta->ap = nap; |
| 246 |
return ap; |
| 247 |
} |
| 248 |
} |
| 249 |
|
| 250 |
static inline ikptr |
| 251 |
gc_alloc_new_data(int size, gc_t* gc){ |
| 252 |
assert(size == align(size)); |
| 253 |
return meta_alloc(size, gc, meta_data); |
| 254 |
} |
| 255 |
|
| 256 |
static inline ikptr |
| 257 |
gc_alloc_new_code(long int size, gc_t* gc){ |
| 258 |
assert(size == align(size)); |
| 259 |
if(size < pagesize){ |
| 260 |
return meta_alloc(size, gc, meta_code); |
| 261 |
} else { |
| 262 |
long int memreq = align_to_next_page(size); |
| 263 |
ikptr mem = ik_mmap_code(memreq, gc->collect_gen, gc->pcb); |
| 264 |
gc->segment_vector = gc->pcb->segment_vector; |
| 265 |
qupages_t* p = ik_malloc(sizeof(qupages_t)); |
| 266 |
p->p = mem; |
| 267 |
p->q = mem+size; |
| 268 |
bzero((char*)(long)(mem+size), memreq-size); |
| 269 |
p->next = gc->queues[meta_code]; |
| 270 |
gc->queues[meta_code] = p; |
| 271 |
return mem; |
| 272 |
} |
| 273 |
} |
| 274 |
|
| 275 |
static void |
| 276 |
add_to_collect_count(ikpcb* pcb, int bytes){ |
| 277 |
int minor = bytes + pcb->allocation_count_minor; |
| 278 |
while(minor >= most_bytes_in_minor){ |
| 279 |
minor -= most_bytes_in_minor; |
| 280 |
pcb->allocation_count_major++; |
| 281 |
} |
| 282 |
pcb->allocation_count_minor = minor; |
| 283 |
} |
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
static void |
| 289 |
gc_tconc_push_extending(gc_t* gc, ikptr tcbucket){ |
| 290 |
if(gc->tconc_base){ |
| 291 |
ikpages* p = ik_malloc(sizeof(ikpages)); |
| 292 |
p->base = gc->tconc_base; |
| 293 |
p->size = pagesize; |
| 294 |
p->next = gc->tconc_queue; |
| 295 |
gc->tconc_queue = p; |
| 296 |
} |
| 297 |
ikptr ap = |
| 298 |
ik_mmap_typed(pagesize, |
| 299 |
meta_mt[meta_ptrs] | gc->collect_gen_tag, |
| 300 |
gc->pcb); |
| 301 |
add_to_collect_count(gc->pcb, pagesize); |
| 302 |
gc->segment_vector = gc->pcb->segment_vector; |
| 303 |
bzero((char*)(long)ap, pagesize); |
| 304 |
ikptr nap = ap + 2*wordsize; |
| 305 |
gc->tconc_base = ap; |
| 306 |
gc->tconc_ap = nap; |
| 307 |
gc->tconc_ep = ap + pagesize; |
| 308 |
ref(ap,0) = tcbucket; |
| 309 |
} |
| 310 |
|
| 311 |
|
| 312 |
static inline void |
| 313 |
gc_tconc_push(gc_t* gc, ikptr tcbucket){ |
| 314 |
ikptr ap = gc->tconc_ap; |
| 315 |
ikptr nap = ap + 2*wordsize; |
| 316 |
if(nap > gc->tconc_ep){ |
| 317 |
gc_tconc_push_extending(gc, tcbucket); |
| 318 |
} else { |
| 319 |
gc->tconc_ap = nap; |
| 320 |
ref(ap,0) = tcbucket; |
| 321 |
} |
| 322 |
} |
| 323 |
|
| 324 |
|
| 325 |
#ifndef NDEBUG |
| 326 |
static ikptr add_object_proc(gc_t* gc, ikptr x, char* caller); |
| 327 |
#define add_object(gc,x,caller) add_object_proc(gc,x,caller) |
| 328 |
#else |
| 329 |
static ikptr add_object_proc(gc_t* gc, ikptr x); |
| 330 |
#define add_object(gc,x,caller) add_object_proc(gc,x) |
| 331 |
#endif |
| 332 |
|
| 333 |
static void collect_stack(gc_t*, ikptr top, ikptr base); |
| 334 |
static void collect_locatives(gc_t*, callback_locative*); |
| 335 |
static void collect_loop(gc_t*); |
| 336 |
static void fix_weak_pointers(gc_t*); |
| 337 |
static void gc_add_tconcs(gc_t*); |
| 338 |
|
| 339 |
/* ik_collect is called from scheme under the following conditions: |
| 340 |
* 1. An attempt is made to allocate a small object and the ap is above |
| 341 |
* the red line. |
| 342 |
* 2. The current frame of the call is dead, so, upon return from ik_collect, |
| 343 |
* the caller returns to its caller. |
| 344 |
* 3. The frame-pointer of the caller to S_collect is saved at |
| 345 |
* pcb->frame_pointer. No variables are live at that frame except for |
| 346 |
* the return point (at *(pcb->frame_pointer)). |
| 347 |
* 4. S_collect must return a new ap (in pcb->allocation_pointer) that has |
| 348 |
* at least 2 pages of memory free. |
| 349 |
* 5. S_collect must also update pcb->allocaton_redline to be 2 pages below |
| 350 |
* the real end of heap. |
| 351 |
* 6. ik_collect should not move the stack. |
| 352 |
*/ |
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
ikpcb* ik_collect_vararg(int req, ikpcb* pcb){ |
| 357 |
return ik_collect(req, pcb); |
| 358 |
} |
| 359 |
|
| 360 |
static int collection_id_to_gen(int id){ |
| 361 |
if((id & 255) == 255) { return 4; } |
| 362 |
if((id & 63) == 63) { return 3; } |
| 363 |
if((id & 15) == 15) { return 2; } |
| 364 |
if((id & 3) == 3) { return 1; } |
| 365 |
return 0; |
| 366 |
} |
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
static void scan_dirty_pages(gc_t*); |
| 371 |
|
| 372 |
static void deallocate_unused_pages(gc_t*); |
| 373 |
|
| 374 |
static void fix_new_pages(gc_t* gc); |
| 375 |
|
| 376 |
extern void verify_integrity(ikpcb* pcb, char*); |
| 377 |
|
| 378 |
ikptr ik_collect_check(unsigned long int req, ikpcb* pcb){ |
| 379 |
long int bytes = ((long int)pcb->allocation_redline) - |
| 380 |
((long int)pcb->allocation_pointer); |
| 381 |
if (bytes >= req) { |
| 382 |
return true_object; |
| 383 |
} else { |
| 384 |
ik_collect(req, pcb); |
| 385 |
return false_object; |
| 386 |
} |
| 387 |
} |
| 388 |
|
| 389 |
ikpcb* |
| 390 |
ik_collect(unsigned long int mem_req, ikpcb* pcb){ |
| 391 |
#ifndef NDEBUG |
| 392 |
verify_integrity(pcb, "entry"); |
| 393 |
#endif |
| 394 |
|
| 395 |
{ /* ACCOUNTING */ |
| 396 |
long int bytes = ((long int)pcb->allocation_pointer) - |
| 397 |
((long int)pcb->heap_base); |
| 398 |
add_to_collect_count(pcb, bytes); |
| 399 |
} |
| 400 |
|
| 401 |
struct rusage t0, t1; |
| 402 |
struct timeval rt0, rt1; |
| 403 |
gettimeofday(&rt0, 0); |
| 404 |
getrusage(RUSAGE_SELF, &t0); |
| 405 |
|
| 406 |
pcb->collect_key = false_object; |
| 407 |
gc_t gc; |
| 408 |
bzero(&gc, sizeof(gc_t)); |
| 409 |
gc.pcb = pcb; |
| 410 |
gc.segment_vector = pcb->segment_vector; |
| 411 |
|
| 412 |
gc.collect_gen = collection_id_to_gen(pcb->collection_id); |
| 413 |
gc.collect_gen_tag = next_gen_tag[gc.collect_gen]; |
| 414 |
pcb->collection_id++; |
| 415 |
#ifndef NDEBUG |
| 416 |
fprintf(stderr, "ik_collect entry %ld free=%ld (collect gen=%d/id=%d)\n", |
| 417 |
mem_req, |
| 418 |
pcb->allocation_redline - pcb->allocation_pointer, |
| 419 |
gc.collect_gen, pcb->collection_id-1); |
| 420 |
#endif |
| 421 |
|
| 422 |
/* cache heap-pages to delete later */ |
| 423 |
ikpages* old_heap_pages = pcb->heap_pages; |
| 424 |
pcb->heap_pages = 0; |
| 425 |
|
| 426 |
/* the roots are: |
| 427 |
* 0. dirty pages not collected in this run |
| 428 |
* 1. the stack |
| 429 |
* 2. the next continuation |
| 430 |
* 3. the symbol-table |
| 431 |
*/ |
| 432 |
|
| 433 |
scan_dirty_pages(&gc); |
| 434 |
|
| 435 |
collect_stack(&gc, pcb->frame_pointer, pcb->frame_base - wordsize); |
| 436 |
collect_locatives(&gc, pcb->callbacks); |
| 437 |
pcb->next_k = add_object(&gc, pcb->next_k, "next_k"); |
| 438 |
pcb->symbol_table = add_object(&gc, pcb->symbol_table, "symbol_table"); |
| 439 |
pcb->gensym_table = add_object(&gc, pcb->gensym_table, "gensym_table"); |
| 440 |
pcb->arg_list = add_object(&gc, pcb->arg_list, "args_list_foo"); |
| 441 |
pcb->base_rtd = add_object(&gc, pcb->base_rtd, "base_rtd"); |
| 442 |
if(pcb->root0) *(pcb->root0) = add_object(&gc, *(pcb->root0), "root0"); |
| 443 |
if(pcb->root1) *(pcb->root1) = add_object(&gc, *(pcb->root1), "root1"); |
| 444 |
|
| 445 |
/* now we trace all live objects */ |
| 446 |
collect_loop(&gc); |
| 447 |
|
| 448 |
/* next we trace all guardian/guarded objects, |
| 449 |
the procedure does a collect_loop at the end */ |
| 450 |
handle_guardians(&gc); |
| 451 |
#ifndef NDEBUG |
| 452 |
fprintf(stderr, "done\n"); |
| 453 |
#endif |
| 454 |
collect_loop(&gc); |
| 455 |
|
| 456 |
/* does not allocate, only bwp's dead pointers */ |
| 457 |
fix_weak_pointers(&gc); |
| 458 |
/* now deallocate all unused pages */ |
| 459 |
deallocate_unused_pages(&gc); |
| 460 |
|
| 461 |
fix_new_pages(&gc); |
| 462 |
gc_finalize_guardians(&gc); |
| 463 |
|
| 464 |
pcb->allocation_pointer = pcb->heap_base; |
| 465 |
/* does not allocate */ |
| 466 |
gc_add_tconcs(&gc); |
| 467 |
/* does not allocate */ |
| 468 |
#ifndef NDEBUG |
| 469 |
fprintf(stderr, "done\n"); |
| 470 |
#endif |
| 471 |
pcb->weak_pairs_ap = 0; |
| 472 |
pcb->weak_pairs_ep = 0; |
| 473 |
|
| 474 |
#if accounting |
| 475 |
fprintf(stderr, |
| 476 |
"[%d cons|%d sym|%d cls|%d vec|%d rec|%d cck|%d str|%d htb]\n", |
| 477 |
pair_count, |
| 478 |
symbol_count, |
| 479 |
closure_count, |
| 480 |
vector_count, |
| 481 |
record_count, |
| 482 |
continuation_count, |
| 483 |
string_count, |
| 484 |
htable_count); |
| 485 |
pair_count = 0; |
| 486 |
symbol_count = 0; |
| 487 |
closure_count = 0; |
| 488 |
vector_count = 0; |
| 489 |
record_count = 0; |
| 490 |
continuation_count = 0; |
| 491 |
string_count = 0; |
| 492 |
htable_count = 0; |
| 493 |
#endif |
| 494 |
//ik_dump_metatable(pcb); |
| 495 |
#ifndef NDEBUG |
| 496 |
fprintf(stderr, "collect done\n"); |
| 497 |
#endif |
| 498 |
|
| 499 |
|
| 500 |
/* delete all old heap pages */ |
| 501 |
if(old_heap_pages){ |
| 502 |
ikpages* p = old_heap_pages; |
| 503 |
do{ |
| 504 |
ikpages* next = p->next; |
| 505 |
ik_munmap_from_segment(p->base, p->size, pcb); |
| 506 |
ik_free(p, sizeof(ikpages)); |
| 507 |
p=next; |
| 508 |
} while(p); |
| 509 |
old_heap_pages = 0; |
| 510 |
} |
| 511 |
|
| 512 |
unsigned long int free_space = |
| 513 |
((unsigned long int)pcb->allocation_redline) - |
| 514 |
((unsigned long int)pcb->allocation_pointer); |
| 515 |
if((free_space <= mem_req) || (pcb->heap_size < IK_HEAPSIZE)){ |
| 516 |
#ifndef NDEBUG |
| 517 |
fprintf(stderr, "REQ=%ld, got %ld\n", mem_req, free_space); |
| 518 |
#endif |
| 519 |
long int memsize = (mem_req > IK_HEAPSIZE) ? mem_req : IK_HEAPSIZE; |
| 520 |
memsize = align_to_next_page(memsize); |
| 521 |
ik_munmap_from_segment( |
| 522 |
pcb->heap_base, |
| 523 |
pcb->heap_size, |
| 524 |
pcb); |
| 525 |
ikptr ptr = ik_mmap_mixed(memsize+2*pagesize, pcb); |
| 526 |
pcb->allocation_pointer = ptr; |
| 527 |
pcb->allocation_redline = ptr+memsize; |
| 528 |
pcb->heap_base = ptr; |
| 529 |
pcb->heap_size = memsize+2*pagesize; |
| 530 |
} |
| 531 |
|
| 532 |
#ifndef NDEBUG |
| 533 |
ikptr x = pcb->allocation_pointer; |
| 534 |
while(x < pcb->allocation_redline){ |
| 535 |
ref(x, 0) = (ikptr)(0x1234FFFF); |
| 536 |
x+=wordsize; |
| 537 |
} |
| 538 |
#endif |
| 539 |
#ifndef NDEBUG |
| 540 |
verify_integrity(pcb, "exit"); |
| 541 |
#endif |
| 542 |
|
| 543 |
getrusage(RUSAGE_SELF, &t1); |
| 544 |
gettimeofday(&rt1, 0); |
| 545 |
|
| 546 |
pcb->collect_utime.tv_usec += t1.ru_utime.tv_usec - t0.ru_utime.tv_usec; |
| 547 |
pcb->collect_utime.tv_sec += t1.ru_utime.tv_sec - t0.ru_utime.tv_sec; |
| 548 |
if (pcb->collect_utime.tv_usec >= 1000000){ |
| 549 |
pcb->collect_utime.tv_usec -= 1000000; |
| 550 |
pcb->collect_utime.tv_sec += 1; |
| 551 |
} |
| 552 |
else if (pcb->collect_utime.tv_usec < 0){ |
| 553 |
pcb->collect_utime.tv_usec += 1000000; |
| 554 |
pcb->collect_utime.tv_sec -= 1; |
| 555 |
} |
| 556 |
|
| 557 |
pcb->collect_stime.tv_usec += t1.ru_stime.tv_usec - t0.ru_stime.tv_usec; |
| 558 |
pcb->collect_stime.tv_sec += t1.ru_stime.tv_sec - t0.ru_stime.tv_sec; |
| 559 |
if (pcb->collect_stime.tv_usec >= 1000000){ |
| 560 |
pcb->collect_stime.tv_usec -= 1000000; |
| 561 |
pcb->collect_stime.tv_sec += 1; |
| 562 |
} |
| 563 |
else if (pcb->collect_stime.tv_usec < 0){ |
| 564 |
pcb->collect_stime.tv_usec += 1000000; |
| 565 |
pcb->collect_stime.tv_sec -= 1; |
| 566 |
} |
| 567 |
|
| 568 |
pcb->collect_rtime.tv_usec += rt1.tv_usec - rt0.tv_usec; |
| 569 |
pcb->collect_rtime.tv_sec += rt1.tv_sec - rt0.tv_sec; |
| 570 |
if (pcb->collect_rtime.tv_usec >= 1000000){ |
| 571 |
pcb->collect_rtime.tv_usec -= 1000000; |
| 572 |
pcb->collect_rtime.tv_sec += 1; |
| 573 |
} |
| 574 |
else if (pcb->collect_rtime.tv_usec < 0){ |
| 575 |
pcb->collect_rtime.tv_usec += 1000000; |
| 576 |
pcb->collect_rtime.tv_sec -= 1; |
| 577 |
} |
| 578 |
return pcb; |
| 579 |
} |
| 580 |
|
| 581 |
static inline int |
| 582 |
is_live(ikptr x, gc_t* gc){ |
| 583 |
if(is_fixnum(x)){ |
| 584 |
return 1; |
| 585 |
} |
| 586 |
int tag = tagof(x); |
| 587 |
if(tag == immediate_tag){ |
| 588 |
return 1; |
| 589 |
} |
| 590 |
if(ref(x, -tag) == forward_ptr){ |
| 591 |
return 1; |
| 592 |
} |
| 593 |
unsigned int t = gc->segment_vector[page_index(x)]; |
| 594 |
int gen = t & gen_mask; |
| 595 |
if(gen > gc->collect_gen){ |
| 596 |
return 1; |
| 597 |
} |
| 598 |
return 0; |
| 599 |
} |
| 600 |
|
| 601 |
static inline int |
| 602 |
next_gen(int i){ |
| 603 |
return ((i == (generation_count-1)) ? i : (i+1)); |
| 604 |
} |
| 605 |
|
| 606 |
|
| 607 |
static ik_ptr_page* |
| 608 |
move_tconc(ikptr tc, ik_ptr_page* ls){ |
| 609 |
if((ls == NULL) || (ls->count == ik_ptr_page_size)){ |
| 610 |
ik_ptr_page* page = (ik_ptr_page*)ik_mmap(pagesize); |
| 611 |
page->count = 0; |
| 612 |
page->next = ls; |
| 613 |
ls = page; |
| 614 |
} |
| 615 |
ls->ptr[ls->count++] = tc; |
| 616 |
return ls; |
| 617 |
} |
| 618 |
|
| 619 |
static void |
| 620 |
handle_guardians(gc_t* gc){ |
| 621 |
ikpcb* pcb = gc->pcb; |
| 622 |
ik_ptr_page* pend_hold_list = 0; |
| 623 |
ik_ptr_page* pend_final_list = 0; |
| 624 |
int gen; |
| 625 |
/* sort protected pairs into pend_hold and pend_final lists */ |
| 626 |
for(gen=0; gen<=gc->collect_gen; gen++){ |
| 627 |
ik_ptr_page* prot_list = pcb->protected_list[gen]; |
| 628 |
pcb->protected_list[gen] = 0; |
| 629 |
while(prot_list){ |
| 630 |
int i; |
| 631 |
for(i=0; i<prot_list->count; i++){ |
| 632 |
ikptr p = prot_list->ptr[i]; |
| 633 |
ikptr tc = ref(p, off_car); |
| 634 |
ikptr obj = ref(p, off_cdr); |
| 635 |
if(tc == forward_ptr){ |
| 636 |
ikptr np = ref(p, off_cdr); |
| 637 |
tc = ref(np, off_car); |
| 638 |
obj = ref(np, off_cdr); |
| 639 |
} |
| 640 |
if(is_live(obj, gc)){ |
| 641 |
pend_hold_list = move_tconc(p, pend_hold_list); |
| 642 |
} else { |
| 643 |
pend_final_list = move_tconc(p, pend_final_list); |
| 644 |
} |
| 645 |
} |
| 646 |
ik_ptr_page* next = prot_list->next; |
| 647 |
ik_munmap((ikptr)prot_list, pagesize); |
| 648 |
prot_list = next; |
| 649 |
} |
| 650 |
} |
| 651 |
/* move live tc pend_final_list pairs into final_list, |
| 652 |
the rest remain in pend_final_list, |
| 653 |
final_list objects are made live and collected in |
| 654 |
gc->forward_list */ |
| 655 |
gc->forward_list = 0; |
| 656 |
int done = 0; |
| 657 |
while(!done){ |
| 658 |
ik_ptr_page* final_list = 0; |
| 659 |
ik_ptr_page* ls = pend_final_list; |
| 660 |
pend_final_list = 0; |
| 661 |
while(ls){ |
| 662 |
int i; |
| 663 |
for(i=0; i<ls->count; i++){ |
| 664 |
ikptr p = ls->ptr[i]; |
| 665 |
ikptr tc = ref(p, off_car); |
| 666 |
if(tc == forward_ptr){ |
| 667 |
ikptr np = ref(p, off_cdr); |
| 668 |
tc = ref(np, off_car); |
| 669 |
} |
| 670 |
if(is_live(tc, gc)){ |
| 671 |
final_list = move_tconc(p, final_list); |
| 672 |
} else { |
| 673 |
pend_final_list = move_tconc(p, pend_final_list); |
| 674 |
} |
| 675 |
} |
| 676 |
ik_ptr_page* next = ls->next; |
| 677 |
ik_munmap((ikptr)ls, pagesize); |
| 678 |
ls = next; |
| 679 |
} |
| 680 |
if(final_list == NULL){ |
| 681 |
done = 1; |
| 682 |
} else { |
| 683 |
ls = final_list; |
| 684 |
while(ls){ |
| 685 |
int i; |
| 686 |
for(i=0; i<ls->count; i++){ |
| 687 |
ikptr p = ls->ptr[i]; |
| 688 |
gc->forward_list = |
| 689 |
move_tconc(add_object(gc, p, "guardian"), |
| 690 |
gc->forward_list); |
| 691 |
} |
| 692 |
ik_ptr_page* next = ls->next; |
| 693 |
ik_munmap((ikptr)ls, pagesize); |
| 694 |
ls = next; |
| 695 |
} |
| 696 |
collect_loop(gc); |
| 697 |
} |
| 698 |
} |
| 699 |
/* pend_final_list now contains things that are dead and |
| 700 |
their tconcs are also dead, deallocate */ |
| 701 |
while(pend_final_list){ |
| 702 |
ik_ptr_page* next = pend_final_list->next; |
| 703 |
ik_munmap((ikptr)pend_final_list, pagesize); |
| 704 |
pend_final_list = next; |
| 705 |
} |
| 706 |
/* pend_hold_list pairs with live tconcs are moved to |
| 707 |
the protected list of next generation. */ |
| 708 |
ik_ptr_page* target = pcb->protected_list[next_gen(gc->collect_gen)]; |
| 709 |
while(pend_hold_list){ |
| 710 |
int i; |
| 711 |
for(i=0; i<pend_hold_list->count; i++){ |
| 712 |
ikptr p = pend_hold_list->ptr[i]; |
| 713 |
ikptr tc = ref(p, off_car); |
| 714 |
if(tc == forward_ptr){ |
| 715 |
ikptr np = ref(p, off_cdr); |
| 716 |
tc = ref(np, off_car); |
| 717 |
} |
| 718 |
if(is_live(tc, gc)){ |
| 719 |
target = move_tconc(add_object(gc, p, "guardian"), target); |
| 720 |
} |
| 721 |
} |
| 722 |
ik_ptr_page* next = pend_hold_list->next; |
| 723 |
ik_munmap((ikptr)pend_hold_list, pagesize); |
| 724 |
pend_hold_list = next; |
| 725 |
} |
| 726 |
collect_loop(gc); |
| 727 |
pcb->protected_list[next_gen(gc->collect_gen)] = target; |
| 728 |
} |
| 729 |
|
| 730 |
static void |
| 731 |
gc_finalize_guardians(gc_t* gc){ |
| 732 |
ik_ptr_page* ls = gc->forward_list; |
| 733 |
int tconc_count = 0; |
| 734 |
unsigned int* dirty_vec = (unsigned int*)(long)gc->pcb->dirty_vector; |
| 735 |
while(ls){ |
| 736 |
int i; |
| 737 |
for(i=0; i<ls->count; i++){ |
| 738 |
tconc_count++; |
| 739 |
ikptr p = ls->ptr[i]; |
| 740 |
ikptr tc = ref(p, off_car); |
| 741 |
ikptr obj = ref(p, off_cdr); |
| 742 |
ikptr last_pair = ref(tc, off_cdr); |
| 743 |
ref(last_pair, off_car) = obj; |
| 744 |
ref(last_pair, off_cdr) = p; |
| 745 |
ref(p, off_car) = false_object; |
| 746 |
ref(p, off_cdr) = false_object; |
| 747 |
ref(tc, off_cdr) = p; |
| 748 |
dirty_vec[page_index(tc)] = -1; |
| 749 |
dirty_vec[page_index(last_pair)] = -1; |
| 750 |
} |
| 751 |
ik_ptr_page* next = ls->next; |
| 752 |
ik_munmap((ikptr)ls, pagesize); |
| 753 |
ls = next; |
| 754 |
} |
| 755 |
} |
| 756 |
|
| 757 |
|
| 758 |
static int alloc_code_count = 0; |
| 759 |
|
| 760 |
|
| 761 |
static ikptr |
| 762 |
add_code_entry(gc_t* gc, ikptr entry){ |
| 763 |
ikptr x = entry - disp_code_data; |
| 764 |
if(ref(x,0) == forward_ptr){ |
| 765 |
return ref(x,wordsize) + off_code_data; |
| 766 |
} |
| 767 |
long int idx = page_index(x); |
| 768 |
unsigned int t = gc->segment_vector[idx]; |
| 769 |
int gen = t & gen_mask; |
| 770 |
if(gen > gc->collect_gen){ |
| 771 |
return entry; |
| 772 |
} |
| 773 |
long int code_size = unfix(ref(x, disp_code_code_size)); |
| 774 |
ikptr reloc_vec = ref(x, disp_code_reloc_vector); |
| 775 |
ikptr freevars = ref(x, disp_code_freevars); |
| 776 |
ikptr annotation = ref(x, disp_code_annotation); |
| 777 |
long int required_mem = align(disp_code_data + code_size); |
| 778 |
if(required_mem >= pagesize){ |
| 779 |
int new_tag = gc->collect_gen_tag; |
| 780 |
long int idx = page_index(x); |
| 781 |
gc->segment_vector[idx] = new_tag | code_mt; |
| 782 |
long int i; |
| 783 |
for(i=pagesize, idx++; i<required_mem; i+=pagesize, idx++){ |
| 784 |
gc->segment_vector[idx] = new_tag | data_mt; |
| 785 |
} |
| 786 |
qupages_t* p = ik_malloc(sizeof(qupages_t)); |
| 787 |
p->p = x; |
| 788 |
p->q = x+required_mem; |
| 789 |
p->next = gc->queues[meta_code]; |
| 790 |
gc->queues[meta_code] = p; |
| 791 |
return entry; |
| 792 |
} else { |
| 793 |
ikptr y = gc_alloc_new_code(required_mem, gc); |
| 794 |
ref(y, 0) = code_tag; |
| 795 |
ref(y, disp_code_code_size) = fix(code_size); |
| 796 |
ref(y, disp_code_reloc_vector) = reloc_vec; |
| 797 |
ref(y, disp_code_freevars) = freevars; |
| 798 |
ref(y, disp_code_annotation) = annotation; |
| 799 |
memcpy((char*)(long)(y+disp_code_data), |
| 800 |
(char*)(long)(x+disp_code_data), |
| 801 |
code_size); |
| 802 |
ref(x, 0) = forward_ptr; |
| 803 |
ref(x, wordsize) = y + vector_tag; |
| 804 |
return y+disp_code_data; |
| 805 |
} |
| 806 |
} |
| 807 |
|
| 808 |
static void |
| 809 |
collect_locatives(gc_t* gc, callback_locative* loc) { |
| 810 |
while(loc) { |
| 811 |
loc->data = add_object(gc, loc->data, "locative"); |
| 812 |
loc = loc->next; |
| 813 |
} |
| 814 |
} |
| 815 |
|
| 816 |
#define DEBUG_STACK 0 |
| 817 |
|
| 818 |
static void collect_stack(gc_t* gc, ikptr top, ikptr end){ |
| 819 |
if(DEBUG_STACK){ |
| 820 |
fprintf(stderr, "collecting stack (size=%ld) from 0x%016lx .. 0x%016lx\n", |
| 821 |
(long)end - (long)top, (long) top, (long) end); |
| 822 |
} |
| 823 |
while(top < end){ |
| 824 |
if(DEBUG_STACK){ |
| 825 |
fprintf(stderr, "collecting frame at 0x%016lx: \n", (long) top); |
| 826 |
} |
| 827 |
ikptr rp = ref(top, 0); |
| 828 |
long int rp_offset = unfix(ref(rp, disp_frame_offset)); |
| 829 |
if(DEBUG_STACK){ |
| 830 |
fprintf(stderr, "rp=0x%016lx\n", rp); |
| 831 |
fprintf(stderr, "rp_offset=%ld\n", rp_offset); |
| 832 |
} |
| 833 |
if(rp_offset <= 0){ |
| 834 |
fprintf(stderr, "invalid rp_offset %ld\n", rp_offset); |
| 835 |
exit(-1); |
| 836 |
} |
| 837 |
/* since the return point is alive, we need to find the code |
| 838 |
* object containing it and mark it live as well. the rp is |
| 839 |
* updated to reflect the new code object. */ |
| 840 |
|
| 841 |
long int code_offset = rp_offset - disp_frame_offset; |
| 842 |
ikptr code_entry = rp - code_offset; |
| 843 |
ikptr new_code_entry = add_code_entry(gc, code_entry); |
| 844 |
ikptr new_rp = new_code_entry + code_offset; |
| 845 |
ref(top, 0) = new_rp; |
| 846 |
|
| 847 |
/* now for some livemask action. |
| 848 |
* every return point has a live mark above it. the live mask |
| 849 |
* is a sequence of bytes (every byte for 8 frame cells). the |
| 850 |
* size of the live mask is determined by the size of the frame. |
| 851 |
* this is how the call frame instruction sequence looks like: |
| 852 |
* |
| 853 |
* | ... | |
| 854 |
* | code junk | |
| 855 |
* +------------+ |
| 856 |
* | byte 0 | for fv0 .. fv7 |
| 857 |
* | byte 1 | for fv8 .. fv15 |
| 858 |
* | ... | ... |
| 859 |
* +------------+ |
| 860 |
* | framesize | |
| 861 |
* | word | |
| 862 |
* +------------+ |
| 863 |
* | multivalue | |
| 864 |
* | word | |
| 865 |
* +------------+ |
| 866 |
* | frameoffst | the frame offset determined how far its |
| 867 |
* | word | address is off from the start of the code |
| 868 |
* +------------+ |
| 869 |
* | padding | the size of this part is fixed so that we |
| 870 |
* | and call | can correlate the frame info (above) with rp |
| 871 |
* +------------+ |
| 872 |
* | code junk | <---- rp |
| 873 |
* | ... | |
| 874 |
* |
| 875 |
* WITH ONE EXCEPTION: |
| 876 |
* if the framesize is 0, then the actual frame size is stored |
| 877 |
* on the stack immediately below the return point. |
| 878 |
* there is no live mask in this case, instead all values in the |
| 879 |
* frame are live. |
| 880 |
*/ |
| 881 |
long int framesize = ref(rp, disp_frame_size); |
| 882 |
if(DEBUG_STACK){ |
| 883 |
fprintf(stderr, "fs=%ld\n", (long)framesize); |
| 884 |
} |
| 885 |
if(framesize < 0){ |
| 886 |
fprintf(stderr, "invalid frame size %ld\n", (long)framesize); |
| 887 |
exit(-1); |
| 888 |
} |
| 889 |
else if(framesize == 0){ |
| 890 |
framesize = ref(top, wordsize); |
| 891 |
if(framesize <= 0){ |
| 892 |
fprintf(stderr, "invalid redirected framesize=%ld\n", (long)framesize); |
| 893 |
exit(-1); |
| 894 |
} |
| 895 |
ikptr base = top + framesize - wordsize; |
| 896 |
while(base > top){ |
| 897 |
ikptr new_obj = add_object(gc,ref(base,0), "frame"); |
| 898 |
ref(base,0) = new_obj; |
| 899 |
base -= wordsize; |
| 900 |
} |
| 901 |
} else { |
| 902 |
long int frame_cells = framesize >> fx_shift; |
| 903 |
long int bytes_in_mask = (frame_cells+7) >> 3; |
| 904 |
char* mask = (char*)(long)(rp+disp_frame_size-bytes_in_mask); |
| 905 |
|
| 906 |
ikptr* fp = (ikptr*)(long)(top + framesize); |
| 907 |
long int i; |
| 908 |
for(i=0; i<bytes_in_mask; i++, fp-=8){ |
| 909 |
unsigned char m = mask[i]; |
| 910 |
#if DEBUG_STACK |
| 911 |
fprintf(stderr, "m[%ld]=0x%x\n", i, m); |
| 912 |
#endif |
| 913 |
if(m & 0x01) { fp[-0] = add_object(gc, fp[-0], "frame0"); } |
| 914 |
if(m & 0x02) { fp[-1] = add_object(gc, fp[-1], "frame1"); } |
| 915 |
if(m & 0x04) { fp[-2] = add_object(gc, fp[-2], "frame2"); } |
| 916 |
if(m & 0x08) { fp[-3] = add_object(gc, fp[-3], "frame3"); } |
| 917 |
if(m & 0x10) { fp[-4] = add_object(gc, fp[-4], "frame4"); } |
| 918 |
if(m & 0x20) { fp[-5] = add_object(gc, fp[-5], "frame5"); } |
| 919 |
if(m & 0x40) { fp[-6] = add_object(gc, fp[-6], "frame6"); } |
| 920 |
if(m & 0x80) { fp[-7] = add_object(gc, fp[-7], "frame7"); } |
| 921 |
} |
| 922 |
} |
| 923 |
top += framesize; |
| 924 |
} |
| 925 |
if(top != end){ |
| 926 |
fprintf(stderr, "frames did not match up 0x%016lx .. 0x%016lx\n", |
| 927 |
(long int) top, (long int) end); |
| 928 |
exit(-1); |
| 929 |
} |
| 930 |
if(DEBUG_STACK){ |
| 931 |
fprintf(stderr, "done with stack!\n"); |
| 932 |
} |
| 933 |
} |
| 934 |
|
| 935 |
|
| 936 |
static void |
| 937 |
add_list(gc_t* gc, unsigned int t, ikptr x, ikptr* loc){ |
| 938 |
int collect_gen = gc->collect_gen; |
| 939 |
while(1){ |
| 940 |
ikptr fst = ref(x, off_car); |
| 941 |
ikptr snd = ref(x, off_cdr); |
| 942 |
ikptr y; |
| 943 |
if((t & type_mask) != weak_pairs_type){ |
| 944 |
y = gc_alloc_new_pair(gc) + pair_tag; |
| 945 |
} else { |
| 946 |
y = gc_alloc_new_weak_pair(gc) + pair_tag; |
| 947 |
} |
| 948 |
*loc = y; |
| 949 |
ref(x,off_car) = forward_ptr; |
| 950 |
ref(x,off_cdr) = y; |
| 951 |
ref(y,off_car) = fst; |
| 952 |
int stag = tagof(snd); |
| 953 |
if(stag == pair_tag){ |
| 954 |
if(ref(snd, -pair_tag) == forward_ptr){ |
| 955 |
ref(y, off_cdr) = ref(snd, wordsize-pair_tag); |
| 956 |
return; |
| 957 |
} |
| 958 |
else { |
| 959 |
t = gc->segment_vector[page_index(snd)]; |
| 960 |
int gen = t & gen_mask; |
| 961 |
if(gen > collect_gen){ |
| 962 |
ref(y, off_cdr) = snd; |
| 963 |
return; |
| 964 |
} else { |
| 965 |
x = snd; |
| 966 |
loc = (ikptr*)(long)(y + off_cdr); |
| 967 |
/* don't return */ |
| 968 |
} |
| 969 |
} |
| 970 |
} |
| 971 |
else if( (stag == immediate_tag) |
| 972 |
|| (stag == 0) |
| 973 |
|| (stag == (1<<fx_shift))) { |
| 974 |
ref(y,off_cdr) = snd; |
| 975 |
return; |
| 976 |
} |
| 977 |
else if (ref(snd, -stag) == forward_ptr){ |
| 978 |
ref(y, off_cdr) = ref(snd, wordsize-stag); |
| 979 |
return; |
| 980 |
} |
| 981 |
else { |
| 982 |
ref(y, off_cdr) = add_object(gc, snd, "add_list"); |
| 983 |
return; |
| 984 |
} |
| 985 |
} |
| 986 |
} |
| 987 |
|
| 988 |
|
| 989 |
static ikptr |
| 990 |
#ifndef NDEBUG |
| 991 |
add_object_proc(gc_t* gc, ikptr x, char* caller) { |
| 992 |
caller = caller; |
| 993 |
#else |
| 994 |
add_object_proc(gc_t* gc, ikptr x) { |
| 995 |
#endif |
| 996 |
if(is_fixnum(x)){ |
| 997 |
return x; |
| 998 |
} |
| 999 |
assert(x != forward_ptr); |
| 1000 |
int tag = tagof(x); |
| 1001 |
if(tag == immediate_tag){ |
| 1002 |
return x; |
| 1003 |
} |
| 1004 |
ikptr fst = ref(x, -tag); |
| 1005 |
if(fst == forward_ptr){ |
| 1006 |
/* already moved */ |
| 1007 |
return ref(x, wordsize-tag); |
| 1008 |
} |
| 1009 |
unsigned int t = gc->segment_vector[page_index(x)]; |
| 1010 |
int gen = t & gen_mask; |
| 1011 |
if(gen > gc->collect_gen){ |
| 1012 |
return x; |
| 1013 |
} |
| 1014 |
if(tag == pair_tag){ |
| 1015 |
ikptr y; |
| 1016 |
add_list(gc, t, x, &y); |
| 1017 |
return y; |
| 1018 |
} |
| 1019 |
#if 0 |
| 1020 |
else if(tag == symbol_tag){ |
| 1021 |
//ikptr y = gc_alloc_new_ptr(align(symbol_size),gen, gc) + symbol_tag; |
| 1022 |
ikptr y = gc_alloc_new_symbol(gen, gc) + symbol_tag; |
| 1023 |
ref(y, off_symbol_string) = ref(x, off_symbol_string); |
| 1024 |
ref(y, off_symbol_ustring) = ref(x, off_symbol_ustring); |
| 1025 |
ref(y, off_symbol_value) = ref(x, off_symbol_value); |
| 1026 |
ref(y, off_symbol_plist) = ref(x, off_symbol_plist); |
| 1027 |
ref(y, off_symbol_system_value) = ref(x, off_symbol_system_value); |
| 1028 |
ref(y, off_symbol_code) = ref(x, off_symbol_code); |
| 1029 |
ref(y, off_symbol_errcode) = ref(x, off_symbol_errcode); |
| 1030 |
ref(y, off_symbol_unused) = 0; |
| 1031 |
ref(x, -symbol_tag) = forward_ptr; |
| 1032 |
ref(x, wordsize-symbol_tag) = y; |
| 1033 |
#if accounting |
| 1034 |
symbol_count++; |
| 1035 |
#endif |
| 1036 |
return y; |
| 1037 |
} |
| 1038 |
#endif |
| 1039 |
else if(tag == closure_tag){ |
| 1040 |
ikptr size = |
| 1041 |
disp_closure_data + |
| 1042 |
ref(fst, disp_code_freevars - disp_code_data); |
| 1043 |
if(size > 1024){ |
| 1044 |
fprintf(stderr, "large closure size=0x%016lx\n", (long)size); |
| 1045 |
} |
| 1046 |
ikptr asize = align(size); |
| 1047 |
ikptr y = gc_alloc_new_ptr(asize, gc) + closure_tag; |
| 1048 |
ref(y, asize-closure_tag-wordsize) = 0; |
| 1049 |
memcpy((char*)(long)(y-closure_tag), |
| 1050 |
(char*)(long)(x-closure_tag), |
| 1051 |
size); |
| 1052 |
ref(y,-closure_tag) = add_code_entry(gc, ref(y,-closure_tag)); |
| 1053 |
ref(x,-closure_tag) = forward_ptr; |
| 1054 |
ref(x,wordsize-closure_tag) = y; |
| 1055 |
#if accounting |
| 1056 |
closure_count++; |
| 1057 |
#endif |
| 1058 |
return y; |
| 1059 |
} |
| 1060 |
else if(tag == vector_tag){ |
| 1061 |
if(is_fixnum(fst)){ |
| 1062 |
/* real vector */ |
| 1063 |
//fprintf(stderr, "X=0x%08x, FST=0x%08x\n", (int)x, (int)fst); |
| 1064 |
ikptr size = fst; |
| 1065 |
assert(((long)size) >= 0); |
| 1066 |
ikptr memreq = align(size + disp_vector_data); |
| 1067 |
if(memreq >= pagesize){ |
| 1068 |
if((t & large_object_mask) == large_object_tag){ |
| 1069 |
enqueue_large_ptr(x-vector_tag, size+disp_vector_data, gc); |
| 1070 |
return x; |
| 1071 |
} else { |
| 1072 |
ikptr y = gc_alloc_new_large_ptr(size+disp_vector_data, gc) |
| 1073 |
+ vector_tag; |
| 1074 |
ref(y, disp_vector_length-vector_tag) = fst; |
| 1075 |
ref(y, memreq-vector_tag-wordsize) = 0; |
| 1076 |
memcpy((char*)(long)(y+off_vector_data), |
| 1077 |
(char*)(long)(x+off_vector_data), |
| 1078 |
size); |
| 1079 |
ref(x,-vector_tag) = forward_ptr; |
| 1080 |
ref(x,wordsize-vector_tag) = y; |
| 1081 |
return y; |
| 1082 |
} |
| 1083 |
} else { |
| 1084 |
ikptr y = gc_alloc_new_ptr(memreq, gc) + vector_tag; |
| 1085 |
ref(y, disp_vector_length-vector_tag) = fst; |
| 1086 |
ref(y, memreq-vector_tag-wordsize) = 0; |
| 1087 |
memcpy((char*)(long)(y+off_vector_data), |
| 1088 |
(char*)(long)(x+off_vector_data), |
| 1089 |
size); |
| 1090 |
ref(x,-vector_tag) = forward_ptr; |
| 1091 |
ref(x,wordsize-vector_tag) = y; |
| 1092 |
return y; |
| 1093 |
} |
| 1094 |
#if accounting |
| 1095 |
vector_count++; |
| 1096 |
#endif |
| 1097 |
} |
| 1098 |
else if(fst == symbol_record_tag){ |
| 1099 |
ikptr y = gc_alloc_new_symbol_record(gc) + record_tag; |
| 1100 |
ref(y, -record_tag) = symbol_record_tag; |
| 1101 |
ref(y, off_symbol_record_string) = ref(x, off_symbol_record_string); |
| 1102 |
ref(y, off_symbol_record_ustring) = ref(x, off_symbol_record_ustring); |
| 1103 |
ref(y, off_symbol_record_value) = ref(x, off_symbol_record_value); |
| 1104 |
ref(y, off_symbol_record_proc) = ref(x, off_symbol_record_proc); |
| 1105 |
ref(y, off_symbol_record_plist) = ref(x, off_symbol_record_plist); |
| 1106 |
ref(x, -record_tag) = forward_ptr; |
| 1107 |
ref(x, wordsize-record_tag) = y; |
| 1108 |
return y; |
| 1109 |
} |
| 1110 |
else if(tagof(fst) == rtd_tag){ |
| 1111 |
/* struct / record */ |
| 1112 |
ikptr size = ref(fst, off_rtd_length); |
| 1113 |
if(size & ((1<<align_shift)-1)) { |
| 1114 |
/* size = n * object_alignment + 4 => |
| 1115 |
memreq = n * object_alignment + 8 |
| 1116 |
= (n+1) * object_alignment => aligned */ |
| 1117 |
ikptr y = gc_alloc_new_ptr(size+wordsize, gc) + vector_tag; |
| 1118 |
ref(y, -vector_tag) = fst; |
| 1119 |
{ |
| 1120 |
ikptr i; |
| 1121 |
ikptr p = y+disp_record_data-vector_tag; |
| 1122 |
ikptr q = x+disp_record_data-vector_tag; |
| 1123 |
ref(p, 0) = ref(q, 0); |
| 1124 |
for(i=wordsize; i<size; i+=(2*wordsize)){ |
| 1125 |
ref(p, i) = ref(q, i); |
| 1126 |
ref(p, i+wordsize) = ref(q, i+wordsize); |
| 1127 |
} |
| 1128 |
} |
| 1129 |
ref(x,-vector_tag) = forward_ptr; |
| 1130 |
ref(x,wordsize-vector_tag) = y; |
| 1131 |
return y; |
| 1132 |
} else { |
| 1133 |
/* size = n * object_alignment => |
| 1134 |
memreq = n * object_alignment + 4 + 4 (pad) */ |
| 1135 |
ikptr y = gc_alloc_new_ptr(size+(2*wordsize), gc) + vector_tag; |
| 1136 |
ref(y, -vector_tag) = fst; |
| 1137 |
{ |
| 1138 |
ikptr i; |
| 1139 |
ikptr p = y+disp_record_data-vector_tag; |
| 1140 |
ikptr q = x+disp_record_data-vector_tag; |
| 1141 |
for(i=0; i<size; i+=(2*wordsize)){ |
| 1142 |
ref(p, i) = ref(q, i); |
| 1143 |
ref(p, i+wordsize) = ref(q, i+wordsize); |
| 1144 |
} |
| 1145 |
} |
| 1146 |
ref(y, size+disp_record_data-vector_tag) = 0; |
| 1147 |
ref(x,-vector_tag) = forward_ptr; |
| 1148 |
ref(x,wordsize-vector_tag) = y; |
| 1149 |
return y; |
| 1150 |
} |
| 1151 |
} |
| 1152 |
else if(fst == code_tag){ |
| 1153 |
ikptr entry = x + off_code_data; |
| 1154 |
ikptr new_entry = add_code_entry(gc, entry); |
| 1155 |
return new_entry - off_code_data; |
| 1156 |
} |
| 1157 |
else if(fst == continuation_tag){ |
| 1158 |
ikptr top = ref(x, off_continuation_top); |
| 1159 |
ikptr size = ref(x, off_continuation_size); |
| 1160 |
#ifndef NDEBUG |
| 1161 |
if(size > 4096){ |
| 1162 |
fprintf(stderr, "large cont size=0x%016lx\n", size); |
| 1163 |
} |
| 1164 |
#endif |
| 1165 |
ikptr next = ref(x, off_continuation_next); |
| 1166 |
ikptr y = gc_alloc_new_ptr(continuation_size, gc) + vector_tag; |
| 1167 |
ref(x, -vector_tag) = forward_ptr; |
| 1168 |
ref(x, wordsize-vector_tag) = y; |
| 1169 |
ikptr new_top = gc_alloc_new_data(align(size), gc); |
| 1170 |
memcpy((char*)(long)new_top, |
| 1171 |
(char*)(long)top, |
| 1172 |
size); |
| 1173 |
collect_stack(gc, new_top, new_top + size); |
| 1174 |
ref(y, -vector_tag) = continuation_tag; |
| 1175 |
ref(y, off_continuation_top) = new_top; |
| 1176 |
ref(y, off_continuation_size) = (ikptr) size; |
| 1177 |
ref(y, off_continuation_next) = next; |
| 1178 |
#if accounting |
| 1179 |
continuation_count++; |
| 1180 |
#endif |
| 1181 |
return y; |
| 1182 |
} |
| 1183 |
else if(fst == system_continuation_tag) { |
| 1184 |
ikptr y = gc_alloc_new_data(system_continuation_size, gc) + vector_tag; |
| 1185 |
ikptr top = ref(x, disp_system_continuation_top - vector_tag); |
| 1186 |
ikptr next = ref(x, disp_system_continuation_next - vector_tag); |
| 1187 |
ref(x, -vector_tag) = forward_ptr; |
| 1188 |
ref(x, wordsize-vector_tag) = y; |
| 1189 |
ref(y, -vector_tag) = fst; |
| 1190 |
ref(y, disp_system_continuation_top - vector_tag) = top; |
| 1191 |
ref(y, disp_system_continuation_next - vector_tag) = |
| 1192 |
add_object(gc, next, "next_k"); |
| 1193 |
return y; |
| 1194 |
} |
| 1195 |
else if(tagof(fst) == pair_tag){ |
| 1196 |
/* tcbucket */ |
| 1197 |
ikptr y = gc_alloc_new_ptr(tcbucket_size, gc) + vector_tag; |
| 1198 |
ref(y,off_tcbucket_tconc) = fst; |
| 1199 |
ikptr key = ref(x, off_tcbucket_key); |
| 1200 |
ref(y,off_tcbucket_key) = key; |
| 1201 |
ref(y,off_tcbucket_val) = ref(x, off_tcbucket_val); |
| 1202 |
ref(y,off_tcbucket_next) = ref(x, off_tcbucket_next); |
| 1203 |
if((! is_fixnum(key)) && (tagof(key) != immediate_tag)){ |
| 1204 |
int gen = gc->segment_vector[page_index(key)] & gen_mask; |
| 1205 |
if(gen <= gc->collect_gen){ |
| 1206 |
/* key will be moved */ |
| 1207 |
gc_tconc_push(gc, y); |
| 1208 |
} |
| 1209 |
} |
| 1210 |
ref(x, -vector_tag) = forward_ptr; |
| 1211 |
ref(x, wordsize-vector_tag) = y; |
| 1212 |
return y; |
| 1213 |
} |
| 1214 |
else if((((long int)fst) & port_mask) == port_tag){ |
| 1215 |
ikptr y = gc_alloc_new_ptr(port_size, gc) + vector_tag; |
| 1216 |
ref(y, -vector_tag) = fst; |
| 1217 |
long int i; |
| 1218 |
for(i=wordsize; i<port_size; i+=wordsize){ |
| 1219 |
ref(y, i-vector_tag) = ref(x, i-vector_tag); |
| 1220 |
} |
| 1221 |
ref(x, -vector_tag) = forward_ptr; |
| 1222 |
ref(x, wordsize-vector_tag) = y; |
| 1223 |
return y; |
| 1224 |
} |
| 1225 |
else if(fst == flonum_tag){ |
| 1226 |
ikptr new = gc_alloc_new_data(flonum_size, gc) + vector_tag; |
| 1227 |
ref(new, -vector_tag) = flonum_tag; |
| 1228 |
flonum_data(new) = flonum_data(x); |
| 1229 |
ref(x, -vector_tag) = forward_ptr; |
| 1230 |
ref(x, wordsize-vector_tag) = new; |
| 1231 |
return new; |
| 1232 |
} |
| 1233 |
else if((fst & bignum_mask) == bignum_tag){ |
| 1234 |
long int len = ((unsigned long int)fst) >> bignum_length_shift; |
| 1235 |
long int memreq = align(disp_bignum_data + len*wordsize); |
| 1236 |
ikptr new = gc_alloc_new_data(memreq, gc) + vector_tag; |
| 1237 |
memcpy((char*)(long)(new-vector_tag), |
| 1238 |
(char*)(long)(x-vector_tag), |
| 1239 |
memreq); |
| 1240 |
ref(x, -vector_tag) = forward_ptr; |
| 1241 |
ref(x, wordsize-vector_tag) = new; |
| 1242 |
return new; |
| 1243 |
} |
| 1244 |
else if(fst == ratnum_tag){ |
| 1245 |
ikptr y = gc_alloc_new_data(ratnum_size, gc) + vector_tag; |
| 1246 |
ikptr num = ref(x, disp_ratnum_num-vector_tag); |
| 1247 |
ikptr den = ref(x, disp_ratnum_den-vector_tag); |
| 1248 |
ref(x, -vector_tag) = forward_ptr; |
| 1249 |
ref(x, wordsize-vector_tag) = y; |
| 1250 |
ref(y, -vector_tag) = fst; |
| 1251 |
ref(y, disp_ratnum_num-vector_tag) = add_object(gc, num, "num"); |
| 1252 |
ref(y, disp_ratnum_den-vector_tag) = add_object(gc, den, "den"); |
| 1253 |
return y; |
| 1254 |
} |
| 1255 |
else if(fst == compnum_tag){ |
| 1256 |
ikptr y = gc_alloc_new_data(compnum_size, gc) + vector_tag; |
| 1257 |
ikptr rl = ref(x, disp_compnum_real-vector_tag); |
| 1258 |
ikptr im = ref(x, disp_compnum_imag-vector_tag); |
| 1259 |
ref(x, -vector_tag) = forward_ptr; |
| 1260 |
ref(x, wordsize-vector_tag) = y; |
| 1261 |
ref(y, -vector_tag) = fst; |
| 1262 |
ref(y, disp_compnum_real-vector_tag) = add_object(gc, rl, "real"); |
| 1263 |
ref(y, disp_compnum_imag-vector_tag) = add_object(gc, im, "imag"); |
| 1264 |
return y; |
| 1265 |
} |
| 1266 |
else if(fst == cflonum_tag){ |
| 1267 |
ikptr y = gc_alloc_new_data(cflonum_size, gc) + vector_tag; |
| 1268 |
ikptr rl = ref(x, disp_cflonum_real-vector_tag); |
| 1269 |
ikptr im = ref(x, disp_cflonum_imag-vector_tag); |
| 1270 |
ref(x, -vector_tag) = forward_ptr; |
| 1271 |
ref(x, wordsize-vector_tag) = y; |
| 1272 |
ref(y, -vector_tag) = fst; |
| 1273 |
ref(y, disp_cflonum_real-vector_tag) = add_object(gc, rl, "real"); |
| 1274 |
ref(y, disp_cflonum_imag-vector_tag) = add_object(gc, im, "imag"); |
| 1275 |
return y; |
| 1276 |
} |
| 1277 |
else if(fst == pointer_tag){ |
| 1278 |
ikptr y = gc_alloc_new_data(pointer_size, gc) + vector_tag; |
| 1279 |
ref(y, -vector_tag) = pointer_tag; |
| 1280 |
ref(y, wordsize-vector_tag) = ref(x, wordsize-vector_tag); |
| 1281 |
ref(x, -vector_tag) = forward_ptr; |
| 1282 |
ref(x, wordsize-vector_tag) = y; |
| 1283 |
return y; |
| 1284 |
} |
| 1285 |
else { |
| 1286 |
fprintf(stderr, "unhandled vector with fst=0x%016lx\n", |
| 1287 |
(long int)fst); |
| 1288 |
assert(0); |
| 1289 |
exit(-1); |
| 1290 |
} |
| 1291 |
} |
| 1292 |
else if(tag == string_tag){ |
| 1293 |
if(is_fixnum(fst)){ |
| 1294 |
long int strlen = unfix(fst); |
| 1295 |
long int memreq = align(strlen*string_char_size + disp_string_data); |
| 1296 |
ikptr new_str = gc_alloc_new_data(memreq, gc) + string_tag; |
| 1297 |
ref(new_str, off_string_length) = fst; |
| 1298 |
memcpy((char*)(long)(new_str+off_string_data), |
| 1299 |
(char*)(long)(x + off_string_data), |
| 1300 |
strlen*string_char_size); |
| 1301 |
ref(x, -string_tag) = forward_ptr; |
| 1302 |
ref(x, wordsize-string_tag) = new_str; |
| 1303 |
#if accounting |
| 1304 |
string_count++; |
| 1305 |
#endif |
| 1306 |
return new_str; |
| 1307 |
} |
| 1308 |
else { |
| 1309 |
fprintf(stderr, "unhandled string 0x%016lx with fst=0x%016lx\n", |
| 1310 |
(long int)x, (long int)fst); |
| 1311 |
exit(-1); |
| 1312 |
} |
| 1313 |
} |
| 1314 |
else if(tag == bytevector_tag){ |
| 1315 |
long int len = unfix(fst); |
| 1316 |
long int memreq = align(len + disp_bytevector_data + 1); |
| 1317 |
ikptr new_bv = gc_alloc_new_data(memreq, gc) + bytevector_tag; |
| 1318 |
ref(new_bv, off_bytevector_length) = fst; |
| 1319 |
memcpy((char*)(long)(new_bv+off_bytevector_data), |
| 1320 |
(char*)(long)(x + off_bytevector_data), |
| 1321 |
len + 1); |
| 1322 |
ref(x, -bytevector_tag) = forward_ptr; |
| 1323 |
ref(x, wordsize-bytevector_tag) = new_bv; |
| 1324 |
return new_bv; |
| 1325 |
} |
| 1326 |
fprintf(stderr, "unhandled tag: %d\n", tag); |
| 1327 |
exit(-1); |
| 1328 |
} |
| 1329 |
|
| 1330 |
static void |
| 1331 |
relocate_new_code(ikptr x, gc_t* gc){ |
| 1332 |
ikptr relocvector = ref(x, disp_code_reloc_vector); |
| 1333 |
relocvector = add_object(gc, relocvector, "relocvec"); |
| 1334 |
ref(x, disp_code_reloc_vector) = relocvector; |
| 1335 |
ref(x, disp_code_annotation) = |
| 1336 |
add_object(gc, ref(x, disp_code_annotation), "annotation"); |
| 1337 |
ikptr relocsize = ref(relocvector, off_vector_length); |
| 1338 |
ikptr p = relocvector + off_vector_data; |
| 1339 |
ikptr q = p + relocsize; |
| 1340 |
ikptr code = x + disp_code_data; |
| 1341 |
while(p < q){ |
| 1342 |
long int r = unfix(ref(p, 0)); |
| 1343 |
long int tag = r & 3; |
| 1344 |
long int code_off = r >> 2; |
| 1345 |
if(tag == 0){ |
| 1346 |
/* undisplaced pointer */ |
| 1347 |
#ifndef NDEBUG |
| 1348 |
// fprintf(stderr, "r=0x%08x code_off=%d reloc_size=0x%08x\n", |
| 1349 |
// r, code_off, relocsize); |
| 1350 |
#endif |
| 1351 |
ikptr old_object = ref(p, wordsize); |
| 1352 |
ikptr new_object = add_object(gc, old_object, "reloc1"); |
| 1353 |
ref(code, code_off) = new_object; |
| 1354 |
p += (2*wordsize); |
| 1355 |
} |
| 1356 |
else if(tag == 2){ |
| 1357 |
/* displaced pointer */ |
| 1358 |
long int obj_off = unfix(ref(p, wordsize)); |
| 1359 |
ikptr old_object = ref(p, 2*wordsize); |
| 1360 |
ikptr new_object = add_object(gc, old_object, "reloc2"); |
| 1361 |
ref(code, code_off) = new_object + obj_off; |
| 1362 |
p += (3 * wordsize); |
| 1363 |
} |
| 1364 |
else if(tag == 3){ |
| 1365 |
/* displaced relative pointer */ |
| 1366 |
long int obj_off = unfix(ref(p, wordsize)); |
| 1367 |
ikptr obj = ref(p, 2*wordsize); |
| 1368 |
#ifndef NDEBUG |
| 1369 |
//fprintf(stderr, "obj=0x%08x, obj_off=0x%08x\n", (int)obj, |
| 1370 |
// obj_off); |
| 1371 |
#endif |
| 1372 |
obj = add_object(gc, obj, "reloc3"); |
| 1373 |
ikptr displaced_object = obj + obj_off; |
| 1374 |
long int next_word = code + code_off + 4; |
| 1375 |
ikptr relative_distance = displaced_object - (long int)next_word; |
| 1376 |
if(((long int)relative_distance) != ((long)((int)relative_distance))){ |
| 1377 |
fprintf(stderr, "relocation error with relative=0x%016lx\n", |
| 1378 |
relative_distance); |
| 1379 |
exit(-1); |
| 1380 |
} |
| 1381 |
*((int*)(code+code_off)) = (int)relative_distance; |
| 1382 |
p += (3*wordsize); |
| 1383 |
} |
| 1384 |
else if(tag == 1){ |
| 1385 |
/* do nothing */ |
| 1386 |
p += (2 * wordsize); |
| 1387 |
} |
| 1388 |
else { |
| 1389 |
fprintf(stderr, "invalid rtag %ld in 0x%016lx\n", tag, r); |
| 1390 |
exit(-1); |
| 1391 |
} |
| 1392 |
} |
| 1393 |
} |
| 1394 |
|
| 1395 |
|
| 1396 |
|
| 1397 |
static void |
| 1398 |
collect_loop(gc_t* gc){ |
| 1399 |
int done; |
| 1400 |
do{ |
| 1401 |
done = 1; |
| 1402 |
{ /* scan the pending pairs pages */ |
| 1403 |
qupages_t* qu = gc->queues[meta_pair]; |
| 1404 |
if(qu){ |
| 1405 |
done = 0; |
| 1406 |
gc->queues[meta_pair] = 0; |
| 1407 |
do{ |
| 1408 |
ikptr p = qu->p; |
| 1409 |
ikptr q = qu->q; |
| 1410 |
while(p < q){ |
| 1411 |
ref(p,0) = add_object(gc, ref(p,0), "loop"); |
| 1412 |
p += (2*wordsize); |
| 1413 |
} |
| 1414 |
qupages_t* next = qu->next; |
| 1415 |
ik_free(qu, sizeof(qupages_t)); |
| 1416 |
qu = next; |
| 1417 |
} while(qu); |
| 1418 |
} |
| 1419 |
} |
| 1420 |
|
| 1421 |
{ /* scan the pending pointer pages */ |
| 1422 |
qupages_t* qu = gc->queues[meta_ptrs]; |
| 1423 |
if(qu){ |
| 1424 |
done = 0; |
| 1425 |
gc->queues[meta_ptrs] = 0; |
| 1426 |
do{ |
| 1427 |
ikptr p = qu->p; |
| 1428 |
ikptr q = qu->q; |
| 1429 |
while(p < q){ |
| 1430 |
ref(p,0) = add_object(gc, ref(p,0), "pending"); |
| 1431 |
p += wordsize; |
| 1432 |
} |
| 1433 |
qupages_t* next = qu->next; |
| 1434 |
ik_free(qu, sizeof(qupages_t)); |
| 1435 |
qu = next; |
| 1436 |
} while(qu); |
| 1437 |
} |
| 1438 |
} |
| 1439 |
|
| 1440 |
{ /* scan the pending symbol pages */ |
| 1441 |
qupages_t* qu = gc->queues[meta_symbol]; |
| 1442 |
if(qu){ |
| 1443 |
done = 0; |
| 1444 |
gc->queues[meta_symbol] = 0; |
| 1445 |
do{ |
| 1446 |
ikptr p = qu->p; |
| 1447 |
ikptr q = qu->q; |
| 1448 |
while(p < q){ |
| 1449 |
ref(p,0) = add_object(gc, ref(p,0), "symbols"); |
| 1450 |
p += wordsize; |
| 1451 |
} |
| 1452 |
qupages_t* next = qu->next; |
| 1453 |
ik_free(qu, sizeof(qupages_t)); |
| 1454 |
qu = next; |
| 1455 |
} while(qu); |
| 1456 |
} |
| 1457 |
} |
| 1458 |
|
| 1459 |
{ /* scan the pending code objects */ |
| 1460 |
qupages_t* codes = gc->queues[meta_code]; |
| 1461 |
if(codes){ |
| 1462 |
gc->queues[meta_code] = 0; |
| 1463 |
done = 0; |
| 1464 |
do{ |
| 1465 |
ikptr p = codes->p; |
| 1466 |
ikptr q = codes->q; |
| 1467 |
while(p < q){ |
| 1468 |
relocate_new_code(p, gc); |
| 1469 |
alloc_code_count--; |
| 1470 |
p += align(disp_code_data + unfix(ref(p, disp_code_code_size))); |
| 1471 |
} |
| 1472 |
qupages_t* next = codes->next; |
| 1473 |
ik_free(codes, sizeof(qupages_t)); |
| 1474 |
codes = next; |
| 1475 |
} while(codes); |
| 1476 |
} |
| 1477 |
} |
| 1478 |
{/* see if there are any remaining in the main ptr segment */ |
| 1479 |
{ |
| 1480 |
meta_t* meta = &gc->meta[meta_pair]; |
| 1481 |
ikptr p = meta->aq; |
| 1482 |
ikptr q = meta->ap; |
| 1483 |
if(p < q){ |
| 1484 |
done = 0; |
| 1485 |
do{ |
| 1486 |
meta->aq = q; |
| 1487 |
while(p < q){ |
| 1488 |
ref(p,0) = add_object(gc, ref(p,0), "rem"); |
| 1489 |
p += (2*wordsize); |
| 1490 |
} |
| 1491 |
p = meta->aq; |
| 1492 |
q = meta->ap; |
| 1493 |
} while (p < q); |
| 1494 |
} |
| 1495 |
} |
| 1496 |
{ |
| 1497 |
meta_t* meta = &gc->meta[meta_symbol]; |
| 1498 |
ikptr p = meta->aq; |
| 1499 |
ikptr q = meta->ap; |
| 1500 |
if(p < q){ |
| 1501 |
done = 0; |
| 1502 |
do{ |
| 1503 |
meta->aq = q; |
| 1504 |
while(p < q){ |
| 1505 |
ref(p,0) = add_object(gc, ref(p,0), "sym"); |
| 1506 |
p += wordsize; |
| 1507 |
} |
| 1508 |
p = meta->aq; |
| 1509 |
q = meta->ap; |
| 1510 |
} while (p < q); |
| 1511 |
} |
| 1512 |
} |
| 1513 |
{ |
| 1514 |
meta_t* meta = &gc->meta[meta_ptrs]; |
| 1515 |
ikptr p = meta->aq; |
| 1516 |
ikptr q = meta->ap; |
| 1517 |
if(p < q){ |
| 1518 |
done = 0; |
| 1519 |
do{ |
| 1520 |
meta->aq = q; |
| 1521 |
while(p < q){ |
| 1522 |
ref(p,0) = add_object(gc, ref(p,0), "rem2"); |
| 1523 |
p += wordsize; |
| 1524 |
} |
| 1525 |
p = meta->aq; |
| 1526 |
q = meta->ap; |
| 1527 |
} while (p < q); |
| 1528 |
} |
| 1529 |
} |
| 1530 |
{ |
| 1531 |
meta_t* meta = &gc->meta[meta_code]; |
| 1532 |
ikptr p = meta->aq; |
| 1533 |
ikptr q = meta->ap; |
| 1534 |
if(p < q){ |
| 1535 |
done = 0; |
| 1536 |
do{ |
| 1537 |
meta->aq = q; |
| 1538 |
do{ |
| 1539 |
alloc_code_count--; |
| 1540 |
relocate_new_code(p, gc); |
| 1541 |
p += align(disp_code_data + unfix(ref(p, disp_code_code_size))); |
| 1542 |
} while (p < q); |
| 1543 |
p = meta->aq; |
| 1544 |
q = meta->ap; |
| 1545 |
} while (p < q); |
| 1546 |
} |
| 1547 |
} |
| 1548 |
} |
| 1549 |
/* phew */ |
| 1550 |
} while (! done); |
| 1551 |
{ |
| 1552 |
/* zero out remaining pointers */ |
| 1553 |
/* FIXME: did you hear of code reuse? */ |
| 1554 |
{ |
| 1555 |
meta_t* meta = &gc->meta[meta_pair]; |
| 1556 |
ikptr p = meta->ap; |
| 1557 |
ikptr q = meta->ep; |
| 1558 |
while(p < q){ |
| 1559 |
ref(p, 0) = 0; |
| 1560 |
p += wordsize; |
| 1561 |
} |
| 1562 |
} |
| 1563 |
{ |
| 1564 |
meta_t* meta = &gc->meta[meta_symbol]; |
| 1565 |
ikptr p = meta->ap; |
| 1566 |
ikptr q = meta->ep; |
| 1567 |
while(p < q){ |
| 1568 |
ref(p, 0) = 0; |
| 1569 |
p += wordsize; |
| 1570 |
} |
| 1571 |
} |
| 1572 |
{ |
| 1573 |
meta_t* meta = &gc->meta[meta_ptrs]; |
| 1574 |
ikptr p = meta->ap; |
| 1575 |
ikptr q = meta->ep; |
| 1576 |
while(p < q){ |
| 1577 |
ref(p, 0) = 0; |
| 1578 |
p += wordsize; |
| 1579 |
} |
| 1580 |
} |
| 1581 |
{ |
| 1582 |
meta_t* meta = &gc->meta[meta_weak]; |
| 1583 |
ikptr p = meta->ap; |
| 1584 |
ikptr q = meta->ep; |
| 1585 |
while(p < q){ |
| 1586 |
ref(p, 0) = 0; |
| 1587 |
p += wordsize; |
| 1588 |
} |
| 1589 |
} |
| 1590 |
{ |
| 1591 |
meta_t* meta = &gc->meta[meta_code]; |
| 1592 |
ikptr p = meta->ap; |
| 1593 |
ikptr q = meta->ep; |
| 1594 |
while(p < q){ |
| 1595 |
ref(p, 0) = 0; |
| 1596 |
p += wordsize; |
| 1597 |
} |
| 1598 |
} |
| 1599 |
} |
| 1600 |
} |
| 1601 |
|
| 1602 |
static void |
| 1603 |
fix_weak_pointers(gc_t* gc){ |
| 1604 |
unsigned int* segment_vec = gc->segment_vector; |
| 1605 |
ikpcb* pcb = gc->pcb; |
| 1606 |
long int lo_idx = page_index(pcb->memory_base); |
| 1607 |
long int hi_idx = page_index(pcb->memory_end); |
| 1608 |
long int i = lo_idx; |
| 1609 |
int collect_gen = gc->collect_gen; |
| 1610 |
while(i < hi_idx){ |
| 1611 |
unsigned int t = segment_vec[i]; |
| 1612 |
if((t & (type_mask|new_gen_mask)) == |
| 1613 |
(weak_pairs_type|new_gen_tag)){ |
| 1614 |
//int gen = t & gen_mask; |
| 1615 |
if (1) { //(gen > collect_gen){ |
| 1616 |
ikptr p = (ikptr)(i << pageshift); |
| 1617 |
ikptr q = p + pagesize; |
| 1618 |
while(p < q){ |
| 1619 |
ikptr x = ref(p, 0); |
| 1620 |
if(! is_fixnum(x)){ |
| 1621 |
int tag = tagof(x); |
| 1622 |
if(tag != immediate_tag){ |
| 1623 |
ikptr fst = ref(x, -tag); |
| 1624 |
if(fst == forward_ptr){ |
| 1625 |
ref(p, 0) = ref(x, wordsize-tag); |
| 1626 |
} else { |
| 1627 |
int x_gen = segment_vec[page_index(x)] & gen_mask; |
| 1628 |
if(x_gen <= collect_gen){ |
| 1629 |
ref(p, 0) = bwp_object; |
| 1630 |
} |
| 1631 |
} |
| 1632 |
} |
| 1633 |
} |
| 1634 |
p += (2*wordsize); |
| 1635 |
} |
| 1636 |
} |
| 1637 |
} |
| 1638 |
i++; |
| 1639 |
} |
| 1640 |
} |
| 1641 |
|
| 1642 |
static unsigned int dirty_mask[generation_count] = { |
| 1643 |
0x88888888, |
| 1644 |
0xCCCCCCCC, |
| 1645 |
0xEEEEEEEE, |
| 1646 |
0xFFFFFFFF, |
| 1647 |
0x00000000 |
| 1648 |
}; |
| 1649 |
|
| 1650 |
|
| 1651 |
static unsigned int cleanup_mask[generation_count] = { |
| 1652 |
0x00000000, |
| 1653 |
0x88888888, |
| 1654 |
0xCCCCCCCC, |
| 1655 |
0xEEEEEEEE, |
| 1656 |
0xFFFFFFFF |
| 1657 |
}; |
| 1658 |
|
| 1659 |
|
| 1660 |
|
| 1661 |
static void |
| 1662 |
scan_dirty_pointers_page(gc_t* gc, long int page_idx, int mask){ |
| 1663 |
unsigned int* segment_vec = (unsigned int*)(long)gc->segment_vector; |
| 1664 |
unsigned int* dirty_vec = (unsigned int*)(long)gc->pcb->dirty_vector; |
| 1665 |
unsigned int t = segment_vec[page_idx]; |
| 1666 |
unsigned int d = dirty_vec[page_idx]; |
| 1667 |
unsigned int masked_d = d & mask; |
| 1668 |
ikptr p = (ikptr)(page_idx << pageshift); |
| 1669 |
int j; |
| 1670 |
unsigned int new_d = 0; |
| 1671 |
for(j=0; j<cards_per_page; j++){ |
| 1672 |
if(masked_d & (0xF << (j*meta_dirty_shift))){ |
| 1673 |
/* dirty card */ |
| 1674 |
ikptr q = p + cardsize; |
| 1675 |
unsigned int card_d = 0; |
| 1676 |
while(p < q){ |
| 1677 |
ikptr x = ref(p, 0); |
| 1678 |
if(is_fixnum(x) || (tagof(x) == immediate_tag)){ |
| 1679 |
/* do nothing */ |
| 1680 |
} else { |
| 1681 |
ikptr y = add_object(gc, x, "nothing"); |
| 1682 |
segment_vec = gc->segment_vector; |
| 1683 |
ref(p, 0) = y; |
| 1684 |
card_d = card_d | segment_vec[page_index(y)]; |
| 1685 |
} |
| 1686 |
p += wordsize; |
| 1687 |
} |
| 1688 |
card_d = (card_d & meta_dirty_mask) >> meta_dirty_shift; |
| 1689 |
new_d = new_d | (card_d<<(j*meta_dirty_shift)); |
| 1690 |
} else { |
| 1691 |
p += cardsize; |
| 1692 |
new_d = new_d | (d & (0xF << (j*meta_dirty_shift))); |
| 1693 |
} |
| 1694 |
} |
| 1695 |
dirty_vec = (unsigned int*)(long)gc->pcb->dirty_vector; |
| 1696 |
new_d = new_d & cleanup_mask[t & gen_mask]; |
| 1697 |
dirty_vec[page_idx] = new_d; |
| 1698 |
} |
| 1699 |
|
| 1700 |
static void |
| 1701 |
scan_dirty_code_page(gc_t* gc, long int page_idx){ |
| 1702 |
ikptr p = (ikptr)(page_idx << pageshift); |
| 1703 |
ikptr start = p; |
| 1704 |
ikptr q = p + pagesize; |
| 1705 |
unsigned int* segment_vec = (unsigned int*)(long)gc->segment_vector; |
| 1706 |
unsigned int* dirty_vec = (unsigned int*)(long)gc->pcb->dirty_vector; |
| 1707 |
//unsigned int d = dirty_vec[page_idx]; |
| 1708 |
unsigned int t = segment_vec[page_idx]; |
| 1709 |
//unsigned int masked_d = d & mask; |
| 1710 |
unsigned int new_d = 0; |
| 1711 |
while(p < q){ |
| 1712 |
if(ref(p, 0) != code_tag){ |
| 1713 |
p = q; |
| 1714 |
} |
| 1715 |
else { |
| 1716 |
long int j = ((long int)p - (long int)start) / cardsize; |
| 1717 |
long int code_size = unfix(ref(p, disp_code_code_size)); |
| 1718 |
relocate_new_code(p, gc); |
| 1719 |
segment_vec = gc->segment_vector; |
| 1720 |
ikptr rvec = ref(p, disp_code_reloc_vector); |
| 1721 |
ikptr len = ref(rvec, off_vector_length); |
| 1722 |
assert(((long)len) >= 0); |
| 1723 |
unsigned long int i; |
| 1724 |
unsigned long int code_d = segment_vec[page_index(rvec)]; |
| 1725 |
for(i=0; i<len; i+=wordsize){ |
| 1726 |
ikptr r = ref(rvec, i+off_vector_data); |
| 1727 |
if(is_fixnum(r) || (tagof(r) == immediate_tag)){ |
| 1728 |
/* do nothing */ |
| 1729 |
} else { |
| 1730 |
r = add_object(gc, r, "nothing2"); |
| 1731 |
segment_vec = gc->segment_vector; |
| 1732 |
code_d = code_d | segment_vec[page_index(r)]; |
| 1733 |
} |
| 1734 |
} |
| 1735 |
new_d = new_d | (code_d<<(j*meta_dirty_shift)); |
| 1736 |
p += align(code_size + disp_code_data); |
| 1737 |
} |
| 1738 |
} |
| 1739 |
dirty_vec = (unsigned int*)(long)gc->pcb->dirty_vector; |
| 1740 |
new_d = new_d & cleanup_mask[t & gen_mask]; |
| 1741 |
dirty_vec[page_idx] = new_d; |
| 1742 |
} |
| 1743 |
|
| 1744 |
|
| 1745 |
|
| 1746 |
|
| 1747 |
|
| 1748 |
static void |
| 1749 |
scan_dirty_pages(gc_t* gc){ |
| 1750 |
ikpcb* pcb = gc->pcb; |
| 1751 |
long int lo_idx = page_index(pcb->memory_base); |
| 1752 |
long int hi_idx = page_index(pcb->memory_end); |
| 1753 |
unsigned int* dirty_vec = (unsigned int*)(long)pcb->dirty_vector; |
| 1754 |
unsigned int* segment_vec = (unsigned int*)(long)pcb->segment_vector; |
| 1755 |
int collect_gen = gc->collect_gen; |
| 1756 |
unsigned int mask = dirty_mask[collect_gen]; |
| 1757 |
long int i = lo_idx; |
| 1758 |
while(i < hi_idx){ |
| 1759 |
unsigned int d = dirty_vec[i]; |
| 1760 |
if(d & mask){ |
| 1761 |
unsigned int t = segment_vec[i]; |
| 1762 |
int tgen = t & gen_mask; |
| 1763 |
if(tgen > collect_gen){ |
| 1764 |
int type = t & type_mask; |
| 1765 |
if(type == pointers_type){ |
| 1766 |
scan_dirty_pointers_page(gc, i, mask); |
| 1767 |
dirty_vec = (unsigned int*)(long)pcb->dirty_vector; |
| 1768 |
segment_vec = (unsigned int*)(long)pcb->segment_vector; |
| 1769 |
} |
| 1770 |
else if(type == symbols_type){ |
| 1771 |
scan_dirty_pointers_page(gc, i, mask); |
| 1772 |
dirty_vec = (unsigned int*)(long)pcb->dirty_vector; |
| 1773 |
segment_vec = (unsigned int*)(long)pcb->segment_vector; |
| 1774 |
} |
| 1775 |
else if (type == weak_pairs_type){ |
| 1776 |
scan_dirty_pointers_page(gc, i, mask); |
| 1777 |
dirty_vec = (unsigned int*)(long)pcb->dirty_vector; |
| 1778 |
segment_vec = (unsigned int*)(long)pcb->segment_vector; |
| 1779 |
} |
| 1780 |
else if (type == code_type){ |
| 1781 |
scan_dirty_code_page(gc, i); |
| 1782 |
dirty_vec = (unsigned int*)(long)pcb->dirty_vector; |
| 1783 |
segment_vec = (unsigned int*)(long)pcb->segment_vector; |
| 1784 |
} |
| 1785 |
else if (t & scannable_mask) { |
| 1786 |
fprintf(stderr, "BUG: unhandled scan of type 0x%08x\n", t); |
| 1787 |
exit(-1); |
| 1788 |
} |
| 1789 |
} |
| 1790 |
} |
| 1791 |
i++; |
| 1792 |
} |
| 1793 |
} |
| 1794 |
|
| 1795 |
|
| 1796 |
|
| 1797 |
|
| 1798 |
static void |
| 1799 |
deallocate_unused_pages(gc_t* gc){ |
| 1800 |
ikpcb* pcb = gc->pcb; |
| 1801 |
int collect_gen = gc->collect_gen; |
| 1802 |
unsigned int* segment_vec = pcb->segment_vector; |
| 1803 |
ikptr memory_base = pcb->memory_base; |
| 1804 |
ikptr memory_end = pcb->memory_end; |
| 1805 |
ikptr lo_idx = page_index(memory_base); |
| 1806 |
ikptr hi_idx = page_index(memory_end); |
| 1807 |
ikptr i = lo_idx; |
| 1808 |
while(i < hi_idx){ |
| 1809 |
unsigned int t = segment_vec[i]; |
| 1810 |
if(t & dealloc_mask){ |
| 1811 |
int gen = t & old_gen_mask; |
| 1812 |
if(gen <= collect_gen){ |
| 1813 |
/* we're interested */ |
| 1814 |
if(t & new_gen_mask){ |
| 1815 |
/* do nothing yet */ |
| 1816 |
} else { |
| 1817 |
ik_munmap_from_segment((ikptr)(i<<pageshift),pagesize,pcb); |
| 1818 |
} |
| 1819 |
} |
| 1820 |
} |
| 1821 |
i++; |
| 1822 |
} |
| 1823 |
} |
| 1824 |
|
| 1825 |
|
| 1826 |
static void |
| 1827 |
fix_new_pages(gc_t* gc){ |
| 1828 |
ikpcb* pcb = gc->pcb; |
| 1829 |
unsigned int* segment_vec = pcb->segment_vector; |
| 1830 |
ikptr memory_base = pcb->memory_base; |
| 1831 |
ikptr memory_end = pcb->memory_end; |
| 1832 |
ikptr lo_idx = page_index(memory_base); |
| 1833 |
ikptr hi_idx = page_index(memory_end); |
| 1834 |
ikptr i = lo_idx; |
| 1835 |
while(i < hi_idx){ |
| 1836 |
segment_vec[i] &= ~new_gen_mask; |
| 1837 |
/* |
| 1838 |
unsigned int t = segment_vec[i]; |
| 1839 |
if(t & new_gen_mask){ |
| 1840 |
segment_vec[i] = t & ~new_gen_mask; |
| 1841 |
} |
| 1842 |
*/ |
| 1843 |
i++; |
| 1844 |
} |
| 1845 |
} |
| 1846 |
|
| 1847 |
static void |
| 1848 |
add_one_tconc(ikpcb* pcb, ikptr p){ |
| 1849 |
ikptr tcbucket = ref(p,0); |
| 1850 |
ikptr tc = ref(tcbucket, off_tcbucket_tconc); |
| 1851 |
assert(tagof(tc) == pair_tag); |
| 1852 |
ikptr d = ref(tc, off_cdr); |
| 1853 |
assert(tagof(d) == pair_tag); |
| 1854 |
ikptr new_pair = p + pair_tag; |
| 1855 |
ref(d, off_car) = tcbucket; |
| 1856 |
ref(d, off_cdr) = new_pair; |
| 1857 |
ref(new_pair, off_car) = false_object; |
| 1858 |
ref(new_pair, off_cdr) = false_object; |
| 1859 |
ref(tc, off_cdr) = new_pair; |
| 1860 |
ref(tcbucket, -vector_tag) = (ikptr)(tcbucket_size - wordsize); |
| 1861 |
((int*)(long)pcb->dirty_vector)[page_index(tc)] = -1; |
| 1862 |
((int*)(long)pcb->dirty_vector)[page_index(d)] = -1; |
| 1863 |
} |
| 1864 |
|
| 1865 |
static void |
| 1866 |
gc_add_tconcs(gc_t* gc){ |
| 1867 |
if(gc->tconc_base == 0){ |
| 1868 |
return; |
| 1869 |
} |
| 1870 |
ikpcb* pcb = gc->pcb; |
| 1871 |
{ |
| 1872 |
ikptr p = gc->tconc_base; |
| 1873 |
ikptr q = gc->tconc_ap; |
| 1874 |
while(p < q){ |
| 1875 |
add_one_tconc(pcb, p); |
| 1876 |
p += 2*wordsize; |
| 1877 |
} |
| 1878 |
} |
| 1879 |
ikpages* qu = gc->tconc_queue; |
| 1880 |
while(qu){ |
| 1881 |
ikptr p = qu->base; |
| 1882 |
ikptr q = p + qu->size; |
| 1883 |
while(p < q){ |
| 1884 |
add_one_tconc(pcb, p); |
| 1885 |
p += 2*wordsize; |
| 1886 |
} |
| 1887 |
ikpages* next = qu->next; |
| 1888 |
ik_free(qu, sizeof(ikpages)); |
| 1889 |
qu = next; |
| 1890 |
} |
| 1891 |
} |
| 1892 |
|
| 1893 |
|