namespace.c 8.09 KB
Newer Older
Thomas Jahns's avatar
Thomas Jahns committed
1
2
3
4
5
6
7
8
#if defined (HAVE_CONFIG_H)
#  include "config.h"
#endif

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

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

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

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

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

34
35
36
37
#else
#define CDI_NETCDF_SWITCHES
#endif

38
39
#define defaultSwitches {                                   \
    { .func = (void (*)()) cdiAbortC_serial },              \
40
    { .func = (void (*)()) cdiWarning },                    \
41
42
43
44
45
46
47
48
49
50
    { .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_ },            \
    { .func = (void (*)()) cdiStreamWriteVar_ },            \
    { .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

64
static struct Namespace
65
66
{
  statusCode resStage;
67
  union namespaceSwitchValue switches[NUM_NAMESPACE_SWITCH];
68
69
70
71
} initialNamespace = {
  .resStage = STAGE_DEFINITION,
  .switches = defaultSwitches
};
72

73
static struct Namespace *namespaces = &initialNamespace;
74

75
static unsigned namespacesSize = 1;
Thomas Jahns's avatar
Thomas Jahns committed
76
77
78
79

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

Thomas Jahns's avatar
Thomas Jahns committed
80
81
82
83
84
85
86
87
88
89
90
91
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
92
93
94

#  define NAMESPACE_LOCK()         pthread_mutex_lock(&namespaceMutex)
#  define NAMESPACE_UNLOCK()       pthread_mutex_unlock(&namespaceMutex)
Thomas Jahns's avatar
Thomas Jahns committed
95
96
97
#  define NAMESPACE_INIT()         pthread_once(&namespaceOnce, \
                                                namespaceInitialize)

Thomas Jahns's avatar
Thomas Jahns committed
98
99
100

#else

Thomas Jahns's avatar
Thomas Jahns committed
101
#  define NAMESPACE_INIT() do { } while (0)
Thomas Jahns's avatar
Thomas Jahns committed
102
103
104
105
106
107
#  define NAMESPACE_LOCK()
#  define NAMESPACE_UNLOCK()

#endif


108
109
110
enum {
  intbits = sizeof(int) * CHAR_BIT,
  nspbits = 4,
111
  idxbits = intbits - nspbits,
Deike Kleberg's avatar
Deike Kleberg committed
112
113
  nspmask = (( 1 << nspbits ) - 1) << idxbits,
  idxmask = ( 1 << idxbits ) - 1,
114
115
116
117
};

enum {
  NUM_NAMESPACES = 1 << nspbits,
118
  NUM_IDX = 1 << idxbits,
119
};
120

121
122
123

int namespaceIdxEncode ( namespaceTuple_t tin )
{
Deike Kleberg's avatar
Deike Kleberg committed
124
  xassert ( tin.nsp < NUM_NAMESPACES && tin.idx < NUM_IDX);
125
  return ( tin.nsp << idxbits ) + tin.idx;
126
}
127

128
int namespaceIdxEncode2 ( int nsp, int idx )
129
{
Deike Kleberg's avatar
Deike Kleberg committed
130
  xassert(nsp < NUM_NAMESPACES && idx < NUM_IDX);
131
132
133
  return ( nsp << idxbits ) + idx;
}

134

Deike Kleberg's avatar
Deike Kleberg committed
135
namespaceTuple_t namespaceResHDecode ( int resH )
136
137
138
{
  namespaceTuple_t tin;

Deike Kleberg's avatar
Deike Kleberg committed
139
140
  tin.idx = resH & idxmask;
  tin.nsp = (int)(((unsigned)( resH & nspmask )) >> idxbits);
141
142
143
144

  return tin;
}

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

Thomas Jahns's avatar
Thomas Jahns committed
201
202
203
void
namespaceDelete(int namespaceID)
{
Thomas Jahns's avatar
Thomas Jahns committed
204
  NAMESPACE_INIT();
Thomas Jahns's avatar
Thomas Jahns committed
205
  NAMESPACE_LOCK();
206
207
  xassert(namespaceID >= 0 && (unsigned)namespaceID < namespacesSize
          && nNamespaces);
Thomas Jahns's avatar
Thomas Jahns committed
208
209
210
211
212
  reshListDestruct(namespaceID);
  namespaces[namespaceID].resStage = STAGE_UNUSED;
  --nNamespaces;
  NAMESPACE_UNLOCK();
}
213

214
215
int namespaceGetNumber ()
{
216
  return (int)nNamespaces;
217
218
219
}


220
void namespaceSetActive ( int nId )
221
{
222
  xassert((unsigned)nId < namespacesSize
Thomas Jahns's avatar
Thomas Jahns committed
223
          && namespaces[nId].resStage != STAGE_UNUSED);
224
225
226
  activeNamespace = nId;
}

227

228
229
230
231
int namespaceGetActive ()
{
  return activeNamespace;
}
232

233
int namespaceAdaptKey ( int originResH, int originNamespace )
234
235
236
237
{
  namespaceTuple_t tin;
  int nsp;

238
  if ( originResH == CDI_UNDEFID ) return CDI_UNDEFID;
239

240
241
  tin.idx = originResH & idxmask;
  tin.nsp = (int)(((unsigned)( originResH & nspmask )) >> idxbits);
242

243
  xassert ( tin.nsp == originNamespace );
244
245

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

247
248
  return namespaceIdxEncode2 ( nsp, tin.idx );
}
Deike Kleberg's avatar
Deike Kleberg committed
249
250


251
int namespaceAdaptKey2 ( int originResH )
Deike Kleberg's avatar
Deike Kleberg committed
252
253
254
255
{
  namespaceTuple_t tin;
  int nsp;

256
  if ( originResH == CDI_UNDEFID ) return CDI_UNDEFID;
Deike Kleberg's avatar
Deike Kleberg committed
257

258
259
  tin.idx = originResH & idxmask;
  tin.nsp = (int)(((unsigned)( originResH & nspmask )) >> idxbits);
Deike Kleberg's avatar
Deike Kleberg committed
260
261

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

Deike Kleberg's avatar
Deike Kleberg committed
263
264
265
  return namespaceIdxEncode2 ( nsp, tin.idx );
}

266

267
void namespaceDefResStatus ( statusCode argResStatus )
268
269
{
  int nsp = namespaceGetActive ();
270
  namespaces[nsp].resStage = argResStatus;
271
272
273
}


274
statusCode namespaceInqResStatus ( void )
275
276
{
  int nsp = namespaceGetActive ();
277
  return namespaces[nsp].resStage;
278
279
}

280
void namespaceSwitchSet(enum namespaceSwitch sw, union namespaceSwitchValue value)
281
282
283
284
285
286
{
  xassert(sw > NSSWITCH_NO_SUCH_SWITCH && sw < NUM_NAMESPACE_SWITCH);
  int nsp = namespaceGetActive();
  namespaces[nsp].switches[sw] = value;
}

287
union namespaceSwitchValue namespaceSwitchGet(enum namespaceSwitch sw)
288
289
290
291
292
293
{
  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
294
295
void cdiReset(void)
{
Thomas Jahns's avatar
Thomas Jahns committed
296
  NAMESPACE_INIT();
Thomas Jahns's avatar
Thomas Jahns committed
297
  NAMESPACE_LOCK();
298
  for (unsigned namespaceID = 0; namespaceID < namespacesSize; ++namespaceID)
Thomas Jahns's avatar
Thomas Jahns committed
299
    if (namespaces[namespaceID].resStage != STAGE_UNUSED)
300
      namespaceDelete((int)namespaceID);
Thomas Jahns's avatar
Thomas Jahns committed
301
302
303
304
305
  if (namespaces != &initialNamespace)
    {
      free(namespaces);
      namespaces = &initialNamespace;
    }
Thomas Jahns's avatar
Thomas Jahns committed
306
307
308
309
310
311
  namespacesSize = 1;
  nNamespaces = 1;
  activeNamespace = 0;
  NAMESPACE_UNLOCK();
}

312
313
314
315
316
317
318
319
320
/*
 * Local Variables:
 * c-file-style: "Java"
 * c-basic-offset: 2
 * indent-tabs-mode: nil
 * show-trailing-whitespace: t
 * require-trailing-newline: t
 * End:
 */