2004-02-09 23:03:03 +03:00
|
|
|
/*
|
2004-11-22 04:38:40 +03:00
|
|
|
* Copyright (c) 2004-2005 The Trustees of Indiana University.
|
|
|
|
* All rights reserved.
|
|
|
|
* Copyright (c) 2004-2005 The Trustees of the University of Tennessee.
|
|
|
|
* 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$
|
|
|
|
*/
|
|
|
|
|
2004-08-19 03:24:27 +04:00
|
|
|
#include "ompi_config.h"
|
|
|
|
|
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>
|
|
|
|
|
2004-03-19 00:35:28 +03:00
|
|
|
#include "include/constants.h"
|
2004-06-07 19:33:53 +04:00
|
|
|
#include "class/ompi_pointer_array.h"
|
2004-03-17 21:45:16 +03:00
|
|
|
#include "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 };
|
|
|
|
|
2004-06-07 19:33:53 +04:00
|
|
|
static void ompi_pointer_array_construct(ompi_pointer_array_t *);
|
|
|
|
static void ompi_pointer_array_destruct(ompi_pointer_array_t *);
|
2004-10-23 23:24:00 +04:00
|
|
|
static bool grow_table(ompi_pointer_array_t *table, size_t soft, size_t hard);
|
2004-02-13 02:23:03 +03:00
|
|
|
|
2005-07-03 20:06:07 +04:00
|
|
|
OBJ_CLASS_INSTANCE(ompi_pointer_array_t, opal_object_t,
|
2004-10-23 23:24:00 +04:00
|
|
|
ompi_pointer_array_construct,
|
|
|
|
ompi_pointer_array_destruct);
|
2004-02-13 16:56:55 +03:00
|
|
|
|
2004-10-23 23:24:00 +04:00
|
|
|
/*
|
2004-06-07 19:33:53 +04:00
|
|
|
* ompi_pointer_array constructor
|
2004-02-13 02:23:03 +03:00
|
|
|
*/
|
2004-06-07 19:33:53 +04:00
|
|
|
void ompi_pointer_array_construct(ompi_pointer_array_t *array)
|
2004-03-03 19:44:41 +03:00
|
|
|
{
|
2004-06-07 19:33:53 +04:00
|
|
|
OBJ_CONSTRUCT(&array->lock, ompi_mutex_t);
|
2004-02-13 16:56:55 +03:00
|
|
|
array->lowest_free = 0;
|
|
|
|
array->number_free = 0;
|
|
|
|
array->size = 0;
|
|
|
|
array->addr = 0;
|
2004-02-13 02:23:03 +03:00
|
|
|
}
|
|
|
|
|
2004-10-23 23:24:00 +04:00
|
|
|
/*
|
2004-06-07 19:33:53 +04:00
|
|
|
* ompi_pointer_array destructor
|
2004-02-13 02:23:03 +03:00
|
|
|
*/
|
2004-10-23 23:24:00 +04:00
|
|
|
void ompi_pointer_array_destruct(ompi_pointer_array_t *array)
|
|
|
|
{
|
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
|
|
|
}
|
|
|
|
|
2004-02-09 23:03:03 +03:00
|
|
|
/**
|
|
|
|
* add a pointer to dynamic pointer table
|
|
|
|
*
|
2004-06-07 19:33:53 +04:00
|
|
|
* @param table Pointer to ompi_pointer_array_t object (IN)
|
2004-02-09 23:03:03 +03:00
|
|
|
* @param ptr Pointer to be added to table (IN)
|
|
|
|
*
|
2004-06-07 19:33:53 +04:00
|
|
|
* @return Array index where ptr is inserted or OMPI_ERROR if it fails
|
2004-02-09 23:03:03 +03:00
|
|
|
*/
|
2004-10-23 23:24:00 +04:00
|
|
|
int ompi_pointer_array_add(ompi_pointer_array_t *table, void *ptr)
|
2004-02-09 23:03:03 +03:00
|
|
|
{
|
|
|
|
void **p;
|
2004-10-23 23:24:00 +04:00
|
|
|
int i;
|
|
|
|
int index;
|
2004-02-09 23:03:03 +03:00
|
|
|
|
2004-03-15 23:53:21 +03:00
|
|
|
if (0) {
|
2004-06-07 19:33:53 +04:00
|
|
|
ompi_output(0,"ompi_pointer_array_add: IN: "
|
2004-02-09 23:03:03 +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);
|
|
|
|
|
2004-06-25 01:39:08 +04:00
|
|
|
OMPI_THREAD_LOCK(&(table->lock));
|
2004-02-09 23:03:03 +03:00
|
|
|
|
|
|
|
if (table->addr == NULL) {
|
|
|
|
|
|
|
|
/*
|
|
|
|
* first time
|
|
|
|
*/
|
|
|
|
|
2004-03-15 23:53:21 +03:00
|
|
|
if (0) {
|
2004-06-07 19:33:53 +04:00
|
|
|
ompi_output(0,"ompi_pointer_array_add: INIT: table %p\n", table);
|
2004-02-09 23:03:03 +03:00
|
|
|
}
|
|
|
|
|
2004-10-23 23:24:00 +04:00
|
|
|
p = (void **) malloc(TABLE_INIT * sizeof(void *));
|
2004-02-09 23:03:03 +03:00
|
|
|
if (p == NULL) {
|
2004-10-23 23:24:00 +04:00
|
|
|
OMPI_THREAD_UNLOCK(&(table->lock));
|
|
|
|
return OMPI_ERROR;
|
2004-02-09 23:03:03 +03:00
|
|
|
}
|
|
|
|
table->lowest_free = 0;
|
|
|
|
table->number_free = TABLE_INIT;
|
|
|
|
table->size = TABLE_INIT;
|
|
|
|
table->addr = p;
|
|
|
|
for (i = 0; i < table->size; i++) {
|
|
|
|
table->addr[i] = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
} else if (table->number_free == 0) {
|
|
|
|
|
2004-10-23 23:24:00 +04:00
|
|
|
/* need to grow table */
|
2004-02-09 23:03:03 +03:00
|
|
|
|
2004-10-23 23:24:00 +04:00
|
|
|
if (!grow_table(table, table->size * TABLE_GROW,
|
|
|
|
OMPI_FORTRAN_HANDLE_MAX)) {
|
|
|
|
OMPI_THREAD_UNLOCK(&(table->lock));
|
|
|
|
return OMPI_ERR_OUT_OF_RESOURCE;
|
2004-02-09 23:03:03 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(table->addr != NULL);
|
|
|
|
assert(table->size > 0);
|
|
|
|
assert(table->lowest_free >= 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
|
|
|
|
*/
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2004-03-15 23:53:21 +03:00
|
|
|
if (0) {
|
2004-06-07 19:33:53 +04:00
|
|
|
ompi_output(0,"ompi_pointer_array_add: 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, ptr);
|
|
|
|
}
|
|
|
|
|
2004-06-25 01:39:08 +04:00
|
|
|
OMPI_THREAD_UNLOCK(&(table->lock));
|
2004-02-09 23:03:03 +03:00
|
|
|
return index;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* free a slot in dynamic pointer table for reuse
|
|
|
|
*
|
|
|
|
*
|
2004-06-07 19:33:53 +04:00
|
|
|
* @param table Pointer to ompi_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
|
|
|
*/
|
2004-10-23 23:24:00 +04:00
|
|
|
int ompi_pointer_array_set_item(ompi_pointer_array_t *table, int index,
|
|
|
|
void * value)
|
2004-02-09 23:03:03 +03:00
|
|
|
{
|
|
|
|
assert(table != NULL);
|
|
|
|
|
2004-03-09 18:11:16 +03:00
|
|
|
#if 0
|
2004-06-07 19:33:53 +04:00
|
|
|
ompi_output(0,"ompi_pointer_array_set_item: IN: "
|
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
|
|
|
|
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
|
|
|
|
2004-06-25 01:39:08 +04:00
|
|
|
OMPI_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)) {
|
2004-06-25 01:39:08 +04:00
|
|
|
OMPI_THREAD_UNLOCK(&(table->lock));
|
2004-06-07 19:33:53 +04:00
|
|
|
return OMPI_ERROR;
|
2004-03-09 18:11:16 +03:00
|
|
|
}
|
|
|
|
}
|
2004-02-09 23:03:03 +03:00
|
|
|
|
2004-03-09 18:11:16 +03:00
|
|
|
/*
|
|
|
|
* allow a specific index to be changed.
|
2004-02-10 23:54:15 +03:00
|
|
|
*/
|
2004-09-13 19:13:13 +04:00
|
|
|
|
|
|
|
if ( NULL == table->addr[index] ) {
|
|
|
|
table->addr[index] = value;
|
|
|
|
/* mark element as free, if NULL element */
|
|
|
|
if( NULL == value ) {
|
|
|
|
if (index < table->lowest_free) {
|
|
|
|
table->lowest_free = index;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
table->number_free--;
|
|
|
|
/* Reset lowest_free if required */
|
|
|
|
if ( index == table->lowest_free ) {
|
2004-10-23 23:24:00 +04:00
|
|
|
int i;
|
2004-09-13 19:13:13 +04:00
|
|
|
|
|
|
|
table->lowest_free=table->size;
|
|
|
|
for ( i=index; i<table->size; i++) {
|
|
|
|
if ( NULL == table->addr[i] ){
|
|
|
|
table->lowest_free = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2004-02-09 23:03:03 +03:00
|
|
|
}
|
2004-06-16 02:14:56 +04:00
|
|
|
else {
|
2004-09-13 19:13:13 +04:00
|
|
|
table->addr[index] = value;
|
|
|
|
/* mark element as free, if NULL element */
|
|
|
|
if( NULL == value ) {
|
|
|
|
if (index < table->lowest_free) {
|
|
|
|
table->lowest_free = index;
|
|
|
|
}
|
|
|
|
table->number_free++;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* 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++) {
|
|
|
|
if ( NULL == table->addr[i] ){
|
|
|
|
table->lowest_free = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2004-06-16 02:14:56 +04:00
|
|
|
}
|
2004-09-13 19:13:13 +04:00
|
|
|
|
2004-02-09 23:03:03 +03:00
|
|
|
|
2004-03-09 18:11:16 +03:00
|
|
|
#if 0
|
2004-06-07 19:33:53 +04:00
|
|
|
ompi_output(0,"ompi_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
|
|
|
|
2004-06-25 01:39:08 +04:00
|
|
|
OMPI_THREAD_UNLOCK(&(table->lock));
|
2004-06-07 19:33:53 +04:00
|
|
|
return OMPI_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 ).
|
|
|
|
*/
|
2004-10-23 23:24:00 +04:00
|
|
|
bool ompi_pointer_array_test_and_set_item (ompi_pointer_array_t *table,
|
|
|
|
int index, void *value)
|
2004-05-21 01:13:11 +04:00
|
|
|
{
|
|
|
|
assert(table != NULL);
|
|
|
|
assert(index >= 0);
|
|
|
|
|
|
|
|
#if 0
|
2004-06-07 19:33:53 +04:00
|
|
|
ompi_output(0,"ompi_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 */
|
2004-06-25 01:39:08 +04:00
|
|
|
OMPI_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 */
|
2004-06-25 01:39:08 +04:00
|
|
|
OMPI_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)) {
|
2004-06-25 01:39:08 +04:00
|
|
|
OMPI_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
|
2004-06-07 19:33:53 +04:00
|
|
|
ompi_output(0,"ompi_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
|
|
|
|
|
2004-06-25 01:39:08 +04:00
|
|
|
OMPI_THREAD_UNLOCK(&(table->lock));
|
2004-10-23 23:24:00 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static bool grow_table(ompi_pointer_array_t *table, size_t soft, size_t hard)
|
|
|
|
{
|
|
|
|
size_t new_size;
|
|
|
|
int i, new_size_int;
|
|
|
|
void *p;
|
|
|
|
|
|
|
|
/* Ensure that we have room to grow -- stay less than
|
|
|
|
OMPI_FORTRAN_HANDLE_MAX. Note that OMPI_FORTRAN_HANDLE_MAX
|
|
|
|
is min(INT_MAX, fortran INTEGER max), so it's guaranteed to
|
|
|
|
fit within a [signed] int. */
|
|
|
|
|
|
|
|
if (table->size >= OMPI_FORTRAN_HANDLE_MAX) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (soft > OMPI_FORTRAN_HANDLE_MAX) {
|
|
|
|
if (hard > OMPI_FORTRAN_HANDLE_MAX) {
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
new_size = hard;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
new_size = soft;
|
|
|
|
}
|
|
|
|
|
|
|
|
p = (void **) realloc(table->addr, new_size * sizeof(void *));
|
|
|
|
if (p == NULL) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We've already established (above) that the arithimetic
|
|
|
|
below will be less than OMPI_FORTRAN_HANDLE_MAX */
|
|
|
|
|
|
|
|
new_size_int = (int) new_size;
|
|
|
|
table->number_free += new_size_int - table->size;
|
|
|
|
table->addr = p;
|
|
|
|
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
|
|
|
}
|