#ifndef _LINUX_KFIFO_H_
#define _LINUX_KFIFO_H_
#include <sys/types.h>
#include <sys/errno.h>
#include <sys/lock.h>
#include <linux/gfp.h>
#include <linux/slab.h>
struct kfifo_meta {
struct lock kfm_lock;
size_t kfm_head;
size_t kfm_tail;
size_t kfm_nbytes;
};
#define _KFIFO_PTR_TYPE(TAG, TYPE) \
struct TAG { \
struct kfifo_meta kf_meta; \
TYPE *kf_buf; \
}
#define DECLARE_KFIFO_PTR(FIFO, TYPE) _KFIFO_PTR_TYPE(, TYPE) FIFO
_KFIFO_PTR_TYPE(kfifo, void);
#define kfifo_alloc(FIFO, SIZE, GFP) \
_kfifo_alloc(&(FIFO)->kf_meta, &(FIFO)->kf_buf, (SIZE), (GFP))
static inline int
_kfifo_alloc(struct kfifo_meta *meta, void *bufp, size_t nbytes, gfp_t gfp)
{
void *buf;
buf = kmalloc(nbytes, M_DRM, gfp);
if (buf == NULL)
return -ENOMEM;
memcpy(bufp, &buf, sizeof(void *));
lockinit(&meta->kfm_lock, "lkfl", 0, LK_CANRECURSE);
meta->kfm_head = 0;
meta->kfm_tail = 0;
meta->kfm_nbytes = nbytes;
return 0;
}
#define kfifo_free(FIFO) \
_kfifo_free(&(FIFO)->kf_meta, &(FIFO)->kf_buf)
static inline void
_kfifo_free(struct kfifo_meta *meta, void *bufp)
{
void *buf;
mutex_destroy(&meta->kfm_lock);
memcpy(&buf, bufp, sizeof(void *));
kfree(buf);
buf = NULL;
memcpy(bufp, &buf, sizeof(void *));
}
#define kfifo_is_empty(FIFO) (kfifo_len(FIFO) == 0)
#define kfifo_len(FIFO) _kfifo_len(&(FIFO)->kf_meta)
static inline size_t
_kfifo_len(struct kfifo_meta *meta)
{
const size_t head = meta->kfm_head;
const size_t tail = meta->kfm_tail;
const size_t nbytes = meta->kfm_nbytes;
return (head <= tail ? tail - head : nbytes + tail - head);
}
#define kfifo_out_peek(FIFO, PTR, SIZE) \
_kfifo_out_peek(&(FIFO)->kf_meta, (FIFO)->kf_buf, (PTR), (SIZE))
static inline size_t
_kfifo_out_peek(struct kfifo_meta *meta, void *buf, void *ptr, size_t size)
{
const char *src = buf;
char *dst = ptr;
size_t copied = 0;
lockmgr(&meta->kfm_lock, LK_EXCLUSIVE);
const size_t head = meta->kfm_head;
const size_t tail = meta->kfm_tail;
const size_t nbytes = meta->kfm_nbytes;
if (head <= tail) {
if (size <= tail - head) {
memcpy(dst, src + head, size);
copied = size;
}
} else {
if (size <= nbytes - head) {
memcpy(dst, src + head, size);
copied = size;
} else if (size <= nbytes + tail - head) {
memcpy(dst, src + head, nbytes - head);
memcpy(dst + nbytes - head, src,
size - (nbytes - head));
copied = size;
}
}
lockmgr(&meta->kfm_lock, LK_RELEASE);
return copied;
}
#define kfifo_out(FIFO, PTR, SIZE) \
_kfifo_out(&(FIFO)->kf_meta, (FIFO)->kf_buf, (PTR), (SIZE))
static inline size_t
_kfifo_out(struct kfifo_meta *meta, const void *buf, void *ptr, size_t size)
{
const char *src = buf;
char *dst = ptr;
size_t copied = 0;
lockmgr(&meta->kfm_lock, LK_EXCLUSIVE);
const size_t head = meta->kfm_head;
const size_t tail = meta->kfm_tail;
const size_t nbytes = meta->kfm_nbytes;
if (head <= tail) {
if (size <= tail - head) {
memcpy(dst, src + head, size);
meta->kfm_head = head + size;
copied = size;
}
} else {
if (size <= nbytes - head) {
memcpy(dst, src + head, size);
meta->kfm_head = head + size;
copied = size;
} else if (size <= nbytes + tail - head) {
memcpy(dst, src + head, nbytes - head);
memcpy(dst + nbytes - head, src,
size - (nbytes - head));
meta->kfm_head = size - (nbytes - head);
copied = size;
}
}
lockmgr(&meta->kfm_lock, LK_RELEASE);
return copied;
}
#define kfifo_in(FIFO, PTR, SIZE) \
_kfifo_in(&(FIFO)->kf_meta, (FIFO)->kf_buf, (PTR), (SIZE))
static inline size_t
_kfifo_in(struct kfifo_meta *meta, void *buf, const void *ptr, size_t size)
{
const char *src = ptr;
char *dst = buf;
size_t copied = 0;
lockmgr(&meta->kfm_lock, LK_EXCLUSIVE);
const size_t head = meta->kfm_head;
const size_t tail = meta->kfm_tail;
const size_t nbytes = meta->kfm_nbytes;
if (tail <= head) {
if (size <= head - tail) {
memcpy(dst + tail, src, size);
meta->kfm_tail = tail + size;
copied = size;
}
} else {
if (size <= nbytes - tail) {
memcpy(dst + tail, src, size);
meta->kfm_tail = tail + size;
} else if (size <= nbytes + tail - head) {
memcpy(dst + tail, src, nbytes - tail);
memcpy(dst, src + nbytes - tail,
size - (nbytes - tail));
meta->kfm_tail = size - (nbytes - tail);
copied = size;
}
}
lockmgr(&meta->kfm_lock, LK_RELEASE);
return copied;
}
struct __kfifo {
unsigned int in;
unsigned int out;
unsigned int mask;
unsigned int esize;
void *data;
};
#define __STRUCT_KFIFO_COMMON(datatype, recsize, ptrtype) \
union { \
struct __kfifo kfifo; \
datatype *type; \
const datatype *const_type; \
char (*rectype)[recsize]; \
ptrtype *ptr; \
ptrtype const *ptr_const; \
}
#define __STRUCT_KFIFO(type, size, recsize, ptrtype) \
{ \
__STRUCT_KFIFO_COMMON(type, recsize, ptrtype); \
type buf[((size < 2) || (size & (size - 1))) ? -1 : size]; \
}
#define _KFIFO_TYPE(TYPE, SIZE) \
struct __STRUCT_KFIFO(TYPE, SIZE, 0, TYPE)
#define DECLARE_KFIFO(FIFO, TYPE, SIZE) _KFIFO_TYPE(TYPE, SIZE) FIFO
#endif