namespace.c 1.98 KB
Newer Older
1
2
#include <assert.h>
#include <limits.h>
3
#include <stdlib.h>
4
#include <stdio.h>
5
#include "cdi.h"
6
#include "namespace.h"
7
8
#include "pio_util.h"

9
10

static int nNamespaces = 1;
11
12
13
static int activeNamespace = 0;
static int HLF = 1;
static int * hasLocalFile = &HLF;
14

15
16
17
enum {
  intbits = sizeof(int) * CHAR_BIT,
  nspbits = 4,
18
19
20
  idxbits = intbits - nspbits,
  nspmask = ((1<<nspbits) - 1) << idxbits,
  idxmask = (1<<idxbits) - 1,
21
22
23
24
};

enum {
  NUM_NAMESPACES = 1 << nspbits,
25
  NUM_IDX = 1 << idxbits,
26
};
27

28
29
30

void namespaceShowbits ( int n, char *name )
{
31
32
33
  int i;
  unsigned mask;
  char bitvalues[intbits + 1];
34

35
  mask = 1;
36
  for ( i = 0; i < intbits; i++ )
37
    {
38
39
      bitvalues[i] = ((unsigned)n & mask) ? '1':'0';
      mask <<= 1;
40
    }
41
42
  bitvalues[intbits] = '\0';
  fprintf (stdout, "%s: %s\n", name, bitvalues );
43
44
45
46
47
}


int namespaceIdxEncode ( namespaceTuple_t tin )
{
48
  assert ( tin.nsp < NUM_NAMESPACES && tin.idx < NUM_IDX);
49
  return ( tin.nsp << idxbits ) + tin.idx;
50
}
51

52
int namespaceIdxEncode2 ( int nsp, int idx )
53
54
55
56
57
{
  assert(nsp < NUM_NAMESPACES && idx < NUM_IDX);
  return ( nsp << idxbits ) + idx;
}

58
59
60
61
62

namespaceTuple_t namespaceIdxDecode ( int code )
{
  namespaceTuple_t tin;

63
64
  tin.idx = code & idxmask;
  tin.nsp = (int)(((unsigned)( code & nspmask )) >> idxbits);
65
66
67
68
69

  return tin;
}


70
void namespaceInit ( int nspn, int * argHasLocalFile )
71
{
72
  int i;
73
  assert(nspn <= NUM_NAMESPACES && nspn >= 1 );
74
  nNamespaces = nspn;
75
76
77
78
79
80
81
82
83
84
85
86

  if ( nspn > 1 )
    {
      hasLocalFile = xmalloc ( nspn * sizeof ( int ));
      for ( i = 0; i < nspn; i++ )  hasLocalFile[i] = argHasLocalFile[i];
    }
}


void namespaceCleanup ( void )
{
  if ( nNamespaces > 1 ) free ( hasLocalFile );
87
88
}

89

90
91
92
93
94
95
96
97
98
99
100
101
102
103
int namespaceGetNumber ()
{
  return nNamespaces;
}


void namespaceSetActive ( int nId )
{
  if ( nId >= nNamespaces || nId < 0 )
    abort ();

  activeNamespace = nId;
}

104

105
106
107
108
int namespaceGetActive ()
{
  return activeNamespace;
}
109
110
111
112
113
114
115
116
117


int namespaceHasLocalFile ( int nId )
{
  if ( nId >= nNamespaces || nId < 0 )
    abort ();
  
  return hasLocalFile[nId];
}