9e39c5ba00a55fa05777cc94b148296af305e135Bill Taylor * CDDL HEADER START
9e39c5ba00a55fa05777cc94b148296af305e135Bill Taylor * The contents of this file are subject to the terms of the
9e39c5ba00a55fa05777cc94b148296af305e135Bill Taylor * Common Development and Distribution License (the "License").
9e39c5ba00a55fa05777cc94b148296af305e135Bill Taylor * You may not use this file except in compliance with the License.
9e39c5ba00a55fa05777cc94b148296af305e135Bill Taylor * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9e39c5ba00a55fa05777cc94b148296af305e135Bill Taylor * See the License for the specific language governing permissions
9e39c5ba00a55fa05777cc94b148296af305e135Bill Taylor * and limitations under the License.
9e39c5ba00a55fa05777cc94b148296af305e135Bill Taylor * When distributing Covered Code, include this CDDL HEADER in each
9e39c5ba00a55fa05777cc94b148296af305e135Bill Taylor * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
9e39c5ba00a55fa05777cc94b148296af305e135Bill Taylor * If applicable, add the following below this CDDL HEADER, with the
9e39c5ba00a55fa05777cc94b148296af305e135Bill Taylor * fields enclosed by brackets "[]" replaced with your own identifying
9e39c5ba00a55fa05777cc94b148296af305e135Bill Taylor * information: Portions Copyright [yyyy] [name of copyright owner]
9e39c5ba00a55fa05777cc94b148296af305e135Bill Taylor * CDDL HEADER END
9e39c5ba00a55fa05777cc94b148296af305e135Bill Taylor * Copyright (c) 2002-2003, Network Appliance, Inc. All rights reserved.
9e39c5ba00a55fa05777cc94b148296af305e135Bill Taylor * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
9e39c5ba00a55fa05777cc94b148296af305e135Bill Taylor * Use is subject to license terms.
9e39c5ba00a55fa05777cc94b148296af305e135Bill Taylor * PURPOSE: Ring buffer management
9e39c5ba00a55fa05777cc94b148296af305e135Bill Taylor * Description: Support and management functions for ring buffers
9e39c5ba00a55fa05777cc94b148296af305e135Bill Taylor * $Id: dapl_ring_buffer_util.c,v 1.9 2003/07/08 14:23:35 sjs2 Exp $
9e39c5ba00a55fa05777cc94b148296af305e135Bill Taylor * dapls_rbuf_alloc
9e39c5ba00a55fa05777cc94b148296af305e135Bill Taylor * Given a DAPL_RING_BUFFER, initialize it and provide memory for
9e39c5ba00a55fa05777cc94b148296af305e135Bill Taylor * the ringbuf itself. A passed in size will be adjusted to the next
9e39c5ba00a55fa05777cc94b148296af305e135Bill Taylor * largest power of two number to simplify management.
9e39c5ba00a55fa05777cc94b148296af305e135Bill Taylor * rbuf pointer to DAPL_RING_BUFFER
9e39c5ba00a55fa05777cc94b148296af305e135Bill Taylor * size number of elements to allocate & manage
9e39c5ba00a55fa05777cc94b148296af305e135Bill Taylor * DAT_SUCCESS
9e39c5ba00a55fa05777cc94b148296af305e135Bill Taylor * DAT_INSUFFICIENT_RESOURCES
9e39c5ba00a55fa05777cc94b148296af305e135Bill Taylor * The circular buffer must be allocated one too large.
9e39c5ba00a55fa05777cc94b148296af305e135Bill Taylor * This eliminates any need for a distinct counter, as that
9e39c5ba00a55fa05777cc94b148296af305e135Bill Taylor * having the two pointers equal always means "empty" -- never "full"
9e39c5ba00a55fa05777cc94b148296af305e135Bill Taylor /* Put size on a power of 2 boundary */
9e39c5ba00a55fa05777cc94b148296af305e135Bill Taylor rbuf->base = (void *) dapl_os_alloc(rsize * sizeof (void *));
9e39c5ba00a55fa05777cc94b148296af305e135Bill Taylor return (DAT_INSUFFICIENT_RESOURCES | DAT_RESOURCE_MEMORY);
9e39c5ba00a55fa05777cc94b148296af305e135Bill Taylor * dapls_rbuf_realloc
9e39c5ba00a55fa05777cc94b148296af305e135Bill Taylor * Resizes an empty DAPL_RING_BUFFER. This function is not thread safe;
9e39c5ba00a55fa05777cc94b148296af305e135Bill Taylor * adding or removing elements from a ring buffer while resizing
9e39c5ba00a55fa05777cc94b148296af305e135Bill Taylor * will have indeterminate results.
9e39c5ba00a55fa05777cc94b148296af305e135Bill Taylor * rbuf pointer to DAPL_RING_BUFFER
9e39c5ba00a55fa05777cc94b148296af305e135Bill Taylor * size number of elements to allocate & manage
9e39c5ba00a55fa05777cc94b148296af305e135Bill Taylor * DAT_SUCCESS
9e39c5ba00a55fa05777cc94b148296af305e135Bill Taylor * DAT_INVALID_STATE
9e39c5ba00a55fa05777cc94b148296af305e135Bill Taylor * DAT_INSUFFICIENT_RESOURCES
9e39c5ba00a55fa05777cc94b148296af305e135Bill Taylor /* if the ring buffer is not empty */
9e39c5ba00a55fa05777cc94b148296af305e135Bill Taylor /* Put size on a power of 2 boundary */
9e39c5ba00a55fa05777cc94b148296af305e135Bill Taylor rbuf->base = (void *)dapl_os_realloc(rbuf->base,
9e39c5ba00a55fa05777cc94b148296af305e135Bill Taylor rsize * sizeof (void *));
9e39c5ba00a55fa05777cc94b148296af305e135Bill Taylor dat_status = DAT_ERROR(DAT_INSUFFICIENT_RESOURCES,
9e39c5ba00a55fa05777cc94b148296af305e135Bill Taylor * dapls_rbuf_destroy
9e39c5ba00a55fa05777cc94b148296af305e135Bill Taylor * Release the buffer and reset pointers to a DAPL_RING_BUFFER
9e39c5ba00a55fa05777cc94b148296af305e135Bill Taylor * rbuf pointer to DAPL_RING_BUFFER
9e39c5ba00a55fa05777cc94b148296af305e135Bill Taylor dapl_os_free(rbuf->base, (rbuf->lim + 1) * sizeof (void *));
9e39c5ba00a55fa05777cc94b148296af305e135Bill Taylor * dapls_rbuf_add
9e39c5ba00a55fa05777cc94b148296af305e135Bill Taylor * Add an entry to the ring buffer
9e39c5ba00a55fa05777cc94b148296af305e135Bill Taylor * rbuf pointer to DAPL_RING_BUFFER
9e39c5ba00a55fa05777cc94b148296af305e135Bill Taylor * entry entry to add
9e39c5ba00a55fa05777cc94b148296af305e135Bill Taylor * DAT_SUCCESS
9e39c5ba00a55fa05777cc94b148296af305e135Bill Taylor * DAT_INSUFFICIENT_RESOURCES (queue full)
9e39c5ba00a55fa05777cc94b148296af305e135Bill Taylor return (DAT_ERROR(DAT_INSUFFICIENT_RESOURCES, DAT_RESOURCE_MEMORY));
9e39c5ba00a55fa05777cc94b148296af305e135Bill Taylor * dapls_rbuf_remove
9e39c5ba00a55fa05777cc94b148296af305e135Bill Taylor * Remove an entry from the ring buffer
9e39c5ba00a55fa05777cc94b148296af305e135Bill Taylor * rbuf pointer to DAPL_RING_BUFFER
9e39c5ba00a55fa05777cc94b148296af305e135Bill Taylor * entry entry removed from the ring buffer
9e39c5ba00a55fa05777cc94b148296af305e135Bill Taylor * a pointer to a buffer entry
9e39c5ba00a55fa05777cc94b148296af305e135Bill Taylor * dapli_rbuf_count
9e39c5ba00a55fa05777cc94b148296af305e135Bill Taylor * Return the number of entries in use in the ring buffer
9e39c5ba00a55fa05777cc94b148296af305e135Bill Taylor * rbuf pointer to DAPL_RING_BUFFER
9e39c5ba00a55fa05777cc94b148296af305e135Bill Taylor * count of entries
9e39c5ba00a55fa05777cc94b148296af305e135Bill Taylor /* add 1 to lim as it is a mask, number of entries - 1 */