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