namespace.c 1.39 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
9
10

static int nNamespaces = 1;
static int activeNamespace;

11
12
13
14
15
16
17
18
19
20
21
22
enum {
  intbits = sizeof(int) * CHAR_BIT,
  nspbits = 4,
  codebits = intbits - nspbits,
  nspmask = ((1<<nspbits) - 1) << codebits,
  codemask = (1<<codebits) - 1,
};

enum {
  NUM_NAMESPACES = 1 << nspbits,
  NUM_CODES = 1 << codebits,
};
23

24
25
26

void namespaceShowbits ( int n, char *name )
{
27
28
29
  int i;
  unsigned mask;
  char bitvalues[intbits + 1];
30

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


int namespaceIdxEncode ( namespaceTuple_t tin )
{
44
45
46
  assert(tin.nsp < NUM_NAMESPACES && tin.idx < NUM_CODES);
  return ( tin.nsp << codebits ) + tin.idx;
}
47
48
49
50
51
52


namespaceTuple_t namespaceIdxDecode ( int code )
{
  namespaceTuple_t tin;

53
54
  tin.idx = code & codemask;
  tin.nsp = (int)(((unsigned)( code & nspmask )) >> codebits);
55
56
57
58
59

  return tin;
}


60
61
void namespaceSetNumber ( int nspn )
{
62
  assert(nspn <= NUM_NAMESPACES && nspn >= 1 );
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
  nNamespaces = nspn;
}

int namespaceGetNumber ()
{
  return nNamespaces;
}


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

  activeNamespace = nId;
}

80

81
82
83
84
int namespaceGetActive ()
{
  return activeNamespace;
}