]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/lib/htable.h
Fix seg fault in SQlite driver
[bacula/bacula] / bacula / src / lib / htable.h
1 /*
2    Bacula® - The Network Backup Solution
3
4    Copyright (C) 2004-2008 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 two of the GNU 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 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  *
30  * Written by Kern Sibbald, MMIV
31  *
32  *   Version $Id$
33  */
34
35 /* ========================================================================
36  *
37  *   Hash table class -- htable
38  *
39  */
40
41 /* 
42  * BIG_MALLOC is to provide a large malloc service to htable
43  */
44 #define BIG_MALLOC
45
46 /*
47  * Loop var through each member of table
48  */
49 #ifdef HAVE_TYPEOF
50 #define foreach_htable(var, tbl) \
51         for((var)=(typeof(var))((tbl)->first()); \
52            (var); \
53            (var)=(typeof(var))((tbl)->next()))
54 #else
55 #define foreach_htable(var, tbl) \
56         for((*((void **)&(var))=(void *)((tbl)->first())); \
57             (var); \
58             (*((void **)&(var))=(void *)((tbl)->next())))
59 #endif
60
61
62
63 struct hlink {
64    void *next;                        /* next hash item */
65    char *key;                         /* key this item */
66    uint32_t hash;                     /* hash for this key */
67 };
68
69 struct h_mem {
70    struct h_mem *next;                /* next buffer */
71    int32_t rem;                       /* remaining bytes in big_buffer */
72    char *mem;                         /* memory pointer */
73    char first[1];                     /* first byte */
74 };
75
76 class htable : public SMARTALLOC {
77    hlink **table;                     /* hash table */
78    int loffset;                       /* link offset in item */
79    uint32_t num_items;                /* current number of items */
80    uint32_t max_items;                /* maximum items before growing */
81    uint32_t buckets;                  /* size of hash table */
82    uint32_t hash;                     /* temp storage */
83    uint32_t index;                    /* temp storage */
84    uint32_t mask;                     /* "remainder" mask */
85    uint32_t rshift;                   /* amount to shift down */
86    hlink *walkptr;                    /* table walk pointer */
87    uint32_t walk_index;               /* table walk index */
88    uint32_t total_size;               /* total bytes malloced */
89    uint32_t blocks;                   /* blocks malloced */
90 #ifdef BIG_MALLOC
91    struct h_mem *mem_block;           /* malloc'ed memory block chain */
92    void malloc_big_buf(int size);     /* Get a big buffer */
93 #endif
94    void hash_index(char *key);        /* produce hash key,index */
95    void grow_table();                 /* grow the table */
96
97 public:
98    htable(void *item, void *link, int tsize = 31);
99    ~htable() { destroy(); }
100    void init(void *item, void *link, int tsize = 31);
101    bool  insert(char *key, void *item);
102    void *lookup(char *key);
103    void *first();                     /* get first item in table */
104    void *next();                      /* get next item in table */
105    void destroy();
106    void stats();                      /* print stats about the table */
107    uint32_t size();                   /* return size of table */
108    char *hash_malloc(int size);       /* malloc bytes for a hash entry */
109 #ifdef BIG_MALLOC
110    void hash_big_free();              /* free all hash allocated big buffers */
111 #endif
112 };