]> git.ozlabs.org Git - ccan/blob - array.h
fb4701359e03889e4e5be6c61ae40dd46b0f473d
[ccan] / array.h
1 /*
2         Copyright (c) 2009  Joseph A. Adams
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         3. The name of the author may not be used to endorse or promote products
14            derived from this software without specific prior written permission.
15         
16         THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17         IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18         OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19         IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20         INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21         NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22         DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23         THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24         (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25         THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28 #ifndef CCAN_ARRAY_H
29 #define CCAN_ARRAY_H
30
31 #define ARRAY_USE_TALLOC
32
33 #include <stdlib.h>
34 #include <string.h>
35 #include "config.h"
36
37 #ifdef ARRAY_USE_TALLOC
38 #include <ccan/talloc/talloc.h>
39 #endif
40
41 //Use the array_alias macro to indicate that a pointer has changed but strict aliasing rules are too stupid to know it
42 #if HAVE_ATTRIBUTE_MAY_ALIAS
43 #define array_alias(ptr) /* nothing */
44 #define array(type) struct {type *item; size_t size; size_t alloc;} __attribute__((__may_alias__))
45 #else
46 #define array_alias(ptr) qsort(ptr, 0, 1, array_alias_helper) //hack
47 #define array(type) struct {type *item; size_t size; size_t alloc;}
48 #endif
49
50 //We call allocator functions directly
51 #ifndef ARRAY_USE_TALLOC
52
53 #define array_new() {0,0,0}
54 #define array_init(array) do {(array).item=0; (array).size=0; (array).alloc=0;} while(0)
55 #define array_realloc(array, newAlloc) do {(array).item = realloc((array).item, ((array).alloc = (newAlloc))*sizeof(*(array).item));} while(0)
56 #define array_free(array) do {free((array).item);} while(0)
57
58 #else
59
60 //note:  the allocations are given an extra byte to prevent free (and thus loss of ctx) on realloc to size 0
61
62 #define array_new(ctx) {talloc_size(ctx,1), 0,0}
63 #define array_init(array, ctx) do {(array).item=talloc_size(ctx,1); (array).size=0; (array).alloc=0;} while(0)
64 #define array_realloc(array, newAlloc) do {(array).item = talloc_realloc_size(NULL, (array).item, ((array).alloc = (newAlloc))*sizeof(*(array).item) +1);} while(0)
65 #define array_free(array) do {talloc_free((array).item);} while(0)
66
67 #endif
68
69
70 //We call helper functions
71 #define array_resize(array, newSize) do {(array).size = (newSize); if ((array).size > (array).alloc) { array_resize_helper((array_char*)&(array), sizeof(*(array).item)); array_alias(&(array));}} while(0)
72 #define array_resize0(array, newSize) do {array_resize0_helper((array_char*)&(array), sizeof(*(array).item), newSize);} while(0)
73 #define array_prepend_lit(array, stringLiteral) do {array_insert_items_helper((array_char*)&(array), sizeof(*(array).item), 0, stringLiteral, sizeof(stringLiteral)-1, 1); array_alias(&(array)); (array).item[--(array).size] = 0;} while(0)
74 #define array_prepend_string(array, str) do {const char *__str = (str); size_t __len = strlen(__str); array_insert_items_helper((array_char*)&(array), sizeof(*(array).item), 0, __str, __len, 1); array_alias(&(array)); (array).item[--(array).size] = 0;} while(0)
75 #define array_prepend_items(array, items, count) do {array_insert_items_helper((array_char*)&(array), sizeof(*(array).item), 0, items, count, 0); array_alias(&(array));} while(0)
76
77
78 //We call other array_* macros
79 #define array_from_c(array, c_array) array_from_items(array, c_array, sizeof(c_array)/sizeof(*(c_array)))
80 #define array_from_lit(array, stringLiteral) do {array_from_items(array, stringLiteral, sizeof(stringLiteral)); (array).size--;} while(0)
81 #define array_from_string(array, str) do {const char *__str = (str); array_from_items(array, __str, strlen(__str)+1); (array).size--;} while(0)
82 #define array_from_items(array, items, count) do {size_t __count = (count); array_resize(array, __count); memcpy((array).item, items, __count*sizeof(*(array).item));} while(0)
83 #define array_append(array, ...) do {array_resize(array, (array).size+1); (array).item[(array).size-1] = (__VA_ARGS__);} while(0)
84 #define array_append_string(array, str) do {const char *__str = (str); array_append_items(array, __str, strlen(__str)+1); (array).size--;} while(0)
85 #define array_append_lit(array, stringLiteral) do {array_append_items(array, stringLiteral, sizeof(stringLiteral)); (array).size--;} while(0)
86 #define array_append_items(array, items, count) do {size_t __count = (count); array_resize(array, (array).size+__count); memcpy((array).item+(array).size-__count, items, __count*sizeof(*(array).item));} while(0)
87 #define array_prepend(array, ...) do {array_resize(array, (array).size+1); memmove((array).item+1, (array).item, ((array).size-1)*sizeof(*(array).item)); *(array).item = (__VA_ARGS__);} while(0)
88 #define array_push(array, ...) array_append(array, __VA_ARGS__)
89 #define array_pop_check(array) ((array).size ? array_pop(array) : NULL)
90
91 #define array_growalloc(array, newAlloc) do {size_t __newAlloc=(newAlloc); if (__newAlloc > (array).alloc) array_realloc(array, (__newAlloc+63)&~63); } while(0)
92 #if HAVE_STATEMENT_EXPR==1
93 #define array_make_room(array, room) ({size_t newAlloc = (array).size+(room); if ((array).alloc<newAlloc) array_realloc(array, newAlloc); (array).item+(array).size; })
94 #endif
95
96
97 //We do just fine by ourselves
98 #define array_pop(array) ((array).item[--(array).size])
99
100 #define array_for_t(var, array, type, ...) do {type *var=(void*)(array).item; size_t _r=(array).size, _i=0; for (;_r--;_i++, var++) { __VA_ARGS__ ;} } while(0)
101
102 #define array_appends_t(array, type, ...) do {type __src[] = {__VA_ARGS__}; array_append_items(array, __src, sizeof(__src)/sizeof(*__src));} while(0)
103
104 #if HAVE_TYPEOF==1
105 #define array_appends(array, ...) array_appends_t(array, typeof((*(array).item)), __VA_ARGS__)
106 #define array_prepends(array, ...) do {typeof((*(array).item)) __src[] = {__VA_ARGS__}; array_prepend_items(array, __src, sizeof(__src)/sizeof(*__src));} while(0)
107 #define array_for(var, array, ...) array_for_t(var, array, typeof(*(array).item), __VA_ARGS__)
108 #define array_rof(var, array, ...) do {typeof(*(array).item) *var=(void*)(array).item; size_t _i=(array).size, _r=0; var += _i; for (;_i--;_r++) { var--; __VA_ARGS__ ;} } while(0)
109 #endif
110
111
112 typedef array(char) array_char;
113
114 void array_resize_helper(array_char *a, size_t itemSize);
115 void array_resize0_helper(array_char *a, size_t itemSize, size_t newSize);
116 void array_insert_items_helper(array_char *a, size_t itemSize, size_t pos, const void *items, size_t count, size_t tailSize);
117         //Note:  there is no array_insert_items yet, but it wouldn't be too hard to add.
118
119 int array_alias_helper(const void *a, const void *b);
120
121 #endif
122
123 /*
124
125 array_growalloc(array, newAlloc) sees if the array can currently hold newAlloc items;
126         if not, it increases the alloc to satisfy this requirement, allocating slack
127         space to avoid having to reallocate for every size increment.
128
129 array_from_string(array, str) copys a string to an array_char.
130
131 array_push(array, item) pushes an item to the end of the array.
132 array_pop_nocheck(array) pops it back out.  Be sure there is at least one item in the array before calling.
133 array_pop(array) does the same as array_pop_nocheck, but returns NULL if there are no more items left in the array.
134
135 array_make_room(array, room) ensures there's 'room' elements of space after the end of the array, and it returns a pointer to this space.
136 Currently requires HAVE_STATEMENT_EXPR, but I plan to remove this dependency by creating an inline function.
137
138 The following require HAVE_TYPEOF==1 :
139
140 array_appends(array, item0, item1...) appends a collection of comma-delimited items to the array.
141 array_prepends(array, item0, item1...) prepends a collection of comma-delimited items to the array.
142 array_for(var, array, commands...) iterates forward through the items in the array using var.  var is a pointer to an item.
143 array_rof(var, array, commands...) iterates backward through the items in the array using var.  var is a pointer to an item.
144
145 Examples:
146
147 array(int) array;
148 array_appends(array, 0,1,2,3,4);
149 array_appends(array, -5,-4,-3,-2,-1);
150 array_for(i, array, printf("%d ", *i));
151 printf("\n");
152 array_free(array);
153
154 array(struct {int n,d;}) fractions;
155 array_appends(fractions, {3,4}, {3,5}, {2,1});
156 array_for(i, fractions, printf("%d/%d\n", i->n, i->d));
157 array_free(fractions);
158 */
159
160 /*
161
162 Direct tests:
163
164 array_push
165 array_prepend
166 array_from_c
167 array_for
168 array_rof
169 array_from_lit
170 array_append_lit
171 array_prepend_lit
172 array_append_string
173 array_prepend_string
174 array_from_string
175 array_resize0
176 array_pop_nocheck
177 array_realloc
178 array_growalloc
179 array_make_room
180 array_pop
181 array_appends
182 array_prepends
183
184 Indirect tests:
185
186 array_append <- array_push
187 array_resize <- array_append
188 array_from_items <- array_from_c, array_from_lit, array_from_string
189 array_append_items <- array_append_string, array_append_lit
190 array_prepend_items <- array_prepends
191
192 Untested:
193
194 */