| 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 <time.h> |
| 21 |
#include <stdio.h> |
| 22 |
#include <stdlib.h> |
| 23 |
#include <sys/types.h> |
| 24 |
#include <sys/stat.h> |
| 25 |
#include <limits.h> |
| 26 |
#include <fcntl.h> |
| 27 |
#include <string.h> |
| 28 |
#include <strings.h> |
| 29 |
#include <errno.h> |
| 30 |
#include <unistd.h> |
| 31 |
#include <sys/mman.h> |
| 32 |
#include <assert.h> |
| 33 |
#include <sys/time.h> |
| 34 |
#include <sys/resource.h> |
| 35 |
#include <sys/wait.h> |
| 36 |
#include <sys/param.h> |
| 37 |
#include <dirent.h> |
| 38 |
#ifdef __CYGWIN__ |
| 39 |
#include "ikarus-winmmap.h" |
| 40 |
#endif |
| 41 |
|
| 42 |
|
| 43 |
extern ikptr ik_errno_to_code(); |
| 44 |
extern ikptr ik_signal_num_to_code(int); |
| 45 |
|
| 46 |
int total_allocated_pages = 0; |
| 47 |
|
| 48 |
extern char **environ; |
| 49 |
|
| 50 |
|
| 51 |
#define segment_size (pagesize*pagesize/4) |
| 52 |
#define segment_shift (pageshift+pageshift-2) |
| 53 |
#define segment_index(x) (((unsigned long int)(x)) >> segment_shift) |
| 54 |
|
| 55 |
ikptr ik_mmap(unsigned long int size); |
| 56 |
void ik_munmap(ikptr mem, unsigned long int size); |
| 57 |
|
| 58 |
static void |
| 59 |
extend_table_maybe(ikptr p, unsigned long int size, ikpcb* pcb){ |
| 60 |
assert(size == align_to_next_page(size)); |
| 61 |
ikptr q = p + size; |
| 62 |
if(p < pcb->memory_base){ |
| 63 |
unsigned long int new_lo = segment_index(p); |
| 64 |
unsigned long int old_lo = segment_index(pcb->memory_base); |
| 65 |
unsigned long int hi = segment_index(pcb->memory_end); |
| 66 |
unsigned long int new_vec_size = (hi - new_lo) * pagesize; |
| 67 |
unsigned long int old_vec_size = (hi - old_lo) * pagesize; |
| 68 |
ikptr v = ik_mmap(new_vec_size); |
| 69 |
bzero((char*)(long)v, new_vec_size - old_vec_size); |
| 70 |
memcpy((char*)(long)(v+new_vec_size-old_vec_size), |
| 71 |
(char*)(long)pcb->dirty_vector_base, |
| 72 |
old_vec_size); |
| 73 |
ik_munmap((ikptr)(long)pcb->dirty_vector_base, old_vec_size); |
| 74 |
pcb->dirty_vector_base = (unsigned int*)(long)v; |
| 75 |
pcb->dirty_vector = (v - new_lo * pagesize); |
| 76 |
ikptr s = ik_mmap(new_vec_size); |
| 77 |
bzero((char*)(long)s, new_vec_size - old_vec_size); |
| 78 |
memcpy((char*)(long)(s+new_vec_size-old_vec_size), |
| 79 |
(char*)(long)(pcb->segment_vector_base), |
| 80 |
old_vec_size); |
| 81 |
ik_munmap((ikptr)(long)pcb->segment_vector_base, old_vec_size); |
| 82 |
pcb->segment_vector_base = (unsigned int*)(long)s; |
| 83 |
pcb->segment_vector = (unsigned int*)(long)(s - new_lo * pagesize); |
| 84 |
pcb->memory_base = (new_lo * segment_size); |
| 85 |
} |
| 86 |
else if (q >= pcb->memory_end){ |
| 87 |
unsigned long int lo = segment_index(pcb->memory_base); |
| 88 |
unsigned long int old_hi = segment_index(pcb->memory_end); |
| 89 |
unsigned long int new_hi = segment_index(q+segment_size-1); |
| 90 |
unsigned long int new_vec_size = (new_hi - lo) * pagesize; |
| 91 |
unsigned long int old_vec_size = (old_hi - lo) * pagesize; |
| 92 |
ikptr v = ik_mmap(new_vec_size); |
| 93 |
memcpy((char*)(long)v, |
| 94 |
(char*)(long)pcb->dirty_vector_base, |
| 95 |
old_vec_size); |
| 96 |
bzero((char*)(long)(v+old_vec_size), new_vec_size - old_vec_size); |
| 97 |
ik_munmap((ikptr)(long)pcb->dirty_vector_base, old_vec_size); |
| 98 |
pcb->dirty_vector_base = (unsigned int*)(long)v; |
| 99 |
pcb->dirty_vector = (v - lo * pagesize); |
| 100 |
ikptr s = ik_mmap(new_vec_size); |
| 101 |
memcpy((char*)(long)s, pcb->segment_vector_base, old_vec_size); |
| 102 |
bzero((char*)(long)(s+old_vec_size), new_vec_size - old_vec_size); |
| 103 |
ik_munmap((ikptr)(long)pcb->segment_vector_base, old_vec_size); |
| 104 |
pcb->segment_vector_base = (unsigned int*)(long) s; |
| 105 |
pcb->segment_vector = (unsigned int*)(s - lo * pagesize); |
| 106 |
pcb->memory_end = (new_hi * segment_size); |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
static void |
| 112 |
set_segment_type(ikptr base, unsigned long int size, unsigned int type, ikpcb* pcb){ |
| 113 |
assert(base >= pcb->memory_base); |
| 114 |
assert((base+size) <= pcb->memory_end); |
| 115 |
assert(size == align_to_next_page(size)); |
| 116 |
unsigned int* p = pcb->segment_vector + page_index(base); |
| 117 |
unsigned int* q = p + page_index(size); |
| 118 |
while(p < q){ |
| 119 |
*p = type; |
| 120 |
p++; |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
void |
| 125 |
ik_munmap_from_segment(ikptr base, unsigned long int size, ikpcb* pcb){ |
| 126 |
assert(base >= pcb->memory_base); |
| 127 |
assert((base+size) <= pcb->memory_end); |
| 128 |
assert(size == align_to_next_page(size)); |
| 129 |
unsigned int* p = |
| 130 |
((unsigned int*)(long)(pcb->segment_vector)) + page_index(base); |
| 131 |
unsigned int* s = |
| 132 |
((unsigned int*)(long)(pcb->dirty_vector)) + page_index(base); |
| 133 |
unsigned int* q = p + page_index(size); |
| 134 |
while(p < q){ |
| 135 |
assert(*p != hole_mt); |
| 136 |
*p = hole_mt; /* holes */ |
| 137 |
*s = 0; |
| 138 |
p++; s++; |
| 139 |
} |
| 140 |
ikpage* r = pcb->uncached_pages; |
| 141 |
if (r){ |
| 142 |
ikpage* cache = pcb->cached_pages; |
| 143 |
do{ |
| 144 |
r->base = base; |
| 145 |
ikpage* next = r->next; |
| 146 |
r->next = cache; |
| 147 |
cache = r; |
| 148 |
r = next; |
| 149 |
base += pagesize; |
| 150 |
size -= pagesize; |
| 151 |
} while(r && size); |
| 152 |
pcb->cached_pages = cache; |
| 153 |
pcb->uncached_pages = r; |
| 154 |
} |
| 155 |
if(size){ |
| 156 |
ik_munmap(base, size); |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
ikptr |
| 163 |
ik_mmap_typed(unsigned long int size, unsigned int type, ikpcb* pcb){ |
| 164 |
ikptr p; |
| 165 |
if(size == pagesize) { |
| 166 |
ikpage* s = pcb->cached_pages; |
| 167 |
if(s){ |
| 168 |
p = s->base; |
| 169 |
pcb->cached_pages = s->next; |
| 170 |
s->next = pcb->uncached_pages; |
| 171 |
pcb->uncached_pages = s; |
| 172 |
} |
| 173 |
else { |
| 174 |
p = ik_mmap(size); |
| 175 |
} |
| 176 |
} |
| 177 |
else { |
| 178 |
p = ik_mmap(size); |
| 179 |
} |
| 180 |
extend_table_maybe(p, size, pcb); |
| 181 |
set_segment_type(p, size, type, pcb); |
| 182 |
return p; |
| 183 |
} |
| 184 |
|
| 185 |
ikptr |
| 186 |
ik_mmap_ptr(unsigned long int size, int gen, ikpcb* pcb){ |
| 187 |
return ik_mmap_typed(size, pointers_mt | gen, pcb); |
| 188 |
} |
| 189 |
|
| 190 |
ikptr |
| 191 |
ik_mmap_data(unsigned long int size, int gen, ikpcb* pcb){ |
| 192 |
return ik_mmap_typed(size, data_mt | gen, pcb); |
| 193 |
} |
| 194 |
|
| 195 |
ikptr |
| 196 |
ik_mmap_code(unsigned long int size, int gen, ikpcb* pcb){ |
| 197 |
ikptr p = ik_mmap_typed(size, code_mt | gen, pcb); |
| 198 |
if(size > pagesize){ |
| 199 |
set_segment_type(p+pagesize, size-pagesize, data_mt|gen, pcb); |
| 200 |
} |
| 201 |
return p; |
| 202 |
} |
| 203 |
|
| 204 |
|
| 205 |
ikptr |
| 206 |
ik_mmap_mixed(unsigned long int size, ikpcb* pcb){ |
| 207 |
return ik_mmap_typed(size, mainheap_mt, pcb); |
| 208 |
} |
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
ikptr |
| 214 |
ik_mmap(unsigned long int size){ |
| 215 |
unsigned long int pages = (size + pagesize - 1) / pagesize; |
| 216 |
total_allocated_pages += pages; |
| 217 |
unsigned long int mapsize = pages * pagesize; |
| 218 |
assert(size == mapsize); |
| 219 |
#ifndef __CYGWIN__ |
| 220 |
char* mem = mmap( |
| 221 |
0, |
| 222 |
mapsize, |
| 223 |
PROT_READ | PROT_WRITE | PROT_EXEC, |
| 224 |
MAP_PRIVATE | MAP_ANONYMOUS, |
| 225 |
-1, |
| 226 |
0); |
| 227 |
/* FIXME: check if in range */ |
| 228 |
if(mem == MAP_FAILED){ |
| 229 |
fprintf(stderr, "Mapping (0x%lx bytes) failed: %s\n", size, strerror(errno)); |
| 230 |
exit(-1); |
| 231 |
} |
| 232 |
#else |
| 233 |
char* mem = win_mmap(mapsize); |
| 234 |
#endif |
| 235 |
memset(mem, -1, mapsize); |
| 236 |
#ifndef NDEBUG |
| 237 |
fprintf(stderr, "MMAP 0x%016lx .. 0x%016lx\n", (long int)mem, |
| 238 |
((long int)(mem))+mapsize-1); |
| 239 |
#endif |
| 240 |
return (ikptr)(long)mem; |
| 241 |
} |
| 242 |
|
| 243 |
void |
| 244 |
ik_munmap(ikptr mem, unsigned long int size){ |
| 245 |
unsigned long int pages = (size + pagesize - 1) / pagesize; |
| 246 |
unsigned long int mapsize = pages * pagesize; |
| 247 |
assert(size == mapsize); |
| 248 |
assert(((-pagesize) & (int)mem) == (int)mem); |
| 249 |
total_allocated_pages -= pages; |
| 250 |
#ifndef __CYGWIN__ |
| 251 |
int err = munmap((char*)mem, mapsize); |
| 252 |
if(err != 0){ |
| 253 |
fprintf(stderr, "ik_munmap failed: %s\n", strerror(errno)); |
| 254 |
exit(-1); |
| 255 |
} |
| 256 |
#else |
| 257 |
win_munmap((char*)mem, mapsize); |
| 258 |
#endif |
| 259 |
#ifndef NDEBUG |
| 260 |
fprintf(stderr, "UNMAP 0x%016lx .. 0x%016lx\n", (long int)mem, |
| 261 |
((long int)(mem))+mapsize-1); |
| 262 |
#endif |
| 263 |
} |
| 264 |
|
| 265 |
int total_malloced = 0; |
| 266 |
|
| 267 |
void* |
| 268 |
ik_malloc(int size){ |
| 269 |
void* x = malloc(size); |
| 270 |
if(x == NULL){ |
| 271 |
fprintf(stderr, "malloc failed: %s\n", strerror(errno)); |
| 272 |
exit(-1); |
| 273 |
} |
| 274 |
total_malloced += size; |
| 275 |
return x; |
| 276 |
} |
| 277 |
|
| 278 |
void ik_free(void* x, int size){ |
| 279 |
total_malloced -= size; |
| 280 |
free(x); |
| 281 |
} |
| 282 |
|
| 283 |
|
| 284 |
#define CACHE_SIZE (pagesize * 1) /* must be multiple of pagesize*/ |
| 285 |
|
| 286 |
ikpcb* ik_make_pcb(){ |
| 287 |
ikpcb* pcb = ik_malloc(sizeof(ikpcb)); |
| 288 |
bzero(pcb, sizeof(ikpcb)); |
| 289 |
pcb->collect_key = false_object; |
| 290 |
#define STAKSIZE (1024 * 4096) |
| 291 |
//#define STAKSIZE (256 * 4096) |
| 292 |
pcb->heap_base = ik_mmap(IK_HEAPSIZE); |
| 293 |
pcb->heap_size = IK_HEAPSIZE; |
| 294 |
pcb->allocation_pointer = pcb->heap_base; |
| 295 |
pcb->allocation_redline = pcb->heap_base + IK_HEAPSIZE - 2 * 4096; |
| 296 |
|
| 297 |
pcb->stack_base = ik_mmap(STAKSIZE); |
| 298 |
pcb->stack_size = STAKSIZE; |
| 299 |
pcb->frame_pointer = pcb->stack_base + pcb->stack_size; |
| 300 |
pcb->frame_base = pcb->frame_pointer; |
| 301 |
pcb->frame_redline = pcb->stack_base + 2 * 4096; |
| 302 |
|
| 303 |
|
| 304 |
{ /* make cache ikpage */ |
| 305 |
ikpage* p = (ikpage*)(long)ik_mmap(CACHE_SIZE * sizeof(ikpage)); |
| 306 |
pcb->cached_pages_base = (ikptr)(long)p; |
| 307 |
pcb->cached_pages_size = CACHE_SIZE * sizeof(ikpage); |
| 308 |
ikpage* q = 0; |
| 309 |
ikpage* e = p + CACHE_SIZE; |
| 310 |
while(p < e){ |
| 311 |
p->next = q; |
| 312 |
q = p; |
| 313 |
p++; |
| 314 |
} |
| 315 |
pcb->uncached_pages = q; |
| 316 |
} |
| 317 |
|
| 318 |
{ |
| 319 |
/* compute extent of heap and stack */ |
| 320 |
ikptr lo_mem; |
| 321 |
ikptr hi_mem; |
| 322 |
if(pcb->heap_base < pcb->stack_base){ |
| 323 |
lo_mem = pcb->heap_base - pagesize; |
| 324 |
hi_mem = pcb->stack_base + pcb->stack_size + pagesize; |
| 325 |
} else { |
| 326 |
lo_mem = pcb->stack_base - pagesize; |
| 327 |
hi_mem = pcb->heap_base + pcb->heap_size + pagesize; |
| 328 |
} |
| 329 |
|
| 330 |
unsigned long int lo_seg = segment_index(lo_mem); |
| 331 |
unsigned long int hi_seg = segment_index(hi_mem+segment_size-1); |
| 332 |
unsigned long int vec_size = (hi_seg - lo_seg) * pagesize; |
| 333 |
ikptr dvec = ik_mmap(vec_size); |
| 334 |
bzero((char*)(long)dvec, vec_size); |
| 335 |
pcb->dirty_vector_base = (unsigned int*)(long) dvec; |
| 336 |
pcb->dirty_vector = (dvec - lo_seg * pagesize); |
| 337 |
ikptr svec = ik_mmap(vec_size); |
| 338 |
bzero((char*)(long)svec, vec_size); |
| 339 |
pcb->segment_vector_base = (unsigned int*)(long)svec; |
| 340 |
pcb->segment_vector = (unsigned int*)(long)(svec - lo_seg * pagesize); |
| 341 |
pcb->memory_base = (ikptr)(lo_seg * segment_size); |
| 342 |
pcb->memory_end = (ikptr)(hi_seg * segment_size); |
| 343 |
set_segment_type(pcb->heap_base, |
| 344 |
pcb->heap_size, |
| 345 |
mainheap_mt, |
| 346 |
pcb); |
| 347 |
set_segment_type(pcb->stack_base, |
| 348 |
pcb->stack_size, |
| 349 |
mainstack_mt, |
| 350 |
pcb); |
| 351 |
} |
| 352 |
/* initialize base rtd */ |
| 353 |
{ |
| 354 |
ikptr r = ik_unsafe_alloc(pcb, align(rtd_size)) + rtd_tag; |
| 355 |
ref(r, off_rtd_rtd) = r; |
| 356 |
ref(r, off_rtd_length) = (ikptr) (rtd_size-wordsize); |
| 357 |
ref(r, off_rtd_name) = 0; |
| 358 |
ref(r, off_rtd_fields) = 0; |
| 359 |
ref(r, off_rtd_printer) = 0; |
| 360 |
ref(r, off_rtd_symbol) = 0; |
| 361 |
pcb->base_rtd = r; |
| 362 |
} |
| 363 |
return pcb; |
| 364 |
} |
| 365 |
|
| 366 |
void ik_delete_pcb(ikpcb* pcb){ |
| 367 |
ikpage* p = pcb->cached_pages; |
| 368 |
pcb->cached_pages = 0; |
| 369 |
pcb->uncached_pages = 0; |
| 370 |
while(p){ |
| 371 |
ik_munmap(p->base, pagesize); |
| 372 |
p = p->next; |
| 373 |
} |
| 374 |
ik_munmap(pcb->cached_pages_base, pcb->cached_pages_size); |
| 375 |
{ |
| 376 |
int i; |
| 377 |
for(i=0; i<generation_count; i++){ |
| 378 |
ik_ptr_page* p = pcb->protected_list[i]; |
| 379 |
while(p){ |
| 380 |
ik_ptr_page* next = p->next; |
| 381 |
ik_munmap((ikptr)(long)p, pagesize); |
| 382 |
p = next; |
| 383 |
} |
| 384 |
} |
| 385 |
} |
| 386 |
ikptr base = pcb->memory_base; |
| 387 |
ikptr end = pcb->memory_end; |
| 388 |
unsigned int* segment_vec = pcb->segment_vector; |
| 389 |
long int i = page_index(base); |
| 390 |
long int j = page_index(end); |
| 391 |
while(i < j){ |
| 392 |
unsigned int t = segment_vec[i]; |
| 393 |
if(t != hole_mt){ |
| 394 |
ik_munmap((ikptr)(i<<pageshift), pagesize); |
| 395 |
} |
| 396 |
i++; |
| 397 |
} |
| 398 |
long int vecsize = (segment_index(end) - segment_index(base)) * pagesize; |
| 399 |
ik_munmap((ikptr)(long)pcb->dirty_vector_base, vecsize); |
| 400 |
ik_munmap((ikptr)(long)pcb->segment_vector_base, vecsize); |
| 401 |
ik_free(pcb, sizeof(ikpcb)); |
| 402 |
} |
| 403 |
|
| 404 |
ikptr |
| 405 |
ik_safe_alloc(ikpcb* pcb, int size){ |
| 406 |
assert(size == align(size)); |
| 407 |
ikptr ap = pcb->allocation_pointer; |
| 408 |
ikptr ep = pcb->heap_base + pcb->heap_size; |
| 409 |
ikptr nap = ap + size; |
| 410 |
if(nap < ep){ |
| 411 |
pcb->allocation_pointer = nap; |
| 412 |
return ap; |
| 413 |
} |
| 414 |
else { |
| 415 |
ik_collect(size, pcb); |
| 416 |
ikptr ap = pcb->allocation_pointer; |
| 417 |
ikptr ep = pcb->heap_base + pcb->heap_size; |
| 418 |
ikptr nap = ap + size; |
| 419 |
if(nap < ep){ |
| 420 |
pcb->allocation_pointer = nap; |
| 421 |
return ap; |
| 422 |
} else { |
| 423 |
fprintf(stderr, |
| 424 |
"ikaurs: BUG: collector did not leave enough room for %d\n", |
| 425 |
size); |
| 426 |
exit(-1); |
| 427 |
} |
| 428 |
} |
| 429 |
} |
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
ikptr |
| 435 |
ik_unsafe_alloc(ikpcb* pcb, int size){ |
| 436 |
assert(size == align(size)); |
| 437 |
ikptr ap = pcb->allocation_pointer; |
| 438 |
ikptr ep = pcb->heap_base + pcb->heap_size; |
| 439 |
ikptr nap = ap + size; |
| 440 |
if(nap < ep){ |
| 441 |
pcb->allocation_pointer = nap; |
| 442 |
return ap; |
| 443 |
} |
| 444 |
else { |
| 445 |
if(ap){ |
| 446 |
ikpages* p = ik_malloc(sizeof(ikpages)); |
| 447 |
p->base = pcb->heap_base; |
| 448 |
p->size = pcb->heap_size; |
| 449 |
p->next = pcb->heap_pages; |
| 450 |
pcb->heap_pages = p; |
| 451 |
} |
| 452 |
|
| 453 |
{ /* ACCOUNTING */ |
| 454 |
long int bytes = |
| 455 |
((long int)pcb->allocation_pointer) - |
| 456 |
((long int)pcb->heap_base); |
| 457 |
long int minor = bytes + pcb->allocation_count_minor; |
| 458 |
while(minor >= most_bytes_in_minor){ |
| 459 |
minor -= most_bytes_in_minor; |
| 460 |
pcb->allocation_count_major++; |
| 461 |
} |
| 462 |
pcb->allocation_count_minor = minor; |
| 463 |
} |
| 464 |
|
| 465 |
int new_size = (size > IK_HEAP_EXT_SIZE) ? size : IK_HEAP_EXT_SIZE; |
| 466 |
new_size += 2 * 4096; |
| 467 |
new_size = align_to_next_page(new_size); |
| 468 |
ap = ik_mmap_mixed(new_size, pcb); |
| 469 |
pcb->heap_base = ap; |
| 470 |
pcb->heap_size = new_size; |
| 471 |
pcb->allocation_redline = ap + new_size - 2 * 4096; |
| 472 |
nap = ap + size; |
| 473 |
pcb->allocation_pointer = nap; |
| 474 |
return ap; |
| 475 |
} |
| 476 |
} |
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
void ik_error(ikptr args){ |
| 481 |
fprintf(stderr, "Error: "); |
| 482 |
ik_fprint(stderr, args); |
| 483 |
fprintf(stderr, "\n"); |
| 484 |
exit(0); |
| 485 |
} |
| 486 |
|
| 487 |
|
| 488 |
void ik_stack_overflow(ikpcb* pcb){ |
| 489 |
#ifndef NDEBUG |
| 490 |
fprintf(stderr, "entered ik_stack_overflow pcb=0x%016lx\n", (long int)pcb); |
| 491 |
#endif |
| 492 |
set_segment_type(pcb->stack_base, pcb->stack_size, data_mt, pcb); |
| 493 |
|
| 494 |
ikptr frame_base = pcb->frame_base; |
| 495 |
ikptr underflow_handler = ref(frame_base, -wordsize); |
| 496 |
#ifndef NDEBUG |
| 497 |
fprintf(stderr, "underflow_handler = 0x%08x\n", (int)underflow_handler); |
| 498 |
#endif |
| 499 |
/* capture continuation and set it as next_k */ |
| 500 |
ikptr k = ik_unsafe_alloc(pcb, align(continuation_size)) + vector_tag; |
| 501 |
ref(k, -vector_tag) = continuation_tag; |
| 502 |
ref(k, off_continuation_top) = pcb->frame_pointer; |
| 503 |
ref(k, off_continuation_size) = |
| 504 |
pcb->frame_base - pcb->frame_pointer - wordsize; |
| 505 |
ref(k, off_continuation_next) = pcb->next_k; |
| 506 |
pcb->next_k = k; |
| 507 |
|
| 508 |
pcb->stack_base = (ikptr)(long)ik_mmap_typed(STAKSIZE, mainstack_mt, pcb); |
| 509 |
pcb->stack_size = STAKSIZE; |
| 510 |
pcb->frame_base = pcb->stack_base + pcb->stack_size; |
| 511 |
pcb->frame_pointer = pcb->frame_base - wordsize; |
| 512 |
pcb->frame_redline = pcb->stack_base + 2 * 4096; |
| 513 |
ref(pcb->frame_pointer, 0) = underflow_handler; |
| 514 |
return; |
| 515 |
} |
| 516 |
|
| 517 |
/* |
| 518 |
char* ik_uuid(char* str){ |
| 519 |
assert((36 << fx_shift) == (int) ref(str, disp_string_length - string_tag)); |
| 520 |
uuid_t u; |
| 521 |
uuid_clear(u); |
| 522 |
uuid_generate(u); |
| 523 |
uuid_unparse_upper(u, str + disp_string_data - string_tag); |
| 524 |
return str; |
| 525 |
} |
| 526 |
*/ |
| 527 |
|
| 528 |
|
| 529 |
static const char* uuid_chars = |
| 530 |
"!$%&/0123456789<=>?ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; |
| 531 |
static int uuid_strlen = 1; |
| 532 |
ikptr ik_uuid(ikptr bv){ |
| 533 |
static int fd = -1; |
| 534 |
if(fd == -1){ |
| 535 |
fd = open("/dev/urandom", O_RDONLY); |
| 536 |
if(fd == -1){ |
| 537 |
return ik_errno_to_code(); |
| 538 |
} |
| 539 |
uuid_strlen = strlen(uuid_chars); |
| 540 |
} |
| 541 |
long int n = unfix(ref(bv, off_bytevector_length)); |
| 542 |
unsigned char* data = (unsigned char*)(long)(bv+off_bytevector_data); |
| 543 |
int r = read(fd, data, n); |
| 544 |
if(r < 0){ |
| 545 |
return ik_errno_to_code(); |
| 546 |
} |
| 547 |
unsigned char* p = data; |
| 548 |
unsigned char* q = data + n; |
| 549 |
while(p < q){ |
| 550 |
*p = uuid_chars[*p % uuid_strlen]; |
| 551 |
p++; |
| 552 |
} |
| 553 |
return bv; |
| 554 |
} |
| 555 |
|
| 556 |
|
| 557 |
ikptr |
| 558 |
ikrt_stat(ikptr filename, ikptr follow /*, ikpcb* pcb */){ |
| 559 |
char* fn = (char*)(filename + off_bytevector_data); |
| 560 |
struct stat s; |
| 561 |
int r; |
| 562 |
if(follow == false_object){ |
| 563 |
r = lstat(fn, &s); |
| 564 |
} else{ |
| 565 |
r = stat(fn, &s); |
| 566 |
} |
| 567 |
if(r == 0){ |
| 568 |
if(S_ISREG(s.st_mode)){ |
| 569 |
return fix(1); |
| 570 |
} |
| 571 |
else if(S_ISDIR(s.st_mode)){ |
| 572 |
return fix(2); |
| 573 |
} |
| 574 |
else if(S_ISLNK(s.st_mode)){ |
| 575 |
return fix(3); |
| 576 |
} |
| 577 |
else { |
| 578 |
return fix(0); |
| 579 |
} |
| 580 |
} |
| 581 |
return ik_errno_to_code(); |
| 582 |
} |
| 583 |
|
| 584 |
|
| 585 |
/* ikrt_file_exists needs to be removed. |
| 586 |
This is here only to be able to use old ikarus.boot.prebuilt */ |
| 587 |
ikptr |
| 588 |
ikrt_file_exists(ikptr filename /*, ikpcb* pcb */){ |
| 589 |
switch (ikrt_stat(filename, true_object /*, pcb */)){ |
| 590 |
case fix(0): |
| 591 |
case fix(1): |
| 592 |
case fix(2): |
| 593 |
case fix(3): |
| 594 |
return true_object; |
| 595 |
default: |
| 596 |
return false_object; |
| 597 |
} |
| 598 |
} |
| 599 |
|
| 600 |
ikptr |
| 601 |
ikrt_delete_file(ikptr filename){ |
| 602 |
char* str; |
| 603 |
if(tagof(filename) == bytevector_tag){ |
| 604 |
str = (char*)(long)(filename + off_bytevector_data); |
| 605 |
} else { |
| 606 |
fprintf(stderr, "bug in ikrt_delete_file\n"); |
| 607 |
exit(-1); |
| 608 |
} |
| 609 |
int err = unlink(str); |
| 610 |
if(err == 0){ |
| 611 |
return true_object; |
| 612 |
} |
| 613 |
return ik_errno_to_code(); |
| 614 |
} |
| 615 |
|
| 616 |
ikptr |
| 617 |
ikrt_directory_list(ikptr filename, ikpcb* pcb){ |
| 618 |
DIR* dir; |
| 619 |
struct dirent* de; |
| 620 |
if((dir = opendir((char*)(filename + off_bytevector_data))) == NULL){ |
| 621 |
return ik_errno_to_code(); |
| 622 |
} |
| 623 |
ikptr ac = null_object; |
| 624 |
pcb->root0 = ∾ |
| 625 |
while(1){ |
| 626 |
errno = 0; |
| 627 |
de = readdir(dir); |
| 628 |
if(de == NULL){ |
| 629 |
pcb->root0 = 0; |
| 630 |
ikptr retval = (errno ? ik_errno_to_code() : ac); |
| 631 |
closedir(dir); |
| 632 |
return retval; |
| 633 |
} |
| 634 |
int len = strlen(de->d_name); |
| 635 |
ikptr bv = ik_safe_alloc(pcb, align(disp_bytevector_data+len+1)) |
| 636 |
+ bytevector_tag; |
| 637 |
ref(bv, off_bytevector_length) = fix(len); |
| 638 |
memcpy((char*)(bv+off_bytevector_data), de->d_name, len+1); |
| 639 |
pcb->root1 = &bv; |
| 640 |
ikptr p = ik_safe_alloc(pcb, pair_size) + pair_tag; |
| 641 |
pcb->root1 = 0; |
| 642 |
ref(p, off_car) = bv; |
| 643 |
ref(p, off_cdr) = ac; |
| 644 |
ac = p; |
| 645 |
} |
| 646 |
} |
| 647 |
|
| 648 |
ikptr |
| 649 |
ikrt_mkdir(ikptr path, ikptr mode /*, ikpcb* pcb */){ |
| 650 |
int r = mkdir((char*)(path+off_bytevector_data), unfix(mode)); |
| 651 |
if(r == 0){ |
| 652 |
return true_object; |
| 653 |
} |
| 654 |
return ik_errno_to_code(); |
| 655 |
} |
| 656 |
|
| 657 |
ikptr |
| 658 |
ikrt_rmdir(ikptr path /*, ikpcb* pcb */){ |
| 659 |
int r = rmdir((char*)(path+off_bytevector_data)); |
| 660 |
if(r == 0){ |
| 661 |
return true_object; |
| 662 |
} |
| 663 |
return ik_errno_to_code(); |
| 664 |
} |
| 665 |
|
| 666 |
ikptr |
| 667 |
ikrt_chmod(ikptr path, ikptr mode /*, ikpcb* pcb */){ |
| 668 |
int r = chmod((char*)(path+off_bytevector_data), (mode_t)unfix(mode)); |
| 669 |
if(r == 0){ |
| 670 |
return true_object; |
| 671 |
} |
| 672 |
return ik_errno_to_code(); |
| 673 |
} |
| 674 |
|
| 675 |
ikptr |
| 676 |
ikrt_symlink(ikptr to, ikptr path /*, ikpcb* pcb */){ |
| 677 |
int r = symlink((char*)(to+off_bytevector_data), (char*)(path+off_bytevector_data)); |
| 678 |
if(r == 0){ |
| 679 |
return true_object; |
| 680 |
} |
| 681 |
return ik_errno_to_code(); |
| 682 |
} |
| 683 |
|
| 684 |
ikptr |
| 685 |
ikrt_link(ikptr to, ikptr path /*, ikpcb* pcb */){ |
| 686 |
int r = link((char*)(to+off_bytevector_data), (char*)(path+off_bytevector_data)); |
| 687 |
if(r == 0){ |
| 688 |
return true_object; |
| 689 |
} |
| 690 |
return ik_errno_to_code(); |
| 691 |
} |
| 692 |
|
| 693 |
ikptr |
| 694 |
ikrt_realpath(ikptr bv, ikpcb* pcb){ |
| 695 |
char buff[PATH_MAX]; |
| 696 |
char* p = realpath((char*)(bv+off_bytevector_data), buff); |
| 697 |
if(p == NULL){ |
| 698 |
return ik_errno_to_code(); |
| 699 |
} |
| 700 |
int n = strlen(p); |
| 701 |
ikptr r = ik_safe_alloc(pcb, align(disp_bytevector_data+n+1)); |
| 702 |
ref(r, 0) = fix(n); |
| 703 |
memcpy((char*)(r+disp_bytevector_data), p, n+1); |
| 704 |
return r+bytevector_tag; |
| 705 |
} |
| 706 |
|
| 707 |
ikptr |
| 708 |
ik_system(ikptr str){ |
| 709 |
if(tagof(str) == bytevector_tag){ |
| 710 |
int r = system((char*)(long)(str+off_bytevector_data)); |
| 711 |
if(r >= 0) { |
| 712 |
return fix(r); |
| 713 |
} else { |
| 714 |
return ik_errno_to_code(); |
| 715 |
} |
| 716 |
} else { |
| 717 |
fprintf(stderr, "bug in ik_system\n"); |
| 718 |
exit(-1); |
| 719 |
} |
| 720 |
} |
| 721 |
|
| 722 |
static char* |
| 723 |
mtname(unsigned int n){ |
| 724 |
if(n == mainheap_type) { return "HEAP_T"; } |
| 725 |
if(n == mainstack_type) { return "STAK_T"; } |
| 726 |
if(n == pointers_type) { return "PTER_T"; } |
| 727 |
if(n == dat_type) { return "DATA_T"; } |
| 728 |
if(n == code_type) { return "CODE_T"; } |
| 729 |
if(n == hole_type) { return " "; } |
| 730 |
return "WHAT_T"; |
| 731 |
} |
| 732 |
|
| 733 |
ikptr |
| 734 |
ik_dump_metatable(ikpcb* pcb){ |
| 735 |
unsigned int* s = pcb->segment_vector_base; |
| 736 |
ikptr p = pcb->memory_base; |
| 737 |
ikptr hi = pcb->memory_end; |
| 738 |
while(p < hi){ |
| 739 |
unsigned int t = *s & type_mask; |
| 740 |
ikptr start = p; |
| 741 |
p += pagesize; |
| 742 |
s++; |
| 743 |
while((p < hi) && ((*s & type_mask) == t)){ |
| 744 |
p += pagesize; |
| 745 |
s++; |
| 746 |
} |
| 747 |
fprintf(stderr, "0x%016lx + %5ld pages = %s\n", |
| 748 |
(long int) start, |
| 749 |
((long int)p-(long int)start)/pagesize, |
| 750 |
mtname(t)); |
| 751 |
} |
| 752 |
return void_object; |
| 753 |
} |
| 754 |
|
| 755 |
ikptr |
| 756 |
ik_dump_dirty_vector(ikpcb* pcb){ |
| 757 |
unsigned int* s = pcb->dirty_vector_base; |
| 758 |
ikptr p = pcb->memory_base; |
| 759 |
ikptr hi = pcb->memory_end; |
| 760 |
while(p < hi){ |
| 761 |
unsigned int t = *s; |
| 762 |
ikptr start = p; |
| 763 |
p += pagesize; |
| 764 |
s++; |
| 765 |
while((p < hi) && (*s == t)){ |
| 766 |
p += pagesize; |
| 767 |
s++; |
| 768 |
} |
| 769 |
fprintf(stderr, "0x%016lx + %5ld pages = 0x%08x\n", |
| 770 |
(long int) start, |
| 771 |
((long int)p-(long int)start)/pagesize, |
| 772 |
t); |
| 773 |
} |
| 774 |
return void_object; |
| 775 |
} |
| 776 |
|
| 777 |
ikptr |
| 778 |
ikrt_make_code(ikptr codesizeptr, ikptr freevars, ikptr rvec, ikpcb* pcb){ |
| 779 |
assert((fx_mask & (int)codesizeptr) == 0); |
| 780 |
long int code_size = unfix(codesizeptr); |
| 781 |
long int memreq = align_to_next_page(code_size + disp_code_data); |
| 782 |
ikptr mem = ik_mmap_code(memreq, 0, pcb); |
| 783 |
bzero((char*)(long)mem, memreq); |
| 784 |
ref(mem, 0) = code_tag; |
| 785 |
ref(mem, disp_code_code_size) = codesizeptr; |
| 786 |
ref(mem, disp_code_freevars) = freevars; |
| 787 |
ref(mem, disp_code_reloc_vector) = rvec; |
| 788 |
ref(mem, disp_code_annotation) = false_object; |
| 789 |
ik_relocate_code(mem); |
| 790 |
return mem+vector_tag; |
| 791 |
} |
| 792 |
|
| 793 |
ikptr |
| 794 |
ikrt_set_code_reloc_vector(ikptr code, ikptr vec, ikpcb* pcb){ |
| 795 |
ref(code, off_code_reloc_vector) = vec; |
| 796 |
ik_relocate_code(code-vector_tag); |
| 797 |
((unsigned int*)(long)pcb->dirty_vector)[page_index(code)] = -1; |
| 798 |
return void_object; |
| 799 |
} |
| 800 |
|
| 801 |
ikptr |
| 802 |
ikrt_set_code_annotation(ikptr code, ikptr annot, ikpcb* pcb){ |
| 803 |
ref(code, off_code_annotation) = annot; |
| 804 |
((unsigned int*)(long)pcb->dirty_vector)[page_index(code)] = -1; |
| 805 |
return void_object; |
| 806 |
} |
| 807 |
|
| 808 |
|
| 809 |
|
| 810 |
ikptr |
| 811 |
ikrt_bvftime(ikptr outbv, ikptr fmtbv){ |
| 812 |
time_t t; |
| 813 |
struct tm* tmp; |
| 814 |
t = time(NULL); |
| 815 |
tmp = localtime(&t); |
| 816 |
if(tmp == NULL){ |
| 817 |
fprintf(stderr, "Error in time: %s\n", strerror(errno)); |
| 818 |
} |
| 819 |
int rv = |
| 820 |
strftime((char*)(long)(outbv+off_bytevector_data), |
| 821 |
unfix(ref(outbv, off_bytevector_length)) + 1, |
| 822 |
(char*)(long)(fmtbv+off_bytevector_data), |
| 823 |
tmp); |
| 824 |
if(rv == 0){ |
| 825 |
fprintf(stderr, "Error in strftime: %s\n", strerror(errno)); |
| 826 |
} |
| 827 |
return fix(rv); |
| 828 |
} |
| 829 |
|
| 830 |
ikptr |
| 831 |
ikrt_register_guardian_pair(ikptr p0, ikpcb* pcb){ |
| 832 |
ik_ptr_page* x = pcb->protected_list[0]; |
| 833 |
if((x == NULL) || (x->count == ik_ptr_page_size)){ |
| 834 |
assert(sizeof(ik_ptr_page) == pagesize); |
| 835 |
ik_ptr_page* y = (ik_ptr_page*)(long)ik_mmap(pagesize); |
| 836 |
y->count = 0; |
| 837 |
y->next = x; |
| 838 |
pcb->protected_list[0] = y; |
| 839 |
x = y; |
| 840 |
} |
| 841 |
x->ptr[x->count++] = p0; |
| 842 |
return void_object; |
| 843 |
} |
| 844 |
|
| 845 |
ikptr |
| 846 |
ikrt_register_guardian(ikptr tc, ikptr obj, ikpcb* pcb){ |
| 847 |
ikptr p0 = ik_unsafe_alloc(pcb, pair_size) + pair_tag; |
| 848 |
ref(p0, off_car) = tc; |
| 849 |
ref(p0, off_cdr) = obj; |
| 850 |
return ikrt_register_guardian_pair(p0, pcb); |
| 851 |
} |
| 852 |
|
| 853 |
ikptr |
| 854 |
ikrt_stats_now(ikptr t, ikpcb* pcb){ |
| 855 |
struct rusage r; |
| 856 |
struct timeval s; |
| 857 |
|
| 858 |
gettimeofday(&s, 0); |
| 859 |
getrusage(RUSAGE_SELF, &r); |
| 860 |
ref(t, off_record_data) = fix(r.ru_utime.tv_sec); |
| 861 |
ref(t, off_record_data + wordsize) = fix(r.ru_utime.tv_usec); |
| 862 |
ref(t, off_record_data + 2 * wordsize) = fix(r.ru_stime.tv_sec); |
| 863 |
ref(t, off_record_data + 3 * wordsize) = fix(r.ru_stime.tv_usec); |
| 864 |
ref(t, off_record_data + 4 * wordsize) = fix(s.tv_sec); |
| 865 |
ref(t, off_record_data + 5 * wordsize) = fix(s.tv_usec); |
| 866 |
ref(t, off_record_data + 6 * wordsize) = fix(pcb->collection_id); |
| 867 |
ref(t, off_record_data + 7 * wordsize) = fix(pcb->collect_utime.tv_sec); |
| 868 |
ref(t, off_record_data + 8 * wordsize) = fix(pcb->collect_utime.tv_usec); |
| 869 |
ref(t, off_record_data + 9 * wordsize) = fix(pcb->collect_stime.tv_sec); |
| 870 |
ref(t, off_record_data + 10 * wordsize) = fix(pcb->collect_stime.tv_usec); |
| 871 |
ref(t, off_record_data + 11 * wordsize) = fix(pcb->collect_rtime.tv_sec); |
| 872 |
ref(t, off_record_data + 12 * wordsize) = fix(pcb->collect_rtime.tv_usec); |
| 873 |
{ |
| 874 |
/* minor bytes */ |
| 875 |
long int bytes_in_heap = ((long int) pcb->allocation_pointer) - |
| 876 |
((long int) pcb->heap_base); |
| 877 |
long int bytes = bytes_in_heap + pcb->allocation_count_minor; |
| 878 |
ref(t, off_record_data + 13 * wordsize) = fix(bytes); |
| 879 |
} |
| 880 |
/* major bytes */ |
| 881 |
ref(t, off_record_data + 14 * wordsize) = fix(pcb->allocation_count_major); |
| 882 |
return void_object; |
| 883 |
} |
| 884 |
|
| 885 |
ikptr |
| 886 |
ikrt_current_time(ikptr t){ |
| 887 |
struct timeval s; |
| 888 |
gettimeofday(&s, 0); |
| 889 |
/* this will break in 8,727,224 years if we stay in 32-bit ptrs */ |
| 890 |
ref(t, off_record_data + 0*wordsize) = fix(s.tv_sec / 1000000); |
| 891 |
ref(t, off_record_data + 1*wordsize) = fix(s.tv_sec % 1000000); |
| 892 |
ref(t, off_record_data + 2*wordsize) = fix(s.tv_usec); |
| 893 |
return t; |
| 894 |
} |
| 895 |
|
| 896 |
ikptr |
| 897 |
ikrt_gmt_offset(ikptr t){ |
| 898 |
time_t clock = |
| 899 |
unfix(ref(t, off_record_data + 0*wordsize)) * 1000000 |
| 900 |
+ unfix(ref(t, off_record_data + 1*wordsize)); |
| 901 |
struct tm* m = gmtime(&clock); |
| 902 |
time_t gmtclock = mktime(m); |
| 903 |
return fix(clock - gmtclock); |
| 904 |
/* |
| 905 |
struct tm* m = localtime(&clock); |
| 906 |
ikptr r = fix(m->tm_gmtoff); |
| 907 |
return r; |
| 908 |
*/ |
| 909 |
} |
| 910 |
|
| 911 |
ikptr |
| 912 |
ikrt_fork(){ |
| 913 |
int pid = fork(); |
| 914 |
if(pid >= 0){ |
| 915 |
return fix(pid); |
| 916 |
} else { |
| 917 |
return ik_errno_to_code(); |
| 918 |
} |
| 919 |
} |
| 920 |
|
| 921 |
|
| 922 |
|
| 923 |
ikptr |
| 924 |
ikrt_getenv(ikptr bv, ikpcb* pcb){ |
| 925 |
char* v = getenv((char*)(long)(bv + off_bytevector_data)); |
| 926 |
if(v){ |
| 927 |
long int n = strlen(v); |
| 928 |
ikptr s = ik_safe_alloc(pcb, align(n+disp_bytevector_data+1)) |
| 929 |
+ bytevector_tag; |
| 930 |
ref(s, -bytevector_tag) = fix(n); |
| 931 |
memcpy((char*)(long)(s+off_bytevector_data), v, n+1); |
| 932 |
return s; |
| 933 |
} |
| 934 |
else { |
| 935 |
return false_object; |
| 936 |
} |
| 937 |
} |
| 938 |
|
| 939 |
ikptr |
| 940 |
ikrt_make_vector1(ikptr len, ikpcb* pcb){ |
| 941 |
int intlen = (int)len; |
| 942 |
if(is_fixnum(len) && (intlen >= 0)){ |
| 943 |
ikptr s = ik_safe_alloc(pcb, align(len + disp_vector_data)); |
| 944 |
ref(s, 0) = len; |
| 945 |
memset((char*)(long)(s+disp_vector_data), 0, len); |
| 946 |
return s+vector_tag; |
| 947 |
} else { |
| 948 |
return 0; |
| 949 |
} |
| 950 |
} |
| 951 |
|
| 952 |
#if 0 |
| 953 |
ikptr |
| 954 |
ikrt_make_vector2(ikptr len, ikptr obj, ikpcb* pcb){ |
| 955 |
if(is_fixnum(len) && ((len >> 31)!=0)){ |
| 956 |
pcb->root0 = &obj; |
| 957 |
ikptr s = ik_safe_alloc(pcb, align(((int)len) + disp_vector_data)); |
| 958 |
pcb->root0 = 0; |
| 959 |
ref(s, 0) = len; |
| 960 |
memset(s+disp_vector_data, 0, (int)len); |
| 961 |
return s+vector_tag; |
| 962 |
} else { |
| 963 |
return false_object; |
| 964 |
} |
| 965 |
} |
| 966 |
#endif |
| 967 |
|
| 968 |
|
| 969 |
ikptr |
| 970 |
ikrt_setenv(ikptr key, ikptr val, ikptr overwrite){ |
| 971 |
int err = setenv((char*)(key+off_bytevector_data), |
| 972 |
(char*)(val+off_bytevector_data), |
| 973 |
overwrite!=false_object); |
| 974 |
if(err){ |
| 975 |
return false_object; |
| 976 |
} else { |
| 977 |
return true_object; |
| 978 |
} |
| 979 |
} |
| 980 |
|
| 981 |
ikptr |
| 982 |
ikrt_unsetenv(ikptr key){ |
| 983 |
unsetenv((char*)(key+off_bytevector_data)); |
| 984 |
return void_object; |
| 985 |
} |
| 986 |
|
| 987 |
|
| 988 |
|
| 989 |
ikptr |
| 990 |
ikrt_environ(ikpcb* pcb){ |
| 991 |
char** es = environ; |
| 992 |
int i; char* e; |
| 993 |
ikptr ac = null_object; |
| 994 |
pcb->root0 = ∾ |
| 995 |
for(i=0; (e=es[i]); i++){ |
| 996 |
long int n = strlen(e); |
| 997 |
ikptr s = ik_safe_alloc(pcb, align(n+disp_bytevector_data+1)) |
| 998 |
+ bytevector_tag; |
| 999 |
ref(s, -bytevector_tag) = fix(n); |
| 1000 |
memcpy((char*)(long)(s+off_bytevector_data), e, n+1); |
| 1001 |
pcb->root1 = &s; |
| 1002 |
ikptr p = ik_safe_alloc(pcb, pair_size) + pair_tag; |
| 1003 |
pcb->root1 = 0; |
| 1004 |
ref(p, off_cdr) = ac; |
| 1005 |
ref(p, off_car) = s; |
| 1006 |
ac = p; |
| 1007 |
} |
| 1008 |
pcb->root0 = 0; |
| 1009 |
return ac; |
| 1010 |
} |
| 1011 |
|
| 1012 |
ikptr |
| 1013 |
ikrt_exit(ikptr status, ikpcb* pcb){ |
| 1014 |
ik_delete_pcb(pcb); |
| 1015 |
assert(total_allocated_pages == 0); |
| 1016 |
if(is_fixnum(status)){ |
| 1017 |
exit(unfix(status)); |
| 1018 |
} else { |
| 1019 |
exit(EXIT_FAILURE); |
| 1020 |
} |
| 1021 |
} |
| 1022 |
|
| 1023 |
ikptr |
| 1024 |
ikrt_nanosleep(ikptr secs, ikptr nsecs /*, ikpcb* pcb */){ |
| 1025 |
struct timespec t; |
| 1026 |
t.tv_sec = |
| 1027 |
is_fixnum(secs) |
| 1028 |
? (unsigned long) unfix(secs) |
| 1029 |
: ref(secs, off_bignum_data); |
| 1030 |
t.tv_nsec = |
| 1031 |
is_fixnum(nsecs) |
| 1032 |
? (unsigned long) unfix(nsecs) |
| 1033 |
: ref(nsecs, off_bignum_data); |
| 1034 |
return fix(nanosleep(&t, NULL)); |
| 1035 |
} |
| 1036 |
|
| 1037 |
ikptr |
| 1038 |
ikrt_chdir(ikptr pathbv /*, ikpcb* pcb */){ |
| 1039 |
int err = chdir(off_bytevector_data+(char*)pathbv); |
| 1040 |
if(err == 0){ |
| 1041 |
return true_object; |
| 1042 |
} |
| 1043 |
return ik_errno_to_code(); |
| 1044 |
} |
| 1045 |
|
| 1046 |
ikptr |
| 1047 |
ikrt_getcwd(ikpcb* pcb){ |
| 1048 |
char buff[MAXPATHLEN+1]; |
| 1049 |
char* path = getcwd(buff, MAXPATHLEN); |
| 1050 |
if(! path){ |
| 1051 |
return ik_errno_to_code(); |
| 1052 |
} |
| 1053 |
int len = strlen(path); |
| 1054 |
ikptr bv = ik_safe_alloc(pcb, align(disp_bytevector_data+len+1)); |
| 1055 |
ref(bv,0) = fix(len); |
| 1056 |
memcpy(disp_bytevector_data+(char*)(bv), path, len+1); |
| 1057 |
return bv+bytevector_tag; |
| 1058 |
} |
| 1059 |
|
| 1060 |
|
| 1061 |
|
| 1062 |
ikptr |
| 1063 |
ikrt_debug(ikptr x){ |
| 1064 |
fprintf(stderr, "DEBUG 0x%016lx\n", (long int)x); |
| 1065 |
return 0; |
| 1066 |
} |
| 1067 |
|
| 1068 |
ikptr |
| 1069 |
ikrt_access(ikptr filename, ikptr how /*, ikpcb* pcb */){ |
| 1070 |
char* fn = (char*)(filename + off_bytevector_data); |
| 1071 |
int r; |
| 1072 |
int ik_how; |
| 1073 |
int c_how; |
| 1074 |
|
| 1075 |
ik_how = unfix(how); |
| 1076 |
if (ik_how == 0) { |
| 1077 |
c_how = F_OK; |
| 1078 |
} else { |
| 1079 |
c_how = 0; |
| 1080 |
if (ik_how & 1) c_how |= R_OK; |
| 1081 |
if (ik_how & 2) c_how |= W_OK; |
| 1082 |
if (ik_how & 4) c_how |= X_OK; |
| 1083 |
} |
| 1084 |
|
| 1085 |
r = access(fn, c_how); |
| 1086 |
if (r == 0) { |
| 1087 |
return true_object; |
| 1088 |
} else if ((errno == EACCES) || |
| 1089 |
(errno == EROFS) || |
| 1090 |
(errno == ETXTBSY)) { |
| 1091 |
return false_object; |
| 1092 |
} else { |
| 1093 |
return ik_errno_to_code(); |
| 1094 |
} |
| 1095 |
} |
| 1096 |
|
| 1097 |
ikptr |
| 1098 |
ikrt_file_size(ikptr filename, ikpcb* pcb){ |
| 1099 |
char* fn = (char*)(filename + off_bytevector_data); |
| 1100 |
struct stat s; |
| 1101 |
int r = stat(fn, &s); |
| 1102 |
if (r == 0) { |
| 1103 |
if (sizeof(off_t) == sizeof(long)) { |
| 1104 |
return u_to_number(s.st_size, pcb); |
| 1105 |
} else if (sizeof(off_t) == sizeof(long long)) { |
| 1106 |
return ull_to_number(s.st_size, pcb); |
| 1107 |
} else { |
| 1108 |
fprintf(stderr, "internal error in ikarus: invalid off_t size\n"); |
| 1109 |
exit(-1); |
| 1110 |
} |
| 1111 |
} else { |
| 1112 |
return ik_errno_to_code(); |
| 1113 |
} |
| 1114 |
} |
| 1115 |
|
| 1116 |
ikptr |
| 1117 |
ikrt_rename_file(ikptr src, ikptr dst /* ikpcb* pcb */){ |
| 1118 |
int err = rename((char*)(src + off_bytevector_data), |
| 1119 |
(char*)(dst + off_bytevector_data)); |
| 1120 |
if (err == 0) { |
| 1121 |
return true_object; |
| 1122 |
} else { |
| 1123 |
return ik_errno_to_code(); |
| 1124 |
} |
| 1125 |
} |
| 1126 |
|
| 1127 |
ikptr |
| 1128 |
ikrt_last_errno(ikpcb* pcb){ |
| 1129 |
return s_to_number(pcb->last_errno, pcb); |
| 1130 |
} |
| 1131 |
|
| 1132 |
ikptr |
| 1133 |
ikrt_interrupted_signal(ikpcb* pcb){ |
| 1134 |
return ik_signal_num_to_code(pcb->interrupted); |
| 1135 |
} |