From d03c41353fc01b1901305702552cf99fe4bad5f9 Mon Sep 17 00:00:00 2001 From: Thadeu Lima de Souza Cascardo Date: Sun, 26 Apr 2015 19:16:36 +0000 Subject: [PATCH] =?utf8?q?Adiciona=20implementa=C3=A7=C3=A3o=20de=20lista.?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- list.c | 83 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ list.h | 30 +++++++++++++++++++++ 2 files changed, 113 insertions(+) create mode 100644 list.c create mode 100644 list.h diff --git a/list.c b/list.c new file mode 100644 index 0000000..90068e7 --- /dev/null +++ b/list.c @@ -0,0 +1,83 @@ +/* + * Copyright (C) 2012-2015 Thadeu Lima de Souza Cascardo + * + * 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 +#include + +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); +} diff --git a/list.h b/list.h new file mode 100644 index 0000000..43fc8e2 --- /dev/null +++ b/list.h @@ -0,0 +1,30 @@ +/* + * Copyright (C) 2012-2015 Thadeu Lima de Souza Cascardo + * + * 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 -- 2.20.1