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