Minor fix.
[oota-llvm.git] / lib / Support / FoldingSet.cpp
index 2ae987605871ca350078181b3435c5cf090d2037..bcab5b3ec43b863154b15979ca4b512c9c6f8d8b 100644 (file)
@@ -16,6 +16,7 @@
 
 #include "llvm/ADT/FoldingSet.h"
 #include "llvm/Support/MathExtras.h"
+#include <cassert>
 using namespace llvm;
 
 //===----------------------------------------------------------------------===//
@@ -51,6 +52,9 @@ void FoldingSetImpl::NodeID::AddDouble(double D) {
 }
 void FoldingSetImpl::NodeID::AddString(const std::string &String) {
   unsigned Size = String.size();
+  Bits.push_back(Size);
+  if (!Size) return;
+
   unsigned Units = Size / 4;
   unsigned Pos = 0;
   const unsigned *Base = (const unsigned *)String.data();
@@ -58,10 +62,10 @@ void FoldingSetImpl::NodeID::AddString(const std::string &String) {
   // If the string is aligned do a bulk transfer.
   if (!((intptr_t)Base & 3)) {
     Bits.append(Base, Base + Units);
-    Pos = Units * sizeof(unsigned);
+    Pos = (Units + 1) * 4;
   } else {
     // Otherwise do it the hard way.
-    for ( Pos += 4; Pos < Size; Pos += 4) {
+    for ( Pos += 4; Pos <= Size; Pos += 4) {
       unsigned V = ((unsigned char)String[Pos - 4] << 24) |
                    ((unsigned char)String[Pos - 3] << 16) |
                    ((unsigned char)String[Pos - 2] << 8) |
@@ -77,7 +81,7 @@ void FoldingSetImpl::NodeID::AddString(const std::string &String) {
   case 1: V = (V << 8) | (unsigned char)String[Size - 3]; // Fall thru.
   case 2: V = (V << 8) | (unsigned char)String[Size - 2]; // Fall thru.
   case 3: V = (V << 8) | (unsigned char)String[Size - 1]; break;
-  case 0: return; // Nothing left.
+  default: return; // Nothing left.
   }
 
   Bits.push_back(V);
@@ -148,8 +152,10 @@ static void **GetBucketFor(const FoldingSetImpl::NodeID &ID,
 //===----------------------------------------------------------------------===//
 // FoldingSetImpl Implementation
 
-FoldingSetImpl::FoldingSetImpl() : NumNodes(0) {
-  NumBuckets = 64;
+FoldingSetImpl::FoldingSetImpl(unsigned Log2InitSize) : NumNodes(0) {
+  assert(5 < Log2InitSize && Log2InitSize < 32 &&
+         "Initial hash table size out of range");
+  NumBuckets = 1 << Log2InitSize;
   Buckets = new void*[NumBuckets];
   memset(Buckets, 0, NumBuckets*sizeof(void*));
 }