mirror of git://gcc.gnu.org/git/gcc.git
In libobjc/: 2010-12-21 Nicola Pero <nicola.pero@meta-innovation.com>
In libobjc/: 2010-12-21 Nicola Pero <nicola.pero@meta-innovation.com> * hash.c: Tidied up comments and indentation. No code changes. From-SVN: r168110
This commit is contained in:
parent
d4d92cd36c
commit
57d75eebe2
|
|
@ -1,3 +1,7 @@
|
||||||
|
2010-12-21 Nicola Pero <nicola.pero@meta-innovation.com>
|
||||||
|
|
||||||
|
* hash.c: Tidied up comments and indentation. No code changes.
|
||||||
|
|
||||||
2010-12-19 Nicola Pero <nicola.pero@meta-innovation.com>
|
2010-12-19 Nicola Pero <nicola.pero@meta-innovation.com>
|
||||||
|
|
||||||
PR libobjc/47012
|
PR libobjc/47012
|
||||||
|
|
|
||||||
285
libobjc/hash.c
285
libobjc/hash.c
|
|
@ -23,12 +23,12 @@ see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
|
||||||
<http://www.gnu.org/licenses/>. */
|
<http://www.gnu.org/licenses/>. */
|
||||||
|
|
||||||
#include "objc-private/common.h"
|
#include "objc-private/common.h"
|
||||||
#include <assert.h> /* For assert */
|
#include <assert.h> /* For assert. */
|
||||||
|
|
||||||
#include "objc/runtime.h" /* For objc_calloc */
|
#include "objc/runtime.h" /* For objc_calloc. */
|
||||||
#include "objc/thr.h" /* Required by objc-private/runtime.h. */
|
#include "objc/thr.h" /* Required by objc-private/runtime.h. */
|
||||||
#include "objc-private/hash.h"
|
#include "objc-private/hash.h"
|
||||||
#include "objc-private/runtime.h" /* for DEBUG_PRINTF */
|
#include "objc-private/runtime.h" /* for DEBUG_PRINTF. */
|
||||||
|
|
||||||
/* These two macros determine when a hash table is full and
|
/* These two macros determine when a hash table is full and
|
||||||
by how much it should be expanded respectively.
|
by how much it should be expanded respectively.
|
||||||
|
|
@ -49,27 +49,27 @@ objc_hash_new (unsigned int size, hash_func_type hash_func,
|
||||||
assert (size);
|
assert (size);
|
||||||
assert (! (size & (size - 1)));
|
assert (! (size & (size - 1)));
|
||||||
|
|
||||||
/* Allocate the cache structure. calloc insures
|
/* Allocate the cache structure. calloc insures its initialization
|
||||||
its initialization for default values. */
|
for default values. */
|
||||||
cache = (cache_ptr) objc_calloc (1, sizeof (struct cache));
|
cache = (cache_ptr) objc_calloc (1, sizeof (struct cache));
|
||||||
assert (cache);
|
assert (cache);
|
||||||
|
|
||||||
/* Allocate the array of buckets for the cache.
|
/* Allocate the array of buckets for the cache. calloc initializes
|
||||||
calloc initializes all of the pointers to NULL. */
|
all of the pointers to NULL. */
|
||||||
cache->node_table
|
cache->node_table
|
||||||
= (node_ptr *) objc_calloc (size, sizeof (node_ptr));
|
= (node_ptr *) objc_calloc (size, sizeof (node_ptr));
|
||||||
assert (cache->node_table);
|
assert (cache->node_table);
|
||||||
|
|
||||||
cache->size = size;
|
cache->size = size;
|
||||||
|
|
||||||
/* This should work for all processor architectures? */
|
/* This should work for all processor architectures (?). */
|
||||||
cache->mask = (size - 1);
|
cache->mask = (size - 1);
|
||||||
|
|
||||||
/* Store the hashing function so that codes can be computed. */
|
/* Store the hashing function so that codes can be computed. */
|
||||||
cache->hash_func = hash_func;
|
cache->hash_func = hash_func;
|
||||||
|
|
||||||
/* Store the function that compares hash keys to
|
/* Store the function that compares hash keys to determine if they
|
||||||
determine if they are equal. */
|
are equal. */
|
||||||
cache->compare_func = compare_func;
|
cache->compare_func = compare_func;
|
||||||
|
|
||||||
return cache;
|
return cache;
|
||||||
|
|
@ -85,19 +85,22 @@ objc_hash_delete (cache_ptr cache)
|
||||||
|
|
||||||
/* Purge all key/value pairs from the table. */
|
/* Purge all key/value pairs from the table. */
|
||||||
/* Step through the nodes one by one and remove every node WITHOUT
|
/* Step through the nodes one by one and remove every node WITHOUT
|
||||||
using objc_hash_next. this makes objc_hash_delete much more efficient. */
|
using objc_hash_next. this makes objc_hash_delete much more
|
||||||
for (i = 0;i < cache->size;i++) {
|
efficient. */
|
||||||
if ((node = cache->node_table[i])) {
|
for (i = 0; i < cache->size; i++)
|
||||||
/* an entry in the hash table has been found, now step through the
|
{
|
||||||
nodes next in the list and free them. */
|
if ((node = cache->node_table[i]))
|
||||||
while ((next_node = node->next)) {
|
{
|
||||||
objc_hash_remove (cache,node->key);
|
/* An entry in the hash table has been found. Now step
|
||||||
node = next_node;
|
through the nodes next in the list and free them. */
|
||||||
}
|
while ((next_node = node->next))
|
||||||
|
{
|
||||||
objc_hash_remove (cache,node->key);
|
objc_hash_remove (cache,node->key);
|
||||||
|
node = next_node;
|
||||||
|
}
|
||||||
|
objc_hash_remove (cache,node->key);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
/* Release the array of nodes and the cache itself. */
|
/* Release the array of nodes and the cache itself. */
|
||||||
objc_free(cache->node_table);
|
objc_free(cache->node_table);
|
||||||
|
|
@ -108,10 +111,9 @@ objc_hash_delete (cache_ptr cache)
|
||||||
void
|
void
|
||||||
objc_hash_add (cache_ptr *cachep, const void *key, void *value)
|
objc_hash_add (cache_ptr *cachep, const void *key, void *value)
|
||||||
{
|
{
|
||||||
size_t indx = (*(*cachep)->hash_func)(*cachep, key);
|
size_t indx = (*(*cachep)->hash_func) (*cachep, key);
|
||||||
node_ptr node = (node_ptr) objc_calloc (1, sizeof (struct cache_node));
|
node_ptr node = (node_ptr) objc_calloc (1, sizeof (struct cache_node));
|
||||||
|
|
||||||
|
|
||||||
assert (node);
|
assert (node);
|
||||||
|
|
||||||
/* Initialize the new node. */
|
/* Initialize the new node. */
|
||||||
|
|
@ -119,16 +121,15 @@ objc_hash_add (cache_ptr *cachep, const void *key, void *value)
|
||||||
node->value = value;
|
node->value = value;
|
||||||
node->next = (*cachep)->node_table[indx];
|
node->next = (*cachep)->node_table[indx];
|
||||||
|
|
||||||
/* Debugging.
|
/* Debugging. Check the list for another key. */
|
||||||
Check the list for another key. */
|
|
||||||
#ifdef DEBUG
|
#ifdef DEBUG
|
||||||
{ node_ptr node1 = (*cachep)->node_table[indx];
|
{
|
||||||
|
node_ptr node1 = (*cachep)->node_table[indx];
|
||||||
while (node1) {
|
while (node1)
|
||||||
|
{
|
||||||
assert (node1->key != key);
|
assert (node1->key != key);
|
||||||
node1 = node1->next;
|
node1 = node1->next;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
@ -137,69 +138,72 @@ objc_hash_add (cache_ptr *cachep, const void *key, void *value)
|
||||||
|
|
||||||
/* Bump the number of entries in the cache. */
|
/* Bump the number of entries in the cache. */
|
||||||
++(*cachep)->used;
|
++(*cachep)->used;
|
||||||
|
|
||||||
|
/* Check the hash table's fullness. We're going to expand if it is
|
||||||
|
above the fullness level. */
|
||||||
|
if (FULLNESS (*cachep))
|
||||||
|
{
|
||||||
|
/* The hash table has reached its fullness level. Time to
|
||||||
|
expand it.
|
||||||
|
|
||||||
/* Check the hash table's fullness. We're going
|
I'm using a slow method here but is built on other primitive
|
||||||
to expand if it is above the fullness level. */
|
functions thereby increasing its correctness. */
|
||||||
if (FULLNESS (*cachep)) {
|
node_ptr node1 = NULL;
|
||||||
|
cache_ptr new = objc_hash_new (EXPANSION (*cachep),
|
||||||
/* The hash table has reached its fullness level. Time to
|
(*cachep)->hash_func,
|
||||||
expand it.
|
(*cachep)->compare_func);
|
||||||
|
|
||||||
I'm using a slow method here but is built on other
|
DEBUG_PRINTF ("Expanding cache %#x from %d to %d\n",
|
||||||
primitive functions thereby increasing its
|
(int) *cachep, (*cachep)->size, new->size);
|
||||||
correctness. */
|
|
||||||
node_ptr node1 = NULL;
|
/* Copy the nodes from the first hash table to the new one. */
|
||||||
cache_ptr new = objc_hash_new (EXPANSION (*cachep),
|
while ((node1 = objc_hash_next (*cachep, node1)))
|
||||||
(*cachep)->hash_func,
|
objc_hash_add (&new, node1->key, node1->value);
|
||||||
(*cachep)->compare_func);
|
|
||||||
|
/* Trash the old cache. */
|
||||||
DEBUG_PRINTF ("Expanding cache %#x from %d to %d\n",
|
objc_hash_delete (*cachep);
|
||||||
(int) *cachep, (*cachep)->size, new->size);
|
|
||||||
|
/* Return a pointer to the new hash table. */
|
||||||
/* Copy the nodes from the first hash table to the new one. */
|
*cachep = new;
|
||||||
while ((node1 = objc_hash_next (*cachep, node1)))
|
}
|
||||||
objc_hash_add (&new, node1->key, node1->value);
|
|
||||||
|
|
||||||
/* Trash the old cache. */
|
|
||||||
objc_hash_delete (*cachep);
|
|
||||||
|
|
||||||
/* Return a pointer to the new hash table. */
|
|
||||||
*cachep = new;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void
|
void
|
||||||
objc_hash_remove (cache_ptr cache, const void *key)
|
objc_hash_remove (cache_ptr cache, const void *key)
|
||||||
{
|
{
|
||||||
size_t indx = (*cache->hash_func)(cache, key);
|
size_t indx = (*cache->hash_func) (cache, key);
|
||||||
node_ptr node = cache->node_table[indx];
|
node_ptr node = cache->node_table[indx];
|
||||||
|
|
||||||
|
/* We assume there is an entry in the table. Error if it is
|
||||||
/* We assume there is an entry in the table. Error if it is not. */
|
not. */
|
||||||
assert (node);
|
assert (node);
|
||||||
|
|
||||||
/* Special case. First element is the key/value pair to be removed. */
|
/* Special case. First element is the key/value pair to be
|
||||||
if ((*cache->compare_func)(node->key, key)) {
|
removed. */
|
||||||
cache->node_table[indx] = node->next;
|
if ((*cache->compare_func) (node->key, key))
|
||||||
objc_free(node);
|
{
|
||||||
} else {
|
cache->node_table[indx] = node->next;
|
||||||
|
objc_free(node);
|
||||||
/* Otherwise, find the hash entry. */
|
}
|
||||||
node_ptr prev = node;
|
else
|
||||||
BOOL removed = NO;
|
{
|
||||||
|
/* Otherwise, find the hash entry. */
|
||||||
do {
|
node_ptr prev = node;
|
||||||
|
BOOL removed = NO;
|
||||||
if ((*cache->compare_func)(node->key, key)) {
|
do
|
||||||
prev->next = node->next, removed = YES;
|
{
|
||||||
objc_free(node);
|
if ((*cache->compare_func) (node->key, key))
|
||||||
} else
|
{
|
||||||
prev = node, node = node->next;
|
prev->next = node->next, removed = YES;
|
||||||
} while (! removed && node);
|
objc_free(node);
|
||||||
assert (removed);
|
}
|
||||||
}
|
else
|
||||||
|
prev = node, node = node->next;
|
||||||
|
}
|
||||||
|
while (!removed && node);
|
||||||
|
assert (removed);
|
||||||
|
}
|
||||||
|
|
||||||
/* Decrement the number of entries in the hash table. */
|
/* Decrement the number of entries in the hash table. */
|
||||||
--cache->used;
|
--cache->used;
|
||||||
}
|
}
|
||||||
|
|
@ -208,76 +212,85 @@ objc_hash_remove (cache_ptr cache, const void *key)
|
||||||
node_ptr
|
node_ptr
|
||||||
objc_hash_next (cache_ptr cache, node_ptr node)
|
objc_hash_next (cache_ptr cache, node_ptr node)
|
||||||
{
|
{
|
||||||
/* If the scan is being started then reset the last node
|
/* If the scan is being started then reset the last node visitied
|
||||||
visitied pointer and bucket index. */
|
pointer and bucket index. */
|
||||||
if (! node)
|
if (!node)
|
||||||
cache->last_bucket = 0;
|
cache->last_bucket = 0;
|
||||||
|
|
||||||
/* If there is a node visited last then check for another
|
/* If there is a node visited last then check for another entry in
|
||||||
entry in the same bucket; Otherwise step to the next bucket. */
|
the same bucket. Otherwise step to the next bucket. */
|
||||||
if (node) {
|
if (node)
|
||||||
if (node->next)
|
{
|
||||||
/* There is a node which follows the last node
|
if (node->next)
|
||||||
returned. Step to that node and retun it. */
|
{
|
||||||
return node->next;
|
/* There is a node which follows the last node returned.
|
||||||
else
|
Step to that node and retun it. */
|
||||||
++cache->last_bucket;
|
return node->next;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
++cache->last_bucket;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* If the list isn't exhausted then search the buckets for
|
/* If the list isn't exhausted then search the buckets for other
|
||||||
other nodes. */
|
nodes. */
|
||||||
if (cache->last_bucket < cache->size) {
|
if (cache->last_bucket < cache->size)
|
||||||
/* Scan the remainder of the buckets looking for an entry
|
{
|
||||||
at the head of the list. Return the first item found. */
|
/* Scan the remainder of the buckets looking for an entry at
|
||||||
while (cache->last_bucket < cache->size)
|
the head of the list. Return the first item found. */
|
||||||
if (cache->node_table[cache->last_bucket])
|
while (cache->last_bucket < cache->size)
|
||||||
return cache->node_table[cache->last_bucket];
|
if (cache->node_table[cache->last_bucket])
|
||||||
else
|
return cache->node_table[cache->last_bucket];
|
||||||
++cache->last_bucket;
|
else
|
||||||
|
++cache->last_bucket;
|
||||||
/* No further nodes were found in the hash table. */
|
|
||||||
return NULL;
|
/* No further nodes were found in the hash table. */
|
||||||
} else
|
return NULL;
|
||||||
|
}
|
||||||
|
else
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/* Given KEY, return corresponding value for it in CACHE.
|
/* Given KEY, return corresponding value for it in CACHE. Return NULL
|
||||||
Return NULL if the KEY is not recorded. */
|
if the KEY is not recorded. */
|
||||||
|
|
||||||
void *
|
void *
|
||||||
objc_hash_value_for_key (cache_ptr cache, const void *key)
|
objc_hash_value_for_key (cache_ptr cache, const void *key)
|
||||||
{
|
{
|
||||||
node_ptr node = cache->node_table[(*cache->hash_func)(cache, key)];
|
node_ptr node = cache->node_table[(*cache->hash_func) (cache, key)];
|
||||||
void *retval = NULL;
|
void *retval = NULL;
|
||||||
|
|
||||||
if (node)
|
if (node)
|
||||||
do {
|
do
|
||||||
if ((*cache->compare_func)(node->key, key)) {
|
{
|
||||||
retval = node->value;
|
if ((*cache->compare_func) (node->key, key))
|
||||||
break;
|
{
|
||||||
} else
|
retval = node->value;
|
||||||
node = node->next;
|
break;
|
||||||
} while (! retval && node);
|
}
|
||||||
|
else
|
||||||
|
node = node->next;
|
||||||
|
}
|
||||||
|
while (! retval && node);
|
||||||
|
|
||||||
return retval;
|
return retval;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Given KEY, return YES if it exists in the CACHE.
|
/* Given KEY, return YES if it exists in the CACHE. Return NO if it
|
||||||
Return NO if it does not */
|
does not */
|
||||||
|
|
||||||
BOOL
|
BOOL
|
||||||
objc_hash_is_key_in_hash (cache_ptr cache, const void *key)
|
objc_hash_is_key_in_hash (cache_ptr cache, const void *key)
|
||||||
{
|
{
|
||||||
node_ptr node = cache->node_table[(*cache->hash_func)(cache, key)];
|
node_ptr node = cache->node_table[(*cache->hash_func) (cache, key)];
|
||||||
|
|
||||||
if (node)
|
if (node)
|
||||||
do {
|
do
|
||||||
if ((*cache->compare_func)(node->key, key))
|
{
|
||||||
|
if ((*cache->compare_func)(node->key, key))
|
||||||
return YES;
|
return YES;
|
||||||
else
|
else
|
||||||
node = node->next;
|
node = node->next;
|
||||||
} while (node);
|
}
|
||||||
|
while (node);
|
||||||
|
|
||||||
return NO;
|
return NO;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue