xref: /haiku/src/libs/compat/freebsd_network/fbsd_subr_bufring.c (revision dba28784c21beab5d397068303881fe024a76859)
1*dba28784SAugustin Cavalier /*-
2*dba28784SAugustin Cavalier  * Copyright (c) 2007, 2008 Kip Macy <kmacy@freebsd.org>
3*dba28784SAugustin Cavalier  * All rights reserved.
4*dba28784SAugustin Cavalier  *
5*dba28784SAugustin Cavalier  * Redistribution and use in source and binary forms, with or without
6*dba28784SAugustin Cavalier  * modification, are permitted provided that the following conditions
7*dba28784SAugustin Cavalier  * are met:
8*dba28784SAugustin Cavalier  * 1. Redistributions of source code must retain the above copyright
9*dba28784SAugustin Cavalier  *    notice, this list of conditions and the following disclaimer.
10*dba28784SAugustin Cavalier  * 2. Redistributions in binary form must reproduce the above copyright
11*dba28784SAugustin Cavalier  *    notice, this list of conditions and the following disclaimer in the
12*dba28784SAugustin Cavalier  *    documentation and/or other materials provided with the distribution.
13*dba28784SAugustin Cavalier  *
14*dba28784SAugustin Cavalier  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15*dba28784SAugustin Cavalier  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16*dba28784SAugustin Cavalier  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17*dba28784SAugustin Cavalier  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18*dba28784SAugustin Cavalier  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19*dba28784SAugustin Cavalier  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20*dba28784SAugustin Cavalier  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21*dba28784SAugustin Cavalier  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22*dba28784SAugustin Cavalier  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23*dba28784SAugustin Cavalier  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24*dba28784SAugustin Cavalier  * SUCH DAMAGE.
25*dba28784SAugustin Cavalier  */
26*dba28784SAugustin Cavalier 
27*dba28784SAugustin Cavalier #include <sys/cdefs.h>
28*dba28784SAugustin Cavalier __FBSDID("$FreeBSD$");
29*dba28784SAugustin Cavalier 
30*dba28784SAugustin Cavalier 
31*dba28784SAugustin Cavalier #define FBSD_DRIVER
32*dba28784SAugustin Cavalier #include <sys/param.h>
33*dba28784SAugustin Cavalier #include <sys/systm.h>
34*dba28784SAugustin Cavalier #include <sys/kernel.h>
35*dba28784SAugustin Cavalier #include <sys/malloc.h>
36*dba28784SAugustin Cavalier #include <sys/ktr.h>
37*dba28784SAugustin Cavalier #include <sys/buf_ring.h>
38*dba28784SAugustin Cavalier 
39*dba28784SAugustin Cavalier 
40*dba28784SAugustin Cavalier struct buf_ring *
buf_ring_alloc(int count,struct malloc_type * type,int flags,struct mtx * lock)41*dba28784SAugustin Cavalier buf_ring_alloc(int count, struct malloc_type *type, int flags, struct mtx *lock)
42*dba28784SAugustin Cavalier {
43*dba28784SAugustin Cavalier 	struct buf_ring *br;
44*dba28784SAugustin Cavalier 
45*dba28784SAugustin Cavalier 	KASSERT(powerof2(count), ("buf ring must be size power of 2"));
46*dba28784SAugustin Cavalier 
47*dba28784SAugustin Cavalier 	br = malloc(sizeof(struct buf_ring) + count*sizeof(caddr_t),
48*dba28784SAugustin Cavalier 	    type, flags|M_ZERO);
49*dba28784SAugustin Cavalier 	if (br == NULL)
50*dba28784SAugustin Cavalier 		return (NULL);
51*dba28784SAugustin Cavalier #ifdef DEBUG_BUFRING
52*dba28784SAugustin Cavalier 	br->br_lock = lock;
53*dba28784SAugustin Cavalier #endif
54*dba28784SAugustin Cavalier 	br->br_prod_size = br->br_cons_size = count;
55*dba28784SAugustin Cavalier 	br->br_prod_mask = br->br_cons_mask = count-1;
56*dba28784SAugustin Cavalier 	br->br_prod_head = br->br_cons_head = 0;
57*dba28784SAugustin Cavalier 	br->br_prod_tail = br->br_cons_tail = 0;
58*dba28784SAugustin Cavalier 
59*dba28784SAugustin Cavalier 	return (br);
60*dba28784SAugustin Cavalier }
61*dba28784SAugustin Cavalier 
62*dba28784SAugustin Cavalier void
buf_ring_free(struct buf_ring * br,struct malloc_type * type)63*dba28784SAugustin Cavalier buf_ring_free(struct buf_ring *br, struct malloc_type *type)
64*dba28784SAugustin Cavalier {
65*dba28784SAugustin Cavalier 	free(br, type);
66*dba28784SAugustin Cavalier }
67