1 /* $NetBSD: kfifo.h,v 1.3 2018/08/27 14:41:53 riastradh Exp $ */
4 * Copyright (c) 2018 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Taylor R. Campbell.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
32 #ifndef _LINUX_KFIFO_H_
33 #define _LINUX_KFIFO_H_
35 #include <sys/types.h>
36 #include <sys/errno.h>
37 #include <sys/mutex.h>
39 #include <linux/gfp.h>
40 #include <linux/slab.h>
49 #define _KFIFO_PTR_TYPE(TAG, TYPE) \
51 struct kfifo_meta kf_meta; \
55 #define DECLARE_KFIFO_PTR(FIFO, TYPE) _KFIFO_PTR_TYPE(, TYPE) FIFO
57 _KFIFO_PTR_TYPE(kfifo, void);
59 #define kfifo_alloc(FIFO, SIZE, GFP) \
60 _kfifo_alloc(&(FIFO)->kf_meta, &(FIFO)->kf_buf, (SIZE), (GFP))
63 _kfifo_alloc(struct kfifo_meta *meta, void *bufp, size_t nbytes, gfp_t gfp)
67 buf = kmalloc(nbytes, gfp);
71 /* Type pun! Hope void * == struct whatever *. */
72 memcpy(bufp, &buf, sizeof(void *));
74 mutex_init(&meta->kfm_lock, MUTEX_DEFAULT, IPL_VM);
77 meta->kfm_nbytes = nbytes;
82 #define kfifo_free(FIFO) \
83 _kfifo_free(&(FIFO)->kf_meta, &(FIFO)->kf_buf)
86 _kfifo_free(struct kfifo_meta *meta, void *bufp)
90 mutex_destroy(&meta->kfm_lock);
92 memcpy(&buf, bufp, sizeof(void *));
97 memcpy(bufp, &buf, sizeof(void *));
100 #define kfifo_is_empty(FIFO) (kfifo_len(FIFO) == 0)
101 #define kfifo_len(FIFO) _kfifo_len(&(FIFO)->kf_meta)
104 _kfifo_len(struct kfifo_meta *meta)
106 const size_t head = meta->kfm_head;
107 const size_t tail = meta->kfm_tail;
108 const size_t nbytes = meta->kfm_nbytes;
110 return (head <= tail ? tail - head : nbytes + tail - head);
113 #define kfifo_out_peek(FIFO, PTR, SIZE) \
114 _kfifo_out_peek(&(FIFO)->kf_meta, (FIFO)->kf_buf, (PTR), (SIZE))
117 _kfifo_out_peek(struct kfifo_meta *meta, void *buf, void *ptr, size_t size)
119 const char *src = buf;
123 mutex_spin_enter(&meta->kfm_lock);
124 const size_t head = meta->kfm_head;
125 const size_t tail = meta->kfm_tail;
126 const size_t nbytes = meta->kfm_nbytes;
128 if (size <= tail - head) {
129 memcpy(dst, src + head, size);
133 if (size <= nbytes - head) {
134 memcpy(dst, src + head, size);
136 } else if (size <= nbytes + tail - head) {
137 memcpy(dst, src + head, nbytes - head);
138 memcpy(dst + nbytes - head, src,
139 size - (nbytes - head));
143 mutex_spin_exit(&meta->kfm_lock);
148 #define kfifo_out(FIFO, PTR, SIZE) \
149 _kfifo_out(&(FIFO)->kf_meta, (FIFO)->kf_buf, (PTR), (SIZE))
152 _kfifo_out(struct kfifo_meta *meta, const void *buf, void *ptr, size_t size)
154 const char *src = buf;
158 mutex_spin_enter(&meta->kfm_lock);
159 const size_t head = meta->kfm_head;
160 const size_t tail = meta->kfm_tail;
161 const size_t nbytes = meta->kfm_nbytes;
163 if (size <= tail - head) {
164 memcpy(dst, src + head, size);
165 meta->kfm_head = head + size;
169 if (size <= nbytes - head) {
170 memcpy(dst, src + head, size);
171 meta->kfm_head = head + size;
173 } else if (size <= nbytes + tail - head) {
174 memcpy(dst, src + head, nbytes - head);
175 memcpy(dst + nbytes - head, src,
176 size - (nbytes - head));
177 meta->kfm_head = size - (nbytes - head);
181 mutex_spin_exit(&meta->kfm_lock);
186 #define kfifo_in(FIFO, PTR, SIZE) \
187 _kfifo_in(&(FIFO)->kf_meta, (FIFO)->kf_buf, (PTR), (SIZE))
190 _kfifo_in(struct kfifo_meta *meta, void *buf, const void *ptr, size_t size)
192 const char *src = ptr;
196 mutex_spin_enter(&meta->kfm_lock);
197 const size_t head = meta->kfm_head;
198 const size_t tail = meta->kfm_tail;
199 const size_t nbytes = meta->kfm_nbytes;
201 if (size <= head - tail) {
202 memcpy(dst + tail, src, size);
203 meta->kfm_tail = tail + size;
207 if (size <= nbytes - tail) {
208 memcpy(dst + tail, src, size);
209 meta->kfm_tail = tail + size;
210 } else if (size <= nbytes + tail - head) {
211 memcpy(dst + tail, src, nbytes - tail);
212 memcpy(dst, src + nbytes - tail,
213 size - (nbytes - tail));
214 meta->kfm_tail = size - (nbytes - tail);
218 mutex_spin_exit(&meta->kfm_lock);
223 #endif /* _LINUX_KFIFO_H_ */