summaryrefslogtreecommitdiff
path: root/pool_list.c
blob: 6e31be64e7d00e0b5e970cc4604e0ee0f26ba900 (plain)
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/* -*-pgsql-c-*- */
/*
 *
 * $Header$
 *
 * pgpool: a language independent connection pool server for PostgreSQL 
 * written by Tatsuo Ishii
 *
 * Portions Copyright (c) 2003-2007,	PgPool Global Development Group
 * Portions Copyright (c) 2004, PostgreSQL Global Development Group
 *
 * Permission to use, copy, modify, and distribute this software and
 * its documentation for any purpose and without fee is hereby
 * granted, provided that the above copyright notice appear in all
 * copies and that both that copyright notice and this permission
 * notice appear in supporting documentation, and that the name of the
 * author not be used in advertising or publicity pertaining to
 * distribution of the software without specific, written prior
 * permission. The author makes no representations about the
 * suitability of this software for any purpose.  It is provided "as
 * is" without express or implied warranty.
 *
 * pool_list.c.: Implementation of singly-linked homogeneous lists
 *
 */

#include <string.h>
#include <errno.h>

#include "pool.h"
#include "pool_type.h"
#include "pool_list.h"

static List * new_list(void);
/* static void new_head_cell(List *list); */
static void new_tail_cell(List *list);
static void list_free_private(List *list, bool deep);

#ifndef __GNUC__

ListCell * list_head(List *l)
{
	return l ? l->head : NULL;
}

ListCell * list_tail(List *l)
{
	return l ? l->tail : NULL;
}

int list_length(List *l)
{
	return l ? l->length : 0;
}

#endif /* __GNUC__ */

List * lappend(List *list, void *datum)
{
	if (list == NIL)
		list = new_list();
	else
		new_tail_cell(list);

	lfirst(list->tail) = datum;

	return list;
}

List * lappend_int(List *list, int datum)
{
	if (list == NIL)
		list = new_list();
	else
		new_tail_cell(list);

	lfirst_int(list->tail) = datum;

	return list;
}

static List * new_list(void)
{
	List *new_list;
	ListCell *new_head;

	new_head = (ListCell *)malloc(sizeof(*new_head));
	if (new_head == NULL)
	{
		pool_error("new_list: malloc failed: %s", strerror(errno));
		exit(1);
	}
	new_head->next = NULL;

	new_list = (List *)malloc(sizeof(*new_list));
	if (new_list == NULL)
	{
		pool_error("new_list: malloc failed: %s", strerror(errno));
		exit(1);
	}
	new_list->length = 1;
	new_list->head = new_head;
	new_list->tail = new_head;

	return new_list;
}

#ifdef NOT_USED
static void new_head_cell(List *list)
{
	ListCell *new_head;

	new_head = (ListCell *)malloc(sizeof(*new_head));
	if (new_head == NULL)
	{
		pool_error("new_head_cell: malloc failed: %s", strerror(errno));
		exit(1);
	}
	new_head->next = list->head;

	list->head = new_head;
	list->length++;
}
#endif

static void new_tail_cell(List *list)
{
	ListCell *new_tail;

	new_tail = (ListCell *)malloc(sizeof(*new_tail));
	if (new_tail == NULL)
	{
		pool_error("new_tail_cell: malloc failed: %s", strerror(errno));
		exit(1);
	}
	new_tail->next = NULL;

	list->tail->next = new_tail;
	list->tail = new_tail;
	list->length++;
}

/*
 * free all storage in a list, but not the pointed-to elements
 */
void list_free(List *list)
{
	list_free_private(list, false);
}

/*
 * free all storage in a list, and the pointed-to elements iff deep is true
 */
static void list_free_private(List *list, bool deep)
{
	ListCell *cell;

	cell = list_head(list);
	while (cell != NULL)
	{
		ListCell *tmp = cell;

		cell = lnext(cell);
		if (deep)
			free(lfirst(tmp));
		free(tmp);
	}

	if (list)
		free(list);
}