[llvm-commits] [llvm] r40556 - in /llvm/trunk: include/llvm/ADT/SmallPtrSet.h lib/Support/SmallPtrSet.cpp
Owen Anderson
resistor at mac.com
Fri Jul 27 11:07:02 PDT 2007
Author: resistor
Date: Fri Jul 27 13:07:02 2007
New Revision: 40556
URL: http://llvm.org/viewvc/llvm-project?rev=40556&view=rev
Log:
Allow SmallPtrSet to hold pointers to const data.
Modified:
llvm/trunk/include/llvm/ADT/SmallPtrSet.h
llvm/trunk/lib/Support/SmallPtrSet.cpp
Modified: llvm/trunk/include/llvm/ADT/SmallPtrSet.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/ADT/SmallPtrSet.h?rev=40556&r1=40555&r2=40556&view=diff
==============================================================================
--- llvm/trunk/include/llvm/ADT/SmallPtrSet.h (original)
+++ llvm/trunk/include/llvm/ADT/SmallPtrSet.h Fri Jul 27 13:07:02 2007
@@ -43,7 +43,7 @@
protected:
/// CurArray - This is the current set of buckets. If it points to
/// SmallArray, then the set is in 'small mode'.
- void **CurArray;
+ const void **CurArray;
/// CurArraySize - The allocated size of CurArray, always a power of two.
/// Note that CurArray points to an array that has CurArraySize+1 elements in
/// it, so that the end iterator actually points to valid memory.
@@ -52,7 +52,7 @@
// If small, this is # elts allocated consequtively
unsigned NumElements;
unsigned NumTombstones;
- void *SmallArray[1]; // Must be last ivar.
+ const void *SmallArray[1]; // Must be last ivar.
// Helper to copy construct a SmallPtrSet.
SmallPtrSetImpl(const SmallPtrSetImpl& that);
@@ -88,7 +88,7 @@
/// insert - This returns true if the pointer was new to the set, false if it
/// was already in the set.
- bool insert(void *Ptr);
+ bool insert(const void * Ptr);
template <typename IterT>
void insert(IterT I, IterT E) {
@@ -98,12 +98,12 @@
/// erase - If the set contains the specified pointer, remove it and return
/// true, otherwise return false.
- bool erase(void *Ptr);
+ bool erase(void * const Ptr);
- bool count(void *Ptr) const {
+ bool count(void * const Ptr) const {
if (isSmall()) {
// Linear search for the item.
- for (void *const *APtr = SmallArray, *const *E = SmallArray+NumElements;
+ for (const void *const *APtr = SmallArray, *const *E = SmallArray+NumElements;
APtr != E; ++APtr)
if (*APtr == Ptr)
return true;
@@ -117,10 +117,10 @@
private:
bool isSmall() const { return CurArray == &SmallArray[0]; }
- unsigned Hash(void *Ptr) const {
+ unsigned Hash(const void *Ptr) const {
return ((uintptr_t)Ptr >> 4) & (CurArraySize-1);
}
- void * const *FindBucketFor(void *Ptr) const;
+ const void * const *FindBucketFor(const void *Ptr) const;
/// Grow - Allocate a larger backing store for the buckets and move it over.
void Grow();
@@ -134,9 +134,9 @@
/// instances of SmallPtrSetIterator.
class SmallPtrSetIteratorImpl {
protected:
- void *const *Bucket;
+ const void *const *Bucket;
public:
- SmallPtrSetIteratorImpl(void *const *BP) : Bucket(BP) {
+ SmallPtrSetIteratorImpl(const void *const *BP) : Bucket(BP) {
AdvanceIfNotValid();
}
@@ -162,12 +162,12 @@
template<typename PtrTy>
class SmallPtrSetIterator : public SmallPtrSetIteratorImpl {
public:
- SmallPtrSetIterator(void *const *BP) : SmallPtrSetIteratorImpl(BP) {}
+ SmallPtrSetIterator(const void *const *BP) : SmallPtrSetIteratorImpl(BP) {}
// Most methods provided by baseclass.
- PtrTy operator*() const {
- return static_cast<PtrTy>(*Bucket);
+ const PtrTy operator*() const {
+ return static_cast<const PtrTy>(const_cast<void*>(*Bucket));
}
inline SmallPtrSetIterator& operator++() { // Preincrement
Modified: llvm/trunk/lib/Support/SmallPtrSet.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/SmallPtrSet.cpp?rev=40556&r1=40555&r2=40556&view=diff
==============================================================================
--- llvm/trunk/lib/Support/SmallPtrSet.cpp (original)
+++ llvm/trunk/lib/Support/SmallPtrSet.cpp Fri Jul 27 13:07:02 2007
@@ -18,10 +18,10 @@
using namespace llvm;
-bool SmallPtrSetImpl::insert(void *Ptr) {
+bool SmallPtrSetImpl::insert(const void * Ptr) {
if (isSmall()) {
// Check to see if it is already in the set.
- for (void **APtr = SmallArray, **E = SmallArray+NumElements;
+ for (const void **APtr = SmallArray, **E = SmallArray+NumElements;
APtr != E; ++APtr)
if (*APtr == Ptr)
return false;
@@ -40,21 +40,21 @@
Grow();
// Okay, we know we have space. Find a hash bucket.
- void **Bucket = const_cast<void**>(FindBucketFor(Ptr));
+ void **Bucket = const_cast<void**>(FindBucketFor((void*)Ptr));
if (*Bucket == Ptr) return false; // Already inserted, good.
// Otherwise, insert it!
if (*Bucket == getTombstoneMarker())
--NumTombstones;
- *Bucket = Ptr;
+ *Bucket = (void*)Ptr;
++NumElements; // Track density.
return true;
}
-bool SmallPtrSetImpl::erase(void *Ptr) {
+bool SmallPtrSetImpl::erase(void * const Ptr) {
if (isSmall()) {
// Check to see if it is in the set.
- for (void **APtr = SmallArray, **E = SmallArray+NumElements;
+ for (const void **APtr = SmallArray, **E = SmallArray+NumElements;
APtr != E; ++APtr)
if (*APtr == Ptr) {
// If it is in the set, replace this element.
@@ -78,12 +78,12 @@
return true;
}
-void * const *SmallPtrSetImpl::FindBucketFor(void *Ptr) const {
+const void * const *SmallPtrSetImpl::FindBucketFor(const void *Ptr) const {
unsigned Bucket = Hash(Ptr);
unsigned ArraySize = CurArraySize;
unsigned ProbeAmt = 1;
- void *const *Array = CurArray;
- void *const *Tombstone = 0;
+ const void *const *Array = CurArray;
+ const void *const *Tombstone = 0;
while (1) {
// Found Ptr's bucket?
if (Array[Bucket] == Ptr)
@@ -112,11 +112,11 @@
unsigned OldSize = CurArraySize;
unsigned NewSize = OldSize < 64 ? 128 : OldSize*2;
- void **OldBuckets = CurArray;
+ const void **OldBuckets = CurArray;
bool WasSmall = isSmall();
// Install the new array. Clear all the buckets to empty.
- CurArray = (void**)malloc(sizeof(void*) * (NewSize+1));
+ CurArray = (const void**)malloc(sizeof(void*) * (NewSize+1));
assert(CurArray && "Failed to allocate memory?");
CurArraySize = NewSize;
memset(CurArray, -1, NewSize*sizeof(void*));
@@ -128,19 +128,19 @@
// Copy over all the elements.
if (WasSmall) {
// Small sets store their elements in order.
- for (void **BucketPtr = OldBuckets, **E = OldBuckets+NumElements;
+ for (const void **BucketPtr = OldBuckets, **E = OldBuckets+NumElements;
BucketPtr != E; ++BucketPtr) {
- void *Elt = *BucketPtr;
- *const_cast<void**>(FindBucketFor(Elt)) = Elt;
+ const void *Elt = *BucketPtr;
+ *const_cast<void**>(FindBucketFor(Elt)) = const_cast<void*>(Elt);
}
} else {
// Copy over all valid entries.
- for (void **BucketPtr = OldBuckets, **E = OldBuckets+OldSize;
+ for (const void **BucketPtr = OldBuckets, **E = OldBuckets+OldSize;
BucketPtr != E; ++BucketPtr) {
// Copy over the element if it is valid.
- void *Elt = *BucketPtr;
+ const void *Elt = *BucketPtr;
if (Elt != getTombstoneMarker() && Elt != getEmptyMarker())
- *const_cast<void**>(FindBucketFor(Elt)) = Elt;
+ *const_cast<void**>(FindBucketFor(Elt)) = const_cast<void*>(Elt);
}
free(OldBuckets);
@@ -154,7 +154,7 @@
CurArray = &SmallArray[0];
// Otherwise, allocate new heap space (unless we were the same size)
} else {
- CurArray = (void**)malloc(sizeof(void*) * (that.CurArraySize+1));
+ CurArray = (const void**)malloc(sizeof(void*) * (that.CurArraySize+1));
assert(CurArray && "Failed to allocate memory?");
}
@@ -183,9 +183,9 @@
// Otherwise, allocate new heap space (unless we were the same size)
} else if (CurArraySize != RHS.CurArraySize) {
if (isSmall())
- CurArray = (void**)malloc(sizeof(void*) * (RHS.CurArraySize+1));
+ CurArray = (const void**)malloc(sizeof(void*) * (RHS.CurArraySize+1));
else
- CurArray = (void**)realloc(CurArray, sizeof(void*)*(RHS.CurArraySize+1));
+ CurArray = (const void**)realloc(CurArray, sizeof(void*)*(RHS.CurArraySize+1));
assert(CurArray && "Failed to allocate memory?");
}
More information about the llvm-commits
mailing list