annotate array.c @ 65:161edab0d361

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