File: list.c

package info (click to toggle)
tsdecrypt 8.1-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 1,084 kB
  • sloc: ansic: 13,568; makefile: 248; sh: 183
file content (173 lines) | stat: -rw-r--r-- 3,640 bytes parent folder | download | duplicates (4)
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
172
173
/*
 * List manipulations
 * Copyright (C) 2006-2010 Unix Solutions Ltd.
 *
 * Released under MIT license.
 * See LICENSE-MIT.txt for license terms.
 */
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <pthread.h>

#include "libfuncs.h"
#include "list.h"

//#define LIST_DEBUG

#ifdef LIST_DEBUG
	#define Ldbg fprintf
#else
	#define Ldbg(...) do { /* ... */ } while(0)
#endif

LIST *list_new(char *name) {
	pthread_mutex_t *mutex = malloc(sizeof(pthread_mutex_t));
	if (pthread_mutex_init(mutex,NULL) != 0) {
		perror("list_new: mutex_init");
		return NULL;
	}
	LIST *list = calloc(1, sizeof(LIST));
	if (!list)
		return NULL;
	list->mutex = mutex;
	list->name = strdup(name);

	LNODE *node = malloc(sizeof(LNODE));
	if (!node)
		return NULL;

	node->data = NULL;
	node->next = node;
	node->prev = node;

	list->head = node;	// Point to first
	list->tail = node;	// Set prev element

	Ldbg(stderr, "list_new(%s) l=%p l->head=%p l->tail=%p\n", list->name, list, list->head, list->tail);
	return list;
}

void list_free(LIST **plist, void (*free_func)(void *), void (*freep_func)(void **)) {
	LIST *list = *plist;
	if (!list)
		return;
	Ldbg(stderr, "list_free(%s)\n", list->name);
	LNODE *node, *tmp;
	list_lock(list);
	list_for_each(list, node, tmp) {
		if (free_func && node->data)
			free_func(node->data);
		if (freep_func && node->data)
			freep_func(&node->data);
		list_del_unlocked(list, &node);
	}
	FREE(list->head);
	list_unlock(list);
	pthread_mutex_destroy(list->mutex);
	FREE(list->mutex);
	FREE(list->name);
	FREE(*plist);
}

void list_lock(LIST *list) {
	pthread_mutex_lock(list->mutex);
}

void list_unlock(LIST *list) {
	pthread_mutex_unlock(list->mutex);
}

void list_add(LIST *list, void *data) {
	if (!list) {
		Ldbg(stderr, "list_add(), list == NULL\n");
		return;
	}

	if (!data) {
		Ldbg(stderr, "list_add(%s), data == NULL\n", list->name);
		return;
	}

	LNODE *node = malloc(sizeof(LNODE));
	if (!node) {
		Ldbg(stderr, "list_add(%s), can't alloc node\n", list->name);
		return;
	}

	node->data = data;

//	if (strcmp(list->name, "clients") == 0 || strcmp(list->name, "r->clients") == 0)
	Ldbg(stderr, "list_add(%s), node=%p data=%p\n", list->name, node, node->data);

	list_lock(list);

	node->prev = list->tail;
	node->next = list->head;

	list->tail->next = node;
	list->tail = node;
	list->head->prev = node;

	list->items++;
	list_unlock(list);
}

void list_dump(LIST *list) {
	if (list == NULL) {
		fprintf(stderr, "list_dump(), list is null\n");
		return;
	}
	fprintf(stderr, "list_dump(%s), nodes:%d\n", list->name, list->items);
	LNODE *node, *tmp;
	int i = 0;
	list_for_each(list, node, tmp) {
		fprintf(stderr, "   Node:%d Head:%p Tail:%p Node:%p NodeNext:%p NodePrev:%p Data:%p -> %s\n",
			i++, list->head, list->tail, node, node->next, node->prev, node->data, (char *)node->data);
	}
}

int list_del_unlocked(LIST *list, LNODE **node) {
	if (!list || !*node)
		return 0;

	LNODE *prev = (*node)->prev;
	LNODE *next = (*node)->next;

	next->prev = prev;
	prev->next = next;
	if (*node == list->tail)
		list->tail = prev;

	list->items--;

	FREE(*node);
	return 1;
}


int list_del(LIST *list, LNODE **node) {
	if (!list || !*node)
		return 0;
	list_lock(list);
	int ret = list_del_unlocked(list, node);
	list_unlock(list);
	return ret;
}

int list_del_entry(LIST *list, void *data) {
	int ret = 0;
	if (!list || !data)
		return 0;
	Ldbg(stderr, "list_del_entry(%s, %p)\n", list->name, data);
	list_lock(list);
	LNODE *node, *tmp;
	list_for_each(list, node, tmp) {
		if (node->data == data) {
			ret = list_del_unlocked(list, &node);
			break;
		}
	}
	list_unlock(list);
	return ret;
}