Kill the LLVM global lock.
[oota-llvm.git] / include / llvm / Support / OnDiskHashTable.h
index 54adc89de7f70f9a755a394aa70735466def32a8..f6d43a440788d28ecf337d30d29e08408e696f20 100644 (file)
@@ -39,41 +39,45 @@ namespace llvm {
 ///   typedef ExampleKey &key_type_ref;
 ///   typedef ExampleData data_type; // Must be copy constructible
 ///   typedef ExampleData &data_type_ref;
+///   typedef uint32_t hash_value_type; // The type the hash function returns.
+///   typedef uint32_t offset_type; // The type for offsets into the table.
 ///
 ///   /// Calculate the hash for Key
-///   static unsigned ComputeHash(key_type_ref Key);
+///   static hash_value_type ComputeHash(key_type_ref Key);
 ///   /// Return the lengths, in bytes, of the given Key/Data pair.
-///   static std::pair<unsigned, unsigned>
+///   static std::pair<offset_type, offset_type>
 ///   EmitKeyDataLength(raw_ostream &Out, key_type_ref Key, data_type_ref Data);
 ///   /// Write Key to Out.  KeyLen is the length from EmitKeyDataLength.
-///   static void EmitKey(raw_ostream &Out, key_type_ref Key, unsigned KeyLen);
+///   static void EmitKey(raw_ostream &Out, key_type_ref Key,
+///                       offset_type KeyLen);
 ///   /// Write Data to Out.  DataLen is the length from EmitKeyDataLength.
 ///   static void EmitData(raw_ostream &Out, key_type_ref Key,
-///                        data_type_ref Data, unsigned DataLen);
+///                        data_type_ref Data, offset_type DataLen);
 /// };
 /// \endcode
 template <typename Info> class OnDiskChainedHashTableGenerator {
-  unsigned NumBuckets;
-  unsigned NumEntries;
-  llvm::BumpPtrAllocator BA;
-
   /// \brief A single item in the hash table.
   class Item {
   public:
     typename Info::key_type Key;
     typename Info::data_type Data;
     Item *Next;
-    const uint32_t Hash;
+    const typename Info::hash_value_type Hash;
 
     Item(typename Info::key_type_ref Key, typename Info::data_type_ref Data,
          Info &InfoObj)
-        : Key(Key), Data(Data), Next(0), Hash(InfoObj.ComputeHash(Key)) {}
+        : Key(Key), Data(Data), Next(nullptr), Hash(InfoObj.ComputeHash(Key)) {}
   };
 
+  typedef typename Info::offset_type offset_type;
+  offset_type NumBuckets;
+  offset_type NumEntries;
+  llvm::SpecificBumpPtrAllocator<Item> BA;
+
   /// \brief A linked list of values in a particular hash bucket.
   class Bucket {
   public:
-    uint32_t Off;
+    offset_type Off;
     Item *Head;
     unsigned Length;
 
@@ -95,10 +99,10 @@ private:
   void resize(size_t NewSize) {
     Bucket *NewBuckets = (Bucket *)std::calloc(NewSize, sizeof(Bucket));
     // Populate NewBuckets with the old entries.
-    for (unsigned I = 0; I < NumBuckets; ++I)
+    for (size_t I = 0; I < NumBuckets; ++I)
       for (Item *E = Buckets[I].Head; E;) {
         Item *N = E->Next;
-        E->Next = 0;
+        E->Next = nullptr;
         insert(NewBuckets, NewSize, E);
         E = N;
       }
@@ -125,12 +129,11 @@ public:
     ++NumEntries;
     if (4 * NumEntries >= 3 * NumBuckets)
       resize(NumBuckets * 2);
-    insert(Buckets, NumBuckets,
-           new (BA.Allocate<Item>()) Item(Key, Data, InfoObj));
+    insert(Buckets, NumBuckets, new (BA.Allocate()) Item(Key, Data, InfoObj));
   }
 
   /// \brief Emit the table to Out, which must not be at offset 0.
-  uint32_t Emit(raw_ostream &Out) {
+  offset_type Emit(raw_ostream &Out) {
     Info InfoObj;
     return Emit(Out, InfoObj);
   }
@@ -138,12 +141,12 @@ public:
   /// \brief Emit the table to Out, which must not be at offset 0.
   ///
   /// Uses the provided Info instead of a stack allocated one.
-  uint32_t Emit(raw_ostream &Out, Info &InfoObj) {
+  offset_type Emit(raw_ostream &Out, Info &InfoObj) {
     using namespace llvm::support;
     endian::Writer<little> LE(Out);
 
     // Emit the payload of the table.
-    for (unsigned I = 0; I < NumBuckets; ++I) {
+    for (offset_type I = 0; I < NumBuckets; ++I) {
       Bucket &B = Buckets[I];
       if (!B.Head)
         continue;
@@ -158,8 +161,8 @@ public:
 
       // Write out the entries in the bucket.
       for (Item *I = B.Head; I; I = I->Next) {
-        LE.write<uint32_t>(I->Hash);
-        const std::pair<unsigned, unsigned> &Len =
+        LE.write<typename Info::hash_value_type>(I->Hash);
+        const std::pair<offset_type, offset_type> &Len =
             InfoObj.EmitKeyDataLength(Out, I->Key, I->Data);
         InfoObj.EmitKey(Out, I->Key, Len.first);
         InfoObj.EmitData(Out, I->Key, I->Data, Len.second);
@@ -167,17 +170,17 @@ public:
     }
 
     // Pad with zeros so that we can start the hashtable at an aligned address.
-    uint32_t TableOff = Out.tell();
-    uint64_t N = llvm::OffsetToAlignment(TableOff, alignOf<uint32_t>());
+    offset_type TableOff = Out.tell();
+    uint64_t N = llvm::OffsetToAlignment(TableOff, alignOf<offset_type>());
     TableOff += N;
     while (N--)
       LE.write<uint8_t>(0);
 
     // Emit the hashtable itself.
-    LE.write<uint32_t>(NumBuckets);
-    LE.write<uint32_t>(NumEntries);
-    for (unsigned I = 0; I < NumBuckets; ++I)
-      LE.write<uint32_t>(Buckets[I].Off);
+    LE.write<offset_type>(NumBuckets);
+    LE.write<offset_type>(NumEntries);
+    for (offset_type I = 0; I < NumBuckets; ++I)
+      LE.write<offset_type>(Buckets[I].Off);
 
     return TableOff;
   }
@@ -205,11 +208,13 @@ public:
 ///   typedef ExampleData data_type;
 ///   typedef ExampleInternalKey internal_key_type; // The stored key type.
 ///   typedef ExampleKey external_key_type; // The type to pass to find().
+///   typedef uint32_t hash_value_type; // The type the hash function returns.
+///   typedef uint32_t offset_type; // The type for offsets into the table.
 ///
 ///   /// Compare two keys for equality.
 ///   static bool EqualKey(internal_key_type &Key1, internal_key_type &Key2);
 ///   /// Calculate the hash for the given key.
-///   static unsigned ComputeHash(internal_key_type &IKey);
+///   static hash_value_type ComputeHash(internal_key_type &IKey);
 ///   /// Translate from the semantic type of a key in the hash table to the
 ///   /// type that is actually stored and used for hashing and comparisons.
 ///   /// The internal and external types are often the same, in which case this
@@ -217,21 +222,21 @@ public:
 ///   static const internal_key_type &GetInternalKey(external_key_type &EKey);
 ///   /// Read the key and data length from Buffer, leaving it pointing at the
 ///   /// following byte.
-///   static std::pair<unsigned, unsigned>
+///   static std::pair<offset_type, offset_type>
 ///   ReadKeyDataLength(const unsigned char *&Buffer);
 ///   /// Read the key from Buffer, given the KeyLen as reported from
 ///   /// ReadKeyDataLength.
 ///   const internal_key_type &ReadKey(const unsigned char *Buffer,
-///                                    unsigned KeyLen);
+///                                    offset_type KeyLen);
 ///   /// Read the data for Key from Buffer, given the DataLen as reported from
 ///   /// ReadKeyDataLength.
 ///   data_type ReadData(StringRef Key, const unsigned char *Buffer,
-///                      unsigned DataLen);
+///                      offset_type DataLen);
 /// };
 /// \endcode
 template <typename Info> class OnDiskChainedHashTable {
-  const unsigned NumBuckets;
-  const unsigned NumEntries;
+  const typename Info::offset_type NumBuckets;
+  const typename Info::offset_type NumEntries;
   const unsigned char *const Buckets;
   const unsigned char *const Base;
   Info InfoObj;
@@ -240,8 +245,10 @@ public:
   typedef typename Info::internal_key_type internal_key_type;
   typedef typename Info::external_key_type external_key_type;
   typedef typename Info::data_type         data_type;
+  typedef typename Info::hash_value_type   hash_value_type;
+  typedef typename Info::offset_type       offset_type;
 
-  OnDiskChainedHashTable(unsigned NumBuckets, unsigned NumEntries,
+  OnDiskChainedHashTable(offset_type NumBuckets, offset_type NumEntries,
                          const unsigned char *Buckets,
                          const unsigned char *Base,
                          const Info &InfoObj = Info())
@@ -251,8 +258,8 @@ public:
            "'buckets' must have a 4-byte alignment");
   }
 
-  unsigned getNumBuckets() const { return NumBuckets; }
-  unsigned getNumEntries() const { return NumEntries; }
+  offset_type getNumBuckets() const { return NumBuckets; }
+  offset_type getNumEntries() const { return NumEntries; }
   const unsigned char *getBase() const { return Base; }
   const unsigned char *getBuckets() const { return Buckets; }
 
@@ -261,12 +268,12 @@ public:
   class iterator {
     internal_key_type Key;
     const unsigned char *const Data;
-    const unsigned Len;
+    const offset_type Len;
     Info *InfoObj;
 
   public:
-    iterator() : Data(0), Len(0) {}
-    iterator(const internal_key_type K, const unsigned char *D, unsigned L,
+    iterator() : Data(nullptr), Len(0) {}
+    iterator(const internal_key_type K, const unsigned char *D, offset_type L,
              Info *InfoObj)
         : Key(K), Data(D), Len(L), InfoObj(InfoObj) {}
 
@@ -282,13 +289,13 @@ public:
 
     using namespace llvm::support;
     const internal_key_type &IKey = InfoObj.GetInternalKey(EKey);
-    unsigned KeyHash = InfoObj.ComputeHash(IKey);
+    hash_value_type KeyHash = InfoObj.ComputeHash(IKey);
 
-    // Each bucket is just a 32-bit offset into the hash table file.
-    unsigned Idx = KeyHash & (NumBuckets - 1);
-    const unsigned char *Bucket = Buckets + sizeof(uint32_t) * Idx;
+    // Each bucket is just an offset into the hash table file.
+    offset_type Idx = KeyHash & (NumBuckets - 1);
+    const unsigned char *Bucket = Buckets + sizeof(offset_type) * Idx;
 
-    unsigned Offset = endian::readNext<uint32_t, little, aligned>(Bucket);
+    offset_type Offset = endian::readNext<offset_type, little, aligned>(Bucket);
     if (Offset == 0)
       return iterator(); // Empty bucket.
     const unsigned char *Items = Base + Offset;
@@ -299,11 +306,13 @@ public:
 
     for (unsigned i = 0; i < Len; ++i) {
       // Read the hash.
-      uint32_t ItemHash = endian::readNext<uint32_t, little, unaligned>(Items);
+      hash_value_type ItemHash =
+          endian::readNext<hash_value_type, little, unaligned>(Items);
 
       // Determine the length of the key and the data.
-      const std::pair<unsigned, unsigned> &L = Info::ReadKeyDataLength(Items);
-      unsigned ItemLen = L.first + L.second;
+      const std::pair<offset_type, offset_type> &L =
+          Info::ReadKeyDataLength(Items);
+      offset_type ItemLen = L.first + L.second;
 
       // Compare the hashes.  If they are not the same, skip the entry entirely.
       if (ItemHash != KeyHash) {
@@ -349,8 +358,10 @@ public:
     assert((reinterpret_cast<uintptr_t>(Buckets) & 0x3) == 0 &&
            "buckets should be 4-byte aligned.");
 
-    unsigned NumBuckets = endian::readNext<uint32_t, little, aligned>(Buckets);
-    unsigned NumEntries = endian::readNext<uint32_t, little, aligned>(Buckets);
+    offset_type NumBuckets =
+        endian::readNext<offset_type, little, aligned>(Buckets);
+    offset_type NumEntries =
+        endian::readNext<offset_type, little, aligned>(Buckets);
     return new OnDiskChainedHashTable<Info>(NumBuckets, NumEntries, Buckets,
                                             Base, InfoObj);
   }
@@ -368,8 +379,10 @@ public:
   typedef typename base_type::internal_key_type internal_key_type;
   typedef typename base_type::external_key_type external_key_type;
   typedef typename base_type::data_type         data_type;
+  typedef typename base_type::hash_value_type   hash_value_type;
+  typedef typename base_type::offset_type       offset_type;
 
-  OnDiskIterableChainedHashTable(unsigned NumBuckets, unsigned NumEntries,
+  OnDiskIterableChainedHashTable(offset_type NumBuckets, offset_type NumEntries,
                                  const unsigned char *Buckets,
                                  const unsigned char *Payload,
                                  const unsigned char *Base,
@@ -380,19 +393,20 @@ public:
   /// \brief Iterates over all of the keys in the table.
   class key_iterator {
     const unsigned char *Ptr;
-    unsigned NumItemsInBucketLeft;
-    unsigned NumEntriesLeft;
+    offset_type NumItemsInBucketLeft;
+    offset_type NumEntriesLeft;
     Info *InfoObj;
 
   public:
     typedef external_key_type value_type;
 
-    key_iterator(const unsigned char *const Ptr, unsigned NumEntries,
+    key_iterator(const unsigned char *const Ptr, offset_type NumEntries,
                  Info *InfoObj)
         : Ptr(Ptr), NumItemsInBucketLeft(0), NumEntriesLeft(NumEntries),
           InfoObj(InfoObj) {}
     key_iterator()
-        : Ptr(0), NumItemsInBucketLeft(0), NumEntriesLeft(0), InfoObj(0) {}
+        : Ptr(nullptr), NumItemsInBucketLeft(0), NumEntriesLeft(0),
+          InfoObj(0) {}
 
     friend bool operator==(const key_iterator &X, const key_iterator &Y) {
       return X.NumEntriesLeft == Y.NumEntriesLeft;
@@ -409,9 +423,10 @@ public:
         NumItemsInBucketLeft =
             endian::readNext<uint16_t, little, unaligned>(Ptr);
       }
-      Ptr += 4; // Skip the hash.
+      Ptr += sizeof(hash_value_type); // Skip the hash.
       // Determine the length of the key and the data.
-      const std::pair<unsigned, unsigned> &L = Info::ReadKeyDataLength(Ptr);
+      const std::pair<offset_type, offset_type> &L =
+          Info::ReadKeyDataLength(Ptr);
       Ptr += L.first + L.second;
       assert(NumItemsInBucketLeft);
       --NumItemsInBucketLeft;
@@ -427,10 +442,10 @@ public:
       const unsigned char *LocalPtr = Ptr;
       if (!NumItemsInBucketLeft)
         LocalPtr += 2; // number of items in bucket
-      LocalPtr += 4;   // Skip the hash.
+      LocalPtr += sizeof(hash_value_type); // Skip the hash.
 
       // Determine the length of the key and the data.
-      const std::pair<unsigned, unsigned> &L =
+      const std::pair<offset_type, offset_type> &L =
           Info::ReadKeyDataLength(LocalPtr);
 
       // Read the key.
@@ -444,22 +459,27 @@ public:
   }
   key_iterator key_end() { return key_iterator(); }
 
+  iterator_range<key_iterator> keys() {
+    return make_range(key_begin(), key_end());
+  }
+
   /// \brief Iterates over all the entries in the table, returning the data.
   class data_iterator {
     const unsigned char *Ptr;
-    unsigned NumItemsInBucketLeft;
-    unsigned NumEntriesLeft;
+    offset_type NumItemsInBucketLeft;
+    offset_type NumEntriesLeft;
     Info *InfoObj;
 
   public:
     typedef data_type value_type;
 
-    data_iterator(const unsigned char *const Ptr, unsigned NumEntries,
+    data_iterator(const unsigned char *const Ptr, offset_type NumEntries,
                   Info *InfoObj)
         : Ptr(Ptr), NumItemsInBucketLeft(0), NumEntriesLeft(NumEntries),
           InfoObj(InfoObj) {}
     data_iterator()
-        : Ptr(0), NumItemsInBucketLeft(0), NumEntriesLeft(0), InfoObj(0) {}
+        : Ptr(nullptr), NumItemsInBucketLeft(0), NumEntriesLeft(0),
+          InfoObj(nullptr) {}
 
     bool operator==(const data_iterator &X) const {
       return X.NumEntriesLeft == NumEntriesLeft;
@@ -476,9 +496,10 @@ public:
         NumItemsInBucketLeft =
             endian::readNext<uint16_t, little, unaligned>(Ptr);
       }
-      Ptr += 4; // Skip the hash.
+      Ptr += sizeof(hash_value_type); // Skip the hash.
       // Determine the length of the key and the data.
-      const std::pair<unsigned, unsigned> &L = Info::ReadKeyDataLength(Ptr);
+      const std::pair<offset_type, offset_type> &L =
+          Info::ReadKeyDataLength(Ptr);
       Ptr += L.first + L.second;
       assert(NumItemsInBucketLeft);
       --NumItemsInBucketLeft;
@@ -494,10 +515,10 @@ public:
       const unsigned char *LocalPtr = Ptr;
       if (!NumItemsInBucketLeft)
         LocalPtr += 2; // number of items in bucket
-      LocalPtr += 4;   // Skip the hash.
+      LocalPtr += sizeof(hash_value_type); // Skip the hash.
 
       // Determine the length of the key and the data.
-      const std::pair<unsigned, unsigned> &L =
+      const std::pair<offset_type, offset_type> &L =
           Info::ReadKeyDataLength(LocalPtr);
 
       // Read the key.
@@ -511,6 +532,10 @@ public:
   }
   data_iterator data_end() { return data_iterator(); }
 
+  iterator_range<data_iterator> data() {
+    return make_range(data_begin(), data_end());
+  }
+
   /// \brief Create the hash table.
   ///
   /// \param Buckets is the beginning of the hash table itself, which follows
@@ -532,8 +557,10 @@ public:
     assert((reinterpret_cast<uintptr_t>(Buckets) & 0x3) == 0 &&
            "buckets should be 4-byte aligned.");
 
-    unsigned NumBuckets = endian::readNext<uint32_t, little, aligned>(Buckets);
-    unsigned NumEntries = endian::readNext<uint32_t, little, aligned>(Buckets);
+    offset_type NumBuckets =
+        endian::readNext<offset_type, little, aligned>(Buckets);
+    offset_type NumEntries =
+        endian::readNext<offset_type, little, aligned>(Buckets);
     return new OnDiskIterableChainedHashTable<Info>(
         NumBuckets, NumEntries, Buckets, Payload, Base, InfoObj);
   }