mirror of
https://github.com/c64scene-ar/llvm-6502.git
synced 2025-11-23 16:19:52 +00:00
Allow SmallPtrSet to hold pointers to const data.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@40556 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
@@ -43,7 +43,7 @@ class SmallPtrSetImpl {
|
|||||||
protected:
|
protected:
|
||||||
/// CurArray - This is the current set of buckets. If it points to
|
/// CurArray - This is the current set of buckets. If it points to
|
||||||
/// SmallArray, then the set is in 'small mode'.
|
/// SmallArray, then the set is in 'small mode'.
|
||||||
void **CurArray;
|
const void **CurArray;
|
||||||
/// CurArraySize - The allocated size of CurArray, always a power of two.
|
/// CurArraySize - The allocated size of CurArray, always a power of two.
|
||||||
/// Note that CurArray points to an array that has CurArraySize+1 elements in
|
/// Note that CurArray points to an array that has CurArraySize+1 elements in
|
||||||
/// it, so that the end iterator actually points to valid memory.
|
/// it, so that the end iterator actually points to valid memory.
|
||||||
@@ -52,7 +52,7 @@ protected:
|
|||||||
// If small, this is # elts allocated consequtively
|
// If small, this is # elts allocated consequtively
|
||||||
unsigned NumElements;
|
unsigned NumElements;
|
||||||
unsigned NumTombstones;
|
unsigned NumTombstones;
|
||||||
void *SmallArray[1]; // Must be last ivar.
|
const void *SmallArray[1]; // Must be last ivar.
|
||||||
|
|
||||||
// Helper to copy construct a SmallPtrSet.
|
// Helper to copy construct a SmallPtrSet.
|
||||||
SmallPtrSetImpl(const SmallPtrSetImpl& that);
|
SmallPtrSetImpl(const SmallPtrSetImpl& that);
|
||||||
@@ -88,7 +88,7 @@ public:
|
|||||||
|
|
||||||
/// insert - This returns true if the pointer was new to the set, false if it
|
/// insert - This returns true if the pointer was new to the set, false if it
|
||||||
/// was already in the set.
|
/// was already in the set.
|
||||||
bool insert(void *Ptr);
|
bool insert(const void * Ptr);
|
||||||
|
|
||||||
template <typename IterT>
|
template <typename IterT>
|
||||||
void insert(IterT I, IterT E) {
|
void insert(IterT I, IterT E) {
|
||||||
@@ -98,12 +98,12 @@ public:
|
|||||||
|
|
||||||
/// erase - If the set contains the specified pointer, remove it and return
|
/// erase - If the set contains the specified pointer, remove it and return
|
||||||
/// true, otherwise return false.
|
/// 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()) {
|
if (isSmall()) {
|
||||||
// Linear search for the item.
|
// 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)
|
APtr != E; ++APtr)
|
||||||
if (*APtr == Ptr)
|
if (*APtr == Ptr)
|
||||||
return true;
|
return true;
|
||||||
@@ -117,10 +117,10 @@ public:
|
|||||||
private:
|
private:
|
||||||
bool isSmall() const { return CurArray == &SmallArray[0]; }
|
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);
|
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.
|
/// Grow - Allocate a larger backing store for the buckets and move it over.
|
||||||
void Grow();
|
void Grow();
|
||||||
@@ -134,9 +134,9 @@ protected:
|
|||||||
/// instances of SmallPtrSetIterator.
|
/// instances of SmallPtrSetIterator.
|
||||||
class SmallPtrSetIteratorImpl {
|
class SmallPtrSetIteratorImpl {
|
||||||
protected:
|
protected:
|
||||||
void *const *Bucket;
|
const void *const *Bucket;
|
||||||
public:
|
public:
|
||||||
SmallPtrSetIteratorImpl(void *const *BP) : Bucket(BP) {
|
SmallPtrSetIteratorImpl(const void *const *BP) : Bucket(BP) {
|
||||||
AdvanceIfNotValid();
|
AdvanceIfNotValid();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -162,12 +162,12 @@ protected:
|
|||||||
template<typename PtrTy>
|
template<typename PtrTy>
|
||||||
class SmallPtrSetIterator : public SmallPtrSetIteratorImpl {
|
class SmallPtrSetIterator : public SmallPtrSetIteratorImpl {
|
||||||
public:
|
public:
|
||||||
SmallPtrSetIterator(void *const *BP) : SmallPtrSetIteratorImpl(BP) {}
|
SmallPtrSetIterator(const void *const *BP) : SmallPtrSetIteratorImpl(BP) {}
|
||||||
|
|
||||||
// Most methods provided by baseclass.
|
// Most methods provided by baseclass.
|
||||||
|
|
||||||
PtrTy operator*() const {
|
const PtrTy operator*() const {
|
||||||
return static_cast<PtrTy>(*Bucket);
|
return static_cast<const PtrTy>(const_cast<void*>(*Bucket));
|
||||||
}
|
}
|
||||||
|
|
||||||
inline SmallPtrSetIterator& operator++() { // Preincrement
|
inline SmallPtrSetIterator& operator++() { // Preincrement
|
||||||
|
|||||||
@@ -18,10 +18,10 @@
|
|||||||
|
|
||||||
using namespace llvm;
|
using namespace llvm;
|
||||||
|
|
||||||
bool SmallPtrSetImpl::insert(void *Ptr) {
|
bool SmallPtrSetImpl::insert(const void * Ptr) {
|
||||||
if (isSmall()) {
|
if (isSmall()) {
|
||||||
// Check to see if it is already in the set.
|
// 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)
|
APtr != E; ++APtr)
|
||||||
if (*APtr == Ptr)
|
if (*APtr == Ptr)
|
||||||
return false;
|
return false;
|
||||||
@@ -40,21 +40,21 @@ bool SmallPtrSetImpl::insert(void *Ptr) {
|
|||||||
Grow();
|
Grow();
|
||||||
|
|
||||||
// Okay, we know we have space. Find a hash bucket.
|
// 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.
|
if (*Bucket == Ptr) return false; // Already inserted, good.
|
||||||
|
|
||||||
// Otherwise, insert it!
|
// Otherwise, insert it!
|
||||||
if (*Bucket == getTombstoneMarker())
|
if (*Bucket == getTombstoneMarker())
|
||||||
--NumTombstones;
|
--NumTombstones;
|
||||||
*Bucket = Ptr;
|
*Bucket = (void*)Ptr;
|
||||||
++NumElements; // Track density.
|
++NumElements; // Track density.
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool SmallPtrSetImpl::erase(void *Ptr) {
|
bool SmallPtrSetImpl::erase(void * const Ptr) {
|
||||||
if (isSmall()) {
|
if (isSmall()) {
|
||||||
// Check to see if it is in the set.
|
// 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)
|
APtr != E; ++APtr)
|
||||||
if (*APtr == Ptr) {
|
if (*APtr == Ptr) {
|
||||||
// If it is in the set, replace this element.
|
// If it is in the set, replace this element.
|
||||||
@@ -78,12 +78,12 @@ bool SmallPtrSetImpl::erase(void *Ptr) {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void * const *SmallPtrSetImpl::FindBucketFor(void *Ptr) const {
|
const void * const *SmallPtrSetImpl::FindBucketFor(const void *Ptr) const {
|
||||||
unsigned Bucket = Hash(Ptr);
|
unsigned Bucket = Hash(Ptr);
|
||||||
unsigned ArraySize = CurArraySize;
|
unsigned ArraySize = CurArraySize;
|
||||||
unsigned ProbeAmt = 1;
|
unsigned ProbeAmt = 1;
|
||||||
void *const *Array = CurArray;
|
const void *const *Array = CurArray;
|
||||||
void *const *Tombstone = 0;
|
const void *const *Tombstone = 0;
|
||||||
while (1) {
|
while (1) {
|
||||||
// Found Ptr's bucket?
|
// Found Ptr's bucket?
|
||||||
if (Array[Bucket] == Ptr)
|
if (Array[Bucket] == Ptr)
|
||||||
@@ -112,11 +112,11 @@ void SmallPtrSetImpl::Grow() {
|
|||||||
unsigned OldSize = CurArraySize;
|
unsigned OldSize = CurArraySize;
|
||||||
unsigned NewSize = OldSize < 64 ? 128 : OldSize*2;
|
unsigned NewSize = OldSize < 64 ? 128 : OldSize*2;
|
||||||
|
|
||||||
void **OldBuckets = CurArray;
|
const void **OldBuckets = CurArray;
|
||||||
bool WasSmall = isSmall();
|
bool WasSmall = isSmall();
|
||||||
|
|
||||||
// Install the new array. Clear all the buckets to empty.
|
// 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?");
|
assert(CurArray && "Failed to allocate memory?");
|
||||||
CurArraySize = NewSize;
|
CurArraySize = NewSize;
|
||||||
memset(CurArray, -1, NewSize*sizeof(void*));
|
memset(CurArray, -1, NewSize*sizeof(void*));
|
||||||
@@ -128,19 +128,19 @@ void SmallPtrSetImpl::Grow() {
|
|||||||
// Copy over all the elements.
|
// Copy over all the elements.
|
||||||
if (WasSmall) {
|
if (WasSmall) {
|
||||||
// Small sets store their elements in order.
|
// 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) {
|
BucketPtr != E; ++BucketPtr) {
|
||||||
void *Elt = *BucketPtr;
|
const void *Elt = *BucketPtr;
|
||||||
*const_cast<void**>(FindBucketFor(Elt)) = Elt;
|
*const_cast<void**>(FindBucketFor(Elt)) = const_cast<void*>(Elt);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// Copy over all valid entries.
|
// Copy over all valid entries.
|
||||||
for (void **BucketPtr = OldBuckets, **E = OldBuckets+OldSize;
|
for (const void **BucketPtr = OldBuckets, **E = OldBuckets+OldSize;
|
||||||
BucketPtr != E; ++BucketPtr) {
|
BucketPtr != E; ++BucketPtr) {
|
||||||
// Copy over the element if it is valid.
|
// Copy over the element if it is valid.
|
||||||
void *Elt = *BucketPtr;
|
const void *Elt = *BucketPtr;
|
||||||
if (Elt != getTombstoneMarker() && Elt != getEmptyMarker())
|
if (Elt != getTombstoneMarker() && Elt != getEmptyMarker())
|
||||||
*const_cast<void**>(FindBucketFor(Elt)) = Elt;
|
*const_cast<void**>(FindBucketFor(Elt)) = const_cast<void*>(Elt);
|
||||||
}
|
}
|
||||||
|
|
||||||
free(OldBuckets);
|
free(OldBuckets);
|
||||||
@@ -154,7 +154,7 @@ SmallPtrSetImpl::SmallPtrSetImpl(const SmallPtrSetImpl& that) {
|
|||||||
CurArray = &SmallArray[0];
|
CurArray = &SmallArray[0];
|
||||||
// Otherwise, allocate new heap space (unless we were the same size)
|
// Otherwise, allocate new heap space (unless we were the same size)
|
||||||
} else {
|
} else {
|
||||||
CurArray = (void**)malloc(sizeof(void*) * (that.CurArraySize+1));
|
CurArray = (const void**)malloc(sizeof(void*) * (that.CurArraySize+1));
|
||||||
assert(CurArray && "Failed to allocate memory?");
|
assert(CurArray && "Failed to allocate memory?");
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -183,9 +183,9 @@ void SmallPtrSetImpl::CopyFrom(const SmallPtrSetImpl &RHS) {
|
|||||||
// Otherwise, allocate new heap space (unless we were the same size)
|
// Otherwise, allocate new heap space (unless we were the same size)
|
||||||
} else if (CurArraySize != RHS.CurArraySize) {
|
} else if (CurArraySize != RHS.CurArraySize) {
|
||||||
if (isSmall())
|
if (isSmall())
|
||||||
CurArray = (void**)malloc(sizeof(void*) * (RHS.CurArraySize+1));
|
CurArray = (const void**)malloc(sizeof(void*) * (RHS.CurArraySize+1));
|
||||||
else
|
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?");
|
assert(CurArray && "Failed to allocate memory?");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user