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

Unified Diff: src/ports/SkMemory_malloc.cpp

Issue 24251008: Add sk_calloc. Remove SkMemory_stdlib, which seems unused. (Closed) Base URL: https://skia.googlecode.com/svn/trunk
Patch Set: use sk_out_of_memory Created 7 years, 3 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 | « src/core/SkMemory_stdlib.cpp ('k') | src/ports/SkMemory_mozalloc.cpp » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: src/ports/SkMemory_malloc.cpp
diff --git a/src/ports/SkMemory_malloc.cpp b/src/ports/SkMemory_malloc.cpp
index 73b56079a619e9db7f9dd012ab1328c2067df8f3..6db65a8524524831aaf3402ea9e49cec4e75051c 100644
--- a/src/ports/SkMemory_malloc.cpp
+++ b/src/ports/SkMemory_malloc.cpp
@@ -9,6 +9,14 @@
#include <stdio.h>
#include <stdlib.h>
+static inline void* throwOnFailure(size_t size, void* p) {
+ if (size > 0 && p == NULL) {
+ // If we've got a NULL here, the only reason we should have failed is running out of RAM.
+ sk_out_of_memory();
+ }
+ return p;
+}
+
void sk_throw() {
SkDEBUGFAIL("sk_throw");
abort();
@@ -24,14 +32,7 @@ void* sk_malloc_throw(size_t size) {
}
void* sk_realloc_throw(void* addr, size_t size) {
- void* p = realloc(addr, size);
- if (size == 0) {
- return p;
- }
- if (p == NULL) {
- sk_throw();
- }
- return p;
+ return throwOnFailure(size, realloc(addr, size));
}
void sk_free(void* p) {
@@ -42,10 +43,17 @@ void sk_free(void* p) {
void* sk_malloc_flags(size_t size, unsigned flags) {
void* p = malloc(size);
- if (p == NULL) {
- if (flags & SK_MALLOC_THROW) {
- sk_throw();
- }
+ if (flags & SK_MALLOC_THROW) {
+ return throwOnFailure(size, p);
+ } else {
+ return p;
}
- return p;
+}
+
+void* sk_calloc(size_t size) {
+ return calloc(size, 1);
+}
+
+void* sk_calloc_throw(size_t size) {
+ return throwOnFailure(size, sk_calloc(size));
}
« no previous file with comments | « src/core/SkMemory_stdlib.cpp ('k') | src/ports/SkMemory_mozalloc.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698