1da2e3ebdSchin /***********************************************************************
2da2e3ebdSchin * *
3da2e3ebdSchin * This software is part of the ast package *
4*3e14f97fSRoger A. Faulkner * Copyright (c) 1985-2010 AT&T Intellectual Property *
5da2e3ebdSchin * and is licensed under the *
6da2e3ebdSchin * Common Public License, Version 1.0 *
77c2fbfb3SApril Chin * by AT&T Intellectual Property *
8da2e3ebdSchin * *
9da2e3ebdSchin * A copy of the License is available at *
10da2e3ebdSchin * http://www.opensource.org/licenses/cpl1.0.txt *
11da2e3ebdSchin * (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9) *
12da2e3ebdSchin * *
13da2e3ebdSchin * Information and Software Systems Research *
14da2e3ebdSchin * AT&T Research *
15da2e3ebdSchin * Florham Park NJ *
16da2e3ebdSchin * *
17da2e3ebdSchin * Glenn Fowler <gsf@research.att.com> *
18da2e3ebdSchin * David Korn <dgk@research.att.com> *
19da2e3ebdSchin * Phong Vo <kpv@research.att.com> *
20da2e3ebdSchin * *
21da2e3ebdSchin ***********************************************************************/
22da2e3ebdSchin #pragma prototyped
23da2e3ebdSchin /*
24da2e3ebdSchin * Glenn Fowler
25da2e3ebdSchin * AT&T Bell Laboratories
26da2e3ebdSchin *
27da2e3ebdSchin * hash table library
28da2e3ebdSchin */
29da2e3ebdSchin
30da2e3ebdSchin #include "hashlib.h"
31da2e3ebdSchin
32da2e3ebdSchin /*
33da2e3ebdSchin * dump HASH_* flags
34da2e3ebdSchin */
35da2e3ebdSchin
36da2e3ebdSchin static void
dumpflags(register int flags)37da2e3ebdSchin dumpflags(register int flags)
38da2e3ebdSchin {
39da2e3ebdSchin if (flags & HASH_ALLOCATE) sfprintf(sfstderr, "allocate ");
40da2e3ebdSchin if (flags & HASH_BUCKET) sfprintf(sfstderr, "bucket ");
41da2e3ebdSchin if (flags & HASH_FIXED) sfprintf(sfstderr, "fixed ");
42da2e3ebdSchin if (flags & HASH_HASHED) sfprintf(sfstderr, "hashed ");
43da2e3ebdSchin if (flags & HASH_RESIZE) sfprintf(sfstderr, "resize ");
44da2e3ebdSchin if (flags & HASH_STATIC) sfprintf(sfstderr, "static ");
45da2e3ebdSchin if (flags & HASH_VALUE) sfprintf(sfstderr, "value ");
46da2e3ebdSchin }
47da2e3ebdSchin
48da2e3ebdSchin /*
49da2e3ebdSchin * dump hash table bucket info
50da2e3ebdSchin */
51da2e3ebdSchin
52da2e3ebdSchin static void
dumpbucket(register Hash_table_t * tab,int flags)53da2e3ebdSchin dumpbucket(register Hash_table_t* tab, int flags)
54da2e3ebdSchin {
55da2e3ebdSchin register Hash_bucket_t** sp;
56da2e3ebdSchin register Hash_bucket_t* b;
57da2e3ebdSchin Hash_bucket_t** sx;
58da2e3ebdSchin int n;
59da2e3ebdSchin unsigned char* s;
60da2e3ebdSchin
61da2e3ebdSchin NoP(flags);
62da2e3ebdSchin sx = tab->table + tab->size;
63da2e3ebdSchin for (sp = tab->table; sp < sx; sp++)
64da2e3ebdSchin {
65da2e3ebdSchin n = 0;
66da2e3ebdSchin for (b = *sp; b; b = b->next)
67da2e3ebdSchin if (!(b->hash & HASH_DELETED) && (!(tab->flags & HASH_VALUE) || b->value))
68da2e3ebdSchin n++;
69da2e3ebdSchin if (n)
70da2e3ebdSchin {
71da2e3ebdSchin sfprintf(sfstderr, "%5d %2d :", sp - tab->table, n);
72da2e3ebdSchin for (b = *sp; b; b = b->next)
73da2e3ebdSchin if (!(b->hash & HASH_DELETED) && (!(tab->flags & HASH_VALUE) || b->value))
74da2e3ebdSchin {
75da2e3ebdSchin if (n = tab->root->namesize)
76da2e3ebdSchin {
77da2e3ebdSchin sfprintf(sfstderr, " 0x");
78da2e3ebdSchin s = (unsigned char*)hashname(b);
79da2e3ebdSchin while (n-- > 0)
80da2e3ebdSchin sfprintf(sfstderr, "%02x", *s++);
81da2e3ebdSchin }
82da2e3ebdSchin else sfprintf(sfstderr, " %s", hashname(b));
83da2e3ebdSchin if (b->hash & HASH_FLAGS)
84da2e3ebdSchin {
85da2e3ebdSchin sfprintf(sfstderr, "|");
86da2e3ebdSchin if (b->hash & HASH_HIDES) sfprintf(sfstderr, "hides|");
87da2e3ebdSchin if (b->hash & HASH_HIDDEN) sfprintf(sfstderr, "hidden|");
88da2e3ebdSchin if (b->hash & HASH_KEEP) sfprintf(sfstderr, "keep|");
89da2e3ebdSchin if (b->hash & HASH_OPAQUED) sfprintf(sfstderr, "opaque|");
90da2e3ebdSchin }
91da2e3ebdSchin if (tab->flags & HASH_VALUE) sfprintf(sfstderr, "=0x%08lx", (long)b->value);
92da2e3ebdSchin }
93da2e3ebdSchin sfprintf(sfstderr, "\n");
94da2e3ebdSchin }
95da2e3ebdSchin }
96da2e3ebdSchin sfprintf(sfstderr, "\n");
97da2e3ebdSchin }
98da2e3ebdSchin
99da2e3ebdSchin /*
100da2e3ebdSchin * dump info on a single table
101da2e3ebdSchin */
102da2e3ebdSchin
103da2e3ebdSchin static void
dumptable(register Hash_table_t * tab,register int flags)104da2e3ebdSchin dumptable(register Hash_table_t* tab, register int flags)
105da2e3ebdSchin {
106da2e3ebdSchin Hash_table_t* scope;
107da2e3ebdSchin int level;
108da2e3ebdSchin
109da2e3ebdSchin sfprintf(sfstderr, " name: %s", tab->name ? tab->name : "*no name*");
110da2e3ebdSchin if (scope = tab->scope)
111da2e3ebdSchin {
112da2e3ebdSchin level = 1;
113da2e3ebdSchin while (scope = scope->scope) level++;
114da2e3ebdSchin sfprintf(sfstderr, " level %d scope on 0x%08lx", level, (unsigned long)tab->scope);
115da2e3ebdSchin }
116da2e3ebdSchin sfprintf(sfstderr, "\n");
117da2e3ebdSchin sfprintf(sfstderr, " address: 0x%08lx\n", (unsigned long)tab);
118da2e3ebdSchin sfprintf(sfstderr, " flags: ");
119da2e3ebdSchin if (tab->frozen) sfprintf(sfstderr, "frozen=%d ", tab->frozen);
120da2e3ebdSchin dumpflags(tab->flags);
121da2e3ebdSchin sfprintf(sfstderr, "\n");
122da2e3ebdSchin sfprintf(sfstderr, " size: %d\n", tab->size);
123da2e3ebdSchin sfprintf(sfstderr, " buckets: %d\n", tab->buckets);
124da2e3ebdSchin sfprintf(sfstderr, " bucketsize: %d\n", tab->bucketsize * sizeof(char*));
125da2e3ebdSchin sfprintf(sfstderr, "\n");
126da2e3ebdSchin if ((flags | tab->flags) & HASH_BUCKET) dumpbucket(tab, flags);
127da2e3ebdSchin }
128da2e3ebdSchin
129da2e3ebdSchin /*
130da2e3ebdSchin * dump hash table root info
131da2e3ebdSchin */
132da2e3ebdSchin
133da2e3ebdSchin static void
dumproot(register Hash_root_t * root,register int flags)134da2e3ebdSchin dumproot(register Hash_root_t* root, register int flags)
135da2e3ebdSchin {
136da2e3ebdSchin register Hash_table_t* tab;
137da2e3ebdSchin
138da2e3ebdSchin sfprintf(sfstderr, " root\n");
139da2e3ebdSchin sfprintf(sfstderr, " address: 0x%08lx\n", (unsigned long)root);
140da2e3ebdSchin sfprintf(sfstderr, " flags: ");
141da2e3ebdSchin dumpflags(root->flags);
142da2e3ebdSchin if (root->namesize) sfprintf(sfstderr, "namesize=%d ", root->namesize);
143da2e3ebdSchin if (root->local->alloc) sfprintf(sfstderr, "alloc=0x%08lx ", (unsigned long)root->local->alloc);
144da2e3ebdSchin if (root->local->compare) sfprintf(sfstderr, "compare=0x%08lx ", (unsigned long)root->local->compare);
145da2e3ebdSchin if (root->local->free) sfprintf(sfstderr, "free=0x%08lx ", (unsigned long)root->local->free);
146da2e3ebdSchin if (root->local->hash) sfprintf(sfstderr, "hash=0x%08lx ", (unsigned long)root->local->hash);
147da2e3ebdSchin if (root->local->region) sfprintf(sfstderr, "region=0x%08lx handle=0x%08lx ", (unsigned long)root->local->region, (unsigned long)root->local->handle);
148da2e3ebdSchin sfprintf(sfstderr, "\n");
149da2e3ebdSchin sfprintf(sfstderr, " meanchain: %d\n", root->meanchain);
150da2e3ebdSchin sfprintf(sfstderr, " accesses: %d\n", root->accesses);
151da2e3ebdSchin sfprintf(sfstderr, " collisions: %d\n", root->collisions);
152da2e3ebdSchin sfprintf(sfstderr, "\n");
153da2e3ebdSchin for (tab = root->references; tab; tab = tab->next)
154da2e3ebdSchin dumptable(tab, flags);
155da2e3ebdSchin }
156da2e3ebdSchin
157da2e3ebdSchin /*
158da2e3ebdSchin * dump hash table accounting info
159da2e3ebdSchin * if tab is 0 then dump all tables in hash_info.list
160da2e3ebdSchin * flags are HASH_* flags that specifiy optional dump info
161da2e3ebdSchin */
162da2e3ebdSchin
163da2e3ebdSchin void
hashdump(register Hash_table_t * tab,int flags)164da2e3ebdSchin hashdump(register Hash_table_t* tab, int flags)
165da2e3ebdSchin {
166da2e3ebdSchin register Hash_root_t* root;
167da2e3ebdSchin
168da2e3ebdSchin sfprintf(sfstderr, "\nhash table information:\n\n");
169da2e3ebdSchin if (tab) dumproot(tab->root, flags);
170da2e3ebdSchin else for (root = hash_info.list; root; root = root->next)
171da2e3ebdSchin dumproot(root, flags);
172da2e3ebdSchin sfsync(sfstderr);
173da2e3ebdSchin }
174