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