debuggers.hg

view tools/libxl/flexarray.c @ 21067:b4a1832a916f

Update Xen version to 4.0.0-rc6
author Keir Fraser <keir.fraser@citrix.com>
date Tue Mar 09 18:18:05 2010 +0000 (2010-03-09)
parents 8a91056bea81
children 3bbca5d47840
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 <stdlib.h>
17 #include "flexarray.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->data = calloc(size, sizeof(void *));
26 }
27 return array;
28 }
30 void flexarray_free(flexarray_t *array)
31 {
32 free(array->data);
33 free(array);
34 }
36 int flexarray_grow(flexarray_t *array, int extents)
37 {
38 void **data;
39 int newsize;
41 newsize = array->size + extents;
42 data = realloc(array->data, sizeof(void *) * newsize);
43 if (!data)
44 return 1;
45 array->size += extents;
46 array->data = data;
47 return 0;
48 }
50 int flexarray_set(flexarray_t *array, unsigned int index, void *ptr)
51 {
52 if (index >= array->size) {
53 int newsize;
54 if (!array->autogrow)
55 return 1;
56 newsize = (array->size * 2 < index) ? index + 1 : array->size * 2;
57 if (flexarray_grow(array, newsize - array->size))
58 return 2;
59 }
60 array->data[index] = ptr;
61 return 0;
62 }
64 int flexarray_get(flexarray_t *array, int index, void **ptr)
65 {
66 if (index >= array->size)
67 return 1;
68 *ptr = array->data[index];
69 return 0;
70 }
72 void **flexarray_contents(flexarray_t *array)
73 {
74 void **data;
75 data = array->data;
76 free(array);
77 return data;
78 }