This is Unofficial EPICS BASE Doxygen Site
symtab.c
Go to the documentation of this file.
1 /*************************************************************************\
2 * Copyright (c) 2002 The University of Chicago, as Operator of Argonne
3 * National Laboratory.
4 * Copyright (c) 2002 The Regents of the University of California, as
5 * Operator of Los Alamos National Laboratory.
6 * EPICS BASE is distributed subject to a Software License Agreement found
7 * in file LICENSE that is included with this distribution.
8 \*************************************************************************/
9 #include "defs.h"
10 
11 
12 /* TABLE_SIZE is the number of entries in the symbol table. */
13 /* TABLE_SIZE must be a power of two. */
14 
15 #define TABLE_SIZE 1024
16 
17 
21 
22 
23 static int
24 hash(char *name)
25 {
26  char *s;
27  int c, k;
28 
29  assert(name && *name);
30  s = name;
31  k = *s;
32  while ((c = *++s))
33  k = (31*k + c) & (TABLE_SIZE - 1);
34 
35  return (k);
36 }
37 
38 
39 bucket *
40 make_bucket(char *name)
41 {
42  bucket *bp;
43 
44  assert(name);
45  bp = (bucket *) MALLOC(sizeof(bucket));
46  if (bp == 0) no_space();
47  bp->link = 0;
48  bp->next = 0;
49  bp->name = MALLOC(strlen(name) + 1);
50  if (bp->name == 0) no_space();
51  bp->tag = 0;
52  bp->value = UNDEFINED;
53  bp->index = 0;
54  bp->prec = 0;
55  bp-> class = UNKNOWN;
56  bp->assoc = TOKEN;
57 
58  if (bp->name == 0) no_space();
59  strcpy(bp->name, name);
60 
61  return (bp);
62 }
63 
64 
65 bucket *
66 lookup(char *name)
67 {
68  bucket *bp, **bpp;
69 
70  bpp = symbol_table + hash(name);
71  bp = *bpp;
72 
73  while (bp)
74  {
75  if (strcmp(name, bp->name) == 0) return (bp);
76  bpp = &bp->link;
77  bp = *bpp;
78  }
79 
80  *bpp = bp = make_bucket(name);
81  last_symbol->next = bp;
82  last_symbol = bp;
83 
84  return (bp);
85 }
86 
87 void
89 {
90  int i;
91  bucket *bp;
92 
93  symbol_table = (bucket **) MALLOC(TABLE_SIZE*sizeof(bucket *));
94  if (symbol_table == 0) no_space();
95  for (i = 0; i < TABLE_SIZE; i++)
96  symbol_table[i] = 0;
97 
98  bp = make_bucket("error");
99  bp->index = 1;
100  bp->class = TERM;
101 
102  first_symbol = bp;
103  last_symbol = bp;
104  symbol_table[hash("error")] = bp;
105 }
106 
107 
108 void
110 {
111  FREE(symbol_table);
112  symbol_table = 0;
113 }
114 
115 
116 void
118 {
119  bucket *p, *q;
120 
121  for (p = first_symbol; p; p = q)
122  {
123  q = p->next;
124  FREE(p);
125  }
126 }
void free_symbol_table(void)
Definition: symtab.c:109
void free_symbols(void)
Definition: symtab.c:117
char * tag
Definition: defs.h:125
#define TOKEN
Definition: defs.h:64
#define assert(exp)
Declare that a condition should be true.
Definition: epicsAssert.h:70
#define MALLOC(n)
Definition: defs.h:111
int i
Definition: scan.c:967
bucket ** symbol_table
Definition: symtab.c:18
#define UNDEFINED
Definition: defs.h:85
char * name
Definition: defs.h:124
#define UNKNOWN
Definition: defs.h:78
#define TERM
Definition: defs.h:79
short prec
Definition: defs.h:128
void create_symbol_table(void)
Definition: symtab.c:88
struct bucket * link
Definition: defs.h:122
bucket * make_bucket(char *name)
Definition: symtab.c:40
char class
Definition: defs.h:129
bucket * first_symbol
Definition: symtab.c:19
bucket * last_symbol
Definition: symtab.c:20
short index
Definition: defs.h:127
#define FREE(x)
Definition: defs.h:110
struct bucket * next
Definition: defs.h:123
#define TABLE_SIZE
Definition: symtab.c:15
short value
Definition: defs.h:126
void no_space(void) NORETURN
Definition: error.c:23
char assoc
Definition: defs.h:130
bucket * lookup(char *name)
Definition: symtab.c:66
Internal: Hash table structure.
Definition: bucketLib.h:48