skia2/src/core/SkAutoPixmapStorage.cpp
Mike Reed 8dc8dbc821 begin cleanup of malloc porting layer
1. Merge some of the allocators into sk_malloc_flags by redefining a flag to mean zero-init
2. Add more private helpers to simplify our call-sites (and handle some overflow mul checks)
3. The 2-param helpers rely on the saturating SkSafeMath::Mul to pass max_size_t as the request,
which should always fail.

Bug:508641
Change-Id: I322f1e6ed91113467e0fdb12c91c3dad33d890c8
Reviewed-on: https://skia-review.googlesource.com/90940
Commit-Queue: Mike Reed <reed@google.com>
Reviewed-by: Robert Phillips <robertphillips@google.com>
Reviewed-by: Stephan Altmueller <stephana@google.com>
2018-01-05 21:29:35 +00:00

67 lines
1.6 KiB
C++

/*
* Copyright 2016 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "SkAutoPixmapStorage.h"
#include "SkData.h"
SkAutoPixmapStorage::SkAutoPixmapStorage() : fStorage(nullptr) {}
SkAutoPixmapStorage::~SkAutoPixmapStorage() {
this->freeStorage();
}
SkAutoPixmapStorage& SkAutoPixmapStorage::operator=(SkAutoPixmapStorage&& other) {
this->fStorage = other.fStorage;
this->INHERITED::reset(other.info(), this->fStorage, other.rowBytes());
other.fStorage = nullptr;
other.INHERITED::reset();
return *this;
}
size_t SkAutoPixmapStorage::AllocSize(const SkImageInfo& info, size_t* rowBytes) {
size_t rb = info.minRowBytes();
if (rowBytes) {
*rowBytes = rb;
}
return info.computeByteSize(rb);
}
bool SkAutoPixmapStorage::tryAlloc(const SkImageInfo& info) {
this->freeStorage();
size_t rb;
size_t size = AllocSize(info, &rb);
if (SkImageInfo::ByteSizeOverflowed(size)) {
return false;
}
void* pixels = sk_malloc_canfail(size);
if (nullptr == pixels) {
return false;
}
this->reset(info, pixels, rb);
fStorage = pixels;
return true;
}
void SkAutoPixmapStorage::alloc(const SkImageInfo& info) {
SkASSERT_RELEASE(this->tryAlloc(info));
}
const SkData* SkAutoPixmapStorage::detachPixelsAsData() {
if (!fStorage) {
return nullptr;
}
auto data = SkData::MakeFromMalloc(fStorage, this->computeByteSize());
fStorage = nullptr;
this->INHERITED::reset();
return data.release();
}