namespace.c 8.07 KB
Newer Older
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1
2
#ifdef HAVE_CONFIG_H
#include "config.h"
Thomas Jahns's avatar
Thomas Jahns committed
3
4
5
6
7
8
#endif

#ifndef _XOPEN_SOURCE
#define _XOPEN_SOURCE 600 /* PTHREAD_MUTEX_RECURSIVE */
#endif

9
#include <limits.h>
10
#include <stdio.h>
Thomas Jahns's avatar
Thomas Jahns committed
11

12
#include "cdi.h"
13
#include "dmemory.h"
14
#include "namespace.h"
Thomas Jahns's avatar
Thomas Jahns committed
15
#include "resource_handle.h"
16
#include "serialize.h"
Thomas Jahns's avatar
Thomas Jahns committed
17
#include "error.h"
18
#include "cdf_int.h"
19
#include "file.h"
20
#include "cdi_int.h"
Thomas Jahns's avatar
Thomas Jahns committed
21
#include "stream_cdf.h"
22

23
static unsigned nNamespaces = 1;
24
static int activeNamespace = 0;
25

26
#ifdef HAVE_LIBNETCDF
27
28
29
30
#define CDI_NETCDF_SWITCHES                     \
  { .func = (void (*)()) nc__create },          \
  { .func = (void (*)()) cdf_def_var_serial },  \
  { .func = (void (*)()) cdfDefTimestep },      \
31
  { .func = (void (*)()) cdfDefCoordinateVars }
32

33
34
35
36
#else
#define CDI_NETCDF_SWITCHES
#endif

37
38
#define defaultSwitches {                                   \
    { .func = (void (*)()) cdiAbortC_serial },              \
39
    { .func = (void (*)()) cdiWarning },                    \
40
41
42
43
44
45
46
47
    { .func = (void (*)()) serializeGetSizeInCore },        \
    { .func = (void (*)()) serializePackInCore },           \
    { .func = (void (*)()) serializeUnpackInCore },         \
    { .func = (void (*)()) fileOpen_serial },               \
    { .func = (void (*)()) fileWrite },                     \
    { .func = (void (*)()) fileClose_serial },              \
    { .func = (void (*)()) cdiStreamOpenDefaultDelegate },  \
    { .func = (void (*)()) cdiStreamDefVlist_ },            \
48
    { .func = (void (*)()) cdiStreamSetupVlist_ },          \
49
    { .func = (void (*)()) cdiStreamWriteVar_ },            \
Thomas Jahns's avatar
Thomas Jahns committed
50
    { .func = (void (*)()) cdiStreamWriteVarChunk_ },       \
51
    { .func = (void (*)()) 0 },                             \
52
    { .func = (void (*)()) 0 },                             \
53
    { .func = (void (*)()) cdiStreamCloseDefaultDelegate }, \
Thomas Jahns's avatar
Thomas Jahns committed
54
    { .func = (void (*)()) cdiStreamDefTimestep_ }, \
55
56
57
    { .func = (void (*)()) cdiStreamSync_ },                \
    CDI_NETCDF_SWITCHES                        \
    }
58

59
#if defined (SX) || defined (__cplusplus)
60
61
static const union namespaceSwitchValue
  defaultSwitches_[NUM_NAMESPACE_SWITCH] = defaultSwitches;
62
#endif
63

Thomas Jahns's avatar
Thomas Jahns committed
64
65
66
67
68
enum namespaceStatus {
  NAMESPACE_STATUS_INUSE,
  NAMESPACE_STATUS_UNUSED,
};

69
static struct Namespace
70
{
Thomas Jahns's avatar
Thomas Jahns committed
71
  enum namespaceStatus resStage;
72
  union namespaceSwitchValue switches[NUM_NAMESPACE_SWITCH];
73
} initialNamespace = {
Thomas Jahns's avatar
Thomas Jahns committed
74
  .resStage = NAMESPACE_STATUS_INUSE,
75
76
  .switches = defaultSwitches
};
77

78
static struct Namespace *namespaces = &initialNamespace;
79

80
static unsigned namespacesSize = 1;
Thomas Jahns's avatar
Thomas Jahns committed
81
82
83
84

#if  defined  (HAVE_LIBPTHREAD)
#  include <pthread.h>

Thomas Jahns's avatar
Thomas Jahns committed
85
86
87
88
89
90
91
92
93
94
95
96
static pthread_once_t  namespaceOnce = PTHREAD_ONCE_INIT;
static pthread_mutex_t namespaceMutex;

static void
namespaceInitialize(void)
{
  pthread_mutexattr_t ma;
  pthread_mutexattr_init(&ma);
  pthread_mutexattr_settype(&ma, PTHREAD_MUTEX_RECURSIVE);
  pthread_mutex_init(&namespaceMutex, &ma);
  pthread_mutexattr_destroy(&ma);
}
Thomas Jahns's avatar
Thomas Jahns committed
97
98
99

#  define NAMESPACE_LOCK()         pthread_mutex_lock(&namespaceMutex)
#  define NAMESPACE_UNLOCK()       pthread_mutex_unlock(&namespaceMutex)
Thomas Jahns's avatar
Thomas Jahns committed
100
101
102
#  define NAMESPACE_INIT()         pthread_once(&namespaceOnce, \
                                                namespaceInitialize)

Thomas Jahns's avatar
Thomas Jahns committed
103
104
105

#else

Thomas Jahns's avatar
Thomas Jahns committed
106
#  define NAMESPACE_INIT() do { } while (0)
Thomas Jahns's avatar
Thomas Jahns committed
107
108
109
110
111
112
#  define NAMESPACE_LOCK()
#  define NAMESPACE_UNLOCK()

#endif


113
114
115
enum {
  intbits = sizeof(int) * CHAR_BIT,
  nspbits = 4,
116
  idxbits = intbits - nspbits,
Thomas Jahns's avatar
Thomas Jahns committed
117
  nspmask = (int)((( (unsigned)1 << nspbits ) - 1) << idxbits),
Deike Kleberg's avatar
Deike Kleberg committed
118
  idxmask = ( 1 << idxbits ) - 1,
119
120
121
122
};

enum {
  NUM_NAMESPACES = 1 << nspbits,
123
  NUM_IDX = 1 << idxbits,
124
};
125

126
127
128

int namespaceIdxEncode ( namespaceTuple_t tin )
{
Deike Kleberg's avatar
Deike Kleberg committed
129
  xassert ( tin.nsp < NUM_NAMESPACES && tin.idx < NUM_IDX);
130
  return ( tin.nsp << idxbits ) + tin.idx;
131
}
132

133
int namespaceIdxEncode2 ( int nsp, int idx )
134
{
Deike Kleberg's avatar
Deike Kleberg committed
135
  xassert(nsp < NUM_NAMESPACES && idx < NUM_IDX);
136
137
138
  return ( nsp << idxbits ) + idx;
}

139

Deike Kleberg's avatar
Deike Kleberg committed
140
namespaceTuple_t namespaceResHDecode ( int resH )
141
142
143
{
  namespaceTuple_t tin;

Deike Kleberg's avatar
Deike Kleberg committed
144
145
  tin.idx = resH & idxmask;
  tin.nsp = (int)(((unsigned)( resH & nspmask )) >> idxbits);
146
147
148
149

  return tin;
}

Thomas Jahns's avatar
Thomas Jahns committed
150
int
151
namespaceNew()
152
{
Thomas Jahns's avatar
Thomas Jahns committed
153
  int newNamespaceID = -1;
Thomas Jahns's avatar
Thomas Jahns committed
154
  NAMESPACE_INIT();
Thomas Jahns's avatar
Thomas Jahns committed
155
156
  NAMESPACE_LOCK();
  if (namespacesSize > nNamespaces)
157
    {
Thomas Jahns's avatar
Thomas Jahns committed
158
      /* namespace is already available and only needs reinitialization */
159
      for (unsigned i = 0; i < namespacesSize; ++i)
Thomas Jahns's avatar
Thomas Jahns committed
160
        if (namespaces[i].resStage == NAMESPACE_STATUS_UNUSED)
Thomas Jahns's avatar
Thomas Jahns committed
161
          {
162
            newNamespaceID = (int)i;
Thomas Jahns's avatar
Thomas Jahns committed
163
164
            break;
          }
165
    }
Thomas Jahns's avatar
Thomas Jahns committed
166
167
168
  else if (namespacesSize == 1)
    {
      /* make room for additional namespace */
169
      struct Namespace *newNameSpaces
170
        = (struct Namespace *) Malloc(((size_t)namespacesSize + 1) * sizeof (namespaces[0]));
Thomas Jahns's avatar
Thomas Jahns committed
171
172
173
174
175
176
177
178
      memcpy(newNameSpaces, namespaces, sizeof (namespaces[0]));
      namespaces = newNameSpaces;
      ++namespacesSize;
      newNamespaceID = 1;
    }
  else if (namespacesSize < NUM_NAMESPACES)
    {
      /* make room for additional namespace */
179
      newNamespaceID = (int)namespacesSize;
180
      namespaces
181
        = (struct Namespace *) Realloc(namespaces, ((size_t)namespacesSize + 1) * sizeof (namespaces[0]));
Thomas Jahns's avatar
Thomas Jahns committed
182
183
184
185
186
187
188
189
190
      ++namespacesSize;
    }
  else /* implicit: namespacesSize >= NUM_NAMESPACES */
    {
      NAMESPACE_UNLOCK();
      return -1;
    }
  xassert(newNamespaceID >= 0 && newNamespaceID < NUM_NAMESPACES);
  ++nNamespaces;
Thomas Jahns's avatar
Thomas Jahns committed
191
192
  namespaces[newNamespaceID].resStage = NAMESPACE_STATUS_INUSE;
#if defined (SX) || defined (__cplusplus)
193
194
  memcpy(namespaces[newNamespaceID].switches,
         defaultSwitches_,
195
         sizeof (namespaces[newNamespaceID].switches));
Thomas Jahns's avatar
Thomas Jahns committed
196
#else
197
198
199
    memcpy(namespaces[newNamespaceID].switches,
           (union namespaceSwitchValue[NUM_NAMESPACE_SWITCH])defaultSwitches,
           sizeof (namespaces[newNamespaceID].switches));
Thomas Jahns's avatar
Thomas Jahns committed
200
#endif
Thomas Jahns's avatar
Thomas Jahns committed
201
202
203
  reshListCreate(newNamespaceID);
  NAMESPACE_UNLOCK();
  return newNamespaceID;
204
205
}

Thomas Jahns's avatar
Thomas Jahns committed
206
207
208
void
namespaceDelete(int namespaceID)
{
Thomas Jahns's avatar
Thomas Jahns committed
209
  NAMESPACE_INIT();
Thomas Jahns's avatar
Thomas Jahns committed
210
  NAMESPACE_LOCK();
211
212
  xassert(namespaceID >= 0 && (unsigned)namespaceID < namespacesSize
          && nNamespaces);
Thomas Jahns's avatar
Thomas Jahns committed
213
  reshListDestruct(namespaceID);
Thomas Jahns's avatar
Thomas Jahns committed
214
  namespaces[namespaceID].resStage = NAMESPACE_STATUS_UNUSED;
Thomas Jahns's avatar
Thomas Jahns committed
215
216
217
  --nNamespaces;
  NAMESPACE_UNLOCK();
}
218

219
220
int namespaceGetNumber ()
{
221
  return (int)nNamespaces;
222
223
224
}


225
void namespaceSetActive ( int nId )
226
{
227
  xassert((unsigned)nId < namespacesSize
Thomas Jahns's avatar
Thomas Jahns committed
228
          && namespaces[nId].resStage != NAMESPACE_STATUS_UNUSED);
229
230
231
  activeNamespace = nId;
}

232

233
234
235
236
int namespaceGetActive ()
{
  return activeNamespace;
}
237

238
int namespaceAdaptKey ( int originResH, int originNamespace )
239
240
241
242
{
  namespaceTuple_t tin;
  int nsp;

243
  if ( originResH == CDI_UNDEFID ) return CDI_UNDEFID;
244

245
246
  tin.idx = originResH & idxmask;
  tin.nsp = (int)(((unsigned)( originResH & nspmask )) >> idxbits);
247

248
  xassert ( tin.nsp == originNamespace );
249
250

  nsp = namespaceGetActive ();
Uwe Schulzweida's avatar
Uwe Schulzweida committed
251

252
253
  return namespaceIdxEncode2 ( nsp, tin.idx );
}
Deike Kleberg's avatar
Deike Kleberg committed
254
255


256
int namespaceAdaptKey2 ( int originResH )
Deike Kleberg's avatar
Deike Kleberg committed
257
258
259
260
{
  namespaceTuple_t tin;
  int nsp;

261
  if ( originResH == CDI_UNDEFID ) return CDI_UNDEFID;
Deike Kleberg's avatar
Deike Kleberg committed
262

263
264
  tin.idx = originResH & idxmask;
  tin.nsp = (int)(((unsigned)( originResH & nspmask )) >> idxbits);
Deike Kleberg's avatar
Deike Kleberg committed
265
266

  nsp = namespaceGetActive ();
Uwe Schulzweida's avatar
Uwe Schulzweida committed
267

Deike Kleberg's avatar
Deike Kleberg committed
268
269
270
  return namespaceIdxEncode2 ( nsp, tin.idx );
}

271
void namespaceSwitchSet(enum namespaceSwitch sw, union namespaceSwitchValue value)
272
273
274
275
276
277
{
  xassert(sw > NSSWITCH_NO_SUCH_SWITCH && sw < NUM_NAMESPACE_SWITCH);
  int nsp = namespaceGetActive();
  namespaces[nsp].switches[sw] = value;
}

278
union namespaceSwitchValue namespaceSwitchGet(enum namespaceSwitch sw)
279
280
281
282
283
284
{
  xassert(sw > NSSWITCH_NO_SUCH_SWITCH && sw < NUM_NAMESPACE_SWITCH);
  int nsp = namespaceGetActive();
  return namespaces[nsp].switches[sw];
}

Thomas Jahns's avatar
Thomas Jahns committed
285
286
void cdiReset(void)
{
Thomas Jahns's avatar
Thomas Jahns committed
287
  NAMESPACE_INIT();
Thomas Jahns's avatar
Thomas Jahns committed
288
  NAMESPACE_LOCK();
289
  for (unsigned namespaceID = 0; namespaceID < namespacesSize; ++namespaceID)
Thomas Jahns's avatar
Thomas Jahns committed
290
    if (namespaces[namespaceID].resStage != NAMESPACE_STATUS_UNUSED)
291
      namespaceDelete((int)namespaceID);
Thomas Jahns's avatar
Thomas Jahns committed
292
293
  if (namespaces != &initialNamespace)
    {
294
      Free(namespaces);
Thomas Jahns's avatar
Thomas Jahns committed
295
      namespaces = &initialNamespace;
Thomas Jahns's avatar
Thomas Jahns committed
296
      namespaces[0].resStage = NAMESPACE_STATUS_UNUSED;
Thomas Jahns's avatar
Thomas Jahns committed
297
    }
Thomas Jahns's avatar
Thomas Jahns committed
298
  namespacesSize = 1;
Thomas Jahns's avatar
Thomas Jahns committed
299
  nNamespaces = 0;
Thomas Jahns's avatar
Thomas Jahns committed
300
301
302
  NAMESPACE_UNLOCK();
}

303
304
305
306
307
308
309
310
311
/*
 * Local Variables:
 * c-file-style: "Java"
 * c-basic-offset: 2
 * indent-tabs-mode: nil
 * show-trailing-whitespace: t
 * require-trailing-newline: t
 * End:
 */