httpd-cvs mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From f...@locus.apache.org
Subject cvs commit: apache-2.0/src/lib/apr/include apr_hash.h
Date Wed, 03 May 2000 17:24:45 GMT
fanf        00/05/03 10:24:44

  Modified:    src      CHANGES
               src/lib/apr/lib Makefile.in
  Added:       src/lib/apr/lib apr_hash.c
               src/lib/apr/include apr_hash.h
  Log:
  Import support for hash tables.
  
  Revision  Changes    Path
  1.100     +4 -0      apache-2.0/src/CHANGES
  
  Index: CHANGES
  ===================================================================
  RCS file: /home/cvs/apache-2.0/src/CHANGES,v
  retrieving revision 1.99
  retrieving revision 1.100
  diff -u -r1.99 -r1.100
  --- CHANGES	2000/05/03 10:32:45	1.99
  +++ CHANGES	2000/05/03 17:24:40	1.100
  @@ -1,5 +1,9 @@
   Changes with Apache 2.0a4-dev
   
  +  *) Introduce a hash table implementation into APR to be used for
  +     replacing tables and other random data structures in Apache.
  +     [Tony Finch]
  +
     *) Add some more error reporting to htpasswd in the case of problems
        generating or accessing the temporary file.  [Ken Coar]
   
  
  
  
  1.18      +6 -0      apache-2.0/src/lib/apr/lib/Makefile.in
  
  Index: Makefile.in
  ===================================================================
  RCS file: /home/cvs/apache-2.0/src/lib/apr/lib/Makefile.in,v
  retrieving revision 1.17
  retrieving revision 1.18
  diff -u -r1.17 -r1.18
  --- Makefile.in	2000/04/30 17:58:26	1.17
  +++ Makefile.in	2000/05/03 17:24:42	1.18
  @@ -24,6 +24,7 @@
   	apr_signal.o \
   	apr_snprintf.o \
   	apr_tables.o \
  +	apr_hash.o \
   	apr_getpass.o \
   	apr_strnatcmp.o
   
  @@ -73,6 +74,11 @@
    $(INCDIR)/apr_lib.h $(INCDIR)/apr_general.h $(INCDIR)/apr.h \
    $(INCDIR)/apr_errno.h $(INCDIR)/apr_file_io.h $(INCDIR)/apr_time.h \
    $(INCDIR)/apr_thread_proc.h
  +apr_hash.o: apr_hash.c $(INCDIR)/apr_private.h \
  + $(INCDIR)/apr_general.h $(INCDIR)/apr.h $(INCDIR)/apr_errno.h \
  + $(INCDIR)/apr_pools.h $(INCDIR)/apr_lib.h $(INCDIR)/apr_file_io.h \
  + $(INCDIR)/apr_time.h $(INCDIR)/apr_thread_proc.h \
  + $(INCDIR)/apr_hash.h
   apr_md5.o: apr_md5.c $(INCDIR)/apr_private.h $(INCDIR)/apr_md5.h \
    $(INCDIR)/apr_lib.h $(INCDIR)/apr_general.h $(INCDIR)/apr.h \
    $(INCDIR)/apr_errno.h $(INCDIR)/apr_file_io.h $(INCDIR)/apr_time.h \
  
  
  
  1.1                  apache-2.0/src/lib/apr/lib/apr_hash.c
  
  Index: apr_hash.c
  ===================================================================
  /* ====================================================================
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2000 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution,
   *    if any, must include the following acknowledgment:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgment may appear in the software itself,
   *    if and wherever such third-party acknowledgments normally appear.
   *
   * 4. The names "Apache" and "Apache Software Foundation" must
   *    not be used to endorse or promote products derived from this
   *    software without prior written permission. For written
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache",
   *    nor may "Apache" appear in their name, without prior written
   *    permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   *
   * Portions of this software are based upon public domain software
   * originally written at the National Center for Supercomputing Applications,
   * University of Illinois, Urbana-Champaign.
   */
  
  #include "apr_private.h"
  
  #include "apr_general.h"
  #include "apr_pools.h"
  
  #include "apr_hash.h"
  
  #ifdef HAVE_STDLIB_H
  #include <stdlib.h>
  #endif
  #ifdef HAVE_STDLIB_H
  #include <string.h>
  #endif
  
  
  /*
   * The internal form of a hash table.
   *
   * The table is an array indexed by the hash of the key; collisions
   * are resolved by hanging a linked list of hash entries off each
   * element of the array. Although this is a really simple design it
   * isn't too bad given that pools have a low allocation overhead.
   */
  
  typedef struct ap_hash_entry_t ap_hash_entry_t;
  
  struct ap_hash_entry_t {
      ap_hash_entry_t	*next;
      int			 hash;
      void		*key;
      size_t		 klen;
      void		*val;
  };
  
  /*
   * The size of the array is always a power of two. We use the maximum
   * index rather than the size so that we can use bitwise-AND for
   * modular arithmetic.
   * The count of hash entries may be greater depending on the chosen
   * collision rate.
   */
  struct ap_hash_t {
      ap_pool_t		 *pool;
      ap_hash_entry_t	**array;
      size_t		  count, max;
  };
  #define INITIAL_MAX 15 /* tunable == 2^n - 1 */
  
  /*
   * Data structure for iterating through a hash table.
   *
   * We keep a pointer to the next hash entry here to allow the current
   * hash entry to be freed or otherwise mangled between calls to
   * ap_hash_next().
   */
  struct ap_hash_index_t {
      ap_hash_t		*ht;
      ap_hash_entry_t	*this, *next;
      int			 index;
  };
  
  
  /*
   * Hash creation functions.
   */
  
  static ap_hash_entry_t **alloc_array(ap_hash_t *ht)
  {
     return ap_pcalloc(ht->pool, sizeof(*ht->array) * (ht->max + 1));
  }
  
  API_EXPORT(ap_hash_t *) ap_make_hash(ap_pool_t *pool)
  {
      ap_hash_t *ht;
      ht = ap_palloc(ht->pool, sizeof(*ht));
      ht->pool = pool;
      ht->count = 0;
      ht->max = INITIAL_MAX;
      ht->array = alloc_array(ht);
      return ht;
  }
  
  
  /*
   * Hash iteration functions.
   */
  
  API_EXPORT(ap_hash_index_t *) ap_hash_next(ap_hash_index_t *hi)
  {
      hi->this = hi->next;
      while (!hi->this) {
  	if (hi->index > hi->ht->max)
  	    return NULL;
  	hi->this = hi->ht->array[hi->index++];
      }
      hi->next = hi->this->next;
      return hi;
  }
  
  API_EXPORT(ap_hash_index_t *) ap_hash_first(ap_hash_t *ht)
  {
      ap_hash_index_t *hi;
      hi = ap_palloc(ht->pool, sizeof(*hi));
      hi->ht = ht;
      hi->index = 0;
      hi->this = NULL;
      hi->next = NULL;
      return ap_hash_next(hi);
  }
  
  API_EXPORT(void) ap_hash_this(ap_hash_index_t *hi,
  			      void  **key,
  			      size_t *klen,
  			      void  **val)
  {
      if (key)  *key  = hi->this->key;
      if (klen) *klen = hi->this->klen;
      if (val)  *val  = hi->this->val;
  }
  
  
  /*
   * Resizing a hash table
   */
  
  static void resize_array(ap_hash_t *ht)
  {
      ap_hash_index_t *hi;
      ap_hash_entry_t **new_array;
      int i;
  
      new_array = alloc_array(ht);
      for (hi = ap_hash_first(ht); hi; hi = ap_hash_next(hi)) {
  	i = hi->this->hash & ht->max;
  	hi->this->next = new_array[i];
  	new_array[i] = hi->this;
      }	       
      ht->array = new_array;
  }
  
  /*
   * This is where we keep the details of the hash function and control
   * the maximum collision rate.
   *
   * If val is non-NULL it creates and initializes a new hash entry if
   * there isn't already one there; it returns an updatable pointer so
   * that hash entries can be removed.
   */
  
  static ap_hash_entry_t **find_entry(ap_hash_t *ht,
  				    void *key,
  				    size_t klen,
  				    void *val)
  {
      ap_hash_entry_t **hep, *he;
      unsigned char *p;
      int hash;
      int i;
  
      /*
       * This hash function is used by perl 5; RSE attributes it to DJB.
       * (See Message-ID: <19991013131827.A17702@engelschall.com>
       * in the new-httpd archive for October 1999.)
       */
      hash = 0;
      for (p = key, i = klen; i; i--, p++)
  	hash = hash * 33 + *p;
      
      /* scan linked list */
      for (hep = &ht->array[hash & ht->max], he = *hep;
  	 he;
  	 hep = &he->next, he = *hep) {
  	if (he->hash == hash &&
  	    he->klen == klen &&
  	    memcmp(he->key, key, klen) == 0)
  	    break;
      }
      if (he || !val)
  	return hep;
      /* add a new entry for non-NULL values */
      he = ap_pcalloc(ht->pool, sizeof(*he));
      he->hash = hash;
      he->key  = key;
      he->klen = klen;
      he->val  = val;
      *hep = he;
      /* check that the collision rate isn't too high */
      if (++ht->count > ht->max) {
  	ht->max = ht->max * 2 + 1;
  	resize_array(ht);
      }
      return hep;
  }
  
  API_EXPORT(void *) ap_hash_get(ap_hash_t *ht,
  			       void *key,
  			       size_t klen)
  {
      ap_hash_entry_t *he;
      he = *find_entry(ht, key, klen, NULL);
      if (he)
  	return he->val;
      else
  	return NULL;
  }
  
  API_EXPORT(void) ap_hash_set(ap_hash_t *ht,
  			     void *key,
  			     size_t klen,
  			     void *val)
  {
      ap_hash_entry_t **hep;
      hep = find_entry(ht, key, klen, val);
      if (*hep && !val)
  	/* delete entry */
  	*hep = (*hep)->next;
  }
  
  
  
  1.1                  apache-2.0/src/lib/apr/include/apr_hash.h
  
  Index: apr_hash.h
  ===================================================================
  /* ====================================================================
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2000 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution,
   *    if any, must include the following acknowledgment:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgment may appear in the software itself,
   *    if and wherever such third-party acknowledgments normally appear.
   *
   * 4. The names "Apache" and "Apache Software Foundation" must
   *    not be used to endorse or promote products derived from this
   *    software without prior written permission. For written
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache",
   *    nor may "Apache" appear in their name, without prior written
   *    permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   *
   * Portions of this software are based upon public domain software
   * originally written at the National Center for Supercomputing Applications,
   * University of Illinois, Urbana-Champaign.
   */
  
  #ifndef ap_HASH_H
  #define ap_HASH_H
  
  #ifdef __cplusplus
  extern "C" {
  #endif
  
  /*
   * Abstract type for hash tables.
   */
  typedef struct ap_hash_t ap_hash_t;
  
  /*
   * Abstract type for scanning hash tables.
   */
  typedef struct ap_hash_index_t ap_hash_index_t;
  
  /*
  
  =head1 ap_hash_t *ap_make_hash(ap_pool_t *pool)
  
  B<Create a hash table within a pool.>
  
  =cut
  */
  ap_hash_t *ap_make_hash(ap_pool_t *pool);
  
  /*
  
  =head1 void ap_hash_set(ap_hash_t *ht, void *key, size_t klen, void *val)
  
  B<Associate a value with a key in a hash table.>
  
      arg 1) The hash table
      arg 2) Pointer to the key
      arg 3) Length of the key
      arg 4) Value to associate with the key
  
  If the value is NULL the hash entry is deleted.
  
  =cut
  */
  void ap_hash_set(ap_hash_t *ht, void *key, size_t klen, void *val);
  
  /*
  
  =head1 void *ap_hash_get(ap_hash_t *ht, void *key, size_t klen)
  
  B<Look up the value associated with a key in a hash table.>
  
      arg 1) The hash table
      arg 2) Pointer to the key
      arg 3) Length of the key
  
  Returns NULL if the key is not present.
  
  =cut
  */
  void *ap_hash_get(ap_hash_t *ht, void *key, size_t klen);
  
  /*
  
  =head1 ap_hash_index_t *ap_hash_first(ap_hash_t *ht)
  
  B<Start iterating over the entries in a hash table.>
  
      arg 1) The hash table
  
  Returns a pointer to the iteration state, or NULL if there are no
  entries.
  
  =cut
  */
  ap_hash_index_t *ap_hash_first(ap_hash_t *ht);
  
  /*
  
  =head1 ap_hash_index_t *ap_hash_next(ap_hash_index_t *hi)
  
  B<Continue iterating over the entries in a hash table.>
  
      arg 1) The iteration state
  
  Returns a pointer to the updated iteration state, or NULL if there are
  no more entries.
  
  */
  ap_hash_index_t *ap_hash_next(ap_hash_index_t *hi);
  
  /*
  
  =head1 void ap_hash_this(ap_hash_index_t *hi, void **key, size_t *klen, void **val)
  
  B<Get the current entry's details from the iteration state.>
  
      arg 1) The iteration state
      arg 2) Return pointer for the pointer to the key.
      arg 3) Return pointer for the key length.
      arg 4) Return pointer for the associated value.
  
  The return pointers should point to a variable that will be set to the
  corresponding data, or they may be NULL if the data isn't interesting.
  
  =cut
  */
  void ap_hash_this(ap_hash_index_t *hi, void **key, size_t *klen, void **val);
  
  /*
  
  =head2 Using the iteration functions
  
  Example:
  
      int sum_values(ap_hash_t *ht)
      {
          ap_hash_index_t *hi;
  	void *val;
  	int sum = 0;
  	for (hi = ap_hash_first(ht); hi; hi = ap_hash_next(hi)) {
  	    ap_hash_this(hi, NULL, NULL, &val);
  	    sum += *(int *)val;
  	}
  	return sum;
      }
  
  There is no restriction on adding or deleting hash entries during an
  iteration (although the results may be unpredictable unless all you do
  is delete the current entry) and multiple iterations can be in
  progress at the same time.
  
  =cut
  */
  
  #ifdef __cplusplus
  }
  #endif
  
  #endif	/* !ap_HASH_H */
  
  
  

Mime
View raw message