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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
|
/* $NetBSD: kfifo.h,v 1.6 2021/12/19 12:33:02 riastradh Exp $ */
/*-
* Copyright (c) 2018 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Taylor R. Campbell.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef _LINUX_KFIFO_H_
#define _LINUX_KFIFO_H_
#include <sys/types.h>
#include <sys/errno.h>
#include <sys/mutex.h>
#include <linux/gfp.h>
#include <linux/slab.h>
struct kfifo_meta {
kmutex_t kfm_lock;
size_t kfm_head;
size_t kfm_tail;
size_t kfm_nbytes;
};
#define _KFIFO_PTR_TYPE(TAG, TYPE) \
struct TAG { \
struct kfifo_meta kf_meta; \
TYPE *kf_buf; \
}
#define _KFIFO_TYPE(TAG, TYPE, N) \
struct TAG { \
struct kfifo_meta kf_meta; \
TYPE kf_buf[N]; \
}
#define DECLARE_KFIFO_PTR(FIFO, TYPE) _KFIFO_PTR_TYPE(, TYPE) FIFO
#define DECLARE_KFIFO(FIFO, TYPE, N) _KFIFO_TYPE(, TYPE, N) FIFO
#define INIT_KFIFO(FIFO) do \
{ \
_init_kfifo(&(FIFO).kf_meta, sizeof((FIFO).kf_buf)); \
} while (0)
#define FINI_KFIFO(FIFO) do \
{ \
_fini_kfifo(&(FIFO).kf_meta); \
} while (0)
static inline void
_init_kfifo(struct kfifo_meta *meta, size_t nbytes)
{
mutex_init(&meta->kfm_lock, MUTEX_DEFAULT, IPL_VM);
meta->kfm_head = 0;
meta->kfm_tail = 0;
meta->kfm_nbytes = nbytes;
}
static inline void
_fini_kfifo(struct kfifo_meta *meta)
{
mutex_destroy(&meta->kfm_lock);
}
_KFIFO_PTR_TYPE(kfifo, void);
#define kfifo_alloc(FIFO, SIZE, GFP) \
_kfifo_alloc(&(FIFO)->kf_meta, &(FIFO)->kf_buf, (SIZE), (GFP))
static inline int
_kfifo_alloc(struct kfifo_meta *meta, void *bufp, size_t nbytes, gfp_t gfp)
{
void *buf;
buf = kmalloc(nbytes, gfp);
if (buf == NULL)
return -ENOMEM;
/* Type pun! Hope void * == struct whatever *. */
memcpy(bufp, &buf, sizeof(void *));
_init_kfifo(meta, nbytes);
return 0;
}
#define kfifo_free(FIFO) \
_kfifo_free(&(FIFO)->kf_meta, &(FIFO)->kf_buf)
static inline void
_kfifo_free(struct kfifo_meta *meta, void *bufp)
{
void *buf;
mutex_destroy(&meta->kfm_lock);
memcpy(&buf, bufp, sizeof(void *));
kfree(buf);
/* Paranoia. */
buf = NULL;
memcpy(bufp, &buf, sizeof(void *));
}
#define kfifo_is_empty(FIFO) (kfifo_len(FIFO) == 0)
#define kfifo_len(FIFO) _kfifo_len(&(FIFO)->kf_meta)
static inline size_t
_kfifo_len(struct kfifo_meta *meta)
{
const size_t head = meta->kfm_head;
const size_t tail = meta->kfm_tail;
const size_t nbytes = meta->kfm_nbytes;
return (head <= tail ? tail - head : nbytes + tail - head);
}
#define kfifo_out_peek(FIFO, PTR, SIZE) \
_kfifo_out_peek(&(FIFO)->kf_meta, (FIFO)->kf_buf, (PTR), (SIZE))
static inline size_t
_kfifo_out_peek(struct kfifo_meta *meta, void *buf, void *ptr, size_t size)
{
const char *src = buf;
char *dst = ptr;
size_t copied = 0;
mutex_spin_enter(&meta->kfm_lock);
const size_t head = meta->kfm_head;
const size_t tail = meta->kfm_tail;
const size_t nbytes = meta->kfm_nbytes;
if (head <= tail) {
if (size <= tail - head) {
memcpy(dst, src + head, size);
copied = size;
}
} else {
if (size <= nbytes - head) {
memcpy(dst, src + head, size);
copied = size;
} else if (size <= nbytes + tail - head) {
memcpy(dst, src + head, nbytes - head);
memcpy(dst + nbytes - head, src,
size - (nbytes - head));
copied = size;
}
}
mutex_spin_exit(&meta->kfm_lock);
return copied;
}
#define kfifo_out(FIFO, PTR, SIZE) \
_kfifo_out(&(FIFO)->kf_meta, (FIFO)->kf_buf, (PTR), (SIZE))
static inline size_t
_kfifo_out(struct kfifo_meta *meta, const void *buf, void *ptr, size_t size)
{
const char *src = buf;
char *dst = ptr;
size_t copied = 0;
mutex_spin_enter(&meta->kfm_lock);
const size_t head = meta->kfm_head;
const size_t tail = meta->kfm_tail;
const size_t nbytes = meta->kfm_nbytes;
if (head <= tail) {
if (size <= tail - head) {
memcpy(dst, src + head, size);
meta->kfm_head = head + size;
copied = size;
}
} else {
if (size <= nbytes - head) {
memcpy(dst, src + head, size);
meta->kfm_head = head + size;
copied = size;
} else if (size <= nbytes + tail - head) {
memcpy(dst, src + head, nbytes - head);
memcpy(dst + nbytes - head, src,
size - (nbytes - head));
meta->kfm_head = size - (nbytes - head);
copied = size;
}
}
mutex_spin_exit(&meta->kfm_lock);
return copied;
}
#define kfifo_in(FIFO, PTR, SIZE) \
_kfifo_in(&(FIFO)->kf_meta, (FIFO)->kf_buf, (PTR), (SIZE))
static inline size_t
_kfifo_in(struct kfifo_meta *meta, void *buf, const void *ptr, size_t size)
{
const char *src = ptr;
char *dst = buf;
size_t copied = 0;
mutex_spin_enter(&meta->kfm_lock);
const size_t head = meta->kfm_head;
const size_t tail = meta->kfm_tail;
const size_t nbytes = meta->kfm_nbytes;
if (tail <= head) {
if (size <= head - tail) {
memcpy(dst + tail, src, size);
meta->kfm_tail = tail + size;
copied = size;
}
} else {
if (size <= nbytes - tail) {
memcpy(dst + tail, src, size);
meta->kfm_tail = tail + size;
} else if (size <= nbytes + tail - head) {
memcpy(dst + tail, src, nbytes - tail);
memcpy(dst, src + nbytes - tail,
size - (nbytes - tail));
meta->kfm_tail = size - (nbytes - tail);
copied = size;
}
}
mutex_spin_exit(&meta->kfm_lock);
return copied;
}
#endif /* _LINUX_KFIFO_H_ */
|