void SmallPtrSetImplBase::CopyFrom(const SmallPtrSetImplBase &RHS) { assert(&RHS != this && "Self-copy should be handled by the caller."); if (isSmall() && RHS.isSmall()) assert(CurArraySize == RHS.CurArraySize && "Cannot assign sets with different small sizes"); // If we're becoming small, prepare to insert into our stack space if (RHS.isSmall()) { if (!isSmall()) free(CurArray); CurArray = SmallArray; // Otherwise, allocate new heap space (unless we were the same size) } else if (CurArraySize != RHS.CurArraySize) { if (isSmall()) CurArray = (const void**)malloc(sizeof(void*) * RHS.CurArraySize); else { const void **T = (const void**)realloc(CurArray, sizeof(void*) * RHS.CurArraySize); if (!T) free(CurArray); CurArray = T; } assert(CurArray && "Failed to allocate memory?"); } CopyHelper(RHS); }
void SmallPtrSetImplBase::swap(SmallPtrSetImplBase &RHS) { if (this == &RHS) return; // We can only avoid copying elements if neither set is small. if (!this->isSmall() && !RHS.isSmall()) { std::swap(this->CurArray, RHS.CurArray); std::swap(this->CurArraySize, RHS.CurArraySize); std::swap(this->NumNonEmpty, RHS.NumNonEmpty); std::swap(this->NumTombstones, RHS.NumTombstones); return; } // FIXME: From here on we assume that both sets have the same small size. // If only RHS is small, copy the small elements into LHS and move the pointer // from LHS to RHS. if (!this->isSmall() && RHS.isSmall()) { assert(RHS.CurArray == RHS.SmallArray); std::copy(RHS.CurArray, RHS.CurArray + RHS.NumNonEmpty, this->SmallArray); std::swap(RHS.CurArraySize, this->CurArraySize); std::swap(this->NumNonEmpty, RHS.NumNonEmpty); std::swap(this->NumTombstones, RHS.NumTombstones); RHS.CurArray = this->CurArray; this->CurArray = this->SmallArray; return; } // If only LHS is small, copy the small elements into RHS and move the pointer // from RHS to LHS. if (this->isSmall() && !RHS.isSmall()) { assert(this->CurArray == this->SmallArray); std::copy(this->CurArray, this->CurArray + this->NumNonEmpty, RHS.SmallArray); std::swap(RHS.CurArraySize, this->CurArraySize); std::swap(RHS.NumNonEmpty, this->NumNonEmpty); std::swap(RHS.NumTombstones, this->NumTombstones); this->CurArray = RHS.CurArray; RHS.CurArray = RHS.SmallArray; return; } // Both a small, just swap the small elements. assert(this->isSmall() && RHS.isSmall()); unsigned MinNonEmpty = std::min(this->NumNonEmpty, RHS.NumNonEmpty); std::swap_ranges(this->SmallArray, this->SmallArray + MinNonEmpty, RHS.SmallArray); if (this->NumNonEmpty > MinNonEmpty) { std::copy(this->SmallArray + MinNonEmpty, this->SmallArray + this->NumNonEmpty, RHS.SmallArray + MinNonEmpty); } else { std::copy(RHS.SmallArray + MinNonEmpty, RHS.SmallArray + RHS.NumNonEmpty, this->SmallArray + MinNonEmpty); } assert(this->CurArraySize == RHS.CurArraySize); std::swap(this->NumNonEmpty, RHS.NumNonEmpty); std::swap(this->NumTombstones, RHS.NumTombstones); }
void SmallPtrSetImplBase::CopyHelper(const SmallPtrSetImplBase &RHS) { // Copy over the new array size CurArraySize = RHS.CurArraySize; // Copy over the contents from the other set std::copy(RHS.CurArray, RHS.EndPointer(), CurArray); NumNonEmpty = RHS.NumNonEmpty; NumTombstones = RHS.NumTombstones; }
SmallPtrSetImplBase::SmallPtrSetImplBase(const void **SmallStorage, const SmallPtrSetImplBase &that) { SmallArray = SmallStorage; // If we're becoming small, prepare to insert into our stack space if (that.isSmall()) { CurArray = SmallArray; // Otherwise, allocate new heap space (unless we were the same size) } else { CurArray = (const void**)safe_malloc(sizeof(void*) * that.CurArraySize); } // Copy over the that array. CopyHelper(that); }