1 /*- 2 * Copyright (c) 2012 Alistair Crooks <agc@NetBSD.org> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 17 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 */ 25 #ifndef ARRAY_H_ 26 #define ARRAY_H_ 20120921 27 28 #ifndef PGPV_ARRAY 29 /* creates 2 unsigned vars called "name"c and "name"size in current scope */ 30 /* also creates an array called "name"s in current scope */ 31 #define PGPV_ARRAY(type, name) \ 32 unsigned name##c; unsigned name##vsize; type *name##s 33 #endif 34 35 /* if this isn't part of a struct, need to specifically initialise things */ 36 #define ARRAY_INIT(name) do { \ 37 name##c = name##vsize = 0; \ 38 name##s = NULL; \ 39 } while(/*CONSTCOND*/0) 40 41 /* check the array is big enough - if not, expand it by explicit amount */ 42 /* this is clunky, but there are bugs a-lurking */ 43 #define ARRAY_EXPAND_SIZED(name, mult, add) do { \ 44 if (name##c == name##vsize) { \ 45 void *_v; \ 46 char *_cv = NULL; \ 47 unsigned _ents; \ 48 _ents = (name##vsize * (mult)) + (add); \ 49 _cv = _v = realloc(name##s, _ents * sizeof(*name##s)); \ 50 if (_v == NULL) { \ 51 fprintf(stderr, "ARRAY_EXPAND - bad realloc\n"); \ 52 } else { \ 53 memset(&_cv[name##vsize * sizeof(*name##s)], \ 54 0x0, (_ents - name##vsize) * sizeof(*name##s)); \ 55 name##s = _v; \ 56 name##vsize = _ents; \ 57 } \ 58 } \ 59 } while(/*CONSTCOND*/0) 60 61 /* check the array is big enough - if not, expand it (size * 2) + 10 */ 62 #define ARRAY_EXPAND(name) ARRAY_EXPAND_SIZED(name, 2, 10) 63 64 #define ARRAY_ELEMENT(name, num) name##s[num] 65 #define ARRAY_LAST(name) name##s[name##c - 1] 66 #define ARRAY_COUNT(name) name##c 67 #define ARRAY_SIZE(name) name##vsize 68 #define ARRAY_ARRAY(name) name##s 69 70 #define ARRAY_APPEND(name, newel) do { \ 71 ARRAY_EXPAND(name); \ 72 ARRAY_COUNT(name) += 1; \ 73 ARRAY_LAST(name) = newel; \ 74 } while(/*CONSTCOND*/0) 75 76 #define ARRAY_DELETE(name, num) do { \ 77 ARRAY_COUNT(name) -= 1; \ 78 memmove(&ARRAY_ELEMENT(name, num), &ARRAY_ELEMENT(name, num + 1), \ 79 (ARRAY_COUNT(name) - (num)) * sizeof(ARRAY_ELEMENT(name, 0))); \ 80 } while(/*CONSTCOND*/0) 81 82 #endif 83