From 924f080529d57f5b4a7e76347bc4e14a5dbaa328 Mon Sep 17 00:00:00 2001 From: Matthias Braun Date: Thu, 28 Jan 2016 04:49:11 +0000 Subject: SmallPtrSet: Share some code between copy/move constructor/assignment operator llvm-svn: 259018 --- llvm/lib/Support/SmallPtrSet.cpp | 46 ++++++++++++---------------------------- 1 file changed, 13 insertions(+), 33 deletions(-) (limited to 'llvm/lib/Support/SmallPtrSet.cpp') diff --git a/llvm/lib/Support/SmallPtrSet.cpp b/llvm/lib/Support/SmallPtrSet.cpp index 22274f5bbf9..3717f62150f 100644 --- a/llvm/lib/Support/SmallPtrSet.cpp +++ b/llvm/lib/Support/SmallPtrSet.cpp @@ -164,45 +164,17 @@ SmallPtrSetImplBase::SmallPtrSetImplBase(const void **SmallStorage, assert(CurArray && "Failed to allocate memory?"); } - // Copy over the new array size - CurArraySize = that.CurArraySize; - - // Copy over the contents from the other set - memcpy(CurArray, that.CurArray, sizeof(void*)*CurArraySize); - - NumElements = that.NumElements; - NumTombstones = that.NumTombstones; + // Copy over the that array. + CopyHelper(that); } SmallPtrSetImplBase::SmallPtrSetImplBase(const void **SmallStorage, unsigned SmallSize, SmallPtrSetImplBase &&that) { SmallArray = SmallStorage; - - // Copy over the basic members. - CurArraySize = that.CurArraySize; - NumElements = that.NumElements; - NumTombstones = that.NumTombstones; - - // When small, just copy into our small buffer. - if (that.isSmall()) { - CurArray = SmallArray; - memcpy(CurArray, that.CurArray, sizeof(void *) * CurArraySize); - } else { - // Otherwise, we steal the large memory allocation and no copy is needed. - CurArray = that.CurArray; - that.CurArray = that.SmallArray; - } - - // Make the "that" object small and empty. - that.CurArraySize = SmallSize; - assert(that.CurArray == that.SmallArray); - that.NumElements = 0; - that.NumTombstones = 0; + MoveHelper(SmallSize, std::move(that)); } -/// CopyFrom - implement operator= from a smallptrset that has the same pointer -/// type, but may have a different small size. void SmallPtrSetImplBase::CopyFrom(const SmallPtrSetImplBase &RHS) { assert(&RHS != this && "Self-copy should be handled by the caller."); @@ -229,6 +201,10 @@ void SmallPtrSetImplBase::CopyFrom(const SmallPtrSetImplBase &RHS) { assert(CurArray && "Failed to allocate memory?"); } + CopyHelper(RHS); +} + +void SmallPtrSetImplBase::CopyHelper(const SmallPtrSetImplBase &RHS) { // Copy over the new array size CurArraySize = RHS.CurArraySize; @@ -241,10 +217,14 @@ void SmallPtrSetImplBase::CopyFrom(const SmallPtrSetImplBase &RHS) { void SmallPtrSetImplBase::MoveFrom(unsigned SmallSize, SmallPtrSetImplBase &&RHS) { - assert(&RHS != this && "Self-move should be handled by the caller."); - if (!isSmall()) free(CurArray); + MoveHelper(SmallSize, std::move(RHS)); +} + +void SmallPtrSetImplBase::MoveHelper(unsigned SmallSize, + SmallPtrSetImplBase &&RHS) { + assert(&RHS != this && "Self-move should be handled by the caller."); if (RHS.isSmall()) { // Copy a small RHS rather than moving. -- cgit v1.2.3