summaryrefslogtreecommitdiff
path: root/libqueue/queue/sl.c
blob: ccc6476aeb2f8eaff493ee3c285f48c8528addd2 (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
/* vim: set ts=4 sw=4 ai:
 * sl.c -- singly linked list implementation derived from NetBSD's queue.h
 */

#include <stdlib.h>
#include "sl.h"

struct slh *sl_new(void)
{
	struct slh *hd;

	hd = malloc(sizeof(*hd));
	if (hd != NULL)
		sl_init(hd);

	return hd;
}

struct sle *sl_elem_new(void *data)
{
	struct sle *e;

	e = malloc(sizeof(*e));
	if (e != NULL)
		e->data = data;

	return e;
}

void sl_insert_after(struct sle *e, struct sle *n)
{
	n->next = e->next;
	e->next = n;
}

void sl_insert_head(struct slh *hd, struct sle *e)
{
	e->next = hd->first;
	hd->first = e;
}

void sl_remove_after(struct sle *e)
{
	if (e->next != NULL)
		e->next = e->next->next;
}

void sl_remove_head(struct slh *hd)
{
	if (hd->first != NULL)
		hd->first = hd->first->next;
}

void sl_remove(struct slh *hd, struct sle *e)
{
	struct sle *cur;

	if (hd->first == e) {
		hd->first = hd->first->next;
	}
	else {
		cur = hd->first;
		while (cur->next != NULL && cur->next != e)
			cur = cur->next;
		if (cur->next == NULL)
			return;
		cur->next = cur->next->next;
	}
}

void sl_cleanup(struct slh *hd, void (*func)(void *))
{
	struct sle *e, *t;

	sl_foreach_safe(e, hd, t) {
		sl_remove(hd, e);
		if (func)
			func(e->data);
		free(e);
	}
}

struct sle *sl_find_from_data(struct slh *hd, const void *data, int (*cmp)(const void *, const void *))
{
	struct sle *e;

	if (hd && !sl_empty(hd))
		sl_foreach(e, hd)
			if (e->data && cmp(e->data, data) == 0)
				return e;

	return NULL;
}