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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
|
/* This file is part of IPACCT
Copyright (C) 1999,2000,2001,2002,2003,2004,2005,2008 Sergey Poznyakoff
Ipacct 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, or (at your option)
any later version.
Ipacct 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 Ipacct. If not, see <http://www.gnu.org/licenses/>. */
/* Generic singly-linked lists */
/* $Id: list.c,v 1.5 2008/07/07 14:35:27 gray Exp $ */
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <stdlib.h>
#include <ipacct.h>
#include <syslog.h>
struct list_data {
struct list_data *next;
char data[1];
};
struct _list {
struct list_data *head, *tail;
};
#define ELSIZE(s) (sizeof(struct list_data) - 1 + (s))
static list_t * _list_alloc_internal(size_t size);
struct list_data *
_list_alloc_data(size_t size)
{
struct list_data *p;
p = malloc(ELSIZE(size));
if (!p) {
syslog(LOG_CRIT, "not enough memory: exiting");
abort();
}
p->next = NULL;
return p;
}
void
list_create(list_t *listp)
{
*listp = malloc(sizeof(**listp));
if (!*listp) {
syslog(LOG_CRIT, "not enough memory: exiting");
abort();
}
(*listp)->head = (*listp)->tail = NULL;
}
void *
list_alloc(list_t *listp, void *data, size_t size)
{
struct list_data *dp;
list_t list;
if (!*listp)
list_create(listp);
list = *listp;
dp = _list_alloc_data(size);
if (!list->tail)
list->head = dp;
else
list->tail->next = dp;
list->tail = dp;
memcpy(dp->data, data, size);
return dp->data;
}
void
list_free(list_t *listp)
{
struct list_data *this, *next;
if (!*listp)
return;
this = (*listp)->head;
while (this) {
next = this->next;
free(this);
this = next;
}
free(*listp);
*listp = NULL;
}
void
list_iterate(list_t list, int (*fun)(void *, void *), void *data)
{
struct list_data *this, *next;
if (!list)
return;
for (this = list->head; this; this = this->next)
if ((*fun)(this->data, data))
break;
}
|