include/private/SkTemplates: Cleanup bare pointers.
include/private: - SkAutoTArray, SkAutoTMalloc no longer use bare pointers to owned memory, - SkTHash and SkAutoTArray are now std::move()able. - SkAutoTArray::swap no longer neccesary. - SkAutoTMalloc::operator=() defined. src/pdf: - SkPDFCanon and SkPDFObjectSerializer are now std::move()able. - `template <class T> static void renew(T* t) { t->~T(); new (t) T; }` is gone. Change-Id: I2f36a0780c47d1427a85da240121c898387fb4cf Reviewed-on: https://skia-review.googlesource.com/123401 Reviewed-by: Ben Wagner <bungeman@google.com> Commit-Queue: Hal Canary <halcanary@google.com>
This commit is contained in:
parent
849dce1d7b
commit
6736236faf
@ -173,8 +173,8 @@ private:
|
||||
|
||||
fCount = 0;
|
||||
fCapacity = capacity;
|
||||
SkAutoTArray<Slot> oldSlots(capacity);
|
||||
oldSlots.swap(fSlots);
|
||||
SkAutoTArray<Slot> oldSlots = std::move(fSlots);
|
||||
fSlots = SkAutoTArray<Slot>(capacity);
|
||||
|
||||
for (int i = 0; i < oldCapacity; i++) {
|
||||
Slot& s = oldSlots[i];
|
||||
|
@ -84,40 +84,37 @@ public:
|
||||
|
||||
/** Allocate an array of T elements, and free the array in the destructor
|
||||
*/
|
||||
template <typename T> class SkAutoTArray : SkNoncopyable {
|
||||
template <typename T> class SkAutoTArray {
|
||||
public:
|
||||
SkAutoTArray() {
|
||||
fArray = nullptr;
|
||||
SkDEBUGCODE(fCount = 0;)
|
||||
}
|
||||
SkAutoTArray() {}
|
||||
/** Allocate count number of T elements
|
||||
*/
|
||||
explicit SkAutoTArray(int count) {
|
||||
SkASSERT(count >= 0);
|
||||
fArray = nullptr;
|
||||
if (count) {
|
||||
fArray = new T[count];
|
||||
fArray.reset(new T[count]);
|
||||
}
|
||||
SkDEBUGCODE(fCount = count;)
|
||||
}
|
||||
|
||||
SkAutoTArray(SkAutoTArray&& other) : fArray(std::move(other.fArray)) {
|
||||
SkDEBUGCODE(fCount = other.fCount; other.fCount = 0;)
|
||||
}
|
||||
SkAutoTArray& operator=(SkAutoTArray&& other) {
|
||||
if (this != &other) {
|
||||
fArray = std::move(other.fArray);
|
||||
SkDEBUGCODE(fCount = other.fCount; other.fCount = 0;)
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
/** Reallocates given a new count. Reallocation occurs even if new count equals old count.
|
||||
*/
|
||||
void reset(int count) {
|
||||
delete[] fArray;
|
||||
SkASSERT(count >= 0);
|
||||
fArray = nullptr;
|
||||
if (count) {
|
||||
fArray = new T[count];
|
||||
}
|
||||
SkDEBUGCODE(fCount = count;)
|
||||
}
|
||||
|
||||
~SkAutoTArray() { delete[] fArray; }
|
||||
void reset(int count) { *this = SkAutoTArray(count); }
|
||||
|
||||
/** Return the array of T elements. Will be NULL if count == 0
|
||||
*/
|
||||
T* get() const { return fArray; }
|
||||
T* get() const { return fArray.get(); }
|
||||
|
||||
/** Return the nth element in the array
|
||||
*/
|
||||
@ -126,14 +123,9 @@ public:
|
||||
return fArray[index];
|
||||
}
|
||||
|
||||
void swap(SkAutoTArray& other) {
|
||||
SkTSwap(fArray, other.fArray);
|
||||
SkDEBUGCODE(SkTSwap(fCount, other.fCount));
|
||||
}
|
||||
|
||||
private:
|
||||
T* fArray;
|
||||
SkDEBUGCODE(int fCount;)
|
||||
std::unique_ptr<T[]> fArray;
|
||||
SkDEBUGCODE(int fCount = 0;)
|
||||
};
|
||||
|
||||
/** Wraps SkAutoTArray, with room for kCountRequested elements preallocated.
|
||||
@ -236,79 +228,48 @@ private:
|
||||
/** Manages an array of T elements, freeing the array in the destructor.
|
||||
* Does NOT call any constructors/destructors on T (T must be POD).
|
||||
*/
|
||||
template <typename T> class SkAutoTMalloc : SkNoncopyable {
|
||||
template <typename T> class SkAutoTMalloc {
|
||||
public:
|
||||
/** Takes ownership of the ptr. The ptr must be a value which can be passed to sk_free. */
|
||||
explicit SkAutoTMalloc(T* ptr = nullptr) {
|
||||
fPtr = ptr;
|
||||
}
|
||||
explicit SkAutoTMalloc(T* ptr = nullptr) : fPtr(ptr) {}
|
||||
|
||||
/** Allocates space for 'count' Ts. */
|
||||
explicit SkAutoTMalloc(size_t count) {
|
||||
fPtr = count ? (T*)sk_malloc_throw(count, sizeof(T)) : nullptr;
|
||||
}
|
||||
explicit SkAutoTMalloc(size_t count)
|
||||
: fPtr(count ? (T*)sk_malloc_throw(count, sizeof(T)) : nullptr) {}
|
||||
|
||||
SkAutoTMalloc(SkAutoTMalloc<T>&& that) : fPtr(that.release()) {}
|
||||
|
||||
~SkAutoTMalloc() {
|
||||
sk_free(fPtr);
|
||||
}
|
||||
SkAutoTMalloc(SkAutoTMalloc&&) = default;
|
||||
SkAutoTMalloc& operator=(SkAutoTMalloc&&) = default;
|
||||
|
||||
/** Resize the memory area pointed to by the current ptr preserving contents. */
|
||||
void realloc(size_t count) {
|
||||
if (count) {
|
||||
fPtr = reinterpret_cast<T*>(sk_realloc_throw(fPtr, count * sizeof(T)));
|
||||
} else {
|
||||
this->reset(0);
|
||||
}
|
||||
fPtr.reset(count ? (T*)sk_realloc_throw(fPtr.release(), count * sizeof(T)) : nullptr);
|
||||
}
|
||||
|
||||
/** Resize the memory area pointed to by the current ptr without preserving contents. */
|
||||
T* reset(size_t count = 0) {
|
||||
sk_free(fPtr);
|
||||
fPtr = count ? (T*)sk_malloc_throw(count, sizeof(T)) : nullptr;
|
||||
return fPtr;
|
||||
fPtr.reset(count ? (T*)sk_malloc_throw(count, sizeof(T)) : nullptr);
|
||||
return this->get();
|
||||
}
|
||||
|
||||
T* get() const { return fPtr; }
|
||||
T* get() const { return fPtr.get(); }
|
||||
|
||||
operator T*() {
|
||||
return fPtr;
|
||||
}
|
||||
operator T*() { return fPtr.get(); }
|
||||
|
||||
operator const T*() const {
|
||||
return fPtr;
|
||||
}
|
||||
operator const T*() const { return fPtr.get(); }
|
||||
|
||||
T& operator[](int index) {
|
||||
return fPtr[index];
|
||||
}
|
||||
T& operator[](int index) { return fPtr.get()[index]; }
|
||||
|
||||
const T& operator[](int index) const {
|
||||
return fPtr[index];
|
||||
}
|
||||
|
||||
SkAutoTMalloc& operator=(SkAutoTMalloc<T>&& that) {
|
||||
if (this != &that) {
|
||||
sk_free(fPtr);
|
||||
fPtr = that.release();
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
const T& operator[](int index) const { return fPtr.get()[index]; }
|
||||
|
||||
/**
|
||||
* Transfer ownership of the ptr to the caller, setting the internal
|
||||
* pointer to NULL. Note that this differs from get(), which also returns
|
||||
* the pointer, but it does not transfer ownership.
|
||||
*/
|
||||
T* release() {
|
||||
T* ptr = fPtr;
|
||||
fPtr = nullptr;
|
||||
return ptr;
|
||||
}
|
||||
T* release() { return fPtr.release(); }
|
||||
|
||||
private:
|
||||
T* fPtr;
|
||||
std::unique_ptr<T, SkFunctionWrapper<void, void, sk_free>> fPtr;
|
||||
};
|
||||
|
||||
template <size_t kCountRequested, typename T> class SkAutoSTMalloc : SkNoncopyable {
|
||||
|
@ -319,7 +319,7 @@ public:
|
||||
|
||||
void swapAnimationBuffers() override {
|
||||
this->INHERITED::swapAnimationBuffers();
|
||||
fFrontPaths.swap(fBackPaths);
|
||||
std::swap(fFrontPaths, fBackPaths);
|
||||
}
|
||||
|
||||
void drawGlyphs(SkCanvas* canvas) override {
|
||||
|
@ -12,5 +12,7 @@
|
||||
|
||||
SkPDFCanon::~SkPDFCanon() {}
|
||||
SkPDFCanon::SkPDFCanon() {}
|
||||
SkPDFCanon::SkPDFCanon(SkPDFCanon&&) = default;
|
||||
SkPDFCanon& SkPDFCanon::operator=(SkPDFCanon&&) = default;
|
||||
|
||||
|
||||
|
@ -26,7 +26,9 @@ class SkPDFCanon {
|
||||
public:
|
||||
~SkPDFCanon();
|
||||
SkPDFCanon();
|
||||
SkPDFCanon(SkPDFCanon&&);
|
||||
SkPDFCanon(const SkPDFCanon&) = delete;
|
||||
SkPDFCanon& operator=(SkPDFCanon&&);
|
||||
SkPDFCanon& operator=(const SkPDFCanon&) = delete;
|
||||
|
||||
SkTHashMap<SkPDFImageShaderKey, sk_sp<SkPDFObject>> fImageShaderMap;
|
||||
|
@ -16,13 +16,14 @@
|
||||
|
||||
SkPDFObjectSerializer::SkPDFObjectSerializer() : fBaseOffset(0), fNextToBeSerialized(0) {}
|
||||
|
||||
template <class T> static void renew(T* t) { t->~T(); new (t) T; }
|
||||
|
||||
SkPDFObjectSerializer::~SkPDFObjectSerializer() {
|
||||
for (int i = 0; i < fObjNumMap.objects().count(); ++i) {
|
||||
fObjNumMap.objects()[i]->drop();
|
||||
}
|
||||
}
|
||||
SkPDFObjectSerializer::SkPDFObjectSerializer(SkPDFObjectSerializer&&) = default;
|
||||
SkPDFObjectSerializer& SkPDFObjectSerializer::operator=(SkPDFObjectSerializer&&) = default;
|
||||
|
||||
|
||||
void SkPDFObjectSerializer::addObjectRecursively(const sk_sp<SkPDFObject>& object) {
|
||||
fObjNumMap.addObjectRecursively(object.get());
|
||||
@ -242,8 +243,8 @@ void SkPDFDocument::onAbort() {
|
||||
void SkPDFDocument::reset() {
|
||||
fCanvas.reset(nullptr);
|
||||
fPages.reset();
|
||||
renew(&fCanon);
|
||||
renew(&fObjectSerializer);
|
||||
fCanon = SkPDFCanon();
|
||||
fObjectSerializer = SkPDFObjectSerializer();
|
||||
fFonts.reset();
|
||||
}
|
||||
|
||||
|
@ -38,6 +38,9 @@ struct SkPDFObjectSerializer : SkNoncopyable {
|
||||
|
||||
SkPDFObjectSerializer();
|
||||
~SkPDFObjectSerializer();
|
||||
SkPDFObjectSerializer(SkPDFObjectSerializer&&);
|
||||
SkPDFObjectSerializer& operator=(SkPDFObjectSerializer&&);
|
||||
|
||||
void addObjectRecursively(const sk_sp<SkPDFObject>&);
|
||||
void serializeHeader(SkWStream*, const SkDocument::PDFMetadata&);
|
||||
void serializeObjects(SkWStream*);
|
||||
|
Loading…
Reference in New Issue
Block a user