File: linked_list.h

package info (click to toggle)
translucency 0.6.0-3
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 240 kB
  • ctags: 221
  • sloc: ansic: 2,133; makefile: 260; perl: 172; sh: 102
file content (76 lines) | stat: -rw-r--r-- 1,471 bytes parent folder | download
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
#ifndef LINKED_LIST_H
#define LINKED_LIST_H

/* this file defines structures and access methods for
 * generic linear linked lists
 */

#ifdef __KERNEL__
  #include <linux/vmalloc.h>
  #define malloc(x) kmalloc(x, GFP_KERNEL)
  #define free kfree
#endif

typedef struct
{
	void *next;
	void *data;
} linked_list_t;

static inline linked_list_t *linked_list_get_next(linked_list_t *l)
{
	return (linked_list_t *)l->next;
}

static inline void *linked_list_get_data(linked_list_t *l)
{
	return l->data;
}

static inline void linked_list_set_data(linked_list_t *l, void *newdata)
{
	l->data=newdata;
}

static inline void linked_list_init(linked_list_t **l)
{
	*l=NULL;
}

static inline void _linked_list_insert(linked_list_t **l, linked_list_t *new)
{
	new->next=*l;
	*l=new;
}

/** insert a new element at start of list
*/
static inline void linked_list_insert(linked_list_t **l, void *newdata)
{
	linked_list_t *temp=(linked_list_t *)malloc(sizeof(linked_list_t));
	linked_list_set_data(temp, newdata);
	_linked_list_insert(l, temp);
}

/** remove one element from list
*/
static inline void linked_list_remove(linked_list_t **l)
{
	linked_list_t *temp=*l;
	*l=linked_list_get_next(temp);
	free(temp);
}

/** free whole linked list and set pointer to NULL
*/
static inline void linked_list_finish(linked_list_t **l)
{
  while(*l)
    {
      linked_list_remove(l);
    }
}

#define linked_list_foreach(el,list) for(el=list; el!=NULL; el=linked_list_get_next(el))

#endif