annotate keiko/gc0.c @ 0:bfdcc3820b32

Basis
author Mike Spivey <mike@cs.ox.ac.uk>
date Thu, 05 Oct 2017 08:04:15 +0100
parents
children
rev   line source
0
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
1 /*
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
2 * gc0.c
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
3 *
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
4 * This file is part of the Oxford Oberon-2 compiler
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
5 * Copyright (c) 2006 J. M. Spivey
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
6 * All rights reserved
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
7 *
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
8 * Redistribution and use in source and binary forms, with or without
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
9 * modification, are permitted provided that the following conditions are met:
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
10 *
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
11 * 1. Redistributions of source code must retain the above copyright notice,
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
12 * this list of conditions and the following disclaimer.
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
13 * 2. Redistributions in binary form must reproduce the above copyright notice,
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
14 * this list of conditions and the following disclaimer in the documentation
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
15 * and/or other materials provided with the distribution.
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
16 * 3. The name of the author may not be used to endorse or promote products
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
17 * derived from this software without specific prior written permission.
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
18 *
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
24 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
25 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
26 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
27 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
28 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
29 *
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
30 * $Id: gc.c 1557 2010-01-24 20:59:31Z mike $
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
31 */
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
32
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
33 /* keiko/gc0.c */
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
34 /* Copyright (c) 2017 J. M. Spivey */
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
35
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
36 #include "obx.h"
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
37
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
38 #include <sys/mman.h>
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
39
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
40 #ifndef MAP_ANONYMOUS
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
41 #define MAP_ANONYMOUS MAP_ANON
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
42 #endif
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
43
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
44 #ifdef M64X32
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
45 #define MAP_FLAGS MAP_PRIVATE|MAP_32BIT|MAP_ANONYMOUS
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
46 #else
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
47 #define MAP_FLAGS MAP_PRIVATE|MAP_ANONYMOUS
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
48 #endif
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
49
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
50 static void *get_memory(unsigned size) {
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
51 void *p = mmap(NULL, size, PROT_READ|PROT_WRITE,
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
52 MAP_FLAGS, -1, 0);
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
53 if (p == MAP_FAILED) panic("Out of memory");
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
54 return p;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
55 }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
56
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
57 #define CHUNK 16384
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
58
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
59 static unsigned char *alloc = NULL;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
60 static unsigned char *limit = NULL;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
61
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
62 #define roundup(x, n) (((x)+(n-1))&~(n-1))
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
63
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
64 void *scratch_alloc(unsigned size) {
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
65 if (size % PAGESIZE == 0 || size > CHUNK)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
66 return get_memory(roundup(size, PAGESIZE));
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
67
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
68 if (alloc == NULL || size > limit - alloc) {
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
69 alloc = get_memory(CHUNK);
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
70 limit = alloc + CHUNK;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
71 }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
72
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
73 void *p = alloc;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
74 alloc += size;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
75 return p;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
76 }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
77
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
78 /* gc_init -- initialise everything */
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
79 void gc_init(void) {
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
80 }