netcdf-c/libdispatch/nclistmgr.c

130 lines
2.7 KiB
C
Raw Normal View History

2010-06-03 21:24:43 +08:00
/*********************************************************************
Copyright 2010, UCAR/Unidata See netcdf/COPYRIGHT file for
copying and redistribution conditions.
*********************************************************************/
Primary change: add dap4 support Specific changes: 1. Add dap4 code: libdap4 and dap4_test. Note that until the d4ts server problem is solved, dap4 is turned off. 2. Modify various files to support dap4 flags: configure.ac, Makefile.am, CMakeLists.txt, etc. 3. Add nc_test/test_common.sh. This centralizes the handling of the locations of various things in the build tree: e.g. where is ncgen.exe located. See nc_test/test_common.sh for details. 4. Modify .sh files to use test_common.sh 5. Obsolete separate oc2 by moving it to be part of netcdf-c. This means replacing code with netcdf-c equivalents. 5. Add --with-testserver to configure.ac to allow override of the servers to be used for --enable-dap-remote-tests. 6. There were multiple versions of nctypealignment code. Try to centralize in libdispatch/doffset.c and include/ncoffsets.h 7. Add a unit test for the ncuri code because of its complexity. 8. Move the findserver code out of libdispatch and into a separate, self contained program in ncdap_test and dap4_test. 9. Move the dispatch header files (nc{3,4}dispatch.h) to .../include because they are now shared by modules. 10. Revamp the handling of TOPSRCDIR and TOPBUILDDIR for shell scripts. 11. Make use of MREMAP if available 12. Misc. minor changes e.g. - #include <config.h> -> #include "config.h" - Add some no-install headers to /include - extern -> EXTERNL and vice versa as needed - misc header cleanup - clean up checking for misc. unix vs microsoft functions 13. Change copyright decls in some files to point to LICENSE file. 14. Add notes to RELEASENOTES.md
2017-03-09 08:01:10 +08:00
#include "config.h"
2010-06-03 21:24:43 +08:00
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include "nc.h"
#define ID_SHIFT (16)
#define NCFILELISTLENGTH 0x10000
2010-06-03 21:24:43 +08:00
/* Version one just allocates the max space (sizeof(NC*)*2^16)*/
static NC** nc_filelist = NULL;
static int numfiles = 0;
/* Common */
int
count_NCList(void)
{
return numfiles;
}
void
free_NCList(void)
{
if(numfiles > 0) return; /* not empty */
if(nc_filelist != NULL) free(nc_filelist);
nc_filelist = NULL;
}
int
add_to_NCList(NC* ncp)
{
int i;
int new_id;
if(nc_filelist == NULL) {
if (!(nc_filelist = calloc(1, sizeof(NC*)*NCFILELISTLENGTH)))
return NC_ENOMEM;
numfiles = 0;
}
#ifdef USE_REFCOUNT
/* Check the refcount */
if(ncp->refcount > 0)
return NC_NOERR;
#endif
2010-06-03 21:24:43 +08:00
new_id = 0; /* id's begin at 1 */
for(i=1; i < NCFILELISTLENGTH; i++) {
2010-06-03 21:24:43 +08:00
if(nc_filelist[i] == NULL) {new_id = i; break;}
}
if(new_id == 0) return NC_ENOMEM; /* no more slots */
nc_filelist[new_id] = ncp;
numfiles++;
new_id = (new_id << ID_SHIFT);
ncp->ext_ncid = new_id;
return NC_NOERR;
}
void
del_from_NCList(NC* ncp)
{
unsigned int ncid = ((unsigned int)ncp->ext_ncid) >> ID_SHIFT;
if(numfiles == 0 || ncid == 0 || nc_filelist == NULL) return;
if(nc_filelist[ncid] != ncp) return;
#ifdef USE_REFCOUNT
/* Check the refcount */
if(ncp->refcount > 0)
return; /* assume caller has decrecmented */
#endif
2010-06-03 21:24:43 +08:00
nc_filelist[ncid] = NULL;
numfiles--;
2010-10-14 06:53:25 +08:00
/* If all files have been closed, release the filelist memory. */
if (numfiles == 0)
free_NCList();
2010-06-03 21:24:43 +08:00
}
2010-08-18 23:11:17 +08:00
NC *
2010-06-03 21:24:43 +08:00
find_in_NCList(int ext_ncid)
{
NC* f = NULL;
unsigned int ncid = ((unsigned int)ext_ncid) >> ID_SHIFT;
if(numfiles > 0 && nc_filelist != NULL && ncid < NCFILELISTLENGTH)
f = nc_filelist[ncid];
/* for classic files, ext_ncid must be a multiple of (1<<ID_SHIFT) */
if (f != NULL && f->model == NC_FORMATX_NC3 && (ext_ncid % (1<<ID_SHIFT)))
return NULL;
2010-06-03 21:24:43 +08:00
return f;
}
/*
Added to support open by name
*/
NC*
find_in_NCList_by_name(const char* path)
{
int i;
NC* f = NULL;
if(nc_filelist == NULL)
return NULL;
for(i=1; i < NCFILELISTLENGTH; i++) {
if(nc_filelist[i] != NULL) {
if(strcmp(nc_filelist[i]->path,path)==0) {
f = nc_filelist[i];
break;
}
}
}
return f;
}
int
iterate_NCList(int index, NC** ncp)
{
/* Walk from 0 ...; 0 return => stop */
if(index < 0 || index >= NCFILELISTLENGTH)
return NC_ERANGE;
if(ncp) *ncp = nc_filelist[index];
return NC_NOERR;
}