]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/lib/htable.h
Added support to htable for giving a hint on the number of pages to allocate for...
[bacula/bacula] / bacula / src / lib / htable.h
1 /*
2    Bacula® - The Network Backup Solution
3
4    Copyright (C) 2004-2011 Free Software Foundation Europe e.V.
5
6    The main author of Bacula is Kern Sibbald, with contributions from
7    many others, a complete list can be found in the file AUTHORS.
8    This program is Free Software; you can redistribute it and/or
9    modify it under the terms of version three of the GNU Affero General Public
10    License as published by the Free Software Foundation and included
11    in the file LICENSE.
12
13    This program is distributed in the hope that it will be useful, but
14    WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16    General Public License for more details.
17
18    You should have received a copy of the GNU Affero General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21    02110-1301, USA.
22
23    Bacula® is a registered trademark of Kern Sibbald.
24    The licensor of Bacula is the Free Software Foundation Europe
25    (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zürich,
26    Switzerland, email:ftf@fsfeurope.org.
27 */
28 /*
29  * Written by Kern Sibbald, MMIV
30  */
31
32 #ifndef HTABLE_H
33 #define HTABLE_H
34
35 /* ========================================================================
36  *
37  *   Hash table class -- htable
38  *
39  */
40
41 /*
42  * Loop var through each member of table
43  */
44 #ifdef HAVE_TYPEOF
45 #define foreach_htable(var, tbl) \
46         for((var)=(typeof(var))((tbl)->first()); \
47            (var); \
48            (var)=(typeof(var))((tbl)->next()))
49 #else
50 #define foreach_htable(var, tbl) \
51         for((*((void **)&(var))=(void *)((tbl)->first())); \
52             (var); \
53             (*((void **)&(var))=(void *)((tbl)->next())))
54 #endif
55
56 typedef enum {
57    KEY_TYPE_CHAR = 1,
58    KEY_TYPE_UINT32 = 2,
59    KEY_TYPE_UINT64 = 3
60 } key_type_t;
61
62 union hlink_key {
63    char *char_key;
64    uint32_t uint32_key;
65    uint64_t uint64_key;
66 };
67
68 struct hlink {
69    void *next;                        /* next hash item */
70    key_type_t key_type;               /* type of key used to hash */
71    union hlink_key key;               /* key for this item */
72    uint64_t hash;                     /* hash for this key */
73 };
74
75 struct h_mem {
76    struct h_mem *next;                /* next buffer */
77    int32_t rem;                       /* remaining bytes in big_buffer */
78    char *mem;                         /* memory pointer */
79    char first[1];                     /* first byte */
80 };
81
82 class htable : public SMARTALLOC {
83    hlink **table;                     /* hash table */
84    int loffset;                       /* link offset in item */
85    hlink *walkptr;                    /* table walk pointer */
86    uint64_t hash;                     /* temp storage */
87    uint64_t total_size;               /* total bytes malloced */
88    uint32_t extend_length;            /* number of bytes to allocate when extending buffer */
89    uint32_t walk_index;               /* table walk index */
90    uint32_t num_items;                /* current number of items */
91    uint32_t max_items;                /* maximum items before growing */
92    uint32_t buckets;                  /* size of hash table */
93    uint32_t index;                    /* temp storage */
94    uint32_t mask;                     /* "remainder" mask */
95    uint32_t rshift;                   /* amount to shift down */
96    uint32_t blocks;                   /* blocks malloced */
97    struct h_mem *mem_block;           /* malloc'ed memory block chain */
98    void malloc_big_buf(int size);     /* Get a big buffer */
99    void hash_index(char *key);        /* produce hash key,index */
100    void hash_index(uint32_t key);     /* produce hash key,index */
101    void hash_index(uint64_t key);     /* produce hash key,index */
102    void grow_table();                 /* grow the table */
103
104 public:
105    htable(void *item, void *link, int tsize = 31, int nr_pages = 0);
106    ~htable() { destroy(); }
107    void init(void *item, void *link, int tsize = 31, int nr_pages = 0);
108    bool insert(char *key, void *item);
109    bool insert(uint32_t key, void *item);
110    bool insert(uint64_t key, void *item);
111    void *lookup(char *key);
112    void *lookup(uint32_t key);
113    void *lookup(uint64_t key);
114    void *first();                     /* get first item in table */
115    void *next();                      /* get next item in table */
116    void destroy();
117    void stats();                      /* print stats about the table */
118    uint32_t size();                   /* return size of table */
119    char *hash_malloc(int size);       /* malloc bytes for a hash entry */
120    void hash_big_free();              /* free all hash allocated big buffers */
121 };
122
123 #endif  /* HTABLE_H */