root/src/utils/noit_hash.h

Revision 01751d3c6a2df6acc30c50e9cd1cce9064262450, 4.2 kB (checked in by Theo Schlossnagle <jesus@omniti.com>, 7 years ago)

still nothing working, but substantially more plumbing

  • 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 /* NOTE! "k" and "data" MUST NOT be transient buffers, as the hash table
70  * implementation does not duplicate them.  You provide a pair of
71  * NoitHashFreeFunc functions to free up their storage when you call
72  * noit_hash_delete(), noit_hash_delete_all() or noit_hash_destroy().
73  * */
74 int noit_hash_store(noit_hash_table *h, const char *k, int klen, void *data);
75 int noit_hash_replace(noit_hash_table *h, const char *k, int klen, void *data,
76                       NoitHashFreeFunc keyfree, NoitHashFreeFunc datafree);
77 int noit_hash_retrieve(noit_hash_table *h, const char *k, int klen, void **data);
78 int noit_hash_delete(noit_hash_table *h, const char *k, int klen,
79                      NoitHashFreeFunc keyfree, NoitHashFreeFunc datafree);
80 void noit_hash_delete_all(noit_hash_table *h, NoitHashFreeFunc keyfree,
81                           NoitHashFreeFunc datafree);
82 void noit_hash_destroy(noit_hash_table *h, NoitHashFreeFunc keyfree,
83                        NoitHashFreeFunc datafree);
84
85 /* This is an iterator and requires the hash to not be written to during the
86    iteration process.
87    To use:
88      noit_hash_iter iter = NOIT_HASH_ITER_ZERO;
89
90      const char *k;
91      int klen;
92      void *data;
93
94      while(noit_hash_next(h, &iter, &k, &klen, &data)) {
95        .... use k, klen and data ....
96      }
97 */
98 int noit_hash_next(noit_hash_table *h, noit_hash_iter *iter,
99                    const char **k, int *klen, void **data);
100 int noit_hash_firstkey(noit_hash_table *h, const char **k, int *klen);
101 int noit_hash_nextkey(noit_hash_table *h, const char **k, int *klen, const char *lk, int lklen);
102
103 /* This function serves no real API use sans calculating expected buckets
104    for keys (or extending the hash... which is unsupported) */
105 u_int32_t noit_hash__hash(const char *k, u_int32_t length, u_int32_t initval);
106 noit_hash_bucket *noit_hash__new_bucket(const char *k, int klen, void *data);
107 void noit_hash__rebucket(noit_hash_table *h, int newsize);
108 #endif
Note: See TracBrowser for help on using the browser.