namespace.c 1.51 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
enum {
  intbits = sizeof(int) * CHAR_BIT,
  nspbits = 4,
14
15
16
  idxbits = intbits - nspbits,
  nspmask = ((1<<nspbits) - 1) << idxbits,
  idxmask = (1<<idxbits) - 1,
17
18
19
20
};

enum {
  NUM_NAMESPACES = 1 << nspbits,
21
  NUM_IDX = 1 << idxbits,
22
};
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
  assert ( tin.nsp < NUM_NAMESPACES && tin.idx < NUM_IDX);
45
  return ( tin.nsp << idxbits ) + tin.idx;
46
}
47

48
49
50
51
52
53
int namespaceIdxEncode2 ( int idx, int nsp )
{
  assert(nsp < NUM_NAMESPACES && idx < NUM_IDX);
  return ( nsp << idxbits ) + idx;
}

54
55
56
57
58

namespaceTuple_t namespaceIdxDecode ( int code )
{
  namespaceTuple_t tin;

59
60
  tin.idx = code & idxmask;
  tin.nsp = (int)(((unsigned)( code & nspmask )) >> idxbits);
61
62
63
64
65

  return tin;
}


66
67
void namespaceSetNumber ( int nspn )
{
68
  assert(nspn <= NUM_NAMESPACES && nspn >= 1 );
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
  nNamespaces = nspn;
}

int namespaceGetNumber ()
{
  return nNamespaces;
}


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

  activeNamespace = nId;
}

86

87
88
89
90
int namespaceGetActive ()
{
  return activeNamespace;
}