2005-03-14 23:57:21 +03:00
|
|
|
/*
|
2005-11-05 22:57:48 +03:00
|
|
|
* Copyright (c) 2004-2005 The Trustees of Indiana University and Indiana
|
|
|
|
* University Research and Technology
|
|
|
|
* Corporation. All rights reserved.
|
|
|
|
* Copyright (c) 2004-2005 The University of Tennessee and The University
|
|
|
|
* of Tennessee Research Foundation. All rights
|
|
|
|
* reserved.
|
2005-03-14 23:57:21 +03:00
|
|
|
* Copyright (c) 2004-2005 High Performance Computing Center Stuttgart,
|
|
|
|
* University of Stuttgart. All rights reserved.
|
2005-03-24 15:43:37 +03:00
|
|
|
* Copyright (c) 2004-2005 The Regents of the University of California.
|
|
|
|
* All rights reserved.
|
2005-03-14 23:57:21 +03:00
|
|
|
* $COPYRIGHT$
|
|
|
|
*
|
|
|
|
* Additional copyrights may follow
|
|
|
|
*
|
|
|
|
* $HEADER$
|
|
|
|
*/
|
|
|
|
/** @file
|
|
|
|
*
|
2005-05-26 17:12:11 +04:00
|
|
|
* See ompi_bitmap.h for an explanation of why there is a split
|
|
|
|
* between OMPI and ORTE for this generic class.
|
2005-03-14 23:57:21 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef ORTE_POINTER_ARRAY_H
|
|
|
|
#define ORTE_POINTER_ARRAY_H
|
|
|
|
|
|
|
|
#include "orte_config.h"
|
2006-08-15 23:54:10 +04:00
|
|
|
#include "orte/orte_types.h"
|
2005-03-14 23:57:21 +03:00
|
|
|
|
2005-07-20 10:45:00 +04:00
|
|
|
#if HAVE_STRING_H
|
|
|
|
#include <string.h>
|
|
|
|
#endif /* HAVE_STRING_H */
|
|
|
|
|
2005-07-04 02:45:48 +04:00
|
|
|
#include "opal/threads/mutex.h"
|
2005-07-03 20:06:07 +04:00
|
|
|
#include "opal/class/opal_object.h"
|
2005-03-14 23:57:21 +03:00
|
|
|
|
|
|
|
#if defined(c_plusplus) || defined(__cplusplus)
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/**
|
|
|
|
* dynamic pointer array
|
|
|
|
*/
|
|
|
|
struct orte_pointer_array_t {
|
|
|
|
/** base class */
|
2005-07-03 20:06:07 +04:00
|
|
|
opal_object_t super;
|
2005-03-14 23:57:21 +03:00
|
|
|
/** synchronization object */
|
2005-07-04 02:45:48 +04:00
|
|
|
opal_mutex_t lock;
|
2005-03-14 23:57:21 +03:00
|
|
|
/** Index of lowest free element. NOTE: This is only an
|
|
|
|
optimization to know where to search for the first free slot.
|
|
|
|
It does \em not necessarily imply indices all above this index
|
|
|
|
are not taken! */
|
2006-08-15 23:54:10 +04:00
|
|
|
orte_std_cntr_t lowest_free;
|
2005-03-14 23:57:21 +03:00
|
|
|
/** number of free elements in the list */
|
2006-08-15 23:54:10 +04:00
|
|
|
orte_std_cntr_t number_free;
|
2005-03-14 23:57:21 +03:00
|
|
|
/** size of list, i.e. number of elements in addr */
|
2006-08-15 23:54:10 +04:00
|
|
|
orte_std_cntr_t size;
|
2005-03-14 23:57:21 +03:00
|
|
|
/** maximum size list is allowed to reach */
|
2006-08-15 23:54:10 +04:00
|
|
|
orte_std_cntr_t max_size;
|
2005-03-14 23:57:21 +03:00
|
|
|
/** growth steps for list */
|
2006-08-15 23:54:10 +04:00
|
|
|
orte_std_cntr_t block_size;
|
2005-03-14 23:57:21 +03:00
|
|
|
/** pointer to array of pointers */
|
|
|
|
void **addr;
|
|
|
|
};
|
|
|
|
/**
|
|
|
|
* Convenience typedef
|
|
|
|
*/
|
|
|
|
typedef struct orte_pointer_array_t orte_pointer_array_t;
|
|
|
|
/**
|
|
|
|
* Class declaration
|
|
|
|
*/
|
2006-08-20 19:54:04 +04:00
|
|
|
ORTE_DECLSPEC OBJ_CLASS_DECLARATION(orte_pointer_array_t);
|
2005-03-14 23:57:21 +03:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialize the pointer array
|
|
|
|
*
|
|
|
|
* @param array Address of the pointer array object to be initialized
|
|
|
|
* @param initial_alloc The initial number of elements to be allocated
|
|
|
|
* @param max_size Maximum size the array is allowed to reach
|
|
|
|
* @param block_size Number of array elements to be added when increase required
|
|
|
|
*
|
|
|
|
* @retval ORTE_SUCCESS Initialization successful
|
|
|
|
* @retval ORTE_ERROR(s) Appropriate error code
|
|
|
|
*
|
|
|
|
*/
|
2006-08-20 19:54:04 +04:00
|
|
|
ORTE_DECLSPEC int orte_pointer_array_init(orte_pointer_array_t **array,
|
2006-08-15 23:54:10 +04:00
|
|
|
orte_std_cntr_t initial_allocation,
|
|
|
|
orte_std_cntr_t max_size, orte_std_cntr_t block_size);
|
2005-03-14 23:57:21 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Add a pointer to the array (Grow the array, if need be)
|
|
|
|
*
|
|
|
|
* @param array Pointer to array (IN)
|
|
|
|
* @param ptr Pointer value (IN)
|
|
|
|
*
|
2005-05-01 04:47:35 +04:00
|
|
|
* @param (OUT) Index of inserted array element.
|
|
|
|
* @return Return value less than zero indicates an error.
|
2005-03-14 23:57:21 +03:00
|
|
|
*/
|
2007-01-19 22:48:06 +03:00
|
|
|
ORTE_DECLSPEC int orte_pointer_array_add(orte_std_cntr_t *element_index, orte_pointer_array_t *array, void *ptr);
|
2005-03-14 23:57:21 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the value of an element in array
|
2005-05-01 04:58:06 +04:00
|
|
|
* Automatically extend array if required.
|
|
|
|
*
|
2007-01-19 22:48:06 +03:00
|
|
|
* @param array Pointer to array (IN)
|
|
|
|
* @param element_index Index of element to be reset (IN)
|
|
|
|
* @param value New value to be set at element index (IN)
|
2005-03-14 23:57:21 +03:00
|
|
|
*
|
|
|
|
* @return Error code. (-1) indicates an error.
|
|
|
|
*/
|
2006-08-20 19:54:04 +04:00
|
|
|
ORTE_DECLSPEC int orte_pointer_array_set_item(orte_pointer_array_t *array,
|
2007-01-19 22:48:06 +03:00
|
|
|
orte_std_cntr_t element_index, void *value);
|
2005-03-14 23:57:21 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the value of an element in array
|
|
|
|
*
|
2007-01-19 22:48:06 +03:00
|
|
|
* @param array Pointer to array (IN)
|
|
|
|
* @param element_index Index of element to be returned (IN)
|
2005-03-14 23:57:21 +03:00
|
|
|
*
|
|
|
|
* @return Error code. NULL indicates an error.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static inline void *orte_pointer_array_get_item(orte_pointer_array_t *table,
|
2007-01-19 22:48:06 +03:00
|
|
|
orte_std_cntr_t element_index)
|
2005-03-14 23:57:21 +03:00
|
|
|
{
|
|
|
|
void *p;
|
2005-07-04 02:45:48 +04:00
|
|
|
OPAL_THREAD_LOCK(&(table->lock));
|
2007-01-19 22:48:06 +03:00
|
|
|
p = table->addr[element_index];
|
2005-07-04 02:45:48 +04:00
|
|
|
OPAL_THREAD_UNLOCK(&(table->lock));
|
2005-03-14 23:57:21 +03:00
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the size of the pointer array
|
|
|
|
*
|
|
|
|
* @param array Pointer to array (IN)
|
|
|
|
*
|
|
|
|
* @returns size Size of the array
|
|
|
|
*
|
|
|
|
* Simple inline function to return the size of the array in order to
|
|
|
|
* hide the member field from external users.
|
|
|
|
*/
|
2006-08-15 23:54:10 +04:00
|
|
|
static inline orte_std_cntr_t orte_pointer_array_get_size(orte_pointer_array_t *array)
|
2005-03-14 23:57:21 +03:00
|
|
|
{
|
|
|
|
return array->size;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-07-18 22:49:00 +04:00
|
|
|
/**
|
|
|
|
* Set the size of the pointer array
|
|
|
|
*
|
|
|
|
* @param array Pointer to array (IN)
|
|
|
|
*
|
|
|
|
* @param size Desired size of the array
|
|
|
|
*
|
|
|
|
* Simple function to set the size of the array in order to
|
|
|
|
* hide the member field from external users.
|
|
|
|
*/
|
2006-08-20 19:54:04 +04:00
|
|
|
ORTE_DECLSPEC int orte_pointer_array_set_size(orte_pointer_array_t *array, orte_std_cntr_t size);
|
2005-07-18 22:49:00 +04:00
|
|
|
|
|
|
|
|
2005-03-14 23:57:21 +03:00
|
|
|
/**
|
|
|
|
* Clear the pointer array
|
|
|
|
*
|
|
|
|
* @param array Pointer to array (IN)
|
|
|
|
*
|
|
|
|
* @returns void
|
|
|
|
*
|
|
|
|
* Simple inline function to clear the pointer array and reset all
|
|
|
|
* counters.
|
|
|
|
*/
|
|
|
|
static inline void orte_pointer_array_clear(orte_pointer_array_t *array)
|
|
|
|
{
|
2007-04-12 08:58:47 +04:00
|
|
|
if( array->number_free == array->size )
|
|
|
|
return; /* nothing to do here this time (the array is already empty) */
|
2005-07-04 02:45:48 +04:00
|
|
|
OPAL_THREAD_LOCK(&(array->lock));
|
2005-07-18 22:49:00 +04:00
|
|
|
/* set the array elements to NULL */
|
|
|
|
memset(array->addr, 0, array->size * sizeof(void*));
|
2005-03-14 23:57:21 +03:00
|
|
|
array->lowest_free = 0;
|
|
|
|
array->number_free = array->size;
|
2005-07-04 02:45:48 +04:00
|
|
|
OPAL_THREAD_UNLOCK(&(array->lock));
|
2005-03-14 23:57:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Clear the pointer array, freeing any storage
|
|
|
|
*
|
|
|
|
* @param array Pointer to array (IN)
|
|
|
|
*
|
|
|
|
* @returns void
|
|
|
|
*
|
|
|
|
* Simple inline function to clear the pointer array and reset all
|
|
|
|
* counters.
|
|
|
|
*/
|
|
|
|
static inline void orte_pointer_array_free_clear(orte_pointer_array_t *array)
|
|
|
|
{
|
2006-08-15 23:54:10 +04:00
|
|
|
orte_std_cntr_t i;
|
2005-07-04 02:45:48 +04:00
|
|
|
OPAL_THREAD_LOCK(&(array->lock));
|
2005-03-14 23:57:21 +03:00
|
|
|
for (i=0; i < array->size; i++) {
|
|
|
|
if (NULL != array->addr[i]) free(array->addr[i]);
|
|
|
|
array->addr[i] = NULL;
|
|
|
|
}
|
|
|
|
array->lowest_free = 0;
|
|
|
|
array->number_free = array->size;
|
2005-07-04 02:45:48 +04:00
|
|
|
OPAL_THREAD_UNLOCK(&(array->lock));
|
2005-03-14 23:57:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test whether a certain element is already in use. If not yet
|
|
|
|
* in use, reserve it.
|
|
|
|
*
|
2007-01-19 22:48:06 +03:00
|
|
|
* @param array Pointer to array (IN)
|
|
|
|
* @param element_index Index of element to be tested (IN)
|
|
|
|
* @param value New value to be set at element index (IN)
|
2005-03-14 23:57:21 +03:00
|
|
|
*
|
|
|
|
* @return true/false True if element could be reserved
|
|
|
|
* False if element could not be reserved (e.g., in use).
|
|
|
|
*
|
|
|
|
* In contrary to array_set, this function does not allow to overwrite
|
|
|
|
* a value, unless the previous value is NULL ( equiv. to free ).
|
|
|
|
*/
|
2006-08-20 19:54:04 +04:00
|
|
|
ORTE_DECLSPEC bool orte_pointer_array_test_and_set_item (orte_pointer_array_t *table,
|
2007-01-19 22:48:06 +03:00
|
|
|
orte_std_cntr_t element_index,
|
2005-03-14 23:57:21 +03:00
|
|
|
void *value);
|
|
|
|
#if defined(c_plusplus) || defined(__cplusplus)
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#endif /* OMPI_POINTER_ARRAY_H */
|