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