1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
|
/* $Header: /local/src/CVS/nickle/atom.c,v 1.10 2004/02/27 03:50:15 keithp Exp $ */
/*
* Copyright © 1988-2004 Keith Packard and Bart Massey.
* All Rights Reserved. See the file COPYING in this directory
* for licensing information.
*/
#include "nickle.h"
typedef struct _atom {
DataType *data;
struct _atom *next;
} AtomEntry;
#define AtomEntryName(ae) ((char *) ((ae) + 1))
static void
AtomEntryMark (void *object)
{
;
}
DataType AtomEntryType = { AtomEntryMark, 0, "AtomEntryType" };
# define HASHSIZE 63
typedef struct _atomTable {
DataType *data;
AtomEntry *hash[HASHSIZE];
} AtomTable;
static void
AtomTableMark (void *object)
{
AtomTable *table = object;
int i;
AtomEntry *atom;
for (i = 0; i < HASHSIZE; i++)
for (atom = table->hash[i]; atom; atom = atom->next)
MemReference (atom);
}
DataType AtomTableType = { AtomTableMark, 0, "AtomTableType" };
AtomTable *atomTable;
int
AtomInit (void)
{
ENTER ();
atomTable = ALLOCATE (&AtomTableType, sizeof (AtomTable));
memset (atomTable->hash, '\0', sizeof (atomTable->hash));
MemAddRoot (atomTable);
EXIT ();
return 1;
}
static int
hash (char *name)
{
int h;
h = 0;
while (*name)
h += *name++;
if (h < 0)
h = -h;
return h % HASHSIZE;
}
Atom
AtomId (char *name)
{
AtomEntry **bucket = &atomTable->hash[hash(name)];
AtomEntry *atomEntry;
for (atomEntry = *bucket; atomEntry; atomEntry = atomEntry->next)
if (!strcmp (name, AtomEntryName(atomEntry)))
break;
if (!atomEntry)
{
ENTER ();
atomEntry = ALLOCATE (&AtomEntryType, sizeof (AtomEntry) + strlen (name) + 1);
atomEntry->next = *bucket;
*bucket = atomEntry;
strcpy (AtomEntryName(atomEntry), name);
EXIT();
}
return AtomEntryName (atomEntry);
}
static void
AtomListMark (void *object)
{
AtomListPtr al = object;
MemReference (al->next);
}
DataType AtomListType = { AtomListMark, 0, "AtomListType" };
AtomListPtr
NewAtomList (AtomListPtr next, Atom atom)
{
ENTER ();
AtomListPtr al;
al = ALLOCATE (&AtomListType, sizeof (AtomList));
al->next = next;
al->atom = atom;
RETURN (al);
}
|