#include "llvm/ADT/FoldingSet.h"
#include "llvm/Support/MathExtras.h"
+#include <cassert>
using namespace llvm;
//===----------------------------------------------------------------------===//
}
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();
// 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) |
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);
//===----------------------------------------------------------------------===//
// 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*));
}