netcdf-c/libdispatch/ddispatch.c
Dennis Heimbigner f3e711e2b8 Add support for setting HDF5 alignment property when creating a file
re: https://github.com/Unidata/netcdf-c/issues/2177
re: https://github.com/Unidata/netcdf-c/pull/2178

Provide get/set functions to store global data alignment
information and apply it when a file is created.

The api is as follows:
````
int nc_set_alignment(int threshold, int alignment);
int nc_get_alignment(int* thresholdp, int* alignmentp);
````

If defined, then for every file created opened after the call to
nc_set_alignment, for every new variable added to the file, the
most recently set threshold and alignment values will be applied
to that variable.

The nc_get_alignment function return the last values set by
nc_set_alignment.  If nc_set_alignment has not been called, then
it returns the value 0 for both threshold and alignment.

The alignment parameters are stored in the NCglobalstate object
(see below) for use as needed. Repeated calls to nc_set_alignment
will overwrite any existing values in NCglobalstate.

The alignment parameters are applied in libhdf5/hdf5create.c
and libhdf5/hdf5open.c

The set/get alignment functions are defined in libsrc4/nc4internal.c.

A test program was added as nc_test4/tst_alignment.c.

## Misc. Changes Unrelated to Alignment

* The NCRCglobalstate type was renamed to NCglobalstate to
  indicate that it represented more general global state than
  just .rc data.  It was also moved to nc4internal.h.  This led
  to a large number of small changes: mostly renaming. The
  global state management functions were moved to nc4internal.c.

* The global chunk cache variables have been moved into
  NCglobalstate.  As warranted, other global state will be moved
  as well.

* Some misc. problems with the nczarr performance tests were corrected.
2022-01-29 15:27:52 -07:00

135 lines
2.9 KiB
C

/*
Copyright (c) 1998-2018 University Corporation for Atmospheric Research/Unidata
See LICENSE.txt for license information.
*/
#include "config.h"
#include "ncdispatch.h"
#include "ncuri.h"
#include "nclog.h"
#include "ncbytes.h"
#include "ncrc.h"
#include "ncoffsets.h"
#include "ncpathmgr.h"
#include "ncxml.h"
#include "nc4internal.h"
/* Required for getcwd, other functions. */
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
/* Required for getcwd, other functions. */
#ifdef _WIN32
#include <direct.h>
#endif
#if defined(ENABLE_BYTERANGE) || defined(ENABLE_DAP) || defined(ENABLE_DAP4)
#include <curl/curl.h>
#endif
#ifdef ENABLE_S3_SDK
#include "ncs3sdk.h"
#endif
/* Define vectors of zeros and ones for use with various nc_get_varX functions */
/* Note, this form of initialization fails under Cygwin */
size_t NC_coord_zero[NC_MAX_VAR_DIMS] = {0};
size_t NC_coord_one[NC_MAX_VAR_DIMS] = {1};
ptrdiff_t NC_stride_one[NC_MAX_VAR_DIMS] = {1};
/*
static nc_type longtype = (sizeof(long) == sizeof(int)?NC_INT:NC_INT64);
static nc_type ulongtype = (sizeof(unsigned long) == sizeof(unsigned int)?NC_UINT:NC_UINT64);
*/
/* Allow dispatch to do general initialization and finalization */
int
NCDISPATCH_initialize(void)
{
int status = NC_NOERR;
int i;
NCglobalstate* globalstate = NULL;
for(i=0;i<NC_MAX_VAR_DIMS;i++) {
NC_coord_zero[i] = 0;
NC_coord_one[i] = 1;
NC_stride_one[i] = 1;
}
globalstate = NC_getglobalstate(); /* will allocate and clear */
/* Capture temp dir*/
{
char* tempdir = NULL;
#if defined _WIN32 || defined __MSYS__ || defined __CYGWIN__
tempdir = getenv("TEMP");
#else
tempdir = "/tmp";
#endif
if(tempdir == NULL) {
fprintf(stderr,"Cannot find a temp dir; using ./\n");
tempdir = ".";
}
globalstate->tempdir= strdup(tempdir);
}
/* Capture $HOME */
{
char* home = getenv("HOME");
if(home == NULL) {
/* use tempdir */
home = globalstate->tempdir;
}
globalstate->home = strdup(home);
}
/* Capture $CWD */
{
char cwdbuf[4096];
cwdbuf[0] = '\0';
(void)NCgetcwd(cwdbuf,sizeof(cwdbuf));
if(strlen(cwdbuf) == 0) {
/* use tempdir */
strcpy(cwdbuf, globalstate->tempdir);
}
globalstate->cwd = strdup(cwdbuf);
}
ncloginit();
/* Now load RC Files */
ncrc_initialize();
/* Compute type alignments */
NC_compute_alignments();
#if defined(ENABLE_BYTERANGE) || defined(ENABLE_DAP) || defined(ENABLE_DAP4)
/* Initialize curl if it is being used */
{
CURLcode cstat = curl_global_init(CURL_GLOBAL_ALL);
if(cstat != CURLE_OK)
status = NC_ECURL;
}
#endif
return status;
}
int
NCDISPATCH_finalize(void)
{
int status = NC_NOERR;
NC_freeglobalstate();
#if defined(ENABLE_BYTERANGE) || defined(ENABLE_DAP) || defined(ENABLE_DAP4)
curl_global_cleanup();
#endif
#if defined(ENABLE_DAP4)
ncxml_finalize();
#endif
return status;
}