Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1447)

Unified Diff: skia/ext/SkMemory_new_handler.cpp

Issue 1235863004: Roll Skia to 2ced78866fcadd98895777c8dffe92e229775181 (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: Created 5 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « DEPS ('k') | sky/engine/platform/graphics/DecodingImageGenerator.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: skia/ext/SkMemory_new_handler.cpp
diff --git a/skia/ext/SkMemory_new_handler.cpp b/skia/ext/SkMemory_new_handler.cpp
index 7400c71809b2f601a09845dbd2ea01cb8bee55c2..d4080a13447aa983e6a9f66a607a49832b2d29b3 100644
--- a/skia/ext/SkMemory_new_handler.cpp
+++ b/skia/ext/SkMemory_new_handler.cpp
@@ -2,21 +2,18 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
-#include <stdio.h>
#include <stdlib.h>
-#include <new>
#include "base/process/memory.h"
#include "third_party/skia/include/core/SkTypes.h"
-#include "third_party/skia/include/core/SkThread.h"
-// This implementation of sk_malloc_flags() and friends is identical to
-// SkMemory_malloc.cpp, except that it disables the CRT's new_handler during
-// malloc() and calloc() when SK_MALLOC_THROW is not set (because our normal
-// new_handler itself will crash on failure when using tcmalloc).
-
-SK_DECLARE_STATIC_MUTEX(gSkNewHandlerMutex);
+// This implementation of sk_malloc_flags() and friends is similar to
+// SkMemory_malloc.cpp, except it uses base::UncheckedMalloc and friends
+// for non-SK_MALLOC_THROW calls.
+//
+// The name of this file is historic: a previous implementation tried to
+// use std::set_new_handler() for the same effect, but it didn't actually work.
static inline void* throw_on_failure(size_t size, void* p) {
if (size > 0 && p == NULL) {
@@ -46,21 +43,35 @@ void sk_free(void* p) {
}
}
+// We get lots of bugs filed on us that amount to overcommiting bitmap memory,
+// then some time later failing to back that VM with physical memory.
+// They're hard to track down, so in Debug mode we touch all memory right up front.
+//
+// For malloc, fill is an arbitrary byte and ideally not 0. For calloc, it's got to be 0.
+static void* prevent_overcommit(int fill, size_t size, void* p) {
+ // We probably only need to touch one byte per page, but memset makes things easy.
+ SkDEBUGCODE(memset(p, fill, size));
+ return p;
+}
+
void* sk_malloc_throw(size_t size) {
- return throw_on_failure(size, malloc(size));
+ return prevent_overcommit(0x42, size, throw_on_failure(size, malloc(size)));
}
static void* sk_malloc_nothrow(size_t size) {
// TODO(b.kelemen): we should always use UncheckedMalloc but currently it
// doesn't work as intended everywhere.
+ void* result;
#if defined(OS_IOS)
- return malloc(size);
+ result = malloc(size);
#else
- void* result;
// It's the responsibility of the caller to check the return value.
ignore_result(base::UncheckedMalloc(size, &result));
- return result;
#endif
+ if (result) {
+ prevent_overcommit(0x47, size, result);
+ }
+ return result;
}
void* sk_malloc_flags(size_t size, unsigned flags) {
@@ -71,18 +82,21 @@ void* sk_malloc_flags(size_t size, unsigned flags) {
}
void* sk_calloc_throw(size_t size) {
- return throw_on_failure(size, calloc(size, 1));
+ return prevent_overcommit(0, size, throw_on_failure(size, calloc(size, 1)));
}
void* sk_calloc(size_t size) {
// TODO(b.kelemen): we should always use UncheckedCalloc but currently it
// doesn't work as intended everywhere.
+ void* result;
#if defined(OS_IOS)
- return calloc(1, size);
+ result = calloc(1, size);
#else
- void* result;
// It's the responsibility of the caller to check the return value.
ignore_result(base::UncheckedCalloc(size, 1, &result));
- return result;
#endif
+ if (result) {
+ prevent_overcommit(0, size, result);
+ }
+ return result;
}
« no previous file with comments | « DEPS ('k') | sky/engine/platform/graphics/DecodingImageGenerator.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698