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