root/jlog_hash.h

Revision adf2ee8486352700056029fde45a13fc81b71218, 4.1 kB (checked in by Wez Furlong <wez.furlong@messagesystems.com>, 6 years ago)

Sync with the ecelerity jlog sources

  • Property mode set to 100644
Line 
1 /*
2  * Copyright (c) 2005-2008, Message Systems, 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 Message Systems, 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 #ifndef _JLOG_HASH_H
34 #define _JLOG_HASH_H
35
36 #include "jlog_config.h"
37
38 typedef void (*JLogHashFreeFunc)(void *);
39
40 typedef struct _jlog_hash_bucket {
41   const char *k;
42   int klen;
43   void *data;
44   struct _jlog_hash_bucket *next;
45 } jlog_hash_bucket;
46
47 typedef struct {
48   jlog_hash_bucket **buckets;
49   u_int32_t table_size;
50   u_int32_t initval;
51   u_int32_t num_used_buckets;
52   u_int32_t size;
53   unsigned dont_rebucket:1;
54   unsigned _spare:31;
55 } jlog_hash_table;
56
57 #define JLOG_HASH_EMPTY { NULL, 0, 0, 0, 0, 0, 0 }
58
59 typedef struct {
60   void *p2;
61   int p1;
62 } jlog_hash_iter;
63
64 #define JLOG_HASH_ITER_ZERO { NULL, 0 }
65
66 void jlog_hash_init(jlog_hash_table *h);
67 /* NOTE! "k" and "data" MUST NOT be transient buffers, as the hash table
68  * implementation does not duplicate them.  You provide a pair of
69  * JLogHashFreeFunc functions to free up their storage when you call
70  * jlog_hash_delete(), jlog_hash_delete_all() or jlog_hash_destroy().
71  * */
72 int jlog_hash_store(jlog_hash_table *h, const char *k, int klen, void *data);
73 int jlog_hash_replace(jlog_hash_table *h, const char *k, int klen, void *data,
74                       JLogHashFreeFunc keyfree, JLogHashFreeFunc datafree);
75 int jlog_hash_retrieve(jlog_hash_table *h, const char *k, int klen, void **data);
76 int jlog_hash_delete(jlog_hash_table *h, const char *k, int klen,
77                      JLogHashFreeFunc keyfree, JLogHashFreeFunc datafree);
78 void jlog_hash_delete_all(jlog_hash_table *h, JLogHashFreeFunc keyfree,
79                           JLogHashFreeFunc datafree);
80 void jlog_hash_destroy(jlog_hash_table *h, JLogHashFreeFunc keyfree,
81                        JLogHashFreeFunc datafree);
82
83 /* This is an iterator and requires the hash to not be written to during the
84    iteration process.
85    To use:
86      jlog_hash_iter iter = JLOG_HASH_ITER_ZERO;
87
88      const char *k;
89      int klen;
90      void *data;
91
92      while(jlog_hash_next(h, &iter, &k, &klen, &data)) {
93        .... use k, klen and data ....
94      }
95 */
96 int jlog_hash_next(jlog_hash_table *h, jlog_hash_iter *iter,
97                    const char **k, int *klen, void **data);
98 int jlog_hash_firstkey(jlog_hash_table *h, const char **k, int *klen);
99 int jlog_hash_nextkey(jlog_hash_table *h, const char **k, int *klen, const char *lk, int lklen);
100
101 /* This function serves no real API use sans calculating expected buckets
102    for keys (or extending the hash... which is unsupported) */
103 u_int32_t jlog_hash__hash(const char *k, u_int32_t length, u_int32_t initval);
104 jlog_hash_bucket *jlog_hash__new_bucket(const char *k, int klen, void *data);
105 void jlog_hash__rebucket(jlog_hash_table *h, int newsize);
106 #endif
Note: See TracBrowser for help on using the browser.