annotate array.c @ 4:ee9a66b87c70

Initial version of toplevel and options handling.
author David A. Holland
date Sun, 19 Dec 2010 17:52:59 -0500
parents 9c1cecba517c
children b156910b59b2
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);
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
50 free(a);
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);
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
64 free(a->v);
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 }
2
9c1cecba517c Make arrays crash on malloc failure.
David A. Holland
parents: 1
diff changeset
81 newptr = dorealloc(a->v, newmax*sizeof(*a->v));
1
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
82 a->v = newptr;
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
83 a->max = newmax;
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
84 }
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
85 a->num = num;
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
86 }
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
87
2
9c1cecba517c Make arrays crash on malloc failure.
David A. Holland
parents: 1
diff changeset
88 void
1
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
89 array_insert(struct array *a, unsigned index_)
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
90 {
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
91 unsigned movers;
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
92
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
93 arrayassert(a->num <= a->max);
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
94 arrayassert(index_ < a->num);
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
95
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
96 movers = a->num - index_;
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
97
2
9c1cecba517c Make arrays crash on malloc failure.
David A. Holland
parents: 1
diff changeset
98 array_setsize(a, a->num + 1);
1
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
99
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
100 memmove(a->v + index_+1, a->v + index_, movers*sizeof(*a->v));
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
101 }
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
102
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
103 void
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
104 array_remove(struct array *a, unsigned index_)
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
105 {
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
106 unsigned movers;
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
107
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
108 arrayassert(a->num <= a->max);
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
109 arrayassert(index_ < a->num);
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
110
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
111 movers = a->num - (index_ + 1);
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
112 memmove(a->v + index_, a->v + index_+1, movers*sizeof(*a->v));
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
113 a->num--;
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
114 }