aboutsummaryrefslogtreecommitdiffstats
path: root/tools/libxl/flexarray.c
blob: ba7f5e1902d41c0fb3a461de282f137ab14d13e6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/*
 * Copyright (C) 2009      Citrix Ltd.
 * Author Vincent Hanquez <vincent.hanquez@eu.citrix.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published
 * by the Free Software Foundation; version 2.1 only. with the special
 * exception on linking described in file LICENSE.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 */

#include <stdlib.h>
#include "flexarray.h"

flexarray_t *flexarray_make(int size, int autogrow)
{
    flexarray_t *array = malloc(sizeof(struct flexarray));
    if (array) {
        array->size = size;
        array->autogrow = autogrow;
        array->data = calloc(size, sizeof(void *));
    }
    return array;
}

void flexarray_free(flexarray_t *array)
{
    free(array->data);
    free(array);
}

int flexarray_grow(flexarray_t *array, int extents)
{
    void **data;
    int newsize;

    newsize = array->size + extents;
    data = realloc(array->data, sizeof(void *) * newsize);
    if (!data)
        return 1;
    array->size += extents;
    array->data = data;
    return 0;
}

int flexarray_set(flexarray_t *array, unsigned int index, void *ptr)
{
    if (index >= array->size) {
        int newsize;
        if (!array->autogrow)
            return 1;
        newsize = (array->size * 2 < index) ? index + 1 : array->size * 2;
        if (flexarray_grow(array, newsize - array->size))
            return 2;
    }
    array->data[index] = ptr;
    return 0;
}

int flexarray_get(flexarray_t *array, int index, void **ptr)
{
    if (index >= array->size)
        return 1;
    *ptr = array->data[index];
    return 0;
}

void **flexarray_contents(flexarray_t *array)
{
    void **data;
    data = array->data;
    free(array);
    return data;
}