1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328
|
/*
* Copyright (c) 2004-2005 The Trustees of Indiana University and Indiana
* University Research and Technology
* Corporation. All rights reserved.
* Copyright (c) 2004-2006 The University of Tennessee and The University
* of Tennessee Research Foundation. All rights
* reserved.
* Copyright (c) 2004-2005 High Performance Computing Center Stuttgart,
* University of Stuttgart. All rights reserved.
* Copyright (c) 2004-2005 The Regents of the University of California.
* All rights reserved.
* $COPYRIGHT$
*
* Additional copyrights may follow
*
* $HEADER$
*/
#include "ompi_config.h"
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include "ompi/constants.h"
#include "ompi/class/ompi_pointer_array.h"
#include "opal/util/output.h"
enum { TABLE_INIT = 1, TABLE_GROW = 2 };
static void ompi_pointer_array_construct(ompi_pointer_array_t *);
static void ompi_pointer_array_destruct(ompi_pointer_array_t *);
static bool grow_table(ompi_pointer_array_t *table, size_t soft, size_t hard);
OBJ_CLASS_INSTANCE(ompi_pointer_array_t, opal_object_t,
ompi_pointer_array_construct,
ompi_pointer_array_destruct);
/*
* ompi_pointer_array constructor
*/
void ompi_pointer_array_construct(ompi_pointer_array_t *array)
{
OBJ_CONSTRUCT(&array->lock, opal_mutex_t);
array->lowest_free = 0;
array->number_free = 0;
array->size = 0;
array->addr = 0;
}
/*
* ompi_pointer_array destructor
*/
void ompi_pointer_array_destruct(ompi_pointer_array_t *array)
{
/* free table */
if( NULL != array->addr) {
free(array->addr);
}
OBJ_DESTRUCT(&array->lock);
}
/**
* add a pointer to dynamic pointer table
*
* @param table Pointer to ompi_pointer_array_t object (IN)
* @param ptr Pointer to be added to table (IN)
*
* @return Array index where ptr is inserted or OMPI_ERROR if it fails
*/
int ompi_pointer_array_add(ompi_pointer_array_t *table, void *ptr)
{
int i;
int index;
OPAL_THREAD_LOCK(&(table->lock));
if (table->number_free == 0) {
/* need to grow table */
if (!grow_table(table,
(NULL == table->addr ? TABLE_INIT : table->size * TABLE_GROW),
OMPI_FORTRAN_HANDLE_MAX)) {
OPAL_THREAD_UNLOCK(&(table->lock));
return OMPI_ERR_OUT_OF_RESOURCE;
}
}
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) );
/*
* 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;
}
OPAL_THREAD_UNLOCK(&(table->lock));
return index;
}
/**
* free a slot in dynamic pointer table for reuse
*
*
* @param table Pointer to ompi_pointer_array_t object (IN)
* @param ptr Pointer to be added to table (IN)
*
* @return Error code
*
* Assumption: NULL element is free element.
*/
int ompi_pointer_array_set_item(ompi_pointer_array_t *table, int index,
void * value)
{
assert(table != NULL);
/* expand table if required to set a specific index */
OPAL_THREAD_LOCK(&(table->lock));
if (table->size <= index) {
if (!grow_table(table, ((index / TABLE_GROW) + 1) * TABLE_GROW,
index)) {
OPAL_THREAD_UNLOCK(&(table->lock));
return OMPI_ERROR;
}
}
/*
* allow a specific index to be changed.
*/
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 ) {
int i;
table->lowest_free=table->size;
for ( i=index; i<table->size; i++) {
if ( NULL == table->addr[i] ){
table->lowest_free = i;
break;
}
}
}
}
}
else {
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 ) {
int i;
table->lowest_free=table->size;
for ( i=index; i<table->size; i++) {
if ( NULL == table->addr[i] ){
table->lowest_free = i;
break;
}
}
}
}
}
#if 0
opal_output(0,"ompi_pointer_array_set_item: OUT: "
" 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
OPAL_THREAD_UNLOCK(&(table->lock));
return OMPI_SUCCESS;
}
/**
* 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 ).
*/
bool ompi_pointer_array_test_and_set_item (ompi_pointer_array_t *table,
int index, void *value)
{
assert(table != NULL);
assert(index >= 0);
#if 0
opal_output(0,"ompi_pointer_array_test_and_set_item: IN: "
" 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 */
OPAL_THREAD_LOCK(&(table->lock));
if ( index < table->size && table->addr[index] != NULL ) {
/* This element is already in use */
OPAL_THREAD_UNLOCK(&(table->lock));
return false;
}
/* Do we need to grow the table? */
if (table->size <= index) {
if (!grow_table(table, (((index / TABLE_GROW) + 1) * TABLE_GROW),
index)) {
OPAL_THREAD_UNLOCK(&(table->lock));
return false;
}
}
/*
* allow a specific index to be changed.
*/
table->addr[index] = value;
table->number_free--;
/* Reset lowest_free if required */
if ( index == table->lowest_free ) {
int i;
table->lowest_free = table->size;
for ( i=index; i<table->size; i++) {
if ( NULL == table->addr[i] ){
table->lowest_free = i;
break;
}
}
}
#if 0
opal_output(0,"ompi_pointer_array_test_and_set_item: OUT: "
" 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
OPAL_THREAD_UNLOCK(&(table->lock));
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 = (void**)p;
for (i = table->size; i < new_size_int; ++i) {
table->addr[i] = NULL;
}
table->size = new_size_int;
return true;
}
|