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