core: add support for the autoverbose feature
[fbsplash.git] / core / src / list.c
blob85ed734c2351ccdf9e42b842c9a0a1cce78b945c
1 /*
2 * list.c -- list utility functions
4 * Copyright (C) 2005, 2007 Michal Januszewski <spock@gentoo.org>
6 * This file is subject to the terms and conditions of the GNU General Public
7 * License v2. See the file COPYING in the main directory of this archive for
8 * more details.
11 #include <stdlib.h>
12 #include "common.h"
13 #include "render.h"
15 void list_add(list *l, void *obj)
17 if (l->tail != NULL) {
18 l->tail->next = malloc(sizeof(item));
19 l->tail->next->p = obj;
20 l->tail->next->next = NULL;
21 l->tail = l->tail->next;
22 } else {
23 l->head = l->tail = malloc(sizeof(item));
24 l->tail->next = NULL;
25 l->tail->p = obj;
29 /**
30 * Append an element to the end of the list while simultaneously discarding
31 * the list head.
33 void list_ringadd(list *l, void *obj)
35 item *i = l->head;
37 l->head = i->next;
38 free(i->p);
39 free(i);
40 list_add(l, obj);
43 void list_free(list l, bool free_item)
45 item *i, *j;
47 for (i = l.head; i != NULL; ) {
48 j = i->next;
49 if (free_item)
50 free(i->p);
51 free(i);
52 i = j;
55 list_init(l);
58 void list_del(list *l, item *prev, item *curr)
60 if (prev)
61 prev->next = curr->next;
63 if (l->tail == curr)
64 l->tail = prev;
66 if (l->head == curr)
67 l->head = curr->next;
69 free(curr);