aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/core/SkMallocPixelRef.cpp
diff options
context:
space:
mode:
authorGravatar Mike Reed <reed@google.com>2018-01-05 11:20:10 -0500
committerGravatar Skia Commit-Bot <skia-commit-bot@chromium.org>2018-01-05 21:29:35 +0000
commit8dc8dbc8211e7b0245a6e7db911265efbe0fccaf (patch)
treef112c74f618a536e86a80d9d657b804a039f3f54 /src/core/SkMallocPixelRef.cpp
parentf21b32ccd7bd174ce647078854b2314f8b64d94c (diff)
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>
Diffstat (limited to 'src/core/SkMallocPixelRef.cpp')
-rw-r--r--src/core/SkMallocPixelRef.cpp31
1 files changed, 24 insertions, 7 deletions
diff --git a/src/core/SkMallocPixelRef.cpp b/src/core/SkMallocPixelRef.cpp
index 6e402c98f7..2933e48cc4 100644
--- a/src/core/SkMallocPixelRef.cpp
+++ b/src/core/SkMallocPixelRef.cpp
@@ -8,8 +8,27 @@
#include "SkMallocPixelRef.h"
#include "SkBitmap.h"
#include "SkReadBuffer.h"
+#include "SkSafeMath.h"
#include "SkWriteBuffer.h"
+void* sk_calloc_throw(size_t count, size_t elemSize) {
+ return sk_calloc_throw(SkSafeMath::Mul(count, elemSize));
+}
+
+void* sk_malloc_throw(size_t count, size_t elemSize) {
+ return sk_malloc_throw(SkSafeMath::Mul(count, elemSize));
+}
+
+void* sk_realloc_throw(void* buffer, size_t count, size_t elemSize) {
+ return sk_realloc_throw(buffer, SkSafeMath::Mul(count, elemSize));
+}
+
+void* sk_malloc_canfail(size_t count, size_t elemSize) {
+ return sk_malloc_canfail(SkSafeMath::Mul(count, elemSize));
+}
+
+///////////////////////////////////////////////////////////////////////////////////////////////////
+
// assumes ptr was allocated via sk_malloc
static void sk_free_releaseproc(void* ptr, void*) {
sk_free(ptr);
@@ -63,15 +82,13 @@ sk_sp<SkPixelRef> SkMallocPixelRef::MakeUsing(void*(*allocProc)(size_t),
sk_free_releaseproc, nullptr));
}
-sk_sp<SkPixelRef> SkMallocPixelRef::MakeAllocate(const SkImageInfo& info,
- size_t rowBytes) {
- auto sk_malloc_nothrow = [](size_t size) { return sk_malloc_flags(size, 0); };
- return MakeUsing(sk_malloc_nothrow, info, rowBytes);
+sk_sp<SkPixelRef> SkMallocPixelRef::MakeAllocate(const SkImageInfo& info, size_t rowBytes) {
+ return MakeUsing(sk_malloc_canfail, info, rowBytes);
}
sk_sp<SkPixelRef> SkMallocPixelRef::MakeZeroed(const SkImageInfo& info,
size_t rowBytes) {
- return MakeUsing(sk_calloc, info, rowBytes);
+ return MakeUsing(sk_calloc_canfail, info, rowBytes);
}
static void sk_data_releaseproc(void*, void* dataPtr) {
@@ -93,8 +110,8 @@ sk_sp<SkPixelRef> SkMallocPixelRef::MakeWithProc(const SkImageInfo& info,
}
sk_sp<SkPixelRef> SkMallocPixelRef::MakeWithData(const SkImageInfo& info,
- size_t rowBytes,
- sk_sp<SkData> data) {
+ size_t rowBytes,
+ sk_sp<SkData> data) {
SkASSERT(data != nullptr);
if (!is_valid(info)) {
return nullptr;