Blame complib/cl_ptr_vector.c

Packit Service 54dbc3
/*
Packit Service 54dbc3
 * Copyright (c) 2004-2006 Voltaire, Inc. All rights reserved.
Packit Service 54dbc3
 * Copyright (c) 2002-2005 Mellanox Technologies LTD. All rights reserved.
Packit Service 54dbc3
 * Copyright (c) 1996-2003 Intel Corporation. All rights reserved.
Packit Service 54dbc3
 *
Packit Service 54dbc3
 * This software is available to you under a choice of one of two
Packit Service 54dbc3
 * licenses.  You may choose to be licensed under the terms of the GNU
Packit Service 54dbc3
 * General Public License (GPL) Version 2, available from the file
Packit Service 54dbc3
 * COPYING in the main directory of this source tree, or the
Packit Service 54dbc3
 * OpenIB.org BSD license below:
Packit Service 54dbc3
 *
Packit Service 54dbc3
 *     Redistribution and use in source and binary forms, with or
Packit Service 54dbc3
 *     without modification, are permitted provided that the following
Packit Service 54dbc3
 *     conditions are met:
Packit Service 54dbc3
 *
Packit Service 54dbc3
 *      - Redistributions of source code must retain the above
Packit Service 54dbc3
 *        copyright notice, this list of conditions and the following
Packit Service 54dbc3
 *        disclaimer.
Packit Service 54dbc3
 *
Packit Service 54dbc3
 *      - Redistributions in binary form must reproduce the above
Packit Service 54dbc3
 *        copyright notice, this list of conditions and the following
Packit Service 54dbc3
 *        disclaimer in the documentation and/or other materials
Packit Service 54dbc3
 *        provided with the distribution.
Packit Service 54dbc3
 *
Packit Service 54dbc3
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
Packit Service 54dbc3
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
Packit Service 54dbc3
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
Packit Service 54dbc3
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
Packit Service 54dbc3
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
Packit Service 54dbc3
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
Packit Service 54dbc3
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
Packit Service 54dbc3
 * SOFTWARE.
Packit Service 54dbc3
 *
Packit Service 54dbc3
 */
Packit Service 54dbc3
Packit Service 54dbc3
/*
Packit Service 54dbc3
 * Abstract:
Packit Service 54dbc3
 *	This file contains ivector and isvector implementations.
Packit Service 54dbc3
 *
Packit Service 54dbc3
 */
Packit Service 54dbc3
Packit Service 54dbc3
#if HAVE_CONFIG_H
Packit Service 54dbc3
#  include <config.h>
Packit Service 54dbc3
#endif				/* HAVE_CONFIG_H */
Packit Service 54dbc3
Packit Service 54dbc3
#include <stdlib.h>
Packit Service 54dbc3
#include <string.h>
Packit Service 54dbc3
#include <complib/cl_ptr_vector.h>
Packit Service 54dbc3
Packit Service 54dbc3
void cl_ptr_vector_construct(IN cl_ptr_vector_t * const p_vector)
Packit Service 54dbc3
{
Packit Service 54dbc3
	CL_ASSERT(p_vector);
Packit Service 54dbc3
Packit Service 54dbc3
	memset(p_vector, 0, sizeof(cl_ptr_vector_t));
Packit Service 54dbc3
Packit Service 54dbc3
	p_vector->state = CL_UNINITIALIZED;
Packit Service 54dbc3
}
Packit Service 54dbc3
Packit Service 54dbc3
cl_status_t cl_ptr_vector_init(IN cl_ptr_vector_t * const p_vector,
Packit Service 54dbc3
			       IN const size_t min_size,
Packit Service 54dbc3
			       IN const size_t grow_size)
Packit Service 54dbc3
{
Packit Service 54dbc3
	cl_status_t status = CL_SUCCESS;
Packit Service 54dbc3
Packit Service 54dbc3
	CL_ASSERT(p_vector);
Packit Service 54dbc3
Packit Service 54dbc3
	cl_ptr_vector_construct(p_vector);
Packit Service 54dbc3
Packit Service 54dbc3
	p_vector->grow_size = grow_size;
Packit Service 54dbc3
Packit Service 54dbc3
	/*
Packit Service 54dbc3
	 * Set the state to initialized so that the call to set_size
Packit Service 54dbc3
	 * doesn't assert.
Packit Service 54dbc3
	 */
Packit Service 54dbc3
	p_vector->state = CL_INITIALIZED;
Packit Service 54dbc3
Packit Service 54dbc3
	/* get the storage needed by the user */
Packit Service 54dbc3
	if (min_size) {
Packit Service 54dbc3
		status = cl_ptr_vector_set_size(p_vector, min_size);
Packit Service 54dbc3
		if (status != CL_SUCCESS)
Packit Service 54dbc3
			cl_ptr_vector_destroy(p_vector);
Packit Service 54dbc3
	}
Packit Service 54dbc3
Packit Service 54dbc3
	return (status);
Packit Service 54dbc3
}
Packit Service 54dbc3
Packit Service 54dbc3
void cl_ptr_vector_destroy(IN cl_ptr_vector_t * const p_vector)
Packit Service 54dbc3
{
Packit Service 54dbc3
	CL_ASSERT(p_vector);
Packit Service 54dbc3
	CL_ASSERT(cl_is_state_valid(p_vector->state));
Packit Service 54dbc3
Packit Service 54dbc3
	/* Call the user's destructor for each element in the array. */
Packit Service 54dbc3
	if (p_vector->state == CL_INITIALIZED) {
Packit Service 54dbc3
		/* Destroy the page vector. */
Packit Service 54dbc3
		if (p_vector->p_ptr_array) {
Packit Service 54dbc3
			free((void *)p_vector->p_ptr_array);
Packit Service 54dbc3
			p_vector->p_ptr_array = NULL;
Packit Service 54dbc3
		}
Packit Service 54dbc3
	}
Packit Service 54dbc3
Packit Service 54dbc3
	p_vector->state = CL_UNINITIALIZED;
Packit Service 54dbc3
}
Packit Service 54dbc3
Packit Service 54dbc3
cl_status_t cl_ptr_vector_at(IN const cl_ptr_vector_t * const p_vector,
Packit Service 54dbc3
			     IN const size_t index, OUT void **const p_element)
Packit Service 54dbc3
{
Packit Service 54dbc3
	CL_ASSERT(p_vector);
Packit Service 54dbc3
	CL_ASSERT(p_vector->state == CL_INITIALIZED);
Packit Service 54dbc3
Packit Service 54dbc3
	/* Range check */
Packit Service 54dbc3
	if (index >= p_vector->size)
Packit Service 54dbc3
		return (CL_INVALID_PARAMETER);
Packit Service 54dbc3
Packit Service 54dbc3
	*p_element = cl_ptr_vector_get(p_vector, index);
Packit Service 54dbc3
	return (CL_SUCCESS);
Packit Service 54dbc3
}
Packit Service 54dbc3
Packit Service 54dbc3
cl_status_t cl_ptr_vector_set(IN cl_ptr_vector_t * const p_vector,
Packit Service 54dbc3
			      IN const size_t index,
Packit Service 54dbc3
			      IN const void *const element)
Packit Service 54dbc3
{
Packit Service 54dbc3
	cl_status_t status;
Packit Service 54dbc3
Packit Service 54dbc3
	CL_ASSERT(p_vector);
Packit Service 54dbc3
	CL_ASSERT(p_vector->state == CL_INITIALIZED);
Packit Service 54dbc3
Packit Service 54dbc3
	/* Determine if the vector has room for this element. */
Packit Service 54dbc3
	if (index >= p_vector->size) {
Packit Service 54dbc3
		/* Resize to accomodate the given index. */
Packit Service 54dbc3
		status = cl_ptr_vector_set_size(p_vector, index + 1);
Packit Service 54dbc3
Packit Service 54dbc3
		/* Check for failure on or before the given index. */
Packit Service 54dbc3
		if ((status != CL_SUCCESS) && (p_vector->size < index))
Packit Service 54dbc3
			return (status);
Packit Service 54dbc3
	}
Packit Service 54dbc3
Packit Service 54dbc3
	/* At this point, the array is guaranteed to be big enough */
Packit Service 54dbc3
	p_vector->p_ptr_array[index] = element;
Packit Service 54dbc3
Packit Service 54dbc3
	return (CL_SUCCESS);
Packit Service 54dbc3
}
Packit Service 54dbc3
Packit Service 54dbc3
void *cl_ptr_vector_remove(IN cl_ptr_vector_t * const p_vector,
Packit Service 54dbc3
			   IN const size_t index)
Packit Service 54dbc3
{
Packit Service 54dbc3
	size_t src;
Packit Service 54dbc3
	const void *element;
Packit Service 54dbc3
Packit Service 54dbc3
	CL_ASSERT(p_vector);
Packit Service 54dbc3
	CL_ASSERT(p_vector->state == CL_INITIALIZED);
Packit Service 54dbc3
	CL_ASSERT(p_vector->size > index);
Packit Service 54dbc3
Packit Service 54dbc3
	/* Store a copy of the element to return. */
Packit Service 54dbc3
	element = p_vector->p_ptr_array[index];
Packit Service 54dbc3
	/* Shift all items above the removed item down. */
Packit Service 54dbc3
	if (index < --p_vector->size) {
Packit Service 54dbc3
		for (src = index; src < p_vector->size; src++)
Packit Service 54dbc3
			p_vector->p_ptr_array[src] =
Packit Service 54dbc3
			    p_vector->p_ptr_array[src + 1];
Packit Service 54dbc3
	}
Packit Service 54dbc3
	/* Clear the entry for the element just outside of the new upper bound. */
Packit Service 54dbc3
	p_vector->p_ptr_array[p_vector->size] = NULL;
Packit Service 54dbc3
Packit Service 54dbc3
	return ((void *)element);
Packit Service 54dbc3
}
Packit Service 54dbc3
Packit Service 54dbc3
cl_status_t cl_ptr_vector_set_capacity(IN cl_ptr_vector_t * const p_vector,
Packit Service 54dbc3
				       IN const size_t new_capacity)
Packit Service 54dbc3
{
Packit Service 54dbc3
	void *p_new_ptr_array;
Packit Service 54dbc3
Packit Service 54dbc3
	CL_ASSERT(p_vector);
Packit Service 54dbc3
	CL_ASSERT(p_vector->state == CL_INITIALIZED);
Packit Service 54dbc3
Packit Service 54dbc3
	/* Do we have to do anything here? */
Packit Service 54dbc3
	if (new_capacity <= p_vector->capacity) {
Packit Service 54dbc3
		/* Nope */
Packit Service 54dbc3
		return (CL_SUCCESS);
Packit Service 54dbc3
	}
Packit Service 54dbc3
Packit Service 54dbc3
	/* Allocate our pointer array. */
Packit Service 54dbc3
	p_new_ptr_array = malloc(new_capacity * sizeof(void *));
Packit Service 54dbc3
	if (!p_new_ptr_array)
Packit Service 54dbc3
		return (CL_INSUFFICIENT_MEMORY);
Packit Service 54dbc3
	else
Packit Service 54dbc3
		memset(p_new_ptr_array, 0, new_capacity * sizeof(void *));
Packit Service 54dbc3
Packit Service 54dbc3
	if (p_vector->p_ptr_array) {
Packit Service 54dbc3
		/* Copy the old pointer array into the new. */
Packit Service 54dbc3
		memcpy(p_new_ptr_array, p_vector->p_ptr_array,
Packit Service 54dbc3
		       p_vector->capacity * sizeof(void *));
Packit Service 54dbc3
Packit Service 54dbc3
		/* Free the old pointer array. */
Packit Service 54dbc3
		free((void *)p_vector->p_ptr_array);
Packit Service 54dbc3
	}
Packit Service 54dbc3
Packit Service 54dbc3
	/* Set the new array. */
Packit Service 54dbc3
	p_vector->p_ptr_array = p_new_ptr_array;
Packit Service 54dbc3
Packit Service 54dbc3
	/* Update the vector with the new capactity. */
Packit Service 54dbc3
	p_vector->capacity = new_capacity;
Packit Service 54dbc3
Packit Service 54dbc3
	return (CL_SUCCESS);
Packit Service 54dbc3
}
Packit Service 54dbc3
Packit Service 54dbc3
cl_status_t cl_ptr_vector_set_size(IN cl_ptr_vector_t * const p_vector,
Packit Service 54dbc3
				   IN const size_t size)
Packit Service 54dbc3
{
Packit Service 54dbc3
	cl_status_t status;
Packit Service 54dbc3
	size_t new_capacity;
Packit Service 54dbc3
Packit Service 54dbc3
	CL_ASSERT(p_vector);
Packit Service 54dbc3
	CL_ASSERT(p_vector->state == CL_INITIALIZED);
Packit Service 54dbc3
Packit Service 54dbc3
	/* Check to see if the requested size is the same as the existing size. */
Packit Service 54dbc3
	if (size == p_vector->size)
Packit Service 54dbc3
		return (CL_SUCCESS);
Packit Service 54dbc3
Packit Service 54dbc3
	/* Determine if the vector has room for this element. */
Packit Service 54dbc3
	if (size >= p_vector->capacity) {
Packit Service 54dbc3
		if (!p_vector->grow_size)
Packit Service 54dbc3
			return (CL_INSUFFICIENT_MEMORY);
Packit Service 54dbc3
Packit Service 54dbc3
		/* Calculate the new capacity, taking into account the grow size. */
Packit Service 54dbc3
		new_capacity = size;
Packit Service 54dbc3
		if (size % p_vector->grow_size) {
Packit Service 54dbc3
			/* Round up to nearest grow_size boundary. */
Packit Service 54dbc3
			new_capacity += p_vector->grow_size -
Packit Service 54dbc3
			    (size % p_vector->grow_size);
Packit Service 54dbc3
		}
Packit Service 54dbc3
Packit Service 54dbc3
		status = cl_ptr_vector_set_capacity(p_vector, new_capacity);
Packit Service 54dbc3
		if (status != CL_SUCCESS)
Packit Service 54dbc3
			return (status);
Packit Service 54dbc3
	}
Packit Service 54dbc3
Packit Service 54dbc3
	p_vector->size = size;
Packit Service 54dbc3
	return (CL_SUCCESS);
Packit Service 54dbc3
}
Packit Service 54dbc3
Packit Service 54dbc3
cl_status_t cl_ptr_vector_set_min_size(IN cl_ptr_vector_t * const p_vector,
Packit Service 54dbc3
				       IN const size_t min_size)
Packit Service 54dbc3
{
Packit Service 54dbc3
	CL_ASSERT(p_vector);
Packit Service 54dbc3
	CL_ASSERT(p_vector->state == CL_INITIALIZED);
Packit Service 54dbc3
Packit Service 54dbc3
	if (min_size > p_vector->size) {
Packit Service 54dbc3
		/* We have to resize the array */
Packit Service 54dbc3
		return (cl_ptr_vector_set_size(p_vector, min_size));
Packit Service 54dbc3
	}
Packit Service 54dbc3
Packit Service 54dbc3
	/* We didn't have to do anything */
Packit Service 54dbc3
	return (CL_SUCCESS);
Packit Service 54dbc3
}
Packit Service 54dbc3
Packit Service 54dbc3
void cl_ptr_vector_apply_func(IN const cl_ptr_vector_t * const p_vector,
Packit Service 54dbc3
			      IN cl_pfn_ptr_vec_apply_t pfn_callback,
Packit Service 54dbc3
			      IN const void *const context)
Packit Service 54dbc3
{
Packit Service 54dbc3
	size_t i;
Packit Service 54dbc3
Packit Service 54dbc3
	CL_ASSERT(p_vector);
Packit Service 54dbc3
	CL_ASSERT(p_vector->state == CL_INITIALIZED);
Packit Service 54dbc3
	CL_ASSERT(pfn_callback);
Packit Service 54dbc3
Packit Service 54dbc3
	for (i = 0; i < p_vector->size; i++)
Packit Service 54dbc3
		pfn_callback(i, (void *)p_vector->p_ptr_array[i],
Packit Service 54dbc3
			     (void *)context);
Packit Service 54dbc3
}
Packit Service 54dbc3
Packit Service 54dbc3
size_t cl_ptr_vector_find_from_start(IN const cl_ptr_vector_t * const p_vector,
Packit Service 54dbc3
				     IN cl_pfn_ptr_vec_find_t pfn_callback,
Packit Service 54dbc3
				     IN const void *const context)
Packit Service 54dbc3
{
Packit Service 54dbc3
	size_t i;
Packit Service 54dbc3
Packit Service 54dbc3
	CL_ASSERT(p_vector);
Packit Service 54dbc3
	CL_ASSERT(p_vector->state == CL_INITIALIZED);
Packit Service 54dbc3
	CL_ASSERT(pfn_callback);
Packit Service 54dbc3
Packit Service 54dbc3
	for (i = 0; i < p_vector->size; i++) {
Packit Service 54dbc3
		/* Invoke the callback */
Packit Service 54dbc3
		if (pfn_callback(i, (void *)p_vector->p_ptr_array[i],
Packit Service 54dbc3
				 (void *)context) == CL_SUCCESS) {
Packit Service 54dbc3
			break;
Packit Service 54dbc3
		}
Packit Service 54dbc3
	}
Packit Service 54dbc3
	return (i);
Packit Service 54dbc3
}
Packit Service 54dbc3
Packit Service 54dbc3
size_t cl_ptr_vector_find_from_end(IN const cl_ptr_vector_t * const p_vector,
Packit Service 54dbc3
				   IN cl_pfn_ptr_vec_find_t pfn_callback,
Packit Service 54dbc3
				   IN const void *const context)
Packit Service 54dbc3
{
Packit Service 54dbc3
	size_t i;
Packit Service 54dbc3
Packit Service 54dbc3
	CL_ASSERT(p_vector);
Packit Service 54dbc3
	CL_ASSERT(p_vector->state == CL_INITIALIZED);
Packit Service 54dbc3
	CL_ASSERT(pfn_callback);
Packit Service 54dbc3
Packit Service 54dbc3
	i = p_vector->size;
Packit Service 54dbc3
Packit Service 54dbc3
	while (i) {
Packit Service 54dbc3
		/* Invoke the callback for the current element. */
Packit Service 54dbc3
		i--;
Packit Service 54dbc3
		if (pfn_callback(i, (void *)p_vector->p_ptr_array[i],
Packit Service 54dbc3
				 (void *)context) == CL_SUCCESS) {
Packit Service 54dbc3
			return (i);
Packit Service 54dbc3
		}
Packit Service 54dbc3
	}
Packit Service 54dbc3
Packit Service 54dbc3
	return (p_vector->size);
Packit Service 54dbc3
}