Branch data Line data Source code
1 : : // $Id: Hash.h 6219 2008-10-01 05:39:07Z vern $
2 : : //
3 : : // See the file "COPYING" in the main distribution directory for copyright.
4 : :
5 : : #ifndef hash_h
6 : : #define hash_h
7 : :
8 : : #include <stdlib.h>
9 : :
10 : : #include "BroString.h"
11 : :
12 : : #define UHASH_KEY_SIZE 32
13 : :
14 : : typedef unsigned int hash_t;
15 : :
16 : : typedef enum {
17 : : HASH_KEY_INT,
18 : : HASH_KEY_DOUBLE,
19 : : HASH_KEY_STRING
20 : : #define NUM_HASH_KEYS (int(HASH_KEY_STRING) + 1)
21 : : } HashKeyTag;
22 : :
23 : : class HashKey {
24 : : public:
25 : : HashKey(bro_int_t i);
26 : : HashKey(bro_uint_t u);
27 : : HashKey(uint32 u);
28 : : HashKey(const uint32 u[], int n);
29 : : HashKey(double d);
30 : : HashKey(const void* p);
31 : : HashKey(const char* s);
32 : : HashKey(const BroString* s);
33 : 254015 : ~HashKey()
34 : : {
35 [ + + ]: 254015 : if ( is_our_dynamic )
36 [ + - ]: 56306 : delete [] (char *) key;
37 : 254015 : }
38 : :
39 : : // Create a HashKey given all of its components. "key" is assumed
40 : : // to be dynamically allocated and to now belong to this HashKey
41 : : // (to delete upon destruct'ing). If "copy_key" is true, it's
42 : : // first copied.
43 : : //
44 : : // The calling sequence here is unusual (normally key would be
45 : : // first) to avoid possible ambiguities with the next constructor,
46 : : // which is the more commonly used one.
47 : : HashKey(int copy_key, void* key, int size);
48 : :
49 : : // Same, but automatically copies the key.
50 : : HashKey(const void* key, int size, hash_t hash);
51 : :
52 : : // Builds a key from the given chunk of bytes.
53 : : HashKey(const void* bytes, int size);
54 : :
55 : : // Create a Hashkey given all of its components *without*
56 : : // copying the key and *without* taking ownership. Note that
57 : : // "dont_copy" is a type placeholder to differentiate this member
58 : : // function from the one above; its value is not used.
59 : : HashKey(const void* key, int size, hash_t hash, bool dont_copy);
60 : :
61 : : // Hands over the key to the caller. This means that if the
62 : : // key is our dynamic, we give it to the caller and mark it
63 : : // as not our dynamic. If initially it's not our dynamic,
64 : : // we give them a copy of it.
65 : : void* TakeKey();
66 : :
67 : 204075 : const void* Key() const { return key; }
68 : 249552 : int Size() const { return size; }
69 : 247898 : hash_t Hash() const { return hash; }
70 : :
71 : 0 : unsigned int MemoryAllocation() const { return padded_sizeof(*this) + pad_size(size); }
72 : :
73 : : static hash_t HashBytes(const void* bytes, int size);
74 : : protected:
75 : : void* CopyKey(const void* key, int size) const;
76 : :
77 : : union {
78 : : bro_int_t i;
79 : : uint32 u32;
80 : : double d;
81 : : const void* p;
82 : : } key_u;
83 : :
84 : : void* key;
85 : : int is_our_dynamic;
86 : : int size, hash;
87 : : };
88 : :
89 : : extern int hash_cnt_all, hash_cnt_uhash;
90 : : extern void init_hash_function();
91 : :
92 : : #endif
|