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.
|
2006-08-23 07:32:36 +04:00
|
|
|
* Copyright (c) 2004-2006 The University of Tennessee and The University
|
2005-11-05 22:57:48 +03:00
|
|
|
* 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$
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "orte_config.h"
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <assert.h>
|
2005-03-29 17:25:53 +04:00
|
|
|
#include <string.h>
|
2005-03-14 23:57:21 +03:00
|
|
|
|
2006-02-12 04:33:29 +03:00
|
|
|
#include "orte/orte_constants.h"
|
|
|
|
#include "orte/class/orte_pointer_array.h"
|
2005-07-04 03:31:27 +04:00
|
|
|
#include "opal/util/output.h"
|
2005-03-14 23:57:21 +03:00
|
|
|
|
|
|
|
static void orte_pointer_array_construct(orte_pointer_array_t *);
|
|
|
|
static void orte_pointer_array_destruct(orte_pointer_array_t *);
|
|
|
|
static bool grow_table(orte_pointer_array_t *table);
|
|
|
|
|
2005-07-03 20:06:07 +04:00
|
|
|
OBJ_CLASS_INSTANCE(orte_pointer_array_t, opal_object_t,
|
2005-03-14 23:57:21 +03:00
|
|
|
orte_pointer_array_construct,
|
|
|
|
orte_pointer_array_destruct);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* orte_pointer_array constructor
|
|
|
|
*/
|
|
|
|
void orte_pointer_array_construct(orte_pointer_array_t *array)
|
|
|
|
{
|
2005-07-04 02:45:48 +04:00
|
|
|
OBJ_CONSTRUCT(&array->lock, opal_mutex_t);
|
2005-03-14 23:57:21 +03:00
|
|
|
array->lowest_free = 0;
|
|
|
|
array->number_free = 0;
|
|
|
|
array->size = 0;
|
|
|
|
array->max_size = 0;
|
|
|
|
array->block_size = 0;
|
|
|
|
array->addr = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* orte_pointer_array destructor
|
|
|
|
*/
|
|
|
|
void orte_pointer_array_destruct(orte_pointer_array_t *array)
|
|
|
|
{
|
|
|
|
/* free table */
|
|
|
|
if( NULL != array->addr) {
|
|
|
|
free(array->addr);
|
|
|
|
}
|
|
|
|
|
|
|
|
OBJ_DESTRUCT(&array->lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* initialize an array object
|
|
|
|
*/
|
|
|
|
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
|
|
|
{
|
2006-08-15 23:54:10 +04:00
|
|
|
orte_std_cntr_t num_bytes;
|
2005-03-14 23:57:21 +03:00
|
|
|
|
|
|
|
/* check for errors */
|
|
|
|
if (NULL == array || max_size < block_size) {
|
|
|
|
return ORTE_ERR_BAD_PARAM;
|
|
|
|
}
|
|
|
|
|
|
|
|
*array = OBJ_NEW(orte_pointer_array_t);
|
|
|
|
if (NULL == *array) {
|
|
|
|
return ORTE_ERR_OUT_OF_RESOURCE;
|
|
|
|
}
|
|
|
|
|
|
|
|
(*array)->max_size = max_size;
|
|
|
|
(*array)->block_size = block_size;
|
|
|
|
|
|
|
|
if (0 < initial_allocation) {
|
|
|
|
num_bytes = initial_allocation * sizeof(void*);
|
|
|
|
(*array)->number_free = initial_allocation;
|
|
|
|
(*array)->size = initial_allocation;
|
|
|
|
} else {
|
|
|
|
num_bytes = block_size * sizeof(void*);
|
|
|
|
(*array)->number_free = block_size;
|
|
|
|
(*array)->size = block_size;
|
|
|
|
}
|
|
|
|
|
2006-08-23 07:32:36 +04:00
|
|
|
(*array)->addr = (void **)malloc(num_bytes);
|
2005-03-14 23:57:21 +03:00
|
|
|
if (NULL == (*array)->addr) { /* out of memory */
|
|
|
|
OBJ_RELEASE(*array);
|
|
|
|
return ORTE_ERR_OUT_OF_RESOURCE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* init the array elements to NULL */
|
|
|
|
memset((*array)->addr, 0, num_bytes);
|
|
|
|
|
|
|
|
return ORTE_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* add a pointer to dynamic pointer table
|
|
|
|
*
|
|
|
|
* @param table Pointer to orte_pointer_array_t object (IN)
|
|
|
|
* @param ptr Pointer to be added to table (IN)
|
|
|
|
*
|
2005-05-01 04:47:35 +04:00
|
|
|
* @param (OUT) Array index where ptr is inserted
|
|
|
|
* @return ORTE_ERROR_code if it fails
|
2005-03-14 23:57:21 +03:00
|
|
|
*/
|
2006-08-15 23:54:10 +04:00
|
|
|
int orte_pointer_array_add(orte_std_cntr_t *location, orte_pointer_array_t *table, void *ptr)
|
2005-03-14 23:57:21 +03:00
|
|
|
{
|
2007-01-19 22:48:06 +03:00
|
|
|
orte_std_cntr_t i, element_index;
|
2005-03-14 23:57:21 +03:00
|
|
|
|
|
|
|
if (0) {
|
2005-07-04 03:31:27 +04:00
|
|
|
opal_output(0,"orte_pointer_array_add: IN: "
|
2005-03-14 23:57:21 +03:00
|
|
|
" table %p (size %ld, lowest free %ld, number free %ld)"
|
|
|
|
" ptr = %p\n",
|
|
|
|
table, table->size, table->lowest_free, table->number_free,
|
|
|
|
ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(table != NULL);
|
|
|
|
|
2005-07-04 02:45:48 +04:00
|
|
|
OPAL_THREAD_LOCK(&(table->lock));
|
2005-03-14 23:57:21 +03:00
|
|
|
|
|
|
|
if (table->number_free == 0) {
|
|
|
|
|
|
|
|
/* need to grow table */
|
|
|
|
|
|
|
|
if (!grow_table(table)) {
|
2005-07-04 02:45:48 +04:00
|
|
|
OPAL_THREAD_UNLOCK(&(table->lock));
|
2005-03-14 23:57:21 +03:00
|
|
|
return ORTE_ERR_OUT_OF_RESOURCE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(table->addr != NULL);
|
|
|
|
assert(table->size > 0);
|
|
|
|
assert(table->lowest_free < table->size);
|
|
|
|
assert(table->number_free > 0);
|
|
|
|
assert(table->number_free <= table->size);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* add pointer to table, and return the index
|
|
|
|
*/
|
|
|
|
|
2007-01-19 22:48:06 +03:00
|
|
|
element_index = table->lowest_free;
|
|
|
|
assert(table->addr[element_index] == NULL);
|
|
|
|
table->addr[element_index] = ptr;
|
2005-03-14 23:57:21 +03:00
|
|
|
table->number_free--;
|
|
|
|
if (table->number_free > 0) {
|
|
|
|
for (i = table->lowest_free + 1; i < table->size; i++) {
|
|
|
|
if (table->addr[i] == NULL) {
|
|
|
|
table->lowest_free = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
table->lowest_free = table->size;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (0) {
|
2005-07-04 03:31:27 +04:00
|
|
|
opal_output(0,"orte_pointer_array_add: OUT: "
|
2005-03-14 23:57:21 +03:00
|
|
|
" table %p (size %ld, lowest free %ld, number free %ld)"
|
|
|
|
" addr[%d] = %p\n",
|
|
|
|
table, table->size, table->lowest_free, table->number_free,
|
2007-01-19 22:48:06 +03:00
|
|
|
element_index, ptr);
|
2005-03-14 23:57:21 +03:00
|
|
|
}
|
|
|
|
|
2005-07-04 02:45:48 +04:00
|
|
|
OPAL_THREAD_UNLOCK(&(table->lock));
|
2007-01-19 22:48:06 +03:00
|
|
|
*location = element_index;
|
2005-05-01 04:47:35 +04:00
|
|
|
return ORTE_SUCCESS;
|
2005-03-14 23:57:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* free a slot in dynamic pointer table for reuse
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @param table Pointer to orte_pointer_array_t object (IN)
|
|
|
|
* @param ptr Pointer to be added to table (IN)
|
|
|
|
*
|
|
|
|
* @return Error code
|
|
|
|
*
|
|
|
|
* Assumption: NULL element is free element.
|
|
|
|
*/
|
2007-01-19 22:48:06 +03:00
|
|
|
int orte_pointer_array_set_item(orte_pointer_array_t *table, orte_std_cntr_t element_index,
|
2005-03-14 23:57:21 +03:00
|
|
|
void * value)
|
|
|
|
{
|
|
|
|
assert(table != NULL);
|
|
|
|
|
|
|
|
#if 0
|
2005-07-04 03:31:27 +04:00
|
|
|
opal_output(0,"orte_pointer_array_set_item: IN: "
|
2005-03-14 23:57:21 +03:00
|
|
|
" table %p (size %ld, lowest free %ld, number free %ld)"
|
|
|
|
" addr[%d] = %p\n",
|
|
|
|
table, table->size, table->lowest_free, table->number_free,
|
2007-01-19 22:48:06 +03:00
|
|
|
element_index, table->addr[element_index]);
|
2005-03-14 23:57:21 +03:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* expand table if required to set a specific index */
|
|
|
|
|
2005-07-04 02:45:48 +04:00
|
|
|
OPAL_THREAD_LOCK(&(table->lock));
|
2007-01-19 22:48:06 +03:00
|
|
|
if (table->size <= element_index) {
|
2005-03-14 23:57:21 +03:00
|
|
|
if (!grow_table(table)) {
|
2005-07-04 02:45:48 +04:00
|
|
|
OPAL_THREAD_UNLOCK(&(table->lock));
|
2005-03-14 23:57:21 +03:00
|
|
|
return ORTE_ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* allow a specific index to be changed.
|
|
|
|
*/
|
|
|
|
|
2007-01-19 22:48:06 +03:00
|
|
|
if ( NULL == table->addr[element_index] ) {
|
|
|
|
table->addr[element_index] = value;
|
2005-03-14 23:57:21 +03:00
|
|
|
/* mark element as free, if NULL element */
|
|
|
|
if( NULL == value ) {
|
2007-01-19 22:48:06 +03:00
|
|
|
if (element_index < table->lowest_free) {
|
|
|
|
table->lowest_free = element_index;
|
2005-03-14 23:57:21 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
table->number_free--;
|
|
|
|
/* Reset lowest_free if required */
|
2007-01-19 22:48:06 +03:00
|
|
|
if ( element_index == table->lowest_free ) {
|
2006-08-15 23:54:10 +04:00
|
|
|
orte_std_cntr_t i;
|
2005-03-14 23:57:21 +03:00
|
|
|
|
|
|
|
table->lowest_free=table->size;
|
2007-01-19 22:48:06 +03:00
|
|
|
for ( i=element_index; i<table->size; i++) {
|
2005-03-14 23:57:21 +03:00
|
|
|
if ( NULL == table->addr[i] ){
|
|
|
|
table->lowest_free = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2007-01-19 22:48:06 +03:00
|
|
|
table->addr[element_index] = value;
|
2005-03-14 23:57:21 +03:00
|
|
|
/* mark element as free, if NULL element */
|
|
|
|
if( NULL == value ) {
|
2007-01-19 22:48:06 +03:00
|
|
|
if (element_index < table->lowest_free) {
|
|
|
|
table->lowest_free = element_index;
|
2005-03-14 23:57:21 +03:00
|
|
|
}
|
|
|
|
table->number_free++;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* Reset lowest_free if required */
|
2007-01-19 22:48:06 +03:00
|
|
|
if ( element_index == table->lowest_free ) {
|
2006-08-15 23:54:10 +04:00
|
|
|
orte_std_cntr_t i;
|
2005-03-14 23:57:21 +03:00
|
|
|
|
|
|
|
table->lowest_free=table->size;
|
2007-01-19 22:48:06 +03:00
|
|
|
for ( i=element_index; i<table->size; i++) {
|
2005-03-14 23:57:21 +03:00
|
|
|
if ( NULL == table->addr[i] ){
|
|
|
|
table->lowest_free = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#if 0
|
2005-07-04 03:31:27 +04:00
|
|
|
opal_output(0,"orte_pointer_array_set_item: OUT: "
|
2005-03-14 23:57:21 +03:00
|
|
|
" table %p (size %ld, lowest free %ld, number free %ld)"
|
|
|
|
" addr[%d] = %p\n",
|
|
|
|
table, table->size, table->lowest_free, table->number_free,
|
2007-01-19 22:48:06 +03:00
|
|
|
element_index, table->addr[element_index]);
|
2005-03-14 23:57:21 +03:00
|
|
|
#endif
|
|
|
|
|
2005-07-04 02:45:48 +04:00
|
|
|
OPAL_THREAD_UNLOCK(&(table->lock));
|
2005-03-14 23:57:21 +03:00
|
|
|
return ORTE_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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 ).
|
|
|
|
*/
|
|
|
|
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, void *value)
|
2005-03-14 23:57:21 +03:00
|
|
|
{
|
|
|
|
assert(table != NULL);
|
|
|
|
|
|
|
|
#if 0
|
2005-07-04 03:31:27 +04:00
|
|
|
opal_output(0,"orte_pointer_array_test_and_set_item: IN: "
|
2005-03-14 23:57:21 +03:00
|
|
|
" table %p (size %ld, lowest free %ld, number free %ld)"
|
|
|
|
" addr[%d] = %p\n",
|
|
|
|
table, table->size, table->lowest_free, table->number_free,
|
2007-01-19 22:48:06 +03:00
|
|
|
element_index, table->addr[element_index]);
|
2005-03-14 23:57:21 +03:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* expand table if required to set a specific index */
|
2005-07-04 02:45:48 +04:00
|
|
|
OPAL_THREAD_LOCK(&(table->lock));
|
2007-01-19 22:48:06 +03:00
|
|
|
if ( element_index < table->size && table->addr[element_index] != NULL ) {
|
2005-03-14 23:57:21 +03:00
|
|
|
/* This element is already in use */
|
2005-07-04 02:45:48 +04:00
|
|
|
OPAL_THREAD_UNLOCK(&(table->lock));
|
2005-03-14 23:57:21 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Do we need to grow the table? */
|
|
|
|
|
2007-01-19 22:48:06 +03:00
|
|
|
if (table->size <= element_index) {
|
2005-03-14 23:57:21 +03:00
|
|
|
if (!grow_table(table)) {
|
2005-07-04 02:45:48 +04:00
|
|
|
OPAL_THREAD_UNLOCK(&(table->lock));
|
2005-03-14 23:57:21 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* allow a specific index to be changed.
|
|
|
|
*/
|
2007-01-19 22:48:06 +03:00
|
|
|
table->addr[element_index] = value;
|
2005-03-14 23:57:21 +03:00
|
|
|
table->number_free--;
|
|
|
|
/* Reset lowest_free if required */
|
2007-01-19 22:48:06 +03:00
|
|
|
if ( element_index == table->lowest_free ) {
|
2006-08-15 23:54:10 +04:00
|
|
|
orte_std_cntr_t i;
|
2005-03-14 23:57:21 +03:00
|
|
|
|
|
|
|
table->lowest_free = table->size;
|
2007-01-19 22:48:06 +03:00
|
|
|
for ( i=element_index; i<table->size; i++) {
|
2005-03-14 23:57:21 +03:00
|
|
|
if ( NULL == table->addr[i] ){
|
|
|
|
table->lowest_free = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#if 0
|
2005-07-04 03:31:27 +04:00
|
|
|
opal_output(0,"orte_pointer_array_test_and_set_item: OUT: "
|
2005-03-14 23:57:21 +03:00
|
|
|
" table %p (size %ld, lowest free %ld, number free %ld)"
|
|
|
|
" addr[%d] = %p\n",
|
|
|
|
table, table->size, table->lowest_free, table->number_free,
|
2007-01-19 22:48:06 +03:00
|
|
|
element_index, table->addr[element_index]);
|
2005-03-14 23:57:21 +03:00
|
|
|
#endif
|
|
|
|
|
2005-07-04 02:45:48 +04:00
|
|
|
OPAL_THREAD_UNLOCK(&(table->lock));
|
2005-03-14 23:57:21 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-08-15 23:54:10 +04:00
|
|
|
int orte_pointer_array_set_size(orte_pointer_array_t *array, orte_std_cntr_t new_size)
|
2005-07-18 22:49:00 +04:00
|
|
|
{
|
2005-08-02 19:54:36 +04:00
|
|
|
OPAL_THREAD_LOCK(&(array->lock));
|
2005-07-18 22:49:00 +04:00
|
|
|
while (new_size > orte_pointer_array_get_size(array)) {
|
|
|
|
if (!grow_table(array)) {
|
2005-08-02 19:54:36 +04:00
|
|
|
OPAL_THREAD_UNLOCK(&(array->lock));
|
2005-07-18 22:49:00 +04:00
|
|
|
return ORTE_ERROR;
|
|
|
|
}
|
|
|
|
}
|
2005-08-02 19:54:36 +04:00
|
|
|
OPAL_THREAD_UNLOCK(&(array->lock));
|
2005-07-18 22:49:00 +04:00
|
|
|
return ORTE_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-03-14 23:57:21 +03:00
|
|
|
static bool grow_table(orte_pointer_array_t *table)
|
|
|
|
{
|
2006-08-15 23:54:10 +04:00
|
|
|
orte_std_cntr_t new_size, i;
|
2005-03-14 23:57:21 +03:00
|
|
|
void *p;
|
|
|
|
|
|
|
|
/* Ensure that we have room to grow -- stay less than
|
|
|
|
* specified maximum
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (table->size >= table->max_size) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (table->block_size > (table->max_size - table->size)) { /* not enough space for a full block */
|
|
|
|
new_size = table->max_size;
|
|
|
|
} else {
|
|
|
|
new_size = table->size + table->block_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
p = (void **) realloc(table->addr, new_size * sizeof(void *));
|
|
|
|
if (p == NULL) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Adjust structure counters and pointers */
|
|
|
|
|
|
|
|
table->number_free += new_size - table->size;
|
2006-08-23 07:32:36 +04:00
|
|
|
table->addr = (void**)p;
|
2005-03-14 23:57:21 +03:00
|
|
|
for (i = table->size; i < new_size; ++i) {
|
|
|
|
table->addr[i] = NULL;
|
|
|
|
}
|
|
|
|
table->size = new_size;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|