Commit b6b42da5 authored by Deike Kleberg's avatar Deike Kleberg
Browse files

Adapted taxis to global resource array. Changed implementation of resources...

Adapted taxis to global resource array. Changed implementation of resources operations to be called with XXX_t pointer args in grid, zaxis and taxis.
parent bb6ab17f
......@@ -42,9 +42,9 @@ char *Grids[] = {
};
int gridCompareP ( void * gridID, void * grid );
void gridDestroyP ( void * gridID );
void gridPrintP ( void * gridID );
int gridCompareP ( void * gridptr1, void * gridptr2 );
void gridDestroyP ( void * gridptr );
void gridPrintP ( void * gridptr );
resOps gridOps = { gridCompareP, gridDestroyP, gridPrintP };
......@@ -53,9 +53,9 @@ static int GRID_Debug = 0; /* If set to 1, debugging */
static
void grid_init_entry(grid_t *gridptr)
void grid_init_ptr(grid_t *gridptr)
{
gridptr->self = reshPut (( void * ) gridptr, &gridOps );
gridptr->self = -1;
gridptr->type = CDI_UNDEFID;
gridptr->mask = NULL;
......@@ -119,6 +119,14 @@ void grid_init_entry(grid_t *gridptr)
gridptr->ystdname[0] = 0;
}
static
void grid_init_entry ( grid_t *gridptr )
{
grid_init_ptr ( gridptr );
gridptr->self = reshPut (( void * ) gridptr, &gridOps );
}
static
grid_t *grid_new_entry(void)
{
......@@ -437,6 +445,28 @@ int gridCreate(int gridtype, int size)
return (gridID);
}
void gridDestroyKernel ( grid_t * gridptr )
{
int id;
assert ( gridptr );
id = gridptr->self;
if ( gridptr->mask ) free(gridptr->mask);
if ( gridptr->mask_gme ) free(gridptr->mask_gme);
if ( gridptr->xvals ) free(gridptr->xvals);
if ( gridptr->yvals ) free(gridptr->yvals);
if ( gridptr->area ) free(gridptr->area);
if ( gridptr->xbounds ) free(gridptr->xbounds);
if ( gridptr->ybounds ) free(gridptr->ybounds);
if ( gridptr->rowlon ) free(gridptr->rowlon);
if ( gridptr->reference ) free(gridptr->reference);
free ( gridptr );
reshRemove ( id );
}
/*
@Function gridDestroy
......@@ -453,27 +483,13 @@ void gridDestroy(int gridID)
grid_t *gridptr;
gridptr = ( grid_t *) reshGetVal ( gridID );
grid_check_ptr(gridID, gridptr);
if ( gridptr->mask ) free(gridptr->mask);
if ( gridptr->mask_gme ) free(gridptr->mask_gme);
if ( gridptr->xvals ) free(gridptr->xvals);
if ( gridptr->yvals ) free(gridptr->yvals);
if ( gridptr->area ) free(gridptr->area);
if ( gridptr->xbounds ) free(gridptr->xbounds);
if ( gridptr->ybounds ) free(gridptr->ybounds);
if ( gridptr->rowlon ) free(gridptr->rowlon);
if ( gridptr->reference ) free(gridptr->reference);
free ( gridptr );
reshRemove ( gridID );
gridDestroyKernel ( gridptr );
}
void gridDestroyP ( void * gridID )
void gridDestroyP ( void * gridptr )
{
gridDestroy ( *(( int * ) gridID ));
gridDestroyKernel (( grid_t * ) gridptr );
}
......@@ -2262,9 +2278,15 @@ int gridCompare(int gridID, grid_t grid)
return (differ);
}
int gridCompareP ( void * gridID, void * grid )
int gridCompareP ( void * gridptr1, void * gridptr2 )
{
return gridCompare ( *(( int * ) gridID ), *(( grid_t * ) grid ));
grid_t * g1 = ( grid_t * ) gridptr1;
grid_t * g2 = ( grid_t * ) gridptr2;
assert ( g1 );
assert ( g2 );
return gridCompare ( g1->self, * g2 );
}
......@@ -3272,9 +3294,11 @@ void gridPrint ( int gridID, int opt )
void gridPrintP ( void * grid )
void gridPrintP ( void * gridptr )
{
gridPrintKernel (( grid_t * ) grid , 0 );
assert ( gridptr );
gridPrintKernel (( grid_t * ) gridptr , 0 );
}
......
......@@ -375,3 +375,14 @@ void reshArrayPrint ()
namespaceSetActive ( temp );
}
}
void reshLock ()
{
ARRAY_LOCK ();
}
void reshUnlock ()
{
ARRAY_UNLOCK ();
}
......@@ -37,4 +37,7 @@ reshRemove ( cdiResH );
int reshCountType ( resOps * );
void reshLock ();
void reshUnlock ();
#endif
......@@ -11,6 +11,7 @@
#include "calendar.h"
#include "pio_util.h"
#include "namespace.h"
#include "resource_handle.h"
extern int cdiDefaultCalendar;
......@@ -33,6 +34,16 @@ char *Timeunits[] = {
};
int taxisCompareP ( void * taxisptr1, void * taxisptr2 );
void taxisDestroyP ( void * taxisptr );
void taxisPrintP ( void * taxisptr );
resOps taxisOps = { taxisCompareP, taxisDestroyP, taxisPrintP };
static int TAXIS_Debug = 0; /* If set to 1, debugging */
char *tunitNamePtr(int unitID)
{
char *name;
......@@ -76,211 +87,6 @@ void taxis_defaults(void)
}
static int TAXIS_Debug = 0; /* If set to 1, debugging */
static int *_taxis_min;// = MIN_TAXES;
static int _taxis_max = MAX_TAXES;
static void taxis_initialize(void);
static int _taxis_init = FALSE;
#if defined (HAVE_LIBPTHREAD)
# include <pthread.h>
static pthread_once_t _taxis_init_thread = PTHREAD_ONCE_INIT;
static pthread_mutex_t _taxis_mutex;
# define TAXIS_LOCK() pthread_mutex_lock(&_taxis_mutex)
# define TAXIS_UNLOCK() pthread_mutex_unlock(&_taxis_mutex)
# define TAXIS_INIT() \
if ( _taxis_init == FALSE ) pthread_once(&_taxis_init_thread, taxis_initialize)
#else
# define TAXIS_LOCK()
# define TAXIS_UNLOCK()
# define TAXIS_INIT() \
if ( _taxis_init == FALSE ) taxis_initialize()
#endif
typedef struct _taxisPtrToIdx {
int idx;
int next;
taxis_t *ptr;
} taxisPtrToIdx;
static taxisPtrToIdx **_taxisList = NULL;
static int *_taxisAvail;
static
void taxis_list_new(void)
{
int nnsp, i;
nnsp = namespaceGetNumber ();
for ( i = 0; i < nnsp; i++ )
{
assert(_taxisList[i] == NULL);
_taxisList[i] = (taxisPtrToIdx *) malloc(_taxis_min[i]*sizeof(taxisPtrToIdx));
}
}
static
void taxis_list_delete(void)
{
int i, nnsp;
nnsp = namespaceGetNumber ();
if ( _taxis_min ) free ( _taxis_min );
if ( _taxisAvail ) free ( _taxisAvail );
if ( _taxisList )
{
for ( i = 0; i < nnsp; i++ )
if ( _taxisList[i] ) free ( _taxisList[i] );
free ( _taxisList );
}
}
static
void taxis_init_pointer(void)
{
int nnsp, i, j, code;
namespaceTuple_t nspt;
nnsp = namespaceGetNumber ();
for ( i = 0; i < nnsp; i++ )
{
for ( j = 0; j < _taxis_min[i]; ++j )
{
nspt.nsp = i;
nspt.idx = j;
code = namespaceIdxEncode ( nspt );
_taxisList[i][j].idx = code;
_taxisList[i][j].next = j + 1;
_taxisList[i][j].ptr = NULL;
}
_taxisList[i][_taxis_min[i]-1].next = -1;
_taxisAvail[i] = 0;
}
}
static
void taxis_list_extend(void)
{
int ntaxis;
int i, nsp, code;
namespaceTuple_t nspt;
nsp = namespaceGetActive ();
nspt.nsp = nsp;
assert(_taxisList[nsp] != NULL);
ntaxis = _taxis_min[nsp] + MIN_TAXES;
if ( ntaxis <= _taxis_max)
{
_taxisList[nsp] = (taxisPtrToIdx *) realloc(_taxisList[nsp], ntaxis*sizeof(taxisPtrToIdx));
for ( i = _taxis_min[nsp]; i < ntaxis; ++i )
{
nspt.idx = i;
code = namespaceIdxEncode ( nspt );
_taxisList[nsp][i].idx = code;
_taxisList[nsp][i].next = i + 1;
_taxisList[nsp][i].ptr = NULL;
}
_taxisAvail[nsp] = _taxis_min[nsp];
_taxisList[nsp][_taxis_min[nsp]-1].next = _taxis_min[nsp];
_taxis_min[nsp] = ntaxis;
_taxisList[nsp][_taxis_min[nsp]-1].next = -1;
}
else
Warning("Too many open taxes (limit is %d)!", _taxis_max);
}
static
taxis_t *taxis_to_pointer(int idx)
{
taxis_t *taxisptr = NULL;
int nsp;
namespaceTuple_t nspT;
nsp = namespaceGetActive ();
nspT = namespaceIdxDecode ( idx );
if ( nspT.nsp != nsp )
Error ( "idx %d from namespace %d not available in active namespace %d !\n",
idx, nspT.nsp, nsp );
TAXIS_INIT();
if ( nspT.idx >= 0 && nspT.idx < _taxis_min[nsp] )
{
TAXIS_LOCK();
taxisptr = _taxisList[nsp][nspT.idx].ptr;
TAXIS_UNLOCK();
}
else
Error("taxis index %d undefined!", idx);
return (taxisptr);
}
/* Create an index from a pointer */
static
int taxis_from_pointer(taxis_t *ptr)
{
int idx = -1;
int nsp;
nsp = namespaceGetActive ();
if ( ptr )
{
TAXIS_LOCK();
if ( _taxisAvail[nsp] < 0 ) taxis_list_extend();
if ( _taxisAvail[nsp] >= 0 )
{
taxisPtrToIdx *newptr;
newptr = &_taxisList[nsp][_taxisAvail[nsp]];
_taxisAvail[nsp] = newptr->next;
newptr->next = -1;
idx = newptr->idx;
newptr->ptr = ptr;
if ( TAXIS_Debug )
Message("Pointer %p has idx %d from taxis list", ptr, idx);
}
TAXIS_UNLOCK();
}
else
Error("Internal problem (pointer %p undefined)", ptr);
return (idx);
}
static
void taxis_init_ptr(taxis_t *taxisptr)
{
......@@ -307,7 +113,7 @@ void taxis_init_entry(taxis_t *taxisptr)
{
taxis_init_ptr(taxisptr);
taxisptr->self = taxis_from_pointer(taxisptr);
taxisptr->self = reshPut (( void * ) taxisptr, &taxisOps );
}
static
......@@ -322,68 +128,6 @@ taxis_t *taxis_new_entry(void)
return (taxisptr);
}
static
void taxis_delete_entry(taxis_t *taxisptr)
{
int idx;
int nsp;
nsp = namespaceGetActive ();
idx = taxisptr->self;
TAXIS_LOCK();
free(taxisptr);
_taxisList[nsp][idx].next = _taxisAvail[nsp];
_taxisList[nsp][idx].ptr = 0;
_taxisAvail[nsp] = idx;
TAXIS_UNLOCK();
if ( TAXIS_Debug )
Message("Removed idx %d from taxis list", idx);
}
static
void taxis_initialize(void)
{
char *env;
int nnsp, i;
#if defined (HAVE_LIBPTHREAD)
/* initialize global API mutex lock */
pthread_mutex_init(&_taxis_mutex, NULL);
#endif
env = getenv("TAXIS_DEBUG");
if ( env ) TAXIS_Debug = atoi(env);
nnsp = namespaceGetNumber ();
_taxisList = xmalloc ( nnsp * sizeof ( _taxisList[0] ));
_taxisAvail = xmalloc ( nnsp * sizeof ( int ));
_taxis_min = xmalloc ( nnsp * sizeof ( int ));
for ( i = 0; i < nnsp; i++ )
{
_taxisAvail[i] = -1;
_taxis_min[i] = MIN_TAXES;
}
taxis_list_new();
atexit(taxis_list_delete);
TAXIS_LOCK();
taxis_init_pointer();
TAXIS_UNLOCK();
_taxis_init = TRUE;
taxis_defaults();
}
static
void taxis_copy(taxis_t *taxisptr2, taxis_t *taxisptr1)
......@@ -442,8 +186,6 @@ int taxisCreate(int taxistype)
if ( CDI_Debug )
Message("taxistype: %d", taxistype);
TAXIS_INIT();
taxisptr = taxis_new_entry();
if ( ! taxisptr ) Error("No memory");
......@@ -456,6 +198,21 @@ int taxisCreate(int taxistype)
return (taxisID);
}
void taxisDestroyKernel ( taxis_t * taxisptr )
{
int id;
taxis_check_ptr(__func__, taxisptr);
id = taxisptr->self;
free ( taxisptr );
reshRemove ( id );
}
/*
@Function taxisDestroy
@Title Destroy a Time axis
......@@ -470,11 +227,14 @@ void taxisDestroy(int taxisID)
{
taxis_t *taxisptr;
taxisptr = taxis_to_pointer(taxisID);
taxisptr = ( taxis_t * ) reshGetVal ( taxisID );
taxis_check_ptr(__func__, taxisptr);
taxisDestroyKernel ( taxisptr );
}
taxis_delete_entry(taxisptr);
void taxisDestroyP ( void * taxisptr )
{
taxisDestroyKernel (( taxis_t * ) taxisptr );
}
......@@ -484,7 +244,8 @@ int taxisDuplicate(int taxisID1)
taxis_t *taxisptr1;
taxis_t *taxisptr2;
taxisptr1 = taxis_to_pointer(taxisID1);
taxisptr1 = ( taxis_t * ) reshGetVal ( taxisID1 );
taxisptr2 = taxis_new_entry();
if ( ! taxisptr2 ) Error("No memory");
......@@ -506,7 +267,7 @@ void taxisDefType(int taxisID, int type)
{
taxis_t *taxisptr;
taxisptr = taxis_to_pointer(taxisID);
taxisptr = ( taxis_t * ) reshGetVal ( taxisID );
taxis_check_ptr(__func__, taxisptr);
......@@ -531,7 +292,7 @@ void taxisDefVdate(int taxisID, int vdate)
{
taxis_t *taxisptr;
taxisptr = taxis_to_pointer(taxisID);
taxisptr = ( taxis_t * ) reshGetVal ( taxisID );
taxis_check_ptr(__func__, taxisptr);
......@@ -556,7 +317,7 @@ void taxisDefVtime(int taxisID, int vtime)
{
taxis_t *taxisptr;
taxisptr = taxis_to_pointer(taxisID);
taxisptr = ( taxis_t * ) reshGetVal ( taxisID );
taxis_check_ptr(__func__, taxisptr);
......@@ -581,7 +342,7 @@ void taxisDefRdate(int taxisID, int rdate)
{
taxis_t *taxisptr;
taxisptr = taxis_to_pointer(taxisID);
taxisptr = ( taxis_t * ) reshGetVal ( taxisID );
taxis_check_ptr(__func__, taxisptr);
......@@ -606,7 +367,7 @@ void taxisDefRtime(int taxisID, int rtime)
{
taxis_t *taxisptr;
taxisptr = taxis_to_pointer(taxisID);
taxisptr = ( taxis_t * ) reshGetVal ( taxisID );
taxis_check_ptr(__func__, taxisptr);
......@@ -633,7 +394,7 @@ void taxisDefCalendar(int taxisID, int calendar)
{
taxis_t *taxisptr;
taxisptr = taxis_to_pointer(taxisID);
taxisptr = ( taxis_t * ) reshGetVal ( taxisID );
taxis_check_ptr(__func__, taxisptr);
......@@ -645,7 +406,7 @@ void taxisDefTunit(int taxisID, int unit)
{
taxis_t *taxisptr;
taxisptr = taxis_to_pointer(taxisID);
taxisptr = ( taxis_t * ) reshGetVal ( taxisID );
taxis_check_ptr(__func__, taxisptr);
......@@ -657,7 +418,7 @@ void taxisDefNumavg(int taxisID, int numavg)
{
taxis_t *taxisptr;
taxisptr = taxis_to_pointer(taxisID);
taxisptr = ( taxis_t * ) reshGetVal ( taxisID );
taxis_check_ptr(__func__, taxisptr);
......@@ -673,7 +434,7 @@ int taxisInqType(int taxisID)
{
taxis_t *taxisptr;
taxisptr = taxis_to_pointer(taxisID);
taxisptr = ( taxis_t * ) reshGetVal ( taxisID );
taxis_check_ptr(__func__, taxisptr);
......@@ -685,7 +446,7 @@ int taxisHasBounds(int taxisID)
{
taxis_t *taxisptr;
taxisptr = taxis_to_pointer(taxisID);
taxisptr = ( taxis_t * ) reshGetVal ( taxisID );
taxis_check_ptr(__func__, taxisptr);
......@@ -697,7 +458,7 @@ void taxisDeleteBounds(int taxisID)
{
taxis_t *taxisptr;
taxisptr = taxis_to_pointer(taxisID);
taxisptr = ( taxis_t * ) reshGetVal ( taxisID );
taxis_check_ptr(__func__, taxisptr);
......@@ -710,13 +471,14 @@ void taxisCopyTimestep(int taxisID2, int taxisID1)
taxis_t *taxisptr1;
taxis_t *taxisptr2;
taxisptr1 = taxis_to_pointer(taxisID1);
taxisptr2 = taxis_to_pointer(taxisID2);
taxisptr1 = ( taxis_t * ) reshGetVal ( taxisID1 );
taxisptr2 = ( taxis_t * ) reshGetVal ( taxisID2 );
taxis_check_ptr(__func__, taxisptr1);
taxis_check_ptr(__func__, taxisptr2);
TAXIS_LOCK();
reshLock ();
taxisptr2->rdate = taxisptr1->rdate;
taxisptr2->rtime = taxisptr1->rtime;
......@@ -732,7 +494,7 @@ void taxisCopyTimestep(int taxisID2, int taxisID1)
taxisptr2->vtime_ub = taxisptr1->vtime_ub;
}
TAXIS_UNLOCK();
reshUnlock ();
}