1 1.1 dholland /*- 2 1.1 dholland * Copyright (c) 2009 The NetBSD Foundation, Inc. 3 1.1 dholland * All rights reserved. 4 1.1 dholland * 5 1.1 dholland * This code is derived from software contributed to The NetBSD Foundation 6 1.1 dholland * by David A. Holland. 7 1.1 dholland * 8 1.1 dholland * Redistribution and use in source and binary forms, with or without 9 1.1 dholland * modification, are permitted provided that the following conditions 10 1.1 dholland * are met: 11 1.1 dholland * 1. Redistributions of source code must retain the above copyright 12 1.1 dholland * notice, this list of conditions and the following disclaimer. 13 1.1 dholland * 2. Redistributions in binary form must reproduce the above copyright 14 1.1 dholland * notice, this list of conditions and the following disclaimer in the 15 1.1 dholland * documentation and/or other materials provided with the distribution. 16 1.1 dholland * 17 1.1 dholland * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 18 1.1 dholland * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 19 1.1 dholland * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 20 1.1 dholland * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 21 1.1 dholland * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 22 1.1 dholland * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 23 1.1 dholland * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 24 1.1 dholland * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 25 1.1 dholland * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 26 1.1 dholland * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 27 1.1 dholland * POSSIBILITY OF SUCH DAMAGE. 28 1.1 dholland */ 29 1.1 dholland 30 1.1 dholland #include <stdlib.h> 31 1.1 dholland #include <string.h> 32 1.1 dholland 33 1.1 dholland #define ARRAYINLINE 34 1.1 dholland #include "array.h" 35 1.1 dholland 36 1.1 dholland struct array * 37 1.1 dholland array_create(void) 38 1.1 dholland { 39 1.1 dholland struct array *a; 40 1.1 dholland 41 1.1 dholland a = malloc(sizeof(*a)); 42 1.1 dholland if (a != NULL) { 43 1.1 dholland array_init(a); 44 1.1 dholland } 45 1.1 dholland return a; 46 1.1 dholland } 47 1.1 dholland 48 1.1 dholland void 49 1.1 dholland array_destroy(struct array *a) 50 1.1 dholland { 51 1.1 dholland array_cleanup(a); 52 1.1 dholland free(a); 53 1.1 dholland } 54 1.1 dholland 55 1.1 dholland void 56 1.1 dholland array_init(struct array *a) 57 1.1 dholland { 58 1.1 dholland a->num = a->max = 0; 59 1.1 dholland a->v = NULL; 60 1.1 dholland } 61 1.1 dholland 62 1.1 dholland void 63 1.1 dholland array_cleanup(struct array *a) 64 1.1 dholland { 65 1.1 dholland arrayassert(a->num == 0); 66 1.1 dholland free(a->v); 67 1.1 dholland #ifdef ARRAYS_CHECKED 68 1.1 dholland a->v = NULL; 69 1.1 dholland #endif 70 1.1 dholland } 71 1.1 dholland 72 1.1 dholland int 73 1.1 dholland array_setsize(struct array *a, unsigned num) 74 1.1 dholland { 75 1.1 dholland unsigned newmax; 76 1.1 dholland 77 1.1 dholland if (num > a->max) { 78 1.1 dholland newmax = a->max; 79 1.1 dholland while (num > newmax) { 80 1.1 dholland newmax = newmax ? newmax*2 : 4; 81 1.1 dholland } 82 1.2 nia if (reallocarr(&a->v, newmax, sizeof(*a->v)) != 0) 83 1.1 dholland return -1; 84 1.1 dholland a->max = newmax; 85 1.1 dholland } 86 1.1 dholland a->num = num; 87 1.1 dholland return 0; 88 1.1 dholland } 89 1.1 dholland 90 1.1 dholland int 91 1.1 dholland array_insert(struct array *a, unsigned index_) 92 1.1 dholland { 93 1.1 dholland unsigned movers; 94 1.1 dholland 95 1.1 dholland arrayassert(a->num <= a->max); 96 1.1 dholland arrayassert(index_ < a->num); 97 1.1 dholland 98 1.1 dholland movers = a->num - index_; 99 1.1 dholland 100 1.1 dholland if (array_setsize(a, a->num + 1)) { 101 1.1 dholland return -1; 102 1.1 dholland } 103 1.1 dholland 104 1.1 dholland memmove(a->v + index_+1, a->v + index_, movers*sizeof(*a->v)); 105 1.1 dholland return 0; 106 1.1 dholland } 107 1.1 dholland 108 1.1 dholland void 109 1.1 dholland array_remove(struct array *a, unsigned index_) 110 1.1 dholland { 111 1.1 dholland unsigned movers; 112 1.1 dholland 113 1.1 dholland arrayassert(a->num <= a->max); 114 1.1 dholland arrayassert(index_ < a->num); 115 1.1 dholland 116 1.1 dholland movers = a->num - (index_ + 1); 117 1.1 dholland memmove(a->v + index_, a->v + index_+1, movers*sizeof(*a->v)); 118 1.1 dholland a->num--; 119 1.1 dholland } 120