X-Git-Url: https://git.gag.com/?a=blobdiff_plain;f=src%2FSDCChasht.c;h=dd6322d451c632b5a70231f1ed0f57b6f979e628;hb=HEAD;hp=71821860912bff87a50720c5a815399850190a96;hpb=2f566bab0a5fe0bf025d6c892d6fc309966618f1;p=fw%2Fsdcc diff --git a/src/SDCChasht.c b/src/SDCChasht.c index 71821860..dd6322d4 100644 --- a/src/SDCChasht.c +++ b/src/SDCChasht.c @@ -40,7 +40,7 @@ _newHashtItem (int key, void *pkey, void *item) { hashtItem *htip; - htip = Safe_calloc (1, sizeof (hashtItem)); + htip = Safe_alloc ( sizeof (hashtItem)); htip->key = key; htip->pkey = pkey; @@ -57,12 +57,12 @@ newHashTable (int size) { hTab *htab; - htab = Safe_calloc (1, sizeof (hTab)); + htab = Safe_alloc ( sizeof (hTab)); - if (!(htab->table = calloc ((size + 1), sizeof (hashtItem *)))) + if (!(htab->table = Safe_alloc ((size + 1) * sizeof (hashtItem *)))) { fprintf (stderr, "out of virtual memory %s %d\n", - __FILE__, (size + 1) * sizeof (hashtItem *)); + __FILE__, (size + 1) * (int) sizeof (hashtItem *)); exit (1); } htab->minKey = htab->size = size; @@ -193,13 +193,13 @@ hTabDeleteAll (hTab * p) jn = jc->next; while (jc) { - free (jc); + Safe_free (jc); if ((jc = jn)) jn = jc->next; } p->table[i] = NULL; } - free (p->table); + Safe_free (p->table); } } @@ -513,6 +513,14 @@ hTabItemWithKey (hTab * htab, int key) return htip->item; } +/*-----------------------------------------------------------------*/ +/* hTabMaxKey - return the maxKey of item in the hashTable */ +/*-----------------------------------------------------------------*/ +int hTabMaxKey (hTab *htab) +{ + return (htab ? htab->maxKey : 0); +} + /*-----------------------------------------------------------------*/ /*hTabAddItemIfNotP - adds an item with nothing found with key */ /*-----------------------------------------------------------------*/ @@ -553,11 +561,21 @@ _hash (const char *sz) void shash_add (hTab ** h, const char *szKey, const char *szValue) { + char *val; int key = _hash (szKey); - /* First, delete any that currently exist */ - hTabDeleteByKey (h, key, szKey, _compare); + + /* Find value of the item */ + val = (char *)hTabFindByKey(*h, key, szKey, _compare); + /* Delete any that currently exist */ + hTabDeleteByKey(h, key, szKey, _compare); + /* Deallocate old value in not NULL */ + if (val != NULL) + Safe_free(val); + /* Duplicate new value if not NULL */ + if (szValue != NULL) + szValue = Safe_strdup(szValue); /* Now add in ours */ - hTabAddItemLong (h, key, gc_strdup (szKey), gc_strdup (szValue)); + hTabAddItemLong (h, key, Safe_strdup (szKey), (void *)szValue); } const char *