mirror of
https://github.com/c64scene-ar/llvm-6502.git
synced 2025-01-27 14:34:58 +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:
parent
d300622eba
commit
e992a56ae9
@ -43,7 +43,7 @@ class SmallPtrSetImpl {
|
||||
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 @@ protected:
|
||||
// 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 @@ public:
|
||||
|
||||
/// 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 @@ public:
|
||||
|
||||
/// 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 @@ public:
|
||||
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 @@ protected:
|
||||
/// 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 @@ protected:
|
||||
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
|
||||
|
@ -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 @@ bool SmallPtrSetImpl::insert(void *Ptr) {
|
||||
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 @@ bool SmallPtrSetImpl::erase(void *Ptr) {
|
||||
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 @@ void SmallPtrSetImpl::Grow() {
|
||||
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 @@ void SmallPtrSetImpl::Grow() {
|
||||
// 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 @@ SmallPtrSetImpl::SmallPtrSetImpl(const SmallPtrSetImpl& that) {
|
||||
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 @@ void SmallPtrSetImpl::CopyFrom(const SmallPtrSetImpl &RHS) {
|
||||
// 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?");
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user