kfifo.h revision 1.2 1 1.1 riastrad /* $NetBSD: kfifo.h,v 1.2 2018/08/27 14:00:26 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.2 riastrad #define DECLARE_KFIFO_PTR(FIFO, TYPE) _KFIFO_PTR_TYPE(, TYPE) FIFO
56 1.2 riastrad
57 1.2 riastrad _KFIFO_PTR_TYPE(kfifo, void);
58 1.2 riastrad
59 1.2 riastrad #define kfifo_alloc(FIFO, SIZE, GFP) \
60 1.2 riastrad _kfifo_alloc(&(FIFO)->kf_meta, &(FIFO)->kf_buf, (SIZE), (GFP))
61 1.2 riastrad
62 1.2 riastrad static inline int
63 1.2 riastrad _kfifo_alloc(struct kfifo_meta *meta, void *bufp, size_t nbytes, gfp_t gfp)
64 1.2 riastrad {
65 1.2 riastrad void *buf;
66 1.2 riastrad
67 1.2 riastrad buf = kmalloc(nbytes, gfp);
68 1.2 riastrad if (buf == NULL)
69 1.2 riastrad return -ENOMEM;
70 1.2 riastrad
71 1.2 riastrad /* Type pun! Hope void * == struct whatever *. */
72 1.2 riastrad memcpy(bufp, &buf, sizeof(void *));
73 1.2 riastrad
74 1.2 riastrad mutex_init(&meta->kfm_lock, MUTEX_DEFAULT, IPL_NONE);
75 1.2 riastrad meta->kfm_head = 0;
76 1.2 riastrad meta->kfm_tail = 0;
77 1.2 riastrad meta->kfm_nbytes = nbytes;
78 1.2 riastrad
79 1.2 riastrad return 0;
80 1.2 riastrad }
81 1.2 riastrad
82 1.2 riastrad #define kfifo_free(FIFO) \
83 1.2 riastrad _kfifo_free(&(FIFO)->kf_meta, &(FIFO)->kf_buf)
84 1.2 riastrad
85 1.2 riastrad static inline void
86 1.2 riastrad _kfifo_free(struct kfifo_meta *meta, void *bufp)
87 1.2 riastrad {
88 1.2 riastrad void *buf;
89 1.2 riastrad
90 1.2 riastrad mutex_destroy(&meta->kfm_lock);
91 1.2 riastrad
92 1.2 riastrad memcpy(&buf, bufp, sizeof(void *));
93 1.2 riastrad kfree(buf);
94 1.2 riastrad
95 1.2 riastrad /* Paranoia. */
96 1.2 riastrad buf = NULL;
97 1.2 riastrad memcpy(bufp, &buf, sizeof(void *));
98 1.2 riastrad }
99 1.2 riastrad
100 1.2 riastrad #define kfifo_is_empty(FIFO) (kfifo_len(FIFO) == 0)
101 1.2 riastrad #define kfifo_len(FIFO) _kfifo_len(&(FIFO)->kf_meta)
102 1.2 riastrad
103 1.2 riastrad static inline size_t
104 1.2 riastrad _kfifo_len(struct kfifo_meta *meta)
105 1.2 riastrad {
106 1.2 riastrad const size_t head = meta->kfm_head;
107 1.2 riastrad const size_t tail = meta->kfm_tail;
108 1.2 riastrad const size_t nbytes = meta->kfm_nbytes;
109 1.2 riastrad
110 1.2 riastrad return (head <= tail ? tail - head : nbytes + tail - head);
111 1.2 riastrad }
112 1.2 riastrad
113 1.2 riastrad #define kfifo_out_peek(FIFO, PTR, SIZE) \
114 1.2 riastrad _kfifo_out_peek(&(FIFO)->kf_meta, (FIFO)->kf_buf, (PTR), (SIZE))
115 1.2 riastrad
116 1.2 riastrad static inline size_t
117 1.2 riastrad _kfifo_out_peek(struct kfifo_meta *meta, void *buf, void *ptr, size_t size)
118 1.2 riastrad {
119 1.2 riastrad const char *src = buf;
120 1.2 riastrad char *dst = ptr;
121 1.2 riastrad size_t copied = 0;
122 1.2 riastrad
123 1.2 riastrad mutex_enter(&meta->kfm_lock);
124 1.2 riastrad const size_t head = meta->kfm_head;
125 1.2 riastrad const size_t tail = meta->kfm_tail;
126 1.2 riastrad const size_t nbytes = meta->kfm_nbytes;
127 1.2 riastrad if (head <= tail) {
128 1.2 riastrad if (size <= tail - head) {
129 1.2 riastrad memcpy(dst, src + head, size);
130 1.2 riastrad copied = size;
131 1.2 riastrad }
132 1.2 riastrad } else {
133 1.2 riastrad if (size <= nbytes - head) {
134 1.2 riastrad memcpy(dst, src + head, size);
135 1.2 riastrad copied = size;
136 1.2 riastrad } else if (size <= nbytes + tail - head) {
137 1.2 riastrad memcpy(dst, src + head, nbytes - head);
138 1.2 riastrad memcpy(dst + nbytes - head, src,
139 1.2 riastrad size - (nbytes - head));
140 1.2 riastrad copied = size;
141 1.2 riastrad }
142 1.2 riastrad }
143 1.2 riastrad mutex_exit(&meta->kfm_lock);
144 1.2 riastrad
145 1.2 riastrad return copied;
146 1.2 riastrad }
147 1.2 riastrad
148 1.2 riastrad #define kfifo_out(FIFO, PTR, SIZE) \
149 1.2 riastrad _kfifo_out(&(FIFO)->kf_meta, (FIFO)->kf_buf, (PTR), (SIZE))
150 1.2 riastrad
151 1.2 riastrad static inline size_t
152 1.2 riastrad _kfifo_out(struct kfifo_meta *meta, const void *buf, void *ptr, size_t size)
153 1.2 riastrad {
154 1.2 riastrad const char *src = buf;
155 1.2 riastrad char *dst = ptr;
156 1.2 riastrad size_t copied = 0;
157 1.2 riastrad
158 1.2 riastrad mutex_enter(&meta->kfm_lock);
159 1.2 riastrad const size_t head = meta->kfm_head;
160 1.2 riastrad const size_t tail = meta->kfm_tail;
161 1.2 riastrad const size_t nbytes = meta->kfm_nbytes;
162 1.2 riastrad if (head <= tail) {
163 1.2 riastrad if (size <= tail - head) {
164 1.2 riastrad memcpy(dst, src + head, size);
165 1.2 riastrad meta->kfm_head = head + size;
166 1.2 riastrad copied = size;
167 1.2 riastrad }
168 1.2 riastrad } else {
169 1.2 riastrad if (size <= nbytes - head) {
170 1.2 riastrad memcpy(dst, src + head, size);
171 1.2 riastrad meta->kfm_head = head + size;
172 1.2 riastrad copied = size;
173 1.2 riastrad } else if (size <= nbytes + tail - head) {
174 1.2 riastrad memcpy(dst, src + head, nbytes - head);
175 1.2 riastrad memcpy(dst + nbytes - head, src,
176 1.2 riastrad size - (nbytes - head));
177 1.2 riastrad meta->kfm_head = size - (nbytes - head);
178 1.2 riastrad copied = size;
179 1.2 riastrad }
180 1.2 riastrad }
181 1.2 riastrad mutex_exit(&meta->kfm_lock);
182 1.2 riastrad
183 1.2 riastrad return copied;
184 1.2 riastrad }
185 1.2 riastrad
186 1.2 riastrad #define kfifo_in(FIFO, PTR, SIZE) \
187 1.2 riastrad _kfifo_in(&(FIFO)->kf_meta, (FIFO)->kf_buf, (PTR), (SIZE))
188 1.2 riastrad
189 1.2 riastrad static inline size_t
190 1.2 riastrad _kfifo_in(struct kfifo_meta *meta, void *buf, const void *ptr, size_t size)
191 1.2 riastrad {
192 1.2 riastrad const char *src = ptr;
193 1.2 riastrad char *dst = buf;
194 1.2 riastrad size_t copied = 0;
195 1.2 riastrad
196 1.2 riastrad mutex_enter(&meta->kfm_lock);
197 1.2 riastrad const size_t head = meta->kfm_head;
198 1.2 riastrad const size_t tail = meta->kfm_tail;
199 1.2 riastrad const size_t nbytes = meta->kfm_nbytes;
200 1.2 riastrad if (tail <= head) {
201 1.2 riastrad if (size <= head - tail) {
202 1.2 riastrad memcpy(dst + tail, src, size);
203 1.2 riastrad meta->kfm_tail = tail + size;
204 1.2 riastrad copied = size;
205 1.2 riastrad }
206 1.2 riastrad } else {
207 1.2 riastrad if (size <= nbytes - tail) {
208 1.2 riastrad memcpy(dst + tail, src, size);
209 1.2 riastrad meta->kfm_tail = tail + size;
210 1.2 riastrad } else if (size <= nbytes + tail - head) {
211 1.2 riastrad memcpy(dst + tail, src, nbytes - tail);
212 1.2 riastrad memcpy(dst, src + nbytes - tail,
213 1.2 riastrad size - (nbytes - tail));
214 1.2 riastrad meta->kfm_tail = size - (nbytes - tail);
215 1.2 riastrad copied = size;
216 1.2 riastrad }
217 1.2 riastrad }
218 1.2 riastrad mutex_exit(&meta->kfm_lock);
219 1.1 riastrad
220 1.2 riastrad return copied;
221 1.2 riastrad }
222 1.1 riastrad
223 1.1 riastrad #endif /* _LINUX_KFIFO_H_ */
224