]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/lib/htable.h
Fix #1548 about Solaris SIGBUS with accurate mode backup
[bacula/bacula] / bacula / src / lib / htable.h
1 /*
2    Bacula(R) - The Network Backup Solution
3
4    Copyright (C) 2000-2015 Kern Sibbald
5    Copyright (C) 2004-2014 Free Software Foundation Europe e.V.
6
7    The original author of Bacula is Kern Sibbald, with contributions
8    from many others, a complete list can be found in the file AUTHORS.
9
10    You may use this file and others of this release according to the
11    license defined in the LICENSE file, which includes the Affero General
12    Public License, v3.0 ("AGPLv3") and some additional permissions and
13    terms pursuant to its AGPLv3 Section 7.
14
15    This notice must be preserved when any source code is 
16    conveyed and/or propagated.
17
18    Bacula(R) is a registered trademark of Kern Sibbald.
19 */
20 /*
21  * Written by Kern Sibbald, MMIV
22  */
23
24 #ifndef _HTABLE_H_
25 #define _HTABLE_H_
26
27 /*
28  *
29  * Written by Kern Sibbald, MMIV
30  *
31  */
32
33 /* ========================================================================
34  *
35  *   Hash table class -- htable
36  *
37  */
38
39 /* 
40  * BIG_MALLOC is to provide a large malloc service to htable
41  */
42 #define BIG_MALLOC
43
44 /*
45  * Loop var through each member of table
46  */
47 #ifdef HAVE_TYPEOF
48 #define foreach_htable(var, tbl) \
49         for((var)=(typeof(var))((tbl)->first()); \
50            (var); \
51            (var)=(typeof(var))((tbl)->next()))
52 #else
53 #define foreach_htable(var, tbl) \
54         for((*((void **)&(var))=(void *)((tbl)->first())); \
55             (var); \
56             (*((void **)&(var))=(void *)((tbl)->next())))
57 #endif
58
59 union key_val {
60    char *key;                   /* char key */
61    uint64_t ikey;               /* integer key */
62 }; 
63
64 struct hlink {
65    void *next;                        /* next hash item */
66    uint64_t hash;                     /* 64 bit hash for this key */
67    union key_val key;                 /* key value this key */
68    bool is_ikey;                      /* set if integer key */
69 };
70
71 struct h_mem {
72    struct h_mem *next;                /* next buffer */
73    char   *mem;                       /* memory pointer */
74    int64_t rem;                       /* remaining bytes in big_buffer */
75    char first[1];                     /* first byte */
76 };
77
78 class htable : public SMARTALLOC {
79    hlink **table;                     /* hash table */
80    uint64_t hash;                     /* temp storage */
81    uint64_t total_size;               /* total bytes malloced */
82    int loffset;                       /* link offset in item */
83    uint32_t num_items;                /* current number of items */
84    uint32_t max_items;                /* maximum items before growing */
85    uint32_t buckets;                  /* size of hash table */
86    uint32_t index;                    /* temp storage */
87    uint32_t mask;                     /* "remainder" mask */
88    uint32_t rshift;                   /* amount to shift down */
89    hlink *walkptr;                    /* table walk pointer */
90    uint32_t walk_index;               /* table walk index */
91    uint32_t blocks;                   /* blocks malloced */
92 #ifdef BIG_MALLOC
93    struct h_mem *mem_block;           /* malloc'ed memory block chain */
94    void malloc_big_buf(int size);     /* Get a big buffer */
95 #endif
96    void hash_index(char *key);        /* produce hash key,index */
97    void hash_index(uint64_t ikey);    /* produce hash key,index */
98    void grow_table();                 /* grow the table */
99
100 public:
101    htable(void *item, void *link, int tsize = 31);
102    ~htable() { destroy(); }
103    void  init(void *item, void *link, int tsize = 31);
104    bool  insert(char *key, void *item);      /* char key */
105    bool  insert(uint64_t ikey, void *item);  /* 64 bit key */
106    void *lookup(char *key);                  /* char key */
107    void *lookup(uint64_t ikey);              /* 64 bit key */
108    void *first();                     /* get first item in table */
109    void *next();                      /* get next item in table */
110    void  destroy();
111    void  stats();                     /* print stats about the table */
112    uint32_t size();                   /* return size of table */
113    char *hash_malloc(int size);       /* malloc bytes for a hash entry */
114 #ifdef BIG_MALLOC
115    void hash_big_free();              /* free all hash allocated big buffers */
116 #endif
117 };
118
119 #endif