aboutsummaryrefslogtreecommitdiffstats
path: root/llist.c
blob: 4852f9f403f54fa2bd6aa42d371cb3e2fee4fc7e (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
/*
 * llist.c
 * implementation of a doubly linked list with sentinel node
 *
 * Erik Liodden
 */

#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include <string.h>
#include "structure.h"
#include "misc.h"

/* llist_node_init: initialize a new node (allocated on the heap) for use in the
 * list
 */
static void llist_node_init(struct llist_node *node, size_t elem_size,
		void (*freefn)(void *))
{
	node->prev = node;
	node->next = node;
	node->elem = allocate(elem_size);
	node->elem_size = elem_size;
	node->freefn = freefn;
}
	
/* llist_init: initialise a new list pointed to by l. l itself can be allocated
 * by the caller on the stack.
 */
void llist_init(struct llist *l)
{
	struct llist_node *sentinel = llist_create_node(0, NULL);
	sentinel->next = sentinel;
	sentinel->prev = sentinel;
	l->nil = sentinel;
}

/* llist_create_node: create a new node on the heap and set an element size
 * and assign a free function for the data.
 */
struct llist_node *llist_create_node(size_t elem_size, void (*freefn)(void *))
{
	struct llist_node *node = malloc(sizeof(*node));
	llist_node_init(node, elem_size, freefn);
	return node;
}

/* llist_node_set_elem: copy the value located at elem_addr to the nodes element
 * value. this assumes that elem_addr is of the same size as node->elem_size
 * (which was set when the node was created)
 */
void llist_node_set_elem(struct llist_node *node, void *elem_addr)
{
	memcpy(node->elem, elem_addr, node->elem_size);
}

/* llist_insert_after: insert new_node after node in the linked list
 */
void llist_insert_after(struct llist_node *node, struct llist_node *new_node)
{
	new_node->next = node->next;
	new_node->prev = node;
	node->next->prev = new_node;
	node->next = new_node;
}

/* llist_insert_before: insert new_node before node in the linked list
 */
void llist_insert_before(struct llist_node *node, struct llist_node *new_node)
{
	llist_insert_after(node->prev, new_node);
}

/* llist_insert_end: insert new_node at the end of the linked list l
 */
void llist_insert_end(struct llist *l, struct llist_node *node)
{
	llist_insert_before(l->nil, node);
}

/* llist_insert_beginning: insert new_node at the beginning of the linked list l
 */
void llist_insert_beginning(struct llist *l, struct llist_node *node)
{
	llist_insert_after(l->nil, node);
}

/* llist_destroy_node: delete the node and free the memory pointed to by
 * node->elem and the node itself.
 */
void llist_destroy_node(struct llist_node *node)
{
	if (node->freefn)
		node->freefn(node->elem);
	free(node->elem);
	free(node);
}

/* llist_remove_node: remove node from the linked list it belongs to. the node
 * itself is not destroied afterwards.
 */
void llist_remove_node(struct llist_node *node)
{
	node->prev->next = node->next;
	node->next->prev = node->prev;
	/* llist_destroy_node(node); */
}

/* llist_get_elem: copy the value in node->elem (node->elem_size number of
 * bytes) to the memory location pointed to by elem_addr.
 */
void llist_get_elem(struct llist_node *node, void *elem_addr)
{
	memcpy(elem_addr, node->elem, node->elem_size);
}

/* llist_dispose: destroy all the nodes in the list l, including the sentinel
 * node.
 */
void llist_dispose(struct llist *l)
{
	struct llist_node *node = l->nil->next;
	while ((node = node->next) != l->nil)
		llist_destroy_node(node->prev);
	free(l->nil->elem);
	free(l->nil);
}