--- /dev/null
+/*
+ * Copyright (C) 2012-2015 Thadeu Lima de Souza Cascardo <cascardo@minaslivre.org>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#include "list.h"
+#include <stdlib.h>
+#include <string.h>
+
+struct item {
+ void *val;
+};
+
+struct list {
+ size_t alen;
+ size_t len;
+ struct item items[];
+};
+
+struct list * list_new(void)
+{
+ struct list *list;
+ size_t alen = 128;
+ list = malloc(sizeof(*list) + alen * sizeof(struct item));
+ if (!list)
+ return NULL;
+ list->alen = alen;
+ list->len = 0;
+ memset(list->items, 0, alen * sizeof(struct item));
+ return list;
+}
+
+int list_add(struct list **list, void *val)
+{
+ unsigned int i;
+ struct list *l = *list;
+ l->items[l->len].val = val;
+ l->len++;
+ if (l->len == l->alen) {
+ struct list *nlist;
+ size_t len = l->alen * sizeof(struct item);
+ size_t nlen = len * 2;
+ nlist = realloc(l, sizeof(*nlist) + nlen);
+ if (!nlist)
+ goto out;
+ *list = l = nlist;
+ memset(&l->items[l->len], 0, len);
+ l->alen = l->alen * 2;
+ }
+ return 0;
+out:
+ l->items[l->len].val = NULL;
+ return -1;
+}
+
+void * list_get(struct list *list, int pos)
+{
+ unsigned int i;
+ if (pos >= list->len)
+ return NULL;
+ return list->items[pos].val;
+}
+
+void list_free(struct list *list, free_function_t *ifree)
+{
+ int i;
+ for (i = 0; i < list->len; i++)
+ ifree(list->items[i].val);
+ free(list);
+}
--- /dev/null
+/*
+ * Copyright (C) 2012-2015 Thadeu Lima de Souza Cascardo <cascardo@minaslivre.org>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#ifndef _LIST_H
+#define _LIST_H
+
+typedef void (free_function_t)(void *);
+struct list;
+
+struct list * list_new(void);
+int list_add(struct list **list, void *val);
+void * list_get(struct list *list, int pos);
+void list_free(struct list *list, free_function_t *ifree);
+
+#endif