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