| 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 |
|
|---|
| 87 |
/* This is a convenience function only. It assumes that all keys and values |
|---|
| 88 |
* in the destination hash are strings and allocated with malloc() and |
|---|
| 89 |
* assumes that the source contains only keys and values that can be |
|---|
| 90 |
* suitably duplicated by strdup(). |
|---|
| 91 |
*/ |
|---|
| 92 |
void noit_hash_merge_as_dict(noit_hash_table *dst, noit_hash_table *src); |
|---|
| 93 |
|
|---|
| 94 |
/* This is an iterator and requires the hash to not be written to during the |
|---|
| 95 |
iteration process. |
|---|
| 96 |
To use: |
|---|
| 97 |
noit_hash_iter iter = NOIT_HASH_ITER_ZERO; |
|---|
| 98 |
|
|---|
| 99 |
const char *k; |
|---|
| 100 |
int klen; |
|---|
| 101 |
void *data; |
|---|
| 102 |
|
|---|
| 103 |
while(noit_hash_next(h, &iter, &k, &klen, &data)) { |
|---|
| 104 |
.... use k, klen and data .... |
|---|
| 105 |
} |
|---|
| 106 |
*/ |
|---|
| 107 |
int noit_hash_next(noit_hash_table *h, noit_hash_iter *iter, |
|---|
| 108 |
const char **k, int *klen, void **data); |
|---|
| 109 |
int noit_hash_next_str(noit_hash_table *h, noit_hash_iter *iter, |
|---|
| 110 |
const char **k, int *klen, const char **dstr); |
|---|
| 111 |
int noit_hash_firstkey(noit_hash_table *h, const char **k, int *klen); |
|---|
| 112 |
int noit_hash_nextkey(noit_hash_table *h, const char **k, int *klen, const char *lk, int lklen); |
|---|
| 113 |
|
|---|
| 114 |
/* This function serves no real API use sans calculating expected buckets |
|---|
| 115 |
for keys (or extending the hash... which is unsupported) */ |
|---|
| 116 |
u_int32_t noit_hash__hash(const char *k, u_int32_t length, u_int32_t initval); |
|---|
| 117 |
noit_hash_bucket *noit_hash__new_bucket(const char *k, int klen, void *data); |
|---|
| 118 |
void noit_hash__rebucket(noit_hash_table *h, int newsize); |
|---|
| 119 |
#endif |
|---|