]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/lib/htable.h
First incomplete cut of big malloc blocks for htable.
[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 John Walker.
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  *   not yet implemented, and not yet working.
44  */
45 //#define BIG_MALLOC
46
47 /*
48  * Loop var through each member of table
49  */
50 #define foreach_htable(var, tbl) \
51         for((*((void **)&(var))=(void *)((tbl)->first())); \
52             (var); \
53             (*((void **)&(var))=(void *)((tbl)->next())))
54
55 struct hlink {
56    void *next;                        /* next hash item */
57    char *key;                         /* key this item */
58    uint32_t hash;                     /* hash for this key */
59 };
60
61 struct h_mem {
62    struct h_mem *next;                /* next buffer */
63    int rem;                           /* remaining bytes */
64    char *mem;                         /* memory pointer */
65    char first[1];                     /* first byte */
66 };
67
68 class htable : public SMARTALLOC {
69    hlink **table;                     /* hash table */
70    int loffset;                       /* link offset in item */
71    uint32_t num_items;                /* current number of items */
72    uint32_t max_items;                /* maximum items before growing */
73    uint32_t buckets;                  /* size of hash table */
74    uint32_t hash;                     /* temp storage */
75    uint32_t index;                    /* temp storage */
76    uint32_t mask;                     /* "remainder" mask */
77    uint32_t rshift;                   /* amount to shift down */
78    hlink *walkptr;                    /* table walk pointer */
79    uint32_t walk_index;               /* table walk index */
80    uint32_t total_size;               /* total bytes malloced */
81    uint32_t blocks;                   /* blocks malloced */
82 #ifdef BIG_MALLOC
83    struct h_mem *mem;                 /* malloced memory blocks */
84    void malloc_buf(int size);         /* Get a bit buffer */
85 #endif
86    void hash_index(char *key);        /* produce hash key,index */
87    void grow_table();                 /* grow the table */
88 public:
89    htable(void *item, void *link, int tsize = 31);
90    ~htable() { destroy(); }
91    void init(void *item, void *link, int tsize = 31);
92    bool  insert(char *key, void *item);
93    void *lookup(char *key);
94    void *first();                     /* get first item in table */
95    void *next();                      /* get next item in table */
96    void destroy();
97    void stats();                      /* print stats about the table */
98    uint32_t size();                   /* return size of table */
99 #ifdef BIG_MALLOC
100    char *hash_alloc(int size);        /* malloc bytes for a hash entry */
101    void hash_free();                  /* free all hash allocated bytes */
102 #endif
103 };