OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [tags/] [gnu-dev/] [fsf-gcc-snapshot-1-mar-12/] [or1k-gcc/] [libobjc/] [hash.c] - Diff between revs 739 and 783

Go to most recent revision | Only display areas with differences | Details | Blame | View Log

Rev 739 Rev 783
/* Hash tables for Objective C internal structures
/* Hash tables for Objective C internal structures
   Copyright (C) 1993, 1996, 1997, 2004, 2009, 2010
   Copyright (C) 1993, 1996, 1997, 2004, 2009, 2010
   Free Software Foundation, Inc.
   Free Software Foundation, Inc.
 
 
This file is part of GCC.
This file is part of GCC.
 
 
GCC is free software; you can redistribute it and/or modify
GCC is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3, or (at your option)
the Free Software Foundation; either version 3, or (at your option)
any later version.
any later version.
 
 
GCC is distributed in the hope that it will be useful,
GCC is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
GNU General Public License for more details.
 
 
Under Section 7 of GPL version 3, you are granted additional
Under Section 7 of GPL version 3, you are granted additional
permissions described in the GCC Runtime Library Exception, version
permissions described in the GCC Runtime Library Exception, version
3.1, as published by the Free Software Foundation.
3.1, as published by the Free Software Foundation.
 
 
You should have received a copy of the GNU General Public License and
You should have received a copy of the GNU General Public License and
a copy of the GCC Runtime Library Exception along with this program;
a copy of the GCC Runtime Library Exception along with this program;
see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
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-private/hash.h"
#include "objc-private/hash.h"
 
 
/* 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.
 
 
   These equations are percentages.  */
   These equations are percentages.  */
#define FULLNESS(cache) \
#define FULLNESS(cache) \
   ((((cache)->size * 75) / 100) <= (cache)->used)
   ((((cache)->size * 75) / 100) <= (cache)->used)
#define EXPANSION(cache) \
#define EXPANSION(cache) \
  ((cache)->size * 2)
  ((cache)->size * 2)
 
 
cache_ptr
cache_ptr
objc_hash_new (unsigned int size, hash_func_type hash_func,
objc_hash_new (unsigned int size, hash_func_type hash_func,
               compare_func_type compare_func)
               compare_func_type compare_func)
{
{
  cache_ptr cache;
  cache_ptr cache;
 
 
  /* Pass me a value greater than 0 and a power of 2.  */
  /* Pass me a value greater than 0 and a power of 2.  */
  assert (size);
  assert (size);
  assert (! (size & (size - 1)));
  assert (! (size & (size - 1)));
 
 
  /* Allocate the cache structure.  calloc insures its initialization
  /* Allocate the cache structure.  calloc insures 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.  calloc initializes
  /* Allocate the array of buckets for the cache.  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 determine if they
  /* Store the function that compares hash keys to determine if they
     are equal.  */
     are equal.  */
  cache->compare_func = compare_func;
  cache->compare_func = compare_func;
 
 
  return cache;
  return cache;
}
}
 
 
 
 
void
void
objc_hash_delete (cache_ptr cache)
objc_hash_delete (cache_ptr cache)
{
{
  node_ptr node;
  node_ptr node;
  node_ptr next_node;
  node_ptr next_node;
  unsigned int i;
  unsigned int i;
 
 
  /* 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
     using objc_hash_next. this makes objc_hash_delete much more
     efficient. */
     efficient. */
  for (i = 0; i < cache->size; i++)
  for (i = 0; i < cache->size; i++)
    {
    {
      if ((node = cache->node_table[i]))
      if ((node = cache->node_table[i]))
        {
        {
          /* An entry in the hash table has been found.  Now step
          /* An entry in the hash table has been found.  Now step
             through the nodes next in the list and free them.  */
             through the nodes next in the list and free them.  */
          while ((next_node = node->next))
          while ((next_node = node->next))
            {
            {
              objc_hash_remove (cache,node->key);
              objc_hash_remove (cache,node->key);
              node = next_node;
              node = next_node;
            }
            }
          objc_hash_remove (cache,node->key);
          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);
  objc_free(cache);
  objc_free(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.  */
  node->key    = key;
  node->key    = key;
  node->value  = value;
  node->value  = value;
  node->next  = (*cachep)->node_table[indx];
  node->next  = (*cachep)->node_table[indx];
 
 
  /* Debugging.  Check the list for another key.  */
  /* Debugging.  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
 
 
  /* Install the node as the first element on the list.  */
  /* Install the node as the first element on the list.  */
  (*cachep)->node_table[indx] = node;
  (*cachep)->node_table[indx] = node;
 
 
  /* 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
  /* Check the hash table's fullness.  We're going to expand if it is
     above the fullness level.  */
     above the fullness level.  */
  if (FULLNESS (*cachep))
  if (FULLNESS (*cachep))
    {
    {
      /* The hash table has reached its fullness level.  Time to
      /* The hash table has reached its fullness level.  Time to
         expand it.
         expand it.
 
 
         I'm using a slow method here but is built on other primitive
         I'm using a slow method here but is built on other primitive
         functions thereby increasing its correctness.  */
         functions thereby increasing its correctness.  */
      node_ptr node1 = NULL;
      node_ptr node1 = NULL;
      cache_ptr new = objc_hash_new (EXPANSION (*cachep),
      cache_ptr new = objc_hash_new (EXPANSION (*cachep),
                                     (*cachep)->hash_func,
                                     (*cachep)->hash_func,
                                     (*cachep)->compare_func);
                                     (*cachep)->compare_func);
 
 
      DEBUG_PRINTF ("Expanding cache %#x from %d to %d\n",
      DEBUG_PRINTF ("Expanding cache %#x from %d to %d\n",
                    (int) *cachep, (*cachep)->size, new->size);
                    (int) *cachep, (*cachep)->size, new->size);
 
 
      /* Copy the nodes from the first hash table to the new one.  */
      /* Copy the nodes from the first hash table to the new one.  */
      while ((node1 = objc_hash_next (*cachep, node1)))
      while ((node1 = objc_hash_next (*cachep, node1)))
        objc_hash_add (&new, node1->key, node1->value);
        objc_hash_add (&new, node1->key, node1->value);
 
 
      /* Trash the old cache.  */
      /* Trash the old cache.  */
      objc_hash_delete (*cachep);
      objc_hash_delete (*cachep);
 
 
      /* Return a pointer to the new hash table.  */
      /* Return a pointer to the new hash table.  */
      *cachep = new;
      *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
  /* Special case.  First element is the key/value pair to be
     removed.  */
     removed.  */
  if ((*cache->compare_func) (node->key, key))
  if ((*cache->compare_func) (node->key, key))
    {
    {
      cache->node_table[indx] = node->next;
      cache->node_table[indx] = node->next;
      objc_free(node);
      objc_free(node);
    }
    }
  else
  else
    {
    {
      /* Otherwise, find the hash entry.  */
      /* Otherwise, find the hash entry.  */
      node_ptr prev = node;
      node_ptr prev = node;
      BOOL removed = NO;
      BOOL removed = NO;
      do
      do
        {
        {
          if ((*cache->compare_func) (node->key, key))
          if ((*cache->compare_func) (node->key, key))
            {
            {
              prev->next = node->next, removed = YES;
              prev->next = node->next, removed = YES;
              objc_free(node);
              objc_free(node);
            }
            }
          else
          else
            prev = node, node = node->next;
            prev = node, node = node->next;
        }
        }
      while (!removed && node);
      while (!removed && node);
      assert (removed);
      assert (removed);
    }
    }
 
 
  /* Decrement the number of entries in the hash table.  */
  /* Decrement the number of entries in the hash table.  */
  --cache->used;
  --cache->used;
}
}
 
 
 
 
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 visitied
  /* If the scan is being started then reset the last node 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 entry in
  /* If there is a node visited last then check for another 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)
      if (node->next)
        {
        {
          /* There is a node which follows the last node returned.
          /* There is a node which follows the last node returned.
             Step to that node and retun it.  */
             Step to that node and retun it.  */
          return node->next;
          return node->next;
        }
        }
      else
      else
        ++cache->last_bucket;
        ++cache->last_bucket;
  }
  }
 
 
  /* If the list isn't exhausted then search the buckets for other
  /* If the list isn't exhausted then search the buckets for 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
      /*  Scan the remainder of the buckets looking for an entry at
          the head of the list.  Return the first item found.  */
          the head of the list.  Return the first item found.  */
      while (cache->last_bucket < cache->size)
      while (cache->last_bucket < cache->size)
        if (cache->node_table[cache->last_bucket])
        if (cache->node_table[cache->last_bucket])
          return cache->node_table[cache->last_bucket];
          return cache->node_table[cache->last_bucket];
        else
        else
          ++cache->last_bucket;
          ++cache->last_bucket;
 
 
      /* No further nodes were found in the hash table.  */
      /* No further nodes were found in the hash table.  */
      return NULL;
      return NULL;
    }
    }
  else
  else
    return NULL;
    return NULL;
}
}
 
 
 
 
/* Given KEY, return corresponding value for it in CACHE.  Return NULL
/* Given KEY, return corresponding value for it in CACHE.  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))
        if ((*cache->compare_func) (node->key, key))
          {
          {
            retval = node->value;
            retval = node->value;
            break;
            break;
          }
          }
        else
        else
          node = node->next;
          node = node->next;
      }
      }
    while (! retval && node);
    while (! retval && node);
 
 
  return retval;
  return retval;
}
}
 
 
/* Given KEY, return YES if it exists in the CACHE.  Return NO if it
/* Given KEY, return YES if it exists in the CACHE.  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;
}
}
 
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.