Blame complib/cl_ptr_vector.c

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