2004-02-09 23:03:03 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* $HEADER$
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Utility functions to manage fortran <-> c opaque object translation
|
|
|
|
*/
|
|
|
|
|
|
|
|
#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-09 18:11:16 +03:00
|
|
|
#include "lam/constants.h"
|
2004-02-09 23:03:03 +03:00
|
|
|
#include "lam/lfc/lam_pointer_array.h"
|
|
|
|
#include "lam/util/output.h"
|
|
|
|
|
2004-02-13 02:23:03 +03:00
|
|
|
static void lam_pointer_array_construct(lam_pointer_array_t *);
|
|
|
|
static void lam_pointer_array_destruct(lam_pointer_array_t *);
|
2004-03-09 18:11:16 +03:00
|
|
|
enum { TABLE_INIT = 1, TABLE_GROW = 2 };
|
2004-02-13 02:23:03 +03:00
|
|
|
|
|
|
|
lam_class_t lam_pointer_array_t_class = {
|
|
|
|
"lam_pointer_array_t",
|
|
|
|
OBJ_CLASS(lam_object_t),
|
|
|
|
(lam_construct_t) lam_pointer_array_construct,
|
|
|
|
(lam_destruct_t) lam_pointer_array_destruct
|
|
|
|
};
|
2004-02-13 16:56:55 +03:00
|
|
|
|
2004-02-13 02:23:03 +03:00
|
|
|
/**
|
|
|
|
* lam_pointer_array constructor
|
|
|
|
*/
|
2004-03-03 19:44:41 +03:00
|
|
|
void lam_pointer_array_construct(lam_pointer_array_t *array)
|
|
|
|
{
|
|
|
|
OBJ_CONSTRUCT(&array->lock, lam_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
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* lam_pointer_array destructor
|
|
|
|
*/
|
2004-02-13 16:56:55 +03:00
|
|
|
void lam_pointer_array_destruct(lam_pointer_array_t *array){
|
2004-02-13 02:23:03 +03:00
|
|
|
|
2004-02-13 19:23:29 +03:00
|
|
|
/* free table */
|
|
|
|
if( NULL != array->addr)
|
|
|
|
free(array->addr);
|
|
|
|
|
2004-03-03 19:44:41 +03:00
|
|
|
OBJ_DESTRUCT(&array->lock);
|
2004-02-13 02:23:03 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2004-02-09 23:03:03 +03:00
|
|
|
/**
|
|
|
|
* add a pointer to dynamic pointer table
|
|
|
|
*
|
|
|
|
* @param table Pointer to lam_pointer_array_t object (IN)
|
|
|
|
* @param ptr Pointer to be added to table (IN)
|
|
|
|
*
|
|
|
|
* @return Array index where ptr is inserted
|
|
|
|
*/
|
2004-02-10 02:27:43 +03:00
|
|
|
size_t lam_pointer_array_add(lam_pointer_array_t *table, void *ptr)
|
2004-02-09 23:03:03 +03:00
|
|
|
{
|
|
|
|
void **p;
|
2004-02-10 02:27:43 +03:00
|
|
|
int i;
|
|
|
|
size_t index;
|
2004-02-09 23:03:03 +03:00
|
|
|
|
2004-03-15 23:53:21 +03:00
|
|
|
if (0) {
|
2004-02-09 23:03:03 +03:00
|
|
|
lam_output(0,"lam_pointer_array_add: IN: "
|
|
|
|
" 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);
|
|
|
|
|
|
|
|
THREAD_LOCK(&(table->lock));
|
|
|
|
|
|
|
|
if (table->addr == NULL) {
|
|
|
|
|
|
|
|
/*
|
|
|
|
* first time
|
|
|
|
*/
|
|
|
|
|
2004-03-15 23:53:21 +03:00
|
|
|
if (0) {
|
2004-02-09 23:03:03 +03:00
|
|
|
lam_output(0,"lam_pointer_array_add: INIT: table %p\n", table);
|
|
|
|
}
|
|
|
|
|
|
|
|
p = malloc(TABLE_INIT * sizeof(void *));
|
|
|
|
if (p == NULL) {
|
2004-02-10 23:54:15 +03:00
|
|
|
THREAD_UNLOCK(&(table->lock));
|
2004-02-09 23:03:03 +03:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
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) {
|
|
|
|
|
|
|
|
/*
|
|
|
|
* grow table
|
|
|
|
*/
|
|
|
|
|
2004-03-15 23:53:21 +03:00
|
|
|
if (0) {
|
2004-02-09 23:03:03 +03:00
|
|
|
lam_output(0,"lam_pointer_array_add: GROW: table %p growing %d -> %d\n",
|
|
|
|
table, table->size, table->size * TABLE_GROW);
|
|
|
|
}
|
|
|
|
|
|
|
|
p = realloc(table->addr, TABLE_GROW * table->size * sizeof(void *));
|
|
|
|
if (p == NULL) {
|
|
|
|
THREAD_UNLOCK(&(table->lock));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
table->lowest_free = table->size;
|
|
|
|
table->number_free = (TABLE_GROW - 1) * table->size;
|
|
|
|
table->size *= TABLE_GROW;
|
|
|
|
table->addr = p;
|
|
|
|
for (i = table->lowest_free; i < table->size; i++) {
|
|
|
|
table->addr[i] = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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-02-09 23:03:03 +03:00
|
|
|
lam_output(0,"lam_pointer_array_add: 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, ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
THREAD_UNLOCK(&(table->lock));
|
|
|
|
|
|
|
|
return index;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* free a slot in dynamic pointer table for reuse
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @param table Pointer to lam_pointer_array_t object (IN)
|
|
|
|
* @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
|
|
|
*/
|
|
|
|
int lam_pointer_array_set_item(lam_pointer_array_t *table, size_t index,
|
|
|
|
void * value)
|
|
|
|
{
|
2004-02-10 23:54:15 +03:00
|
|
|
int return_value;
|
2004-02-09 23:03:03 +03:00
|
|
|
assert(table != NULL);
|
|
|
|
|
2004-03-09 18:11:16 +03:00
|
|
|
#if 0
|
|
|
|
lam_output(0,"lam_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-02-09 23:03:03 +03:00
|
|
|
THREAD_LOCK(&(table->lock));
|
2004-03-09 18:11:16 +03:00
|
|
|
if(table->size <= index) {
|
|
|
|
size_t i, new_size = (((index / TABLE_GROW) + 1) * TABLE_GROW);
|
|
|
|
void *p = realloc(table->addr, new_size * sizeof(void *));
|
|
|
|
if (p == NULL) {
|
|
|
|
THREAD_UNLOCK(&(table->lock));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
table->number_free += new_size - table->size;
|
|
|
|
table->addr = p;
|
|
|
|
for (i = table->size; i < new_size; i++) {
|
|
|
|
table->addr[i] = NULL;
|
|
|
|
}
|
|
|
|
table->size = new_size;
|
|
|
|
}
|
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-03-09 18:11:16 +03:00
|
|
|
table->addr[index] = value;
|
|
|
|
/* mark element as free, if NULL element */
|
|
|
|
if( NULL == value ) {
|
|
|
|
if (index < table->lowest_free) {
|
|
|
|
table->lowest_free = index;
|
2004-02-10 23:54:15 +03:00
|
|
|
}
|
2004-03-09 18:11:16 +03:00
|
|
|
table->number_free++;
|
2004-02-09 23:03:03 +03:00
|
|
|
}
|
|
|
|
|
2004-03-09 18:11:16 +03:00
|
|
|
#if 0
|
|
|
|
lam_output(0,"lam_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
|
|
|
|
|
|
|
THREAD_UNLOCK(&(table->lock));
|
2004-03-09 18:11:16 +03:00
|
|
|
return LAM_SUCCESS;
|
2004-02-09 23:03:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* lookup pointer by index in pointer table
|
|
|
|
*
|
|
|
|
* @param table Pointer to lam_pointer_array_t object (IN)
|
|
|
|
* @param ptr Pointer to be added to table (IN)
|
|
|
|
*
|
|
|
|
* @return Pointer
|
|
|
|
*/
|
2004-02-10 02:27:43 +03:00
|
|
|
void *lam_pointer_array_get_item(lam_pointer_array_t *table, size_t index)
|
2004-02-09 23:03:03 +03:00
|
|
|
{
|
|
|
|
void *p;
|
|
|
|
|
2004-03-09 18:11:16 +03:00
|
|
|
#if 0
|
|
|
|
lam_output(0,"lam_pointer_array_get_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
|
|
|
|
|
|
|
THREAD_LOCK(&(table->lock));
|
|
|
|
|
|
|
|
assert(table != NULL);
|
|
|
|
assert(table->addr != NULL);
|
|
|
|
assert(index >= 0);
|
|
|
|
assert(index < table->size);
|
|
|
|
|
|
|
|
p = table->addr[index];
|
|
|
|
|
|
|
|
THREAD_UNLOCK(&(table->lock));
|
|
|
|
|
2004-03-09 18:11:16 +03:00
|
|
|
#if 0
|
|
|
|
lam_output(0,"lam_pointer_array_get_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
|
|
|
return p;
|
|
|
|
}
|
2004-03-09 18:11:16 +03:00
|
|
|
|
|
|
|
|