kfifo.h revision 1.4 1 1.4 riastrad /* $NetBSD: kfifo.h,v 1.4 2021/12/19 10:57:34 riastradh Exp $ */
2 1.1 riastrad
3 1.1 riastrad /*-
4 1.2 riastrad * Copyright (c) 2018 The NetBSD Foundation, Inc.
5 1.1 riastrad * All rights reserved.
6 1.1 riastrad *
7 1.1 riastrad * This code is derived from software contributed to The NetBSD Foundation
8 1.1 riastrad * by Taylor R. Campbell.
9 1.1 riastrad *
10 1.1 riastrad * Redistribution and use in source and binary forms, with or without
11 1.1 riastrad * modification, are permitted provided that the following conditions
12 1.1 riastrad * are met:
13 1.1 riastrad * 1. Redistributions of source code must retain the above copyright
14 1.1 riastrad * notice, this list of conditions and the following disclaimer.
15 1.1 riastrad * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 riastrad * notice, this list of conditions and the following disclaimer in the
17 1.1 riastrad * documentation and/or other materials provided with the distribution.
18 1.1 riastrad *
19 1.1 riastrad * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1 riastrad * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1 riastrad * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1 riastrad * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1 riastrad * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1 riastrad * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1 riastrad * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1 riastrad * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1 riastrad * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1 riastrad * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1 riastrad * POSSIBILITY OF SUCH DAMAGE.
30 1.1 riastrad */
31 1.1 riastrad
32 1.1 riastrad #ifndef _LINUX_KFIFO_H_
33 1.1 riastrad #define _LINUX_KFIFO_H_
34 1.1 riastrad
35 1.2 riastrad #include <sys/types.h>
36 1.2 riastrad #include <sys/errno.h>
37 1.2 riastrad #include <sys/mutex.h>
38 1.2 riastrad
39 1.2 riastrad #include <linux/gfp.h>
40 1.2 riastrad #include <linux/slab.h>
41 1.2 riastrad
42 1.2 riastrad struct kfifo_meta {
43 1.2 riastrad kmutex_t kfm_lock;
44 1.2 riastrad size_t kfm_head;
45 1.2 riastrad size_t kfm_tail;
46 1.2 riastrad size_t kfm_nbytes;
47 1.2 riastrad };
48 1.2 riastrad
49 1.2 riastrad #define _KFIFO_PTR_TYPE(TAG, TYPE) \
50 1.2 riastrad struct TAG { \
51 1.2 riastrad struct kfifo_meta kf_meta; \
52 1.2 riastrad TYPE *kf_buf; \
53 1.2 riastrad }
54 1.2 riastrad
55 1.4 riastrad #define _KFIFO_TYPE(TAG, TYPE, N) \
56 1.4 riastrad struct TAG { \
57 1.4 riastrad struct kfifo_meta kf_meta; \
58 1.4 riastrad TYPE kf_buf[N]; \
59 1.4 riastrad }
60 1.4 riastrad
61 1.2 riastrad #define DECLARE_KFIFO_PTR(FIFO, TYPE) _KFIFO_PTR_TYPE(, TYPE) FIFO
62 1.4 riastrad #define DECLARE_KFIFO(FIFO, TYPE, N) _KFIFO_TYPE(, TYPE, N) FIFO
63 1.2 riastrad
64 1.2 riastrad _KFIFO_PTR_TYPE(kfifo, void);
65 1.2 riastrad
66 1.2 riastrad #define kfifo_alloc(FIFO, SIZE, GFP) \
67 1.2 riastrad _kfifo_alloc(&(FIFO)->kf_meta, &(FIFO)->kf_buf, (SIZE), (GFP))
68 1.2 riastrad
69 1.2 riastrad static inline int
70 1.2 riastrad _kfifo_alloc(struct kfifo_meta *meta, void *bufp, size_t nbytes, gfp_t gfp)
71 1.2 riastrad {
72 1.2 riastrad void *buf;
73 1.2 riastrad
74 1.2 riastrad buf = kmalloc(nbytes, gfp);
75 1.2 riastrad if (buf == NULL)
76 1.2 riastrad return -ENOMEM;
77 1.2 riastrad
78 1.2 riastrad /* Type pun! Hope void * == struct whatever *. */
79 1.2 riastrad memcpy(bufp, &buf, sizeof(void *));
80 1.2 riastrad
81 1.3 riastrad mutex_init(&meta->kfm_lock, MUTEX_DEFAULT, IPL_VM);
82 1.2 riastrad meta->kfm_head = 0;
83 1.2 riastrad meta->kfm_tail = 0;
84 1.2 riastrad meta->kfm_nbytes = nbytes;
85 1.2 riastrad
86 1.2 riastrad return 0;
87 1.2 riastrad }
88 1.2 riastrad
89 1.2 riastrad #define kfifo_free(FIFO) \
90 1.2 riastrad _kfifo_free(&(FIFO)->kf_meta, &(FIFO)->kf_buf)
91 1.2 riastrad
92 1.2 riastrad static inline void
93 1.2 riastrad _kfifo_free(struct kfifo_meta *meta, void *bufp)
94 1.2 riastrad {
95 1.2 riastrad void *buf;
96 1.2 riastrad
97 1.2 riastrad mutex_destroy(&meta->kfm_lock);
98 1.2 riastrad
99 1.2 riastrad memcpy(&buf, bufp, sizeof(void *));
100 1.2 riastrad kfree(buf);
101 1.2 riastrad
102 1.2 riastrad /* Paranoia. */
103 1.2 riastrad buf = NULL;
104 1.2 riastrad memcpy(bufp, &buf, sizeof(void *));
105 1.2 riastrad }
106 1.2 riastrad
107 1.2 riastrad #define kfifo_is_empty(FIFO) (kfifo_len(FIFO) == 0)
108 1.2 riastrad #define kfifo_len(FIFO) _kfifo_len(&(FIFO)->kf_meta)
109 1.2 riastrad
110 1.2 riastrad static inline size_t
111 1.2 riastrad _kfifo_len(struct kfifo_meta *meta)
112 1.2 riastrad {
113 1.2 riastrad const size_t head = meta->kfm_head;
114 1.2 riastrad const size_t tail = meta->kfm_tail;
115 1.2 riastrad const size_t nbytes = meta->kfm_nbytes;
116 1.2 riastrad
117 1.2 riastrad return (head <= tail ? tail - head : nbytes + tail - head);
118 1.2 riastrad }
119 1.2 riastrad
120 1.2 riastrad #define kfifo_out_peek(FIFO, PTR, SIZE) \
121 1.2 riastrad _kfifo_out_peek(&(FIFO)->kf_meta, (FIFO)->kf_buf, (PTR), (SIZE))
122 1.2 riastrad
123 1.2 riastrad static inline size_t
124 1.2 riastrad _kfifo_out_peek(struct kfifo_meta *meta, void *buf, void *ptr, size_t size)
125 1.2 riastrad {
126 1.2 riastrad const char *src = buf;
127 1.2 riastrad char *dst = ptr;
128 1.2 riastrad size_t copied = 0;
129 1.2 riastrad
130 1.3 riastrad mutex_spin_enter(&meta->kfm_lock);
131 1.2 riastrad const size_t head = meta->kfm_head;
132 1.2 riastrad const size_t tail = meta->kfm_tail;
133 1.2 riastrad const size_t nbytes = meta->kfm_nbytes;
134 1.2 riastrad if (head <= tail) {
135 1.2 riastrad if (size <= tail - head) {
136 1.2 riastrad memcpy(dst, src + head, size);
137 1.2 riastrad copied = size;
138 1.2 riastrad }
139 1.2 riastrad } else {
140 1.2 riastrad if (size <= nbytes - head) {
141 1.2 riastrad memcpy(dst, src + head, size);
142 1.2 riastrad copied = size;
143 1.2 riastrad } else if (size <= nbytes + tail - head) {
144 1.2 riastrad memcpy(dst, src + head, nbytes - head);
145 1.2 riastrad memcpy(dst + nbytes - head, src,
146 1.2 riastrad size - (nbytes - head));
147 1.2 riastrad copied = size;
148 1.2 riastrad }
149 1.2 riastrad }
150 1.3 riastrad mutex_spin_exit(&meta->kfm_lock);
151 1.2 riastrad
152 1.2 riastrad return copied;
153 1.2 riastrad }
154 1.2 riastrad
155 1.2 riastrad #define kfifo_out(FIFO, PTR, SIZE) \
156 1.2 riastrad _kfifo_out(&(FIFO)->kf_meta, (FIFO)->kf_buf, (PTR), (SIZE))
157 1.2 riastrad
158 1.2 riastrad static inline size_t
159 1.2 riastrad _kfifo_out(struct kfifo_meta *meta, const void *buf, void *ptr, size_t size)
160 1.2 riastrad {
161 1.2 riastrad const char *src = buf;
162 1.2 riastrad char *dst = ptr;
163 1.2 riastrad size_t copied = 0;
164 1.2 riastrad
165 1.3 riastrad mutex_spin_enter(&meta->kfm_lock);
166 1.2 riastrad const size_t head = meta->kfm_head;
167 1.2 riastrad const size_t tail = meta->kfm_tail;
168 1.2 riastrad const size_t nbytes = meta->kfm_nbytes;
169 1.2 riastrad if (head <= tail) {
170 1.2 riastrad if (size <= tail - head) {
171 1.2 riastrad memcpy(dst, src + head, size);
172 1.2 riastrad meta->kfm_head = head + size;
173 1.2 riastrad copied = size;
174 1.2 riastrad }
175 1.2 riastrad } else {
176 1.2 riastrad if (size <= nbytes - head) {
177 1.2 riastrad memcpy(dst, src + head, size);
178 1.2 riastrad meta->kfm_head = head + size;
179 1.2 riastrad copied = size;
180 1.2 riastrad } else if (size <= nbytes + tail - head) {
181 1.2 riastrad memcpy(dst, src + head, nbytes - head);
182 1.2 riastrad memcpy(dst + nbytes - head, src,
183 1.2 riastrad size - (nbytes - head));
184 1.2 riastrad meta->kfm_head = size - (nbytes - head);
185 1.2 riastrad copied = size;
186 1.2 riastrad }
187 1.2 riastrad }
188 1.3 riastrad mutex_spin_exit(&meta->kfm_lock);
189 1.2 riastrad
190 1.2 riastrad return copied;
191 1.2 riastrad }
192 1.2 riastrad
193 1.2 riastrad #define kfifo_in(FIFO, PTR, SIZE) \
194 1.2 riastrad _kfifo_in(&(FIFO)->kf_meta, (FIFO)->kf_buf, (PTR), (SIZE))
195 1.2 riastrad
196 1.2 riastrad static inline size_t
197 1.2 riastrad _kfifo_in(struct kfifo_meta *meta, void *buf, const void *ptr, size_t size)
198 1.2 riastrad {
199 1.2 riastrad const char *src = ptr;
200 1.2 riastrad char *dst = buf;
201 1.2 riastrad size_t copied = 0;
202 1.2 riastrad
203 1.3 riastrad mutex_spin_enter(&meta->kfm_lock);
204 1.2 riastrad const size_t head = meta->kfm_head;
205 1.2 riastrad const size_t tail = meta->kfm_tail;
206 1.2 riastrad const size_t nbytes = meta->kfm_nbytes;
207 1.2 riastrad if (tail <= head) {
208 1.2 riastrad if (size <= head - tail) {
209 1.2 riastrad memcpy(dst + tail, src, size);
210 1.2 riastrad meta->kfm_tail = tail + size;
211 1.2 riastrad copied = size;
212 1.2 riastrad }
213 1.2 riastrad } else {
214 1.2 riastrad if (size <= nbytes - tail) {
215 1.2 riastrad memcpy(dst + tail, src, size);
216 1.2 riastrad meta->kfm_tail = tail + size;
217 1.2 riastrad } else if (size <= nbytes + tail - head) {
218 1.2 riastrad memcpy(dst + tail, src, nbytes - tail);
219 1.2 riastrad memcpy(dst, src + nbytes - tail,
220 1.2 riastrad size - (nbytes - tail));
221 1.2 riastrad meta->kfm_tail = size - (nbytes - tail);
222 1.2 riastrad copied = size;
223 1.2 riastrad }
224 1.2 riastrad }
225 1.3 riastrad mutex_spin_exit(&meta->kfm_lock);
226 1.1 riastrad
227 1.2 riastrad return copied;
228 1.2 riastrad }
229 1.1 riastrad
230 1.1 riastrad #endif /* _LINUX_KFIFO_H_ */
231