git: 292b2f9c5d14 - stable/13 - buf_ring: Make buf_ring.h amenable to userspace compilation
- Go to: [ bottom of page ] [ top of archives ] [ this month ]
Date: Mon, 02 Sep 2024 09:12:40 UTC
The branch stable/13 has been updated by andrew: URL: https://cgit.FreeBSD.org/src/commit/?id=292b2f9c5d141fbc4a9f7ef2df3e3950a467e69f commit 292b2f9c5d141fbc4a9f7ef2df3e3950a467e69f Author: Mark Johnston <markj@FreeBSD.org> AuthorDate: 2024-07-11 18:55:11 +0000 Commit: Andrew Turner <andrew@FreeBSD.org> CommitDate: 2024-09-02 09:11:57 +0000 buf_ring: Make buf_ring.h amenable to userspace compilation This will be useful for adding test cases. Reviewed by: andrew MFC after: 1 week Differential Revision: https://reviews.freebsd.org/D45869 (cherry picked from commit a161269b2451140a45de2d0cae0dad84f9e7b439) --- sys/sys/buf_ring.h | 37 ++++++++++++++++++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) diff --git a/sys/sys/buf_ring.h b/sys/sys/buf_ring.h index 21e221220331..0645f97aa77c 100644 --- a/sys/sys/buf_ring.h +++ b/sys/sys/buf_ring.h @@ -30,11 +30,18 @@ #ifndef _SYS_BUF_RING_H_ #define _SYS_BUF_RING_H_ +#include <sys/param.h> +#include <sys/kassert.h> +#include <machine/atomic.h> #include <machine/cpu.h> #ifdef DEBUG_BUFRING +#ifdef _KERNEL #include <sys/lock.h> #include <sys/mutex.h> +#else +#error "DEBUG_BUFRING is only supported in kernel" +#endif #endif struct buf_ring { @@ -361,8 +368,36 @@ buf_ring_count(struct buf_ring *br) & br->br_prod_mask); } +#ifdef _KERNEL struct buf_ring *buf_ring_alloc(int count, struct malloc_type *type, int flags, struct mtx *); void buf_ring_free(struct buf_ring *br, struct malloc_type *type); +#else -#endif +#include <stdlib.h> + +static inline struct buf_ring * +buf_ring_alloc(int count) +{ + struct buf_ring *br; + + KASSERT(powerof2(count), ("buf ring must be size power of 2")); + + br = calloc(1, sizeof(struct buf_ring) + count * sizeof(void *)); + if (br == NULL) + return (NULL); + br->br_prod_size = br->br_cons_size = count; + br->br_prod_mask = br->br_cons_mask = count - 1; + br->br_prod_head = br->br_cons_head = 0; + br->br_prod_tail = br->br_cons_tail = 0; + return (br); +} + +static inline void +buf_ring_free(struct buf_ring *br) +{ + free(br); +} + +#endif /* !_KERNEL */ +#endif /* _SYS_BUF_RING_H_ */