Branch data Line data Source code
1 : : // $Id: CompHash.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 comphash_h
6 : : #define comphash_h
7 : :
8 : : #include "Hash.h"
9 : : #include "Type.h"
10 : :
11 : : class ListVal;
12 : :
13 : : class CompositeHash {
14 : : public:
15 : : CompositeHash(TypeList* composite_type);
16 : : ~CompositeHash();
17 : :
18 : : // Compute the hash corresponding to the given index val,
19 : : // or 0 if it fails to typecheck.
20 : : HashKey* ComputeHash(const Val* v, int type_check) const;
21 : :
22 : : // Given a hash key, recover the values used to create it.
23 : : ListVal* RecoverVals(const HashKey* k) const;
24 : :
25 : 0 : unsigned int MemoryAllocation() const { return padded_sizeof(*this) + pad_size(size); }
26 : :
27 : : protected:
28 : : HashKey* ComputeSingletonHash(const Val* v, int type_check) const;
29 : :
30 : : // Computes the piece of the hash for Val*, returning the new kp.
31 : : // Used as a helper for ComputeHash in the non-singleton case.
32 : : char* SingleValHash(int type_check, char* kp,
33 : : BroType* bt, Val* v) const;
34 : :
35 : : // Recovers just one Val of possibly many; called from RecoverVals.
36 : : // Upon return, pval will point to the recovered Val of type t.
37 : : // Returns and updated kp for the next Val. Calls internal_error()
38 : : // upon errors, so there is no return value for invalid input.
39 : : const char* RecoverOneVal(const HashKey* k,
40 : : const char* kp, const char* const k_end,
41 : : BroType* t, Val*& pval) const;
42 : :
43 : : // Rounds the given pointer up to the nearest multiple of the
44 : : // given size, if not already a multiple.
45 : : const void* Align(const char* ptr, unsigned int size) const;
46 : :
47 : : // Rounds the given pointer up to the nearest multiple of the
48 : : // given size, padding the skipped region with 0 bytes.
49 : : void* AlignAndPad(char* ptr, unsigned int size) const;
50 : :
51 : : // Returns offset+size rounded up so it can correctly align data
52 : : // of the given size.
53 : : int SizeAlign(int offset, unsigned int size) const;
54 : :
55 : : template<class T>
56 : 101063 : T* AlignAndPadType(char* ptr) const
57 : : {
58 : 101063 : return reinterpret_cast<T*>(AlignAndPad(ptr, sizeof(T)));
59 : : }
60 : :
61 : : template<class T>
62 : 4368 : const T* AlignType(const char* ptr) const
63 : : {
64 : 4368 : return reinterpret_cast<const T*>(Align(ptr, sizeof(T)));
65 : : }
66 : :
67 : : template<class T>
68 : : int SizeAlignType(int offset) const
69 : : {
70 : : return SizeAlign(offset, sizeof(T));
71 : : }
72 : :
73 : : // Compute the size of the composite key. If v is non-nil then
74 : : // the value is computed for the particular list of values.
75 : : // Returns 0 if the key has an indeterminant size (if v not given),
76 : : // or if v doesn't match the index type (if given).
77 : : int ComputeKeySize(const Val* v = 0, int type_check = 1) const;
78 : :
79 : : int SingleTypeKeySize(BroType*, const Val*,
80 : : int type_check, int sz) const;
81 : :
82 : : TypeList* type;
83 : : char* key; // space for composite key
84 : : int size;
85 : : int is_singleton; // if just one type in index
86 : :
87 : : // If one type, but not normal "singleton", e.g. record.
88 : : int is_complex_type;
89 : :
90 : : InternalTypeTag singleton_tag;
91 : : };
92 : :
93 : : #endif
|