1N/A/***********************************************************************
1N/A* *
1N/A* This software is part of the ast package *
1N/A* Copyright (c) 1985-2011 AT&T Intellectual Property *
1N/A* and is licensed under the *
1N/A* Common Public License, Version 1.0 *
1N/A* by AT&T Intellectual Property *
1N/A* *
1N/A* A copy of the License is available at *
1N/A* http://www.opensource.org/licenses/cpl1.0.txt *
1N/A* (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9) *
1N/A* *
1N/A* Information and Software Systems Research *
1N/A* AT&T Research *
1N/A* Florham Park NJ *
1N/A* *
1N/A* Glenn Fowler <gsf@research.att.com> *
1N/A* David Korn <dgk@research.att.com> *
1N/A* Phong Vo <kpv@research.att.com> *
1N/A* *
1N/A***********************************************************************/
1N/A#pragma prototyped
1N/A/*
1N/A * Glenn Fowler
1N/A * AT&T Research
1N/A *
1N/A * hash table library
1N/A */
1N/A
1N/A#include "hashlib.h"
1N/A
1N/A/*
1N/A * free (remove) a hash table
1N/A * can be called for partially constructed tables
1N/A * scope covered table pointer is returned
1N/A * root info freed when last reference freed
1N/A */
1N/A
1N/AHash_table_t*
1N/Ahashfree(register Hash_table_t* tab)
1N/A{
1N/A register Hash_bucket_t** sp;
1N/A register Hash_bucket_t* b;
1N/A register Hash_bucket_t* p;
1N/A Hash_bucket_t** sx;
1N/A Hash_root_t* rp;
1N/A Hash_table_t* tp;
1N/A Hash_free_f freevalue;
1N/A Hash_free_f freebucket;
1N/A Hash_region_f region;
1N/A void* handle;
1N/A
1N/A if (!tab) return(0);
1N/A if (tab->table)
1N/A {
1N/A freebucket = 0;
1N/A freevalue = 0;
1N/A if (tab->root->local->free)
1N/A {
1N/A if (tab->root->flags & HASH_BUCKET) freebucket = tab->root->local->free;
1N/A else freevalue = tab->root->local->free;
1N/A }
1N/A if (region = tab->root->local->region)
1N/A handle = tab->root->local->handle;
1N/A sx = &tab->table[tab->size];
1N/A sp = &tab->table[0];
1N/A while (sp < sx)
1N/A {
1N/A b = *sp++;
1N/A while (b)
1N/A {
1N/A p = b;
1N/A b = b->next;
1N/A if (freebucket) (*freebucket)((char*)p);
1N/A else if (freevalue && p->value) (*freevalue)(p->value);
1N/A if (p->hash & HASH_FREENAME)
1N/A {
1N/A p->hash &= ~HASH_FREENAME;
1N/A if (region) (*region)(handle, p->name, 0, 0);
1N/A else free(p->name);
1N/A }
1N/A if (!(p->hash & HASH_KEEP))
1N/A {
1N/A if (region) (*region)(handle, p, 0, 0);
1N/A else free(p);
1N/A }
1N/A else if (p->hash & HASH_HIDES)
1N/A {
1N/A p->hash &= ~HASH_HIDES;
1N/A p->name = ((Hash_bucket_t*)p->name)->name;
1N/A }
1N/A }
1N/A }
1N/A if ((tab->flags & (HASH_RESIZE|HASH_STATIC)) != HASH_STATIC)
1N/A {
1N/A if (region) (*region)(handle, tab->table, 0, 0);
1N/A else free(tab->table);
1N/A }
1N/A }
1N/A else region = 0;
1N/A if (tab->root)
1N/A {
1N/A if (!region)
1N/A {
1N/A /*
1N/A * remove from the table lists
1N/A */
1N/A
1N/A if ((tp = tab->root->references) != tab)
1N/A {
1N/A for (; tp; tp = tp->next)
1N/A if (tp->next == tab)
1N/A {
1N/A tp->next = tab->next;
1N/A break;
1N/A }
1N/A }
1N/A else if (!(tab->root->references = tp->next))
1N/A {
1N/A if ((rp = hash_info.list) != tab->root)
1N/A {
1N/A for (; rp; rp = rp->next)
1N/A if (rp->next == tab->root)
1N/A {
1N/A rp->next = tab->root->next;
1N/A break;
1N/A }
1N/A }
1N/A else hash_info.list = rp->next;
1N/A }
1N/A }
1N/A if (!(tab->root->references))
1N/A {
1N/A if (tab->root->local)
1N/A free(tab->root->local);
1N/A if (region) (*region)(handle, tab->root, 0, 0);
1N/A else free(tab->root);
1N/A }
1N/A }
1N/A if (tp = tab->scope) tp->frozen--;
1N/A if (region) (*region)(handle, tab, 0, 0);
1N/A else free(tab);
1N/A return(tp);
1N/A}