annotate keiko/util.h @ 1:b5139af1a420 tip basis

Fixed permissions on compile scripts
author Mike Spivey <mike@cs.ox.ac.uk>
date Fri, 13 Oct 2017 17:27:58 +0100
parents bfdcc3820b32
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 * util.h
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--2016 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
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
31 EXTERN char *progname;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
32 EXTERN char *err_file;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
33 EXTERN int status;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
34
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
35 void error(const char *msg, ...);
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
36 void panic(const char *msg, ...);
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
37 void *must_alloc(int n, const char *msg);
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
38 void *must_realloc(void *p, int n0, int n, const char *msg);
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
39 char *must_strdup(const char *s);
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
40
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
41 /* Auto-grow buffers */
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
42
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
43 /*
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
44 To declare an autogrow buffer called A with size N and elements
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
45 of type T, say:
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
46
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
47 PRIVATE growdecl(A);
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
48 #define A growbuf(A, T)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
49 #define N growsize(A)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
50
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
51 Remarkably, the macro madness (which appears to define A recursively)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
52 all works out fine.
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
53
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
54 To initialize the buffer:
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
55
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
56 buf_init(A, init_size, margin, T, "the array A")
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
57
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
58 The message "Couldn't allocate space for the array A" will be printed
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
59 if allocation fails.
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
60
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
61 To check that at least |margin| elements remain unused:
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
62
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
63 buf_grow(A)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
64 */
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
65
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
66 #define GROW 1.5 /* Growth ratio when buffer full */
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
67
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
68 #define growdecl(b) struct _growbuf _##b
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
69 #define growbuf(b, type) ((type *) _##b.buf)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
70 #define growsize(b) _##b.loc
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
71 #define buf_init(b, size, margin, type, name) \
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
72 _buf_init(&_##b, size, margin, sizeof(type), name)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
73 #define buf_grow(b) _buf_grow(&_##b)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
74
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
75 struct _growbuf {
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
76 void *buf;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
77 int loc, size, margin;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
78 int elsize;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
79 const char *name;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
80 };
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
81
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
82 void _buf_init(struct _growbuf *b, int size, int margin,
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
83 int elsize, const char *name);
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
84 void _buf_grow(struct _growbuf *b);
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
85
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
86
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
87 /* Memory pools */
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
88
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
89 typedef struct {
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
90 unsigned char **p_pool;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
91 int p_current, p_npools, p_size;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
92 uchar *p_alloc;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
93 } mempool;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
94
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
95 void *pool_alloc(mempool *pool, int size);
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
96 void pool_reset(mempool *pool);
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
97
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
98
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
99 /* Splitting lines into words */
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
100
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
101 #define MAXWORDS 16
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
102 int split_line(char *line, char **words);
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
103
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
104 char *squidge(char *name);