root/src/utils/noit_hash.h

Revision 4498d918252e2d09b9873956ea6fe075b91b8939, 4.9 kB (checked in by Theo Schlossnagle <jesus@omniti.com>, 7 months ago)

Add a noit_hash_size function in prep for changing the noit_hash implementation.

  • Property mode set to 100644
Line 
1 /*
2  * Copyright (c) 2005-2007, OmniTI Computer Consulting, Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are
7  * met:
8  *
9  *    * Redistributions of source code must retain the above copyright
10  *      notice, this list of conditions and the following disclaimer.
11  *    * Redistributions in binary form must reproduce the above
12  *      copyright notice, this list of conditions and the following
13  *      disclaimer in the documentation and/or other materials provided
14  *      with the distribution.
15  *    * Neither the name OmniTI Computer Consulting, Inc. nor the names
16  *      of its contributors may be used to endorse or promote products
17  *      derived from this software without specific prior written
18  *      permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32
33 /* this is just a copy (with search and replace) from jlog_hash */
34
35 #ifndef _NOIT_HASH_H
36 #define _NOIT_HASH_H
37
38 #include "noit_config.h"
39
40 typedef void (*NoitHashFreeFunc)(void *);
41
42 typedef struct _noit_hash_bucket {
43   const char *k;
44   int klen;
45   void *data;
46   struct _noit_hash_bucket *next;
47 } noit_hash_bucket;
48
49 typedef struct {
50   noit_hash_bucket **buckets;
51   u_int32_t table_size;
52   u_int32_t initval;
53   u_int32_t num_used_buckets;
54   u_int32_t size;
55   unsigned dont_rebucket:1;
56   unsigned _spare:31;
57 } noit_hash_table;
58
59 #define NOIT_HASH_EMPTY { NULL, 0, 0, 0, 0, 0, 0 }
60
61 typedef struct {
62   void *p2;
63   int p1;
64 } noit_hash_iter;
65
66 #define NOIT_HASH_ITER_ZERO { NULL, 0 }
67
68 void noit_hash_init(noit_hash_table *h);
69 void noit_hash_init_size(noit_hash_table *h, int size);
70 /* NOTE! "k" and "data" MUST NOT be transient buffers, as the hash table
71  * implementation does not duplicate them.  You provide a pair of
72  * NoitHashFreeFunc functions to free up their storage when you call
73  * noit_hash_delete(), noit_hash_delete_all() or noit_hash_destroy().
74  * */
75 int noit_hash_store(noit_hash_table *h, const char *k, int klen, void *data);
76 int noit_hash_replace(noit_hash_table *h, const char *k, int klen, void *data,
77                       NoitHashFreeFunc keyfree, NoitHashFreeFunc datafree);
78 int noit_hash_retrieve(noit_hash_table *h, const char *k, int klen, void **data);
79 int noit_hash_retr_str(noit_hash_table *h, const char *k, int klen, const char **dstr);
80 int noit_hash_delete(noit_hash_table *h, const char *k, int klen,
81                      NoitHashFreeFunc keyfree, NoitHashFreeFunc datafree);
82 void noit_hash_delete_all(noit_hash_table *h, NoitHashFreeFunc keyfree,
83                           NoitHashFreeFunc datafree);
84 void noit_hash_destroy(noit_hash_table *h, NoitHashFreeFunc keyfree,
85                        NoitHashFreeFunc datafree);
86 int noit_hash_size(noit_hash_table *h);
87
88 /* This is a convenience function only.  It assumes that all keys and values
89  * in the destination hash are strings and allocated with malloc() and
90  * assumes that the source contains only keys and values that can be
91  * suitably duplicated by strdup().
92  */
93 void noit_hash_merge_as_dict(noit_hash_table *dst, noit_hash_table *src);
94
95 /* This is an iterator and requires the hash to not be written to during the
96    iteration process.
97    To use:
98      noit_hash_iter iter = NOIT_HASH_ITER_ZERO;
99
100      const char *k;
101      int klen;
102      void *data;
103
104      while(noit_hash_next(h, &iter, &k, &klen, &data)) {
105        .... use k, klen and data ....
106      }
107 */
108 int noit_hash_next(noit_hash_table *h, noit_hash_iter *iter,
109                    const char **k, int *klen, void **data);
110 int noit_hash_next_str(noit_hash_table *h, noit_hash_iter *iter,
111                        const char **k, int *klen, const char **dstr);
112 int noit_hash_firstkey(noit_hash_table *h, const char **k, int *klen);
113 int noit_hash_nextkey(noit_hash_table *h, const char **k, int *klen, const char *lk, int lklen);
114
115 /* This function serves no real API use sans calculating expected buckets
116    for keys (or extending the hash... which is unsupported) */
117 u_int32_t noit_hash__hash(const char *k, u_int32_t length, u_int32_t initval);
118 noit_hash_bucket *noit_hash__new_bucket(const char *k, int klen, void *data);
119 void noit_hash__rebucket(noit_hash_table *h, int newsize);
120 #endif
Note: See TracBrowser for help on using the browser.