debuggers.hg

view tools/libxl/flexarray.c @ 22848:6341fe0f4e5a

Added tag 4.1.0-rc2 for changeset 9dca60d88c63
author Keir Fraser <keir@xen.org>
date Tue Jan 25 14:06:55 2011 +0000 (2011-01-25)
parents 03718b569d97
children
line source
1 /*
2 * Copyright (C) 2009 Citrix Ltd.
3 * Author Vincent Hanquez <vincent.hanquez@eu.citrix.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU Lesser General Public License as published
7 * by the Free Software Foundation; version 2.1 only. with the special
8 * exception on linking described in file LICENSE.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Lesser General Public License for more details.
14 */
16 #include "libxl_internal.h"
17 #include <stdarg.h>
19 flexarray_t *flexarray_make(int size, int autogrow)
20 {
21 flexarray_t *array = malloc(sizeof(struct flexarray));
22 if (array) {
23 array->size = size;
24 array->autogrow = autogrow;
25 array->count = 0;
26 array->data = calloc(size, sizeof(void *));
27 }
28 return array;
29 }
31 void flexarray_free(flexarray_t *array)
32 {
33 free(array->data);
34 free(array);
35 }
37 int flexarray_grow(flexarray_t *array, int extents)
38 {
39 void **data;
40 int newsize;
42 newsize = array->size + extents;
43 data = realloc(array->data, sizeof(void *) * newsize);
44 if (!data)
45 return 1;
46 array->size += extents;
47 array->data = data;
48 return 0;
49 }
51 int flexarray_set(flexarray_t *array, unsigned int index, void *ptr)
52 {
53 if (index >= array->size) {
54 int newsize;
55 if (!array->autogrow)
56 return 1;
57 newsize = (array->size * 2 < index) ? index + 1 : array->size * 2;
58 if (flexarray_grow(array, newsize - array->size))
59 return 2;
60 }
61 if ( index + 1 > array->count )
62 array->count = index + 1;
63 array->data[index] = ptr;
64 return 0;
65 }
67 int flexarray_append(flexarray_t *array, void *ptr)
68 {
69 return flexarray_set(array, array->count, ptr);
70 }
72 int flexarray_vappend(flexarray_t *array, ...)
73 {
74 va_list va;
75 void *ptr;
76 int ret;
78 va_start(va, array);
79 for(ret = 0; (ptr = va_arg(va, void *)); ret++) {
80 if ( flexarray_append(array, ptr) )
81 break;
82 }
83 va_end(va);
84 return ret;
85 }
87 int flexarray_get(flexarray_t *array, int index, void **ptr)
88 {
89 if (index >= array->size)
90 return 1;
91 *ptr = array->data[index];
92 return 0;
93 }
95 void **flexarray_contents(flexarray_t *array)
96 {
97 void **data;
98 data = array->data;
99 free(array);
100 return data;
101 }