2 * Copyright (C) 2012-2015 Thadeu Lima de Souza Cascardo <cascardo@minaslivre.org>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 3 of the License, or
7 * (at your option) any later version.
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.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
33 struct list * list_new(void)
37 list = malloc(sizeof(*list) + alen * sizeof(struct item));
42 memset(list->items, 0, alen * sizeof(struct item));
46 int list_add(struct list **list, void *val)
49 struct list *l = *list;
50 l->items[l->len].val = val;
52 if (l->len == l->alen) {
54 size_t len = l->alen * sizeof(struct item);
55 size_t nlen = len * 2;
56 nlist = realloc(l, sizeof(*nlist) + nlen);
60 memset(&l->items[l->len], 0, len);
61 l->alen = l->alen * 2;
65 l->items[l->len].val = NULL;
69 void * list_get(struct list *list, int pos)
74 return list->items[pos].val;
77 void list_free(struct list *list, free_function_t *ifree)
80 for (i = 0; i < list->len; i++)
81 ifree(list->items[i].val);