00001 // $Id: Hash.h,v 1.4 2005/09/07 17:03:25 vern Exp $ 00002 // 00003 // Copyright (c) 1995, 1996, 1997, 1998, 1999, 2000, 2002 00004 // The Regents of the University of California. All rights reserved. 00005 // 00006 // Redistribution and use in source and binary forms, with or without 00007 // modification, are permitted provided that: (1) source code distributions 00008 // retain the above copyright notice and this paragraph in its entirety, (2) 00009 // distributions including binary code include the above copyright notice and 00010 // this paragraph in its entirety in the documentation or other materials 00011 // provided with the distribution, and (3) all advertising materials mentioning 00012 // features or use of this software display the following acknowledgement: 00013 // ``This product includes software developed by the University of California, 00014 // Lawrence Berkeley Laboratory and its contributors.'' Neither the name of 00015 // the University nor the names of its contributors may be used to endorse 00016 // or promote products derived from this software without specific prior 00017 // written permission. 00018 // THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED 00019 // WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF 00020 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. 00021 00022 #ifndef hash_h 00023 #define hash_h 00024 00025 #include <stdlib.h> 00026 00027 #include "BroString.h" 00028 00029 typedef unsigned int hash_t; 00030 00031 typedef enum { 00032 HASH_KEY_INT, 00033 HASH_KEY_DOUBLE, 00034 HASH_KEY_STRING 00035 #define NUM_HASH_KEYS (int(HASH_KEY_STRING) + 1) 00036 } HashKeyTag; 00037 00038 class HashKey { 00039 public: 00040 HashKey(bro_int_t i); 00041 HashKey(bro_uint_t u); 00042 #ifdef USE_INT64 00043 HashKey(uint32 u); 00044 #endif 00045 HashKey(const uint32 u[], int n); 00046 HashKey(double d); 00047 HashKey(const void* p); 00048 HashKey(const char* s); 00049 HashKey(const BroString* s); 00050 ~HashKey() 00051 { 00052 if ( is_our_dynamic ) 00053 delete [] (char *) key; 00054 } 00055 00056 // Create a HashKey given all of its components. "key" is assumed 00057 // to be dynamically allocated and to now belong to this HashKey 00058 // (to delete upon destruct'ing). If "copy_key" is true, it's 00059 // first copied. 00060 // 00061 // The calling sequence here is unusual (normally key would be 00062 // first) to avoid possible ambiguities with the next constructor, 00063 // which is the more commonly used one. 00064 HashKey(int copy_key, void* key, int size); 00065 00066 // Same, but automatically copies the key. 00067 HashKey(const void* key, int size, hash_t hash); 00068 00069 // Builds a key from the given chunk of bytes. 00070 HashKey(const void* bytes, int size); 00071 00072 // Create a Hashkey given all of its components *without* 00073 // copying the key and *without* taking ownership. Note that 00074 // "dont_copy" is a type placeholder to differentiate this member 00075 // function from the one above; its value is not used. 00076 HashKey(const void* key, int size, hash_t hash, bool dont_copy); 00077 00078 // Hands over the key to the caller. This means that if the 00079 // key is our dynamic, we give it to the caller and mark it 00080 // as not our dynamic. If initially it's not our dynamic, 00081 // we give them a copy of it. 00082 void* TakeKey(); 00083 00084 const void* Key() const { return key; } 00085 int Size() const { return size; } 00086 hash_t Hash() const { return hash; } 00087 00088 unsigned int MemoryAllocation() const { return padded_sizeof(*this) + pad_size(size); } 00089 00090 static hash_t HashBytes(const void* bytes, int size); 00091 protected: 00092 void* CopyKey(const void* key, int size) const; 00093 00094 union { 00095 bro_int_t i; 00096 #ifdef USE_INT64 00097 uint32 u32; 00098 #endif 00099 double d; 00100 const void* p; 00101 } key_u; 00102 00103 void* key; 00104 int is_our_dynamic; 00105 int size, hash; 00106 }; 00107 00108 extern int hash_cnt_all, hash_cnt_uhash; 00109 extern void init_hash_function(); 00110 00111 #endif
1.3.5