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
mike@0 1 /*
mike@0 2 * util.h
mike@0 3 *
mike@0 4 * This file is part of the Oxford Oberon-2 compiler
mike@0 5 * Copyright (c) 2006--2016 J. M. Spivey
mike@0 6 * All rights reserved
mike@0 7 *
mike@0 8 * Redistribution and use in source and binary forms, with or without
mike@0 9 * modification, are permitted provided that the following conditions are met:
mike@0 10 *
mike@0 11 * 1. Redistributions of source code must retain the above copyright notice,
mike@0 12 * this list of conditions and the following disclaimer.
mike@0 13 * 2. Redistributions in binary form must reproduce the above copyright notice,
mike@0 14 * this list of conditions and the following disclaimer in the documentation
mike@0 15 * and/or other materials provided with the distribution.
mike@0 16 * 3. The name of the author may not be used to endorse or promote products
mike@0 17 * derived from this software without specific prior written permission.
mike@0 18 *
mike@0 19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
mike@0 20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
mike@0 21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
mike@0 22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
mike@0 23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
mike@0 24 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
mike@0 25 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
mike@0 26 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
mike@0 27 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
mike@0 28 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
mike@0 29 */
mike@0 30
mike@0 31 EXTERN char *progname;
mike@0 32 EXTERN char *err_file;
mike@0 33 EXTERN int status;
mike@0 34
mike@0 35 void error(const char *msg, ...);
mike@0 36 void panic(const char *msg, ...);
mike@0 37 void *must_alloc(int n, const char *msg);
mike@0 38 void *must_realloc(void *p, int n0, int n, const char *msg);
mike@0 39 char *must_strdup(const char *s);
mike@0 40
mike@0 41 /* Auto-grow buffers */
mike@0 42
mike@0 43 /*
mike@0 44 To declare an autogrow buffer called A with size N and elements
mike@0 45 of type T, say:
mike@0 46
mike@0 47 PRIVATE growdecl(A);
mike@0 48 #define A growbuf(A, T)
mike@0 49 #define N growsize(A)
mike@0 50
mike@0 51 Remarkably, the macro madness (which appears to define A recursively)
mike@0 52 all works out fine.
mike@0 53
mike@0 54 To initialize the buffer:
mike@0 55
mike@0 56 buf_init(A, init_size, margin, T, "the array A")
mike@0 57
mike@0 58 The message "Couldn't allocate space for the array A" will be printed
mike@0 59 if allocation fails.
mike@0 60
mike@0 61 To check that at least |margin| elements remain unused:
mike@0 62
mike@0 63 buf_grow(A)
mike@0 64 */
mike@0 65
mike@0 66 #define GROW 1.5 /* Growth ratio when buffer full */
mike@0 67
mike@0 68 #define growdecl(b) struct _growbuf _##b
mike@0 69 #define growbuf(b, type) ((type *) _##b.buf)
mike@0 70 #define growsize(b) _##b.loc
mike@0 71 #define buf_init(b, size, margin, type, name) \
mike@0 72 _buf_init(&_##b, size, margin, sizeof(type), name)
mike@0 73 #define buf_grow(b) _buf_grow(&_##b)
mike@0 74
mike@0 75 struct _growbuf {
mike@0 76 void *buf;
mike@0 77 int loc, size, margin;
mike@0 78 int elsize;
mike@0 79 const char *name;
mike@0 80 };
mike@0 81
mike@0 82 void _buf_init(struct _growbuf *b, int size, int margin,
mike@0 83 int elsize, const char *name);
mike@0 84 void _buf_grow(struct _growbuf *b);
mike@0 85
mike@0 86
mike@0 87 /* Memory pools */
mike@0 88
mike@0 89 typedef struct {
mike@0 90 unsigned char **p_pool;
mike@0 91 int p_current, p_npools, p_size;
mike@0 92 uchar *p_alloc;
mike@0 93 } mempool;
mike@0 94
mike@0 95 void *pool_alloc(mempool *pool, int size);
mike@0 96 void pool_reset(mempool *pool);
mike@0 97
mike@0 98
mike@0 99 /* Splitting lines into words */
mike@0 100
mike@0 101 #define MAXWORDS 16
mike@0 102 int split_line(char *line, char **words);
mike@0 103
mike@0 104 char *squidge(char *name);