SkDescriptors to be held in unique_ptr.

This also removes the class operator new override along with
directly calling malloc in SkData, since it has a similar requirements.

CQ_INCLUDE_TRYBOTS=master.client.skia:Test-Ubuntu-Clang-GCE-CPU-AVX2-x86_64-Debug-ASAN-Trybot;master.client.skia:Test-Ubuntu-Clang-GCE-CPU-AVX2-x86_64-Debug-MSAN-Trybot

Change-Id: Ic68aacf2028d6964d9735a55558862afc9edd19b
Reviewed-on: https://skia-review.googlesource.com/3541
Reviewed-by: Mike Klein <mtklein@chromium.org>
Commit-Queue: Ben Wagner <bungeman@google.com>
This commit is contained in:
bungeman 2016-10-18 08:03:42 -04:00 committed by Skia Commit-Bot
parent 620ee4f744
commit 520ced63cf
6 changed files with 17 additions and 37 deletions

View File

@ -168,15 +168,6 @@ private:
explicit SkData(size_t size); // inplace new/delete
~SkData();
// Objects of this type are sometimes created in a custom fashion using sk_malloc_throw and
// therefore must be sk_freed. We overload new to also call sk_malloc_throw so that memory
// can be unconditionally released using sk_free in an overloaded delete. Overloading regular
// new means we must also overload placement new.
void* operator new(size_t size) { return sk_malloc_throw(size); }
void* operator new(size_t, void* p) { return p; }
void operator delete(void* p) { sk_free(p); }
// Called the first time someone calls NewEmpty to initialize the singleton.
friend SkData* sk_new_empty_data();

View File

@ -19,10 +19,9 @@ SkData::SkData(const void* ptr, size_t size, ReleaseProc proc, void* context) {
fReleaseProcContext = context;
}
// This constructor means we are inline with our fPtr's contents. Thus we set fPtr
// to point right after this. We also set our releaseproc to sk_inplace_sentinel_releaseproc,
// since we need to handle "delete" ourselves. See internal_displose().
//
/** This constructor means we are inline with our fPtr's contents.
* Thus we set fPtr to point right after this.
*/
SkData::SkData(size_t size) {
fPtr = (char*)(this + 1); // contents are immediately after this
fSize = size;
@ -70,12 +69,12 @@ sk_sp<SkData> SkData::PrivateNewWithCopy(const void* srcOrNull, size_t length) {
sk_throw();
}
char* storage = (char*)sk_malloc_throw(actualLength);
SkData* data = new (storage) SkData(length);
void* storage = ::operator new (actualLength);
sk_sp<SkData> data(new (storage) SkData(length));
if (srcOrNull) {
memcpy(data->writable_data(), srcOrNull, length);
}
return sk_sp<SkData>(data);
return data;
}
void SkData::DummyReleaseProc(const void*, void*) {}

View File

@ -11,6 +11,7 @@
#include "SkOpts.h"
#include "SkTypes.h"
#include <memory>
class SkDescriptor : SkNoncopyable {
public:
@ -19,14 +20,9 @@ public:
return sizeof(SkDescriptor) + entryCount * sizeof(Entry);
}
static SkDescriptor* Alloc(size_t length) {
static std::unique_ptr<SkDescriptor> Alloc(size_t length) {
SkASSERT(SkAlign4(length) == length);
SkDescriptor* desc = (SkDescriptor*)sk_malloc_throw(length);
return desc;
}
static void Free(SkDescriptor* desc) {
sk_free(desc);
return std::unique_ptr<SkDescriptor>(static_cast<SkDescriptor*>(::operator new (length)));
}
void init() {
@ -79,9 +75,9 @@ public:
return nullptr;
}
SkDescriptor* copy() const {
SkDescriptor* desc = SkDescriptor::Alloc(fLength);
memcpy(desc, this, fLength);
std::unique_ptr<SkDescriptor> copy() const {
std::unique_ptr<SkDescriptor> desc = SkDescriptor::Alloc(fLength);
memcpy(desc.get(), this, fLength);
return desc;
}
@ -149,7 +145,7 @@ public:
if (size <= sizeof(fStorage)) {
fDesc = (SkDescriptor*)(void*)fStorage;
} else {
fDesc = SkDescriptor::Alloc(size);
fDesc = SkDescriptor::Alloc(size).release();
}
}
@ -157,7 +153,7 @@ public:
private:
void free() {
if (fDesc != (SkDescriptor*)(void*)fStorage) {
SkDescriptor::Free(fDesc);
delete fDesc;
}
}

View File

@ -58,7 +58,6 @@ SkGlyphCache::~SkGlyphCache() {
if (g->fPathData) {
delete g->fPathData->fPath;
} } );
SkDescriptor::Free(fDesc);
delete fScalerContext;
}

View File

@ -16,6 +16,7 @@
#include "SkScalerContext.h"
#include "SkTemplates.h"
#include "SkTDArray.h"
#include <memory>
class SkTraceMemoryDump;
@ -231,7 +232,7 @@ private:
SkGlyphCache* fNext;
SkGlyphCache* fPrev;
SkDescriptor* const fDesc;
const std::unique_ptr<SkDescriptor> fDesc;
SkScalerContext* const fScalerContext;
SkPaint::FontMetrics fFontMetrics;

View File

@ -53,12 +53,6 @@ public:
#endif
{}
virtual ~GlyphGenerator() {
#ifdef SK_DEBUG
SkDescriptor::Free(fDesc);
#endif
}
int getNumPaths() override {
return fScalerContext->getGlyphCount();
}
@ -76,7 +70,7 @@ public:
private:
const SkAutoTDelete<SkScalerContext> fScalerContext;
#ifdef SK_DEBUG
SkDescriptor* const fDesc;
const std::unique_ptr<SkDescriptor> fDesc;
#endif
};