forked from gozfree/gear-lib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqueue.h
166 lines (149 loc) · 3.29 KB
/
queue.h
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
/*****************************************************************************
* Copyright (C) 2014-2015
* file: queue.h
* author: gozfree <[email protected]>
* created: 2015-08-10 00:16
* updated: 2015-08-10 00:16
*****************************************************************************/
#ifndef QUEUE_H
#define QUEUE_H
#ifdef __cplusplus
extern "C" {
#endif
#include <stdint.h>
#include <stddef.h>
#include <kernel_list.h>
typedef struct {
void *data;
struct list_head entry;
} list_t;
typedef struct {
list_t member;
uint32_t length;
} queue_t;
static inline void queue_init(queue_t *q)
{
if (q == NULL) {
return;
}
INIT_LIST_HEAD(&q->member.entry);
q->member.data = NULL;
q->length = 0;
}
static inline void queue_clear(queue_t *q)
{
if (q == NULL) {
return;
}
list_t *iter = NULL;
list_t *next = NULL;
list_for_each_entry_safe(iter, next, &q->member.entry, entry) {
list_del(&iter->entry);
if (!iter) {
continue;
}
free(iter->data);
free(iter);
}
q->length = 0;
}
static inline void *queue_pop_head(queue_t *q)
{
if (q == NULL) {
return NULL;
}
list_t *h = list_first_entry_or_null(&q->member.entry, list_t, entry);
if (h == NULL) {
return NULL;
}
list_del(&h->entry);
q->length--;
return h->data;
}
static inline uint32_t queue_get_length(queue_t *q)
{
if (q == NULL) {
return 0;
}
return q->length;
}
static inline void *queue_peek_head(queue_t *q)
{
if (q == NULL) {
return NULL;
}
list_t *h = list_first_entry_or_null(&q->member.entry, list_t, entry);
if (h == NULL) {
return NULL;
}
return h->data;
}
static inline void *queue_peek_tail(queue_t *q)
{
if (q == NULL) {
return NULL;
}
list_t *h = list_last_entry_or_null(&q->member.entry, list_t, entry);
if (h == NULL) {
return NULL;
}
return h->data;
}
static inline void queue_push_tail(queue_t *q, void *data)
{
if (q == NULL) {
return;
}
list_t *_new = (list_t *)calloc(1, sizeof(list_t));
_new->data = data;
list_add_tail(&_new->entry, &q->member.entry);
q->length++;
}
static inline list_t *queue_find(queue_t *q, const void *data)
{
if (q == NULL) {
return NULL;
}
list_t *iter;
list_for_each_entry(iter, &q->member.entry, entry) {
if (iter->data == data)
break;
}
return iter;
}
static inline void queue_insert_after(queue_t *q, list_t *sibling, void *data)
{
if (q == NULL || sibling == NULL) {
return;
}
list_t *_new = (list_t *)calloc(1, sizeof(list_t));
_new->data = data;
list_add_tail(&_new->entry, &sibling->entry);
q->length++;
}
static inline list_t *queue_peek_head_link(queue_t *q)
{
if (q == NULL) {
return NULL;
}
list_t *h = list_first_entry_or_null(&q->member.entry, list_t, entry);
if (h == NULL) {
return NULL;
}
return h;
}
static inline list_t *queue_peek_tail_link(queue_t *q)
{
if (q == NULL) {
return NULL;
}
list_t *h = list_last_entry_or_null(&q->member.entry, list_t, entry);
if (h == NULL) {
return NULL;
}
return h;
}
#ifdef __cplusplus
extern "C" {
#endif
#endif