1207673Sjoel/*-
2207673Sjoel * Copyright (c) 2007, 2008 Kip Macy <kmacy@freebsd.org>
3185162Skmacy * All rights reserved.
4185162Skmacy *
5185162Skmacy * Redistribution and use in source and binary forms, with or without
6207673Sjoel * modification, are permitted provided that the following conditions
7207673Sjoel * are met:
8207673Sjoel * 1. Redistributions of source code must retain the above copyright
9207673Sjoel *    notice, this list of conditions and the following disclaimer.
10207673Sjoel * 2. Redistributions in binary form must reproduce the above copyright
11207673Sjoel *    notice, this list of conditions and the following disclaimer in the
12207673Sjoel *    documentation and/or other materials provided with the distribution.
13185162Skmacy *
14207673Sjoel * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15207673Sjoel * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16185162Skmacy * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17207673Sjoel * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18207673Sjoel * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19207673Sjoel * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20207673Sjoel * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21207673Sjoel * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22207673Sjoel * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23207673Sjoel * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24207673Sjoel * SUCH DAMAGE.
25207673Sjoel */
26207673Sjoel
27185162Skmacy#include <sys/cdefs.h>
28185162Skmacy__FBSDID("$FreeBSD$");
29185162Skmacy
30185162Skmacy
31185162Skmacy#include <sys/param.h>
32185162Skmacy#include <sys/systm.h>
33185162Skmacy#include <sys/kernel.h>
34185162Skmacy#include <sys/malloc.h>
35185162Skmacy#include <sys/ktr.h>
36185162Skmacy#include <sys/buf_ring.h>
37185162Skmacy
38185162Skmacy
39185162Skmacystruct buf_ring *
40185162Skmacybuf_ring_alloc(int count, struct malloc_type *type, int flags, struct mtx *lock)
41185162Skmacy{
42185162Skmacy	struct buf_ring *br;
43185162Skmacy
44185162Skmacy	KASSERT(powerof2(count), ("buf ring must be size power of 2"));
45185162Skmacy
46185162Skmacy	br = malloc(sizeof(struct buf_ring) + count*sizeof(caddr_t),
47185162Skmacy	    type, flags|M_ZERO);
48185162Skmacy	if (br == NULL)
49185162Skmacy		return (NULL);
50185162Skmacy#ifdef DEBUG_BUFRING
51185162Skmacy	br->br_lock = lock;
52185162Skmacy#endif
53185162Skmacy	br->br_prod_size = br->br_cons_size = count;
54185162Skmacy	br->br_prod_mask = br->br_cons_mask = count-1;
55185162Skmacy	br->br_prod_head = br->br_cons_head = 0;
56185162Skmacy	br->br_prod_tail = br->br_cons_tail = 0;
57185162Skmacy
58185162Skmacy	return (br);
59185162Skmacy}
60185162Skmacy
61185162Skmacyvoid
62185162Skmacybuf_ring_free(struct buf_ring *br, struct malloc_type *type)
63185162Skmacy{
64185162Skmacy	free(br, type);
65185162Skmacy}
66