2007-12-21 09:02:00 +03:00
|
|
|
/* -*- Mode: C; c-basic-offset:4 ; -*- */
|
2004-02-09 23:03:03 +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.
|
2007-12-21 09:02:00 +03:00
|
|
|
* Copyright (c) 2004-2007 The University of Tennessee and The University
|
2005-11-05 22:57:48 +03:00
|
|
|
* of Tennessee Research Foundation. All rights
|
|
|
|
* reserved.
|
2004-11-28 23:09:25 +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.
|
2004-11-22 04:38:40 +03:00
|
|
|
* $COPYRIGHT$
|
|
|
|
*
|
|
|
|
* Additional copyrights may follow
|
|
|
|
*
|
2004-02-09 23:03:03 +03:00
|
|
|
* $HEADER$
|
|
|
|
*/
|
|
|
|
|
2007-12-21 09:02:00 +03:00
|
|
|
#include "opal_config.h"
|
2004-08-19 03:24:27 +04:00
|
|
|
|
2004-02-09 23:03:03 +03:00
|
|
|
#include <stdlib.h>
|
2004-02-10 23:54:15 +03:00
|
|
|
#include <stdio.h>
|
2004-02-09 23:03:03 +03:00
|
|
|
#include <assert.h>
|
|
|
|
|
2007-12-21 09:02:00 +03:00
|
|
|
#include "opal/constants.h"
|
|
|
|
#include "opal/class/opal_pointer_array.h"
|
2005-07-04 03:31:27 +04:00
|
|
|
#include "opal/util/output.h"
|
2004-02-09 23:03:03 +03:00
|
|
|
|
2004-10-23 23:24:00 +04:00
|
|
|
enum { TABLE_INIT = 1, TABLE_GROW = 2 };
|
|
|
|
|
2007-12-21 09:02:00 +03:00
|
|
|
static void opal_pointer_array_construct(opal_pointer_array_t *);
|
|
|
|
static void opal_pointer_array_destruct(opal_pointer_array_t *);
|
2007-12-21 17:22:27 +03:00
|
|
|
static bool grow_table(opal_pointer_array_t *table, int soft, int hard);
|
2004-02-13 02:23:03 +03:00
|
|
|
|
2007-12-21 09:02:00 +03:00
|
|
|
OBJ_CLASS_INSTANCE(opal_pointer_array_t, opal_object_t,
|
|
|
|
opal_pointer_array_construct,
|
|
|
|
opal_pointer_array_destruct);
|
2004-02-13 16:56:55 +03:00
|
|
|
|
2004-10-23 23:24:00 +04:00
|
|
|
/*
|
2007-12-21 09:02:00 +03:00
|
|
|
* opal_pointer_array constructor
|
2004-02-13 02:23:03 +03:00
|
|
|
*/
|
2007-12-21 09:02:00 +03:00
|
|
|
static void opal_pointer_array_construct(opal_pointer_array_t *array)
|
2004-03-03 19:44:41 +03:00
|
|
|
{
|
2005-07-04 02:45:48 +04:00
|
|
|
OBJ_CONSTRUCT(&array->lock, opal_mutex_t);
|
2004-02-13 16:56:55 +03:00
|
|
|
array->lowest_free = 0;
|
|
|
|
array->number_free = 0;
|
|
|
|
array->size = 0;
|
2007-12-21 09:02:00 +03:00
|
|
|
array->max_size = INT_MAX;
|
|
|
|
array->block_size = 0;
|
2004-02-13 16:56:55 +03:00
|
|
|
array->addr = 0;
|
2004-02-13 02:23:03 +03:00
|
|
|
}
|
|
|
|
|
2004-10-23 23:24:00 +04:00
|
|
|
/*
|
2007-12-21 09:02:00 +03:00
|
|
|
* opal_pointer_array destructor
|
2004-02-13 02:23:03 +03:00
|
|
|
*/
|
2007-12-21 09:02:00 +03:00
|
|
|
static void opal_pointer_array_destruct(opal_pointer_array_t *array)
|
2004-10-23 23:24:00 +04:00
|
|
|
{
|
2004-02-13 19:23:29 +03:00
|
|
|
/* free table */
|
2004-10-23 23:24:00 +04:00
|
|
|
if( NULL != array->addr) {
|
2004-02-13 19:23:29 +03:00
|
|
|
free(array->addr);
|
2004-10-23 23:24:00 +04:00
|
|
|
}
|
2004-02-13 19:23:29 +03:00
|
|
|
|
2004-03-03 19:44:41 +03:00
|
|
|
OBJ_DESTRUCT(&array->lock);
|
2004-02-13 02:23:03 +03:00
|
|
|
}
|
|
|
|
|
2007-12-21 09:02:00 +03:00
|
|
|
/**
|
|
|
|
* initialize an array object
|
|
|
|
*/
|
|
|
|
int opal_pointer_array_init(opal_pointer_array_t* array,
|
|
|
|
int initial_allocation,
|
|
|
|
int max_size, int block_size)
|
|
|
|
{
|
|
|
|
size_t num_bytes;
|
|
|
|
|
|
|
|
/* check for errors */
|
|
|
|
if (NULL == array || max_size < block_size) {
|
|
|
|
return OPAL_ERR_BAD_PARAM;
|
|
|
|
}
|
|
|
|
|
|
|
|
array->max_size = max_size;
|
|
|
|
array->block_size = block_size;
|
|
|
|
|
|
|
|
num_bytes = (0 < initial_allocation ? initial_allocation : block_size);
|
|
|
|
array->number_free = num_bytes;
|
|
|
|
array->size = num_bytes;
|
|
|
|
num_bytes *= sizeof(void*);
|
|
|
|
|
|
|
|
/* Allocate and set the array to NULL */
|
|
|
|
array->addr = (void **)calloc(num_bytes, 1);
|
|
|
|
if (NULL == array->addr) { /* out of memory */
|
|
|
|
return OPAL_ERR_OUT_OF_RESOURCE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return OPAL_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2004-02-09 23:03:03 +03:00
|
|
|
/**
|
|
|
|
* add a pointer to dynamic pointer table
|
|
|
|
*
|
2007-12-21 09:02:00 +03:00
|
|
|
* @param table Pointer to opal_pointer_array_t object (IN)
|
2004-02-09 23:03:03 +03:00
|
|
|
* @param ptr Pointer to be added to table (IN)
|
|
|
|
*
|
2007-12-21 09:02:00 +03:00
|
|
|
* @return Array index where ptr is inserted or OPAL_ERROR if it fails
|
2004-02-09 23:03:03 +03:00
|
|
|
*/
|
2007-12-21 09:02:00 +03:00
|
|
|
int opal_pointer_array_add(opal_pointer_array_t *table, void *ptr)
|
2004-02-09 23:03:03 +03:00
|
|
|
{
|
2007-12-21 09:02:00 +03:00
|
|
|
int i, index;
|
2004-02-09 23:03:03 +03:00
|
|
|
|
2005-07-04 02:45:48 +04:00
|
|
|
OPAL_THREAD_LOCK(&(table->lock));
|
2004-02-09 23:03:03 +03:00
|
|
|
|
2006-08-24 20:38:08 +04:00
|
|
|
if (table->number_free == 0) {
|
2004-10-23 23:24:00 +04:00
|
|
|
/* need to grow table */
|
2006-08-24 20:38:08 +04:00
|
|
|
if (!grow_table(table,
|
2007-02-06 19:53:44 +03:00
|
|
|
(NULL == table->addr ? TABLE_INIT : table->size * TABLE_GROW),
|
2004-10-23 23:24:00 +04:00
|
|
|
OMPI_FORTRAN_HANDLE_MAX)) {
|
2005-07-04 02:45:48 +04:00
|
|
|
OPAL_THREAD_UNLOCK(&(table->lock));
|
2007-12-21 09:02:00 +03:00
|
|
|
return OPAL_ERR_OUT_OF_RESOURCE;
|
2004-02-09 23:03:03 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-08-24 20:38:08 +04:00
|
|
|
assert( (table->addr != NULL) && (table->size > 0) );
|
|
|
|
assert( (table->lowest_free >= 0) && (table->lowest_free < table->size) );
|
|
|
|
assert( (table->number_free > 0) && (table->number_free <= table->size) );
|
2004-02-09 23:03:03 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* add pointer to table, and return the index
|
|
|
|
*/
|
|
|
|
|
|
|
|
index = table->lowest_free;
|
|
|
|
assert(table->addr[index] == NULL);
|
|
|
|
table->addr[index] = ptr;
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2005-07-04 02:45:48 +04:00
|
|
|
OPAL_THREAD_UNLOCK(&(table->lock));
|
2004-02-09 23:03:03 +03:00
|
|
|
return index;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2007-12-21 09:02:00 +03:00
|
|
|
* Set the value of the dynamic array at a specified location.
|
2004-02-09 23:03:03 +03:00
|
|
|
*
|
|
|
|
*
|
2007-12-21 09:02:00 +03:00
|
|
|
* @param table Pointer to opal_pointer_array_t object (IN)
|
2004-02-09 23:03:03 +03:00
|
|
|
* @param ptr Pointer to be added to table (IN)
|
|
|
|
*
|
|
|
|
* @return Error code
|
2004-02-10 23:54:15 +03:00
|
|
|
*
|
|
|
|
* Assumption: NULL element is free element.
|
2004-02-09 23:03:03 +03:00
|
|
|
*/
|
2007-12-21 09:02:00 +03:00
|
|
|
int opal_pointer_array_set_item(opal_pointer_array_t *table, int index,
|
2004-10-23 23:24:00 +04:00
|
|
|
void * value)
|
2004-02-09 23:03:03 +03:00
|
|
|
{
|
|
|
|
assert(table != NULL);
|
|
|
|
|
2004-03-09 18:11:16 +03:00
|
|
|
/* expand table if required to set a specific index */
|
2004-10-23 23:24:00 +04:00
|
|
|
|
2005-07-04 02:45:48 +04:00
|
|
|
OPAL_THREAD_LOCK(&(table->lock));
|
2004-10-23 23:24:00 +04:00
|
|
|
if (table->size <= index) {
|
|
|
|
if (!grow_table(table, ((index / TABLE_GROW) + 1) * TABLE_GROW,
|
|
|
|
index)) {
|
2005-07-04 02:45:48 +04:00
|
|
|
OPAL_THREAD_UNLOCK(&(table->lock));
|
2007-12-21 09:02:00 +03:00
|
|
|
return OPAL_ERROR;
|
2004-03-09 18:11:16 +03:00
|
|
|
}
|
|
|
|
}
|
2004-02-09 23:03:03 +03:00
|
|
|
|
2007-12-21 09:02:00 +03:00
|
|
|
/* mark element as free, if NULL element */
|
|
|
|
if( NULL == value ) {
|
|
|
|
if (index < table->lowest_free) {
|
|
|
|
table->lowest_free = index;
|
|
|
|
}
|
|
|
|
if( NULL != table->addr[index] ) {
|
|
|
|
table->number_free++;
|
|
|
|
}
|
|
|
|
} else {
|
2008-08-12 03:39:09 +04:00
|
|
|
if (NULL == table->addr[index]) {
|
|
|
|
table->number_free--;
|
|
|
|
}
|
2007-12-21 09:02:00 +03:00
|
|
|
/* Reset lowest_free if required */
|
|
|
|
if ( index == table->lowest_free ) {
|
|
|
|
int i;
|
2004-06-16 02:14:56 +04:00
|
|
|
|
2007-12-21 09:02:00 +03:00
|
|
|
table->lowest_free = table->size;
|
2007-12-21 23:19:55 +03:00
|
|
|
for ( i=index + 1; i<table->size; i++) {
|
2007-12-21 09:02:00 +03:00
|
|
|
if ( NULL == table->addr[i] ){
|
|
|
|
table->lowest_free = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2004-06-16 02:14:56 +04:00
|
|
|
}
|
2007-12-21 09:02:00 +03:00
|
|
|
table->addr[index] = value;
|
2004-02-09 23:03:03 +03:00
|
|
|
|
2004-03-09 18:11:16 +03:00
|
|
|
#if 0
|
2007-12-21 09:02:00 +03:00
|
|
|
opal_output(0,"opal_pointer_array_set_item: OUT: "
|
2004-02-09 23:03:03 +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,
|
|
|
|
index, table->addr[index]);
|
2004-03-09 18:11:16 +03:00
|
|
|
#endif
|
2004-02-09 23:03:03 +03:00
|
|
|
|
2005-07-04 02:45:48 +04:00
|
|
|
OPAL_THREAD_UNLOCK(&(table->lock));
|
2007-12-21 09:02:00 +03:00
|
|
|
return OPAL_SUCCESS;
|
2004-02-09 23:03:03 +03:00
|
|
|
}
|
|
|
|
|
2004-05-21 01:13:11 +04:00
|
|
|
/**
|
|
|
|
* Test whether a certain element is already in use. If not yet
|
|
|
|
* in use, reserve it.
|
|
|
|
*
|
|
|
|
* @param array Pointer to array (IN)
|
|
|
|
* @param index Index of element to be tested (IN)
|
|
|
|
* @param value New value to be set at element index (IN)
|
|
|
|
*
|
|
|
|
* @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 ).
|
|
|
|
*/
|
2007-12-21 09:02:00 +03:00
|
|
|
bool opal_pointer_array_test_and_set_item (opal_pointer_array_t *table,
|
2004-10-23 23:24:00 +04:00
|
|
|
int index, void *value)
|
2004-05-21 01:13:11 +04:00
|
|
|
{
|
|
|
|
assert(table != NULL);
|
|
|
|
assert(index >= 0);
|
|
|
|
|
|
|
|
#if 0
|
2007-12-21 09:02:00 +03:00
|
|
|
opal_output(0,"opal_pointer_array_test_and_set_item: IN: "
|
2004-05-21 01:13:11 +04:00
|
|
|
" table %p (size %ld, lowest free %ld, number free %ld)"
|
|
|
|
" addr[%d] = %p\n",
|
|
|
|
table, table->size, table->lowest_free, table->number_free,
|
|
|
|
index, table->addr[index]);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* expand table if required to set a specific index */
|
2005-07-04 02:45:48 +04:00
|
|
|
OPAL_THREAD_LOCK(&(table->lock));
|
2004-05-21 01:13:11 +04:00
|
|
|
if ( index < table->size && table->addr[index] != NULL ) {
|
|
|
|
/* This element is already in use */
|
2005-07-04 02:45:48 +04:00
|
|
|
OPAL_THREAD_UNLOCK(&(table->lock));
|
2004-10-23 23:24:00 +04:00
|
|
|
return false;
|
2004-05-21 01:13:11 +04:00
|
|
|
}
|
|
|
|
|
2004-10-23 23:24:00 +04:00
|
|
|
/* Do we need to grow the table? */
|
|
|
|
|
|
|
|
if (table->size <= index) {
|
|
|
|
if (!grow_table(table, (((index / TABLE_GROW) + 1) * TABLE_GROW),
|
|
|
|
index)) {
|
2005-07-04 02:45:48 +04:00
|
|
|
OPAL_THREAD_UNLOCK(&(table->lock));
|
2004-10-23 23:24:00 +04:00
|
|
|
return false;
|
2004-05-21 01:13:11 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* allow a specific index to be changed.
|
|
|
|
*/
|
|
|
|
table->addr[index] = value;
|
2004-09-13 19:13:13 +04:00
|
|
|
table->number_free--;
|
2004-06-16 02:14:56 +04:00
|
|
|
/* Reset lowest_free if required */
|
|
|
|
if ( index == table->lowest_free ) {
|
2004-10-23 23:24:00 +04:00
|
|
|
int i;
|
2004-06-16 02:14:56 +04:00
|
|
|
|
2004-09-13 19:13:13 +04:00
|
|
|
table->lowest_free = table->size;
|
|
|
|
for ( i=index; i<table->size; i++) {
|
2004-06-16 02:14:56 +04:00
|
|
|
if ( NULL == table->addr[i] ){
|
|
|
|
table->lowest_free = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-05-21 01:13:11 +04:00
|
|
|
#if 0
|
2007-12-21 09:02:00 +03:00
|
|
|
opal_output(0,"opal_pointer_array_test_and_set_item: OUT: "
|
2004-05-21 01:13:11 +04:00
|
|
|
" table %p (size %ld, lowest free %ld, number free %ld)"
|
|
|
|
" addr[%d] = %p\n",
|
|
|
|
table, table->size, table->lowest_free, table->number_free,
|
|
|
|
index, table->addr[index]);
|
|
|
|
#endif
|
|
|
|
|
2005-07-04 02:45:48 +04:00
|
|
|
OPAL_THREAD_UNLOCK(&(table->lock));
|
2004-10-23 23:24:00 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2007-12-21 09:02:00 +03:00
|
|
|
int opal_pointer_array_set_size(opal_pointer_array_t *array, int new_size)
|
|
|
|
{
|
|
|
|
OPAL_THREAD_LOCK(&(array->lock));
|
|
|
|
if(new_size > array->size) {
|
|
|
|
if (!grow_table(array, new_size, new_size)) {
|
|
|
|
OPAL_THREAD_UNLOCK(&(array->lock));
|
|
|
|
return OPAL_ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
OPAL_THREAD_UNLOCK(&(array->lock));
|
|
|
|
return OPAL_SUCCESS;
|
|
|
|
}
|
2004-10-23 23:24:00 +04:00
|
|
|
|
2007-12-21 17:22:27 +03:00
|
|
|
static bool grow_table(opal_pointer_array_t *table, int soft, int hard)
|
2004-10-23 23:24:00 +04:00
|
|
|
{
|
2007-12-21 17:22:27 +03:00
|
|
|
int new_size;
|
2004-10-23 23:24:00 +04:00
|
|
|
int i, new_size_int;
|
|
|
|
void *p;
|
|
|
|
|
2007-12-21 09:02:00 +03:00
|
|
|
/* new_size = ((table->size + num_needed + table->block_size - 1) /
|
|
|
|
table->block_size) * table->block_size; */
|
|
|
|
new_size = soft;
|
|
|
|
if( soft > table->max_size ) {
|
|
|
|
if( hard > table->max_size ) {
|
2004-10-23 23:24:00 +04:00
|
|
|
return false;
|
|
|
|
}
|
2007-12-21 09:02:00 +03:00
|
|
|
new_size = hard;
|
2004-10-23 23:24:00 +04:00
|
|
|
}
|
2007-12-21 09:02:00 +03:00
|
|
|
if( new_size >= table->max_size ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2004-10-23 23:24:00 +04:00
|
|
|
p = (void **) realloc(table->addr, new_size * sizeof(void *));
|
|
|
|
if (p == NULL) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2007-02-06 19:53:44 +03:00
|
|
|
/* We've already established (above) that the arithmetic
|
2004-10-23 23:24:00 +04:00
|
|
|
below will be less than OMPI_FORTRAN_HANDLE_MAX */
|
|
|
|
|
|
|
|
new_size_int = (int) new_size;
|
|
|
|
table->number_free += new_size_int - table->size;
|
2006-08-24 20:38:08 +04:00
|
|
|
table->addr = (void**)p;
|
2004-10-23 23:24:00 +04:00
|
|
|
for (i = table->size; i < new_size_int; ++i) {
|
|
|
|
table->addr[i] = NULL;
|
|
|
|
}
|
|
|
|
table->size = new_size_int;
|
|
|
|
|
|
|
|
return true;
|
2004-05-21 01:13:11 +04:00
|
|
|
}
|