aboutsummaryrefslogtreecommitdiff
path: root/helper/include/odph_list_internal.h
blob: bfcf77ab35dbd8bf0304b23915d5f49f72252fdd (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
/* Copyright (c) 2015-2018, Linaro Limited
 * All rights reserved.
 *
 * SPDX-License-Identifier:     BSD-3-Clause
 */

/**
 * @file
 *
 * ODP list
 * a simple implementation of Doubly linked list
 */

#ifndef ODPH_LIST_INTER_H_
#define ODPH_LIST_INTER_H_

#ifdef __cplusplus
extern "C" {
#endif

/** List object */
typedef struct odph_list_object {
	/** Next element in list */
	struct odph_list_object *next;

	/** Previous element in list */
	struct odph_list_object *prev;
} odph_list_object;

/** Head of list */
typedef odph_list_object odph_list_head;

/**
 * @internal Intiailize list head element
 *
 * @param list List object to be initialized
 */
static inline void ODPH_INIT_LIST_HEAD(odph_list_object *list)
{
	list->next = list;
	list->prev = list;
}

/** @internal Inline function @param new @param prev @param next */
static inline void __odph_list_add(odph_list_object *new,
				   odph_list_object *prev,
				   odph_list_object *next)
{
	next->prev = new;
	new->next = next;
	new->prev = prev;
	prev->next = new;
}

/** @internal Inline function @param new @param head */
static inline void odph_list_add(odph_list_object *new, odph_list_object *head)
{
	__odph_list_add(new, head, head->next);
}

/** @internal Inline function @param new @param head */
static inline void odph_list_add_tail(struct odph_list_object *new,
				      odph_list_object *head)
{
	__odph_list_add(new, head->prev, head);
}

/** @internal Inline function @param prev @param next */
static inline void __odph_list_del(struct odph_list_object *prev,
				   odph_list_object *next)
{
	next->prev = prev;
	prev->next = next;
}

/** @internal Inline function @param entry */
static inline void odph_list_del(struct odph_list_object *entry)
{
	__odph_list_del(entry->prev, entry->next);
	ODPH_INIT_LIST_HEAD(entry);
}

/** @internal Inline function @param head @return */
static inline int odph_list_empty(const struct odph_list_object *head)
{
	return head->next == head;
}

/** @internal */
#define container_of(ptr, type, list_node) \
		((type *)(void *)((char *)ptr - offsetof(type, list_node)))

/** @internal */
#define ODPH_LIST_FOR_EACH(pos, list_head, type, list_node) \
	for (pos = container_of((list_head)->next, type, list_node); \
		&pos->list_node != (list_head); \
		pos = container_of(pos->list_node.next, type, list_node))

#ifdef __cplusplus
}
#endif

#endif