Stackdb
Stackdb is a stackable, multi-target and -level source debugger and memory forensics library.
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
alist.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2011, 2012, 2013 The University of Utah
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of
7  * the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
17  */
18 
19 #ifndef _ARRAY_LIST_H
20 #define _ARRAY_LIST_H
21 
26 #include <stdlib.h>
27 #include <errno.h>
28 #include <string.h>
29 #include <glib.h>
30 
31 struct array_list {
32  int32_t len;
33  int32_t alloc_len;
34  void **list;
35 };
36 
37 static inline struct array_list *array_list_create(int initsize) {
38  struct array_list *list = \
39  (struct array_list *)malloc(sizeof(struct array_list));
40  memset(list,0,sizeof(struct array_list));
41  if (initsize) {
42  list->alloc_len = initsize;
43  list->list = (void **)malloc(sizeof(void *)*initsize);
44  }
45  return list;
46 }
47 
48 static inline struct array_list *array_list_init(struct array_list *list,
49  int initsize) {
50  if (initsize) {
51  list->alloc_len = initsize;
52  list->list = (void **)malloc(sizeof(void *)*initsize);
53  }
54  return list;
55 }
56 
57 static inline struct array_list *array_list_clone(struct array_list *oldlist,
58  int more) {
59  struct array_list *newlist = \
60  (struct array_list *)malloc(sizeof(struct array_list));
61  memset(newlist,0,sizeof(struct array_list));
62  if (oldlist)
63  newlist->alloc_len = oldlist->len + more;
64  else
65  newlist->alloc_len = more;
66  newlist->list = (void **)malloc(sizeof(void *)*(newlist->alloc_len));
67  if (oldlist && oldlist->len) {
68  memcpy(newlist->list,oldlist->list,sizeof(void *)*(oldlist->len));
69  newlist->len = oldlist->len;
70  }
71  return newlist;
72 }
73 
74 static inline int32_t array_list_len(struct array_list *list) {
75  if (!list)
76  return 0;
77 
78  return list->len;
79 }
80 
81 static inline int32_t array_list_alloc_len(struct array_list *list) {
82  if (!list)
83  return 0;
84 
85  return list->alloc_len;
86 }
87 
88 static inline int32_t array_list_space(struct array_list *list) {
89  return list->alloc_len - list->len;
90 }
91 
92 static inline int array_list_resize(struct array_list *list,int newsize) {
93  void **lltmp;
94 
95  if (newsize == list->alloc_len)
96  return 0;
97 
98  if (!(lltmp = (void **)realloc(list->list,newsize*sizeof(void *)))) {
99  return -1;
100  }
101 
102  list->list = lltmp;
103  list->alloc_len = newsize;
104  if (newsize < list->len)
105  list->len = newsize;
106 
107  return 0;
108 }
109 
110 static inline int array_list_compact(struct array_list *list) {
111  return array_list_resize(list,list->len);
112 }
113 
114 static inline int array_list_expand(struct array_list *list,int plussize) {
115  void **lltmp;
116 
117  if (!(lltmp = (void **)realloc(list->list,
118  (list->alloc_len+plussize)*sizeof(void *)))) {
119  return -1;
120  }
121 
122  list->list = lltmp;
123  list->alloc_len = (list->alloc_len+plussize);
124  if (list->alloc_len < list->len)
125  list->len = list->alloc_len;
126 
127  return 0;
128 }
129 
130 static inline int array_list_expand_to(struct array_list *list,int plussize) {
131  int newsize = plussize - (list->alloc_len - list->len);
132 
133  if (newsize <= 0)
134  return 0;
135 
136  return array_list_expand(list,newsize);
137 }
138 
139 static inline struct array_list *array_list_concat(struct array_list *list,
140  struct array_list *newtail) {
141  array_list_expand_to(list,list->len + newtail->len);
142  memcpy(list->list + list->len * sizeof(void *),newtail->list,newtail->len);
143  list->len += newtail->len;
144 
145  return list;
146 }
147 
148 static inline int array_list_add(struct array_list *list,void *element) {
149  void **lltmp;
150 
151  /* allocate space for another entry if necessary */
152  if (list->len == list->alloc_len) {
153  if (!(lltmp = (void **)realloc(list->list,
154  (list->len+1)*sizeof(void *)))) {
155  return -1;
156  }
157  list->list = lltmp;
158  list->alloc_len += 1;
159  }
160 
161  list->list[list->len] = element;
162 
163  list->len += 1;
164 
165  return 0;
166 }
167 
168 /*
169  * NB: @i must be within the list or just off its current end! We
170  * refuse to create sparse lists (lists with NULL entries in this case).
171  */
172 static inline int array_list_add_item_at(struct array_list *list,
173  void *element,int i) {
174  void **lltmp;
175  int j;
176 
177  if (list->len == i)
178  return array_list_add(list,element);
179  else if (list->len < i)
180  return -1;
181 
182  /* allocate space for another entry if necessary */
183  if (list->len == list->alloc_len) {
184  if (!(lltmp = (void **)realloc(list->list,
185  (list->len+1)*sizeof(void *)))) {
186  return -1;
187  }
188  list->list = lltmp;
189  list->alloc_len += 1;
190  }
191 
192  /* Move the ith..len items to i+1 */
193  for (j = i; j < list->len; ++j)
194  list->list[j+1] = list->list[j];
195 
196  list->list[i] = element;
197  list->len += 1;
198 
199  return 0;
200 }
201 
202 static inline int array_list_append(struct array_list *list,void *element) {
203  return array_list_add(list,element);
204 }
205 
206 static inline int array_list_prepend(struct array_list *list,void *element) {
207  void **lltmp;
208  int i;
209 
210  /* allocate space for another entry if necessary */
211  if (list->len == list->alloc_len) {
212  if (!(lltmp = (void **)realloc(list->list,
213  (list->len+1)*sizeof(void *)))) {
214  return -1;
215  }
216  list->list = lltmp;
217  list->alloc_len += 1;
218  }
219 
220  /* shift the whole list over one, ugh */
221  for (i = list->len - 1; i > -1; --i) {
222  list->list[i + 1] = list->list[i];
223  }
224 
225  list->list[0] = element;
226 
227  list->len += 1;
228 
229  return 0;
230 }
231 
232 static inline int array_list_prepend_sublist(struct array_list *list,
233  struct array_list *prelist,
234  int howmany) {
235  void **lltmp;
236  int i;
237 
238  if (howmany < 0)
239  howmany = prelist->len + howmany;
240  else if (howmany == 0)
241  return 0;
242  else if (howmany > prelist->len)
243  return -1;
244 
245  /* allocate space for another entry if necessary */
246  if (list->len == list->alloc_len) {
247  if (!(lltmp = (void **)realloc(list->list,
248  (list->alloc_len + howmany)*sizeof(void *)))) {
249  return -1;
250  }
251  list->list = lltmp;
252  list->alloc_len += howmany;
253  }
254 
255  /* shift the whole list over by howmany, ugh */
256  for (i = list->len - 1; i > -1; --i) {
257  list->list[i + howmany] = list->list[i];
258  }
259 
260  memcpy(list->list,prelist->list,howmany * (sizeof(void *)));
261 
262  list->len += howmany;
263 
264  return 0;
265 }
266 
267 static inline void *array_list_remove(struct array_list *list) {
268  if (list->len)
269  return list->list[--list->len];
270 
271  return NULL;
272 }
273 
274 static inline void array_list_remove_all(struct array_list *list,int maxsize) {
275  if (list->len)
276  list->len = 0;
277  if (list->alloc_len > maxsize)
278  array_list_resize(list,maxsize);
279 }
280 
281 static inline void *array_list_remove_item_at(struct array_list *list,int i) {
282  void *item;
283 
284  if (!list->list || list->len < 1)
285  return NULL;
286 
287  if (i == (list->len - 1)) {
288  item = list->list[i];
289  --list->len;
290  return item;
291  }
292  else if (i < list->len) {
293  item = list->list[i];
294  /* shift the list over one after i, ugh */
295  for ( ; i < list->len - 1; ++i) {
296  list->list[i] = list->list[i + 1];
297  }
298  --list->len;
299  return item;
300  }
301 
302  return NULL;
303 }
304 
305 static inline void *array_list_remove_item(struct array_list *list,void *item) {
306  int i;
307 
308  if (!list->list || list->len < 1)
309  return NULL;
310 
311  for (i = 0; i < list->len; ++i) {
312  if (list->list[i] == item)
313  break;
314  }
315 
316  if (i < list->len)
317  return array_list_remove_item_at(list,i);
318  else
319  return NULL;
320 }
321 
322 static inline int array_list_find(struct array_list *list,void *item) {
323  int i;
324 
325  if (!list->list || list->len < 1)
326  return -1;
327 
328  for (i = 0; i < list->len; ++i) {
329  if (list->list[i] == item)
330  return i;
331  }
332 
333  return -1;
334 }
335 
336 static inline void *array_list_item(struct array_list *list,int i) {
337  if (!list->list || i < 0 || i >= list->len) {
338  errno = EINVAL;
339  return NULL;
340  }
341  return list->list[i];
342 }
343 
344 static inline int array_list_item_set(struct array_list *list,int i,void *item) {
345  if (!list->list || i < 0 || i >= list->len) {
346  errno = EINVAL;
347  return -1;
348  }
349  list->list[i] = item;
350  return 0;
351 }
352 
353 static inline void array_list_free(struct array_list *list) {
354  if (list->list)
355  free(list->list);
356  free(list);
357 }
358 
359 static inline void array_list_internal_free(struct array_list *list) {
360  int i;
361 
362  for (i = 0; i < list->len; ++i)
363  free(list->list[i]);
364 }
365 
366 static inline void array_list_deep_free(struct array_list *list) {
367  array_list_internal_free(list);
368  array_list_free(list);
369 }
370 
371 #define array_list_foreach(alist,lpc,placeholder) \
372  for (lpc = 0, (placeholder) = alist->len ? (typeof(placeholder))alist->list[lpc] : (typeof(placeholder))NULL; \
373  alist->len - lpc > 0; \
374  ++lpc, (placeholder) = lpc < alist->len ? (typeof(placeholder))alist->list[lpc] : (typeof(placeholder))NULL)
375 
376 #define array_list_foreach_continue(alist,lpc,placeholder) \
377  for ((placeholder) = (alist->len - lpc > 0) ? (typeof(placeholder))alist->list[lpc] : (typeof(placeholder))NULL; \
378  alist->len - lpc > 0; \
379  ++lpc, (placeholder) = lpc < alist->len ? (typeof(placeholder))alist->list[lpc] : (typeof(placeholder))NULL)
380 
381 #define array_list_foreach_fakeptr_t(alist,lpc,placeholder,intertype) \
382  for (lpc = 0, (placeholder) = alist->len ? (typeof(placeholder))(intertype)alist->list[lpc] : (typeof(placeholder))(intertype)NULL; \
383  alist->len - lpc > 0; \
384  ++lpc, (placeholder) = lpc < alist->len ? (typeof(placeholder))(intertype)alist->list[lpc] : (typeof(placeholder))(intertype)NULL)
385 
386 #define array_list_foreach_is_last(alist,lpc) \
387  (array_list_len(alist) == (lpc + 1))
388 
389 #define array_list_foreach_delete(alist,lpc) \
390  array_list_remove_item_at(alist,lpc); lpc = lpc - 1;
391 
392 static inline struct array_list *array_list_create_from_g_hash_table(GHashTable *ht) {
393  GHashTableIter iter;
394  gpointer value;
395  int len;
396  struct array_list *list;
397 
398  len = g_hash_table_size(ht);
399  list = (struct array_list *)malloc(sizeof(struct array_list));
400  memset(list,0,sizeof(struct array_list));
401  if (len) {
402  list->alloc_len = len;
403  list->list = (void **)malloc(sizeof(void *)*len);
404  }
405  g_hash_table_iter_init(&iter,ht);
406  while (g_hash_table_iter_next(&iter,NULL,&value))
407  array_list_append(list,value);
408 
409  return list;
410 }
411 
412 static inline struct array_list *array_list_create_from_g_hash_table_keys(GHashTable *ht) {
413  GHashTableIter iter;
414  gpointer key;
415  int len;
416  struct array_list *list;
417 
418  len = g_hash_table_size(ht);
419  list = (struct array_list *)malloc(sizeof(struct array_list));
420  memset(list,0,sizeof(struct array_list));
421  if (len) {
422  list->alloc_len = len;
423  list->list = (void **)malloc(sizeof(void *)*len);
424  }
425  g_hash_table_iter_init(&iter,ht);
426  while (g_hash_table_iter_next(&iter,&key,NULL))
427  array_list_append(list,key);
428 
429  return list;
430 }
431 
432 #endif
int32_t len
Definition: alist.h:32
static uint64_t unsigned int i
void ** list
Definition: alist.h:34
void free(void *ptr)
Definition: debugserver.c:207
int len
Definition: dumptarget.c:52
void * realloc(void *ptr, size_t size)
Definition: debugserver.c:221
int32_t alloc_len
Definition: alist.h:33
void * malloc(size_t size)
Definition: debugserver.c:214