Make SkMemory.h and adjust all files for usage.
authorHerb Derby <herb@google.com>
Mon, 20 Mar 2017 15:19:23 +0000 (11:19 -0400)
committerSkia Commit-Bot <skia-commit-bot@chromium.org>
Mon, 20 Mar 2017 18:40:49 +0000 (18:40 +0000)
This will be rolled out in three stages:
1) make SkMemory.h and have SkTypes.h include it.
2) Adjust chromium and android.
3) no long include SkMemory.h in SkTypes.h

Change-Id: If360ef5e1164d88f50b03f279e2e963ca2f57d5d
Reviewed-on: https://skia-review.googlesource.com/9874
Reviewed-by: Brian Salomon <bsalomon@google.com>
Commit-Queue: Herb Derby <herb@google.com>

33 files changed:
experimental/SkSetPoly3To3.cpp
experimental/SkSetPoly3To3_A.cpp
experimental/SkSetPoly3To3_D.cpp
fuzz/Fuzz.h
gn/core.gni
include/core/SkTypes.h
include/private/SkMemory.h [new file with mode: 0644]
include/private/SkTDArray.h
include/private/SkTemplates.h
src/core/SkAutoMalloc.h
src/core/SkAutoPixmapStorage.h
src/core/SkCachedData.cpp
src/core/SkDeque.cpp
src/core/SkMask.cpp
src/core/SkMetaData.cpp
src/core/SkRWBuffer.cpp
src/core/SkRect.cpp
src/core/SkRegionPriv.h
src/core/SkTLList.h
src/core/SkTSearch.cpp
src/core/SkVarAlloc.cpp
src/effects/gradients/SkGradientBitmapCache.cpp
src/gpu/GrMemoryPool.cpp
src/gpu/GrRectanizer_pow2.h
src/lazy/SkDiscardableMemoryPool.cpp
src/pdf/SkDeflate.cpp
src/ports/SkFontHost_FreeType.cpp
src/ports/SkFontMgr_android_parser.cpp
src/ports/SkMemory_malloc.cpp
src/ports/SkMemory_mozalloc.cpp
src/ports/SkOSFile_win.cpp
src/utils/SkInterpolator.cpp
src/utils/mac/SkStream_mac.cpp

index f8d8d1d..0eac861 100644 (file)
@@ -6,6 +6,7 @@
  * found in the LICENSE file.
  */
 #include "SkMatrix.h"
+#include "SkMemory.h"
 
 // FIXME: needs to be in a header
 bool SkSetPoly3To3(SkMatrix* matrix, const SkPoint src[3], const SkPoint dst[3]);
index bce6314..13026b9 100644 (file)
@@ -6,6 +6,7 @@
  * found in the LICENSE file.
  */
 #include "SkMatrix.h"
+#include "SkMemory.h"
 
 // FIXME: needs to be in a header
 bool SkSetPoly3To3_A(SkMatrix* matrix, const SkPoint src[3], const SkPoint dst[3]);
index 2fb99d8..8302d90 100644 (file)
@@ -6,6 +6,7 @@
  * found in the LICENSE file.
  */
 #include "SkMatrix.h"
+#include "SkMemory.h"
 
 // FIXME: needs to be in a header
 bool SkSetPoly3To3_D(SkMatrix* matrix, const SkPoint src[3], const SkPoint dst[3]);
index 9b3fea7..599abc0 100644 (file)
@@ -10,6 +10,7 @@
 
 #include "SkData.h"
 #include "../tools/Registry.h"
+#include "SkMemory.h"
 #include "SkTypes.h"
 
 #include <cmath>
index 3e2e53e..e03bbb0 100644 (file)
@@ -456,8 +456,9 @@ skia_core_sources = [
   "$_include/private/SkFixed.h",
   "$_include/private/SkFloatBits.h",
   "$_include/private/SkFloatingPoint.h",
-  "$_include/private/SkMiniRecorder.h",
+  "$_include/private/SkMemory.h",
   "$_include/private/SkMessageBus.h",
+  "$_include/private/SkMiniRecorder.h",
   "$_include/private/SkMutex.h",
   "$_include/private/SkOnce.h",
   "$_include/private/SkRecords.h",
index 8baf246..8f32bfb 100644 (file)
 // IWYU pragma: end_exports
 
 #include <string.h>
+// TODO(herb): remove after chromuim skia/ext/SkMemory_new_handler.cpp
+// has been updated to point to private/SkMemory.h
+#include "../private/SkMemory.h"
 
 // enable to test new device-base clipping
 //#define SK_USE_DEVICE_CLIPPING
 
-/**
- *  sk_careful_memcpy() is just like memcpy(), but guards against undefined behavior.
- *
- * It is undefined behavior to call memcpy() with null dst or src, even if len is 0.
- * If an optimizer is "smart" enough, it can exploit this to do unexpected things.
- *     memcpy(dst, src, 0);
- *     if (src) {
- *         printf("%x\n", *src);
- *     }
- * In this code the compiler can assume src is not null and omit the if (src) {...} check,
- * unconditionally running the printf, crashing the program if src really is null.
- * Of the compilers we pay attention to only GCC performs this optimization in practice.
- */
-static inline void* sk_careful_memcpy(void* dst, const void* src, size_t len) {
-    // When we pass >0 len we had better already be passing valid pointers.
-    // So we just need to skip calling memcpy when len == 0.
-    if (len) {
-        memcpy(dst,src,len);
-    }
-    return dst;
-}
-
 /** \file SkTypes.h
 */
 
@@ -69,57 +50,13 @@ static inline void* sk_careful_memcpy(void* dst, const void* src, size_t len) {
 #define SKIA_VERSION_MINOR  0
 #define SKIA_VERSION_PATCH  0
 
-/*
-    memory wrappers to be implemented by the porting layer (platform)
-*/
 
-/** Called internally if we run out of memory. The platform implementation must
-    not return, but should either throw an exception or otherwise exit.
-*/
-SK_API extern void sk_out_of_memory(void);
 /** Called internally if we hit an unrecoverable error.
     The platform implementation must not return, but should either throw
     an exception or otherwise exit.
 */
 SK_API extern void sk_abort_no_print(void);
 
-enum {
-    SK_MALLOC_TEMP  = 0x01, //!< hint to sk_malloc that the requested memory will be freed in the scope of the stack frame
-    SK_MALLOC_THROW = 0x02  //!< instructs sk_malloc to call sk_throw if the memory cannot be allocated.
-};
-/** Return a block of memory (at least 4-byte aligned) of at least the
-    specified size. If the requested memory cannot be returned, either
-    return null (if SK_MALLOC_TEMP bit is clear) or throw an exception
-    (if SK_MALLOC_TEMP bit is set). To free the memory, call sk_free().
-*/
-SK_API extern void* sk_malloc_flags(size_t size, unsigned flags);
-/** Same as sk_malloc(), but hard coded to pass SK_MALLOC_THROW as the flag
-*/
-SK_API extern void* sk_malloc_throw(size_t size);
-/** Same as standard realloc(), but this one never returns null on failure. It will throw
-    an exception if it fails.
-*/
-SK_API extern void* sk_realloc_throw(void* buffer, size_t size);
-/** Free memory returned by sk_malloc(). It is safe to pass null.
-*/
-SK_API extern void sk_free(void*);
-
-/** Much like calloc: returns a pointer to at least size zero bytes, or NULL on failure.
- */
-SK_API extern void* sk_calloc(size_t size);
-
-/** Same as sk_calloc, but throws an exception instead of returning NULL on failure.
- */
-SK_API extern void* sk_calloc_throw(size_t size);
-
-// bzero is safer than memset, but we can't rely on it, so... sk_bzero()
-static inline void sk_bzero(void* buffer, size_t size) {
-    // Please c.f. sk_careful_memcpy.  It's undefined behavior to call memset(null, 0, 0).
-    if (size) {
-        memset(buffer, 0, size);
-    }
-}
-
 ///////////////////////////////////////////////////////////////////////////////
 
 #ifdef override_GLOBAL_NEW
diff --git a/include/private/SkMemory.h b/include/private/SkMemory.h
new file mode 100644 (file)
index 0000000..02ad0ce
--- /dev/null
@@ -0,0 +1,84 @@
+/*
+ * Copyright 2017 Google Inc.
+ *
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+#ifndef SkMemory_DEFINED
+#define SkMemory_DEFINED
+
+#include <cstddef>
+#include <cstring>
+
+#include "SkPreConfig.h"
+
+/*
+    memory wrappers to be implemented by the porting layer (platform)
+*/
+
+enum {
+    SK_MALLOC_TEMP  = 0x01, //!< hint to sk_malloc that the requested memory will be freed in the scope of the stack frame
+    SK_MALLOC_THROW = 0x02  //!< instructs sk_malloc to call sk_throw if the memory cannot be allocated.
+};
+/** Return a block of memory (at least 4-byte aligned) of at least the
+    specified size. If the requested memory cannot be returned, either
+    return null (if SK_MALLOC_TEMP bit is clear) or throw an exception
+    (if SK_MALLOC_TEMP bit is set). To free the memory, call sk_free().
+*/
+SK_API extern void* sk_malloc_flags(size_t size, unsigned flags);
+/** Same as sk_malloc(), but hard coded to pass SK_MALLOC_THROW as the flag
+*/
+SK_API extern void* sk_malloc_throw(size_t size);
+/** Same as standard realloc(), but this one never returns null on failure. It will throw
+    an exception if it fails.
+*/
+SK_API extern void* sk_realloc_throw(void* buffer, size_t size);
+/** Free memory returned by sk_malloc(). It is safe to pass null.
+*/
+SK_API extern void sk_free(void*);
+
+/** Much like calloc: returns a pointer to at least size zero bytes, or NULL on failure.
+ */
+SK_API extern void* sk_calloc(size_t size);
+
+/** Same as sk_calloc, but throws an exception instead of returning NULL on failure.
+ */
+SK_API extern void* sk_calloc_throw(size_t size);
+
+/** Called internally if we run out of memory. The platform implementation must
+    not return, but should either throw an exception or otherwise exit.
+*/
+SK_API extern void sk_out_of_memory(void);
+
+// bzero is safer than memset, but we can't rely on it, so... sk_bzero()
+static inline void sk_bzero(void* buffer, size_t size) {
+    // Please c.f. sk_careful_memcpy.  It's undefined behavior to call memset(null, 0, 0).
+    if (size) {
+        memset(buffer, 0, size);
+    }
+}
+
+/**
+ *  sk_careful_memcpy() is just like memcpy(), but guards against undefined behavior.
+ *
+ * It is undefined behavior to call memcpy() with null dst or src, even if len is 0.
+ * If an optimizer is "smart" enough, it can exploit this to do unexpected things.
+ *     memcpy(dst, src, 0);
+ *     if (src) {
+ *         printf("%x\n", *src);
+ *     }
+ * In this code the compiler can assume src is not null and omit the if (src) {...} check,
+ * unconditionally running the printf, crashing the program if src really is null.
+ * Of the compilers we pay attention to only GCC performs this optimization in practice.
+ */
+static inline void* sk_careful_memcpy(void* dst, const void* src, size_t len) {
+    // When we pass >0 len we had better already be passing valid pointers.
+    // So we just need to skip calling memcpy when len == 0.
+    if (len) {
+        memcpy(dst,src,len);
+    }
+    return dst;
+}
+
+#endif  //SkMemory_DEFINED
index 16f8474..acf69b5 100644 (file)
@@ -11,6 +11,7 @@
 #define SkTDArray_DEFINED
 
 #include "SkTypes.h"
+#include "SkMemory.h"
 
 template <typename T> class SkTDArray {
 public:
@@ -234,7 +235,7 @@ public:
         }
         return -1;
     }
-    
+
     int find(const T& elem) const {
         const T* iter = fArray;
         const T* stop = fArray + fCount;
index 6f29aa1..e8dd04e 100644 (file)
@@ -11,6 +11,7 @@
 #define SkTemplates_DEFINED
 
 #include "SkMath.h"
+#include "SkMemory.h"
 #include "SkTLogic.h"
 #include "SkTypes.h"
 #include <limits.h>
index 7be8825..77901a8 100644 (file)
@@ -9,6 +9,7 @@
 #define SkAutoMalloc_DEFINED
 
 #include "SkTypes.h"
+#include "SkMemory.h"
 
 #include <memory>
 
index 379bf42..ef7f23f 100644 (file)
@@ -8,6 +8,7 @@
 #ifndef SkAutoPixmapStorage_DEFINED
 #define SkAutoPixmapStorage_DEFINED
 
+#include "SkMemory.h"
 #include "SkPixmap.h"
 
 class SK_API SkAutoPixmapStorage : public SkPixmap {
index 1ea232b..5f28c6c 100644 (file)
@@ -7,6 +7,7 @@
 
 #include "SkCachedData.h"
 #include "SkDiscardableMemory.h"
+#include "SkMemory.h"
 
 //#define TRACK_CACHEDDATA_LIFETIME
 
index f9ab4af..6639a77 100644 (file)
@@ -5,8 +5,8 @@
  * found in the LICENSE file.
  */
 
-
 #include "SkDeque.h"
+#include "SkMemory.h"
 
 struct SkDeque::Block {
     Block*  fNext;
index 1115080..25cc651 100644 (file)
@@ -7,6 +7,8 @@
 
 #include "SkMask.h"
 
+#include "SkMemory.h"
+
 //#define TRACK_SKMASK_LIFETIME
 
 /** returns the product if it is positive and fits in 31 bits. Otherwise this
index 6e1f5e2..462325a 100644 (file)
@@ -7,6 +7,8 @@
 
 
 #include "SkMetaData.h"
+
+#include "SkMemory.h"
 #include "SkRefCnt.h"
 
 struct PtrPair {
index c97b646..4ef2d94 100644 (file)
@@ -5,8 +5,10 @@
  * found in the LICENSE file.
  */
 
-#include "SkAtomics.h"
 #include "SkRWBuffer.h"
+
+#include "SkAtomics.h"
+#include "SkMemory.h"
 #include "SkStream.h"
 
 // Force small chunks to be a page's worth
index 0b2723a..d91596d 100644 (file)
@@ -7,6 +7,8 @@
 
 #include "SkRect.h"
 
+#include "SkMemory.h"
+
 void SkIRect::join(int32_t left, int32_t top, int32_t right, int32_t bottom) {
     // do nothing if the params are empty
     if (left >= right || top >= bottom) {
index ef36914..04ec0e2 100644 (file)
@@ -10,7 +10,9 @@
 #define SkRegionPriv_DEFINED
 
 #include "SkRegion.h"
+
 #include "SkAtomics.h"
+#include "SkMemory.h"
 
 inline bool SkRegionValueIsSentinel(int32_t value) {
     return value == (int32_t)SkRegion::kRunTypeSentinel;
index 58fa3f4..c9cb2d3 100644 (file)
@@ -9,6 +9,8 @@
 #define SkTLList_DEFINED
 
 #include "SkTInternalLList.h"
+
+#include "SkMemory.h"
 #include "SkTypes.h"
 #include <utility>
 
index 9ff9777..1744d0a 100644 (file)
@@ -7,6 +7,9 @@
 
 
 #include "SkTSearch.h"
+
+#include "SkMemory.h"
+
 #include <ctype.h>
 
 static inline const char* index_into_base(const char*const* base, int index,
index ea0524b..2877d5b 100644 (file)
@@ -7,6 +7,8 @@
 
 #include "SkVarAlloc.h"
 
+#include "SkMemory.h"
+
 struct SkVarAlloc::Block {
     Block* prev;
     char* data() { return (char*)(this + 1); }
index 20b87e0..f6f248e 100644 (file)
@@ -8,6 +8,8 @@
 
 #include "SkGradientBitmapCache.h"
 
+#include "SkMemory.h"
+
 struct SkGradientBitmapCache::Entry {
     Entry*      fPrev;
     Entry*      fNext;
index c95b59f..0b956ae 100644 (file)
@@ -7,6 +7,8 @@
 
 #include "GrMemoryPool.h"
 
+#include "SkMemory.h"
+
 #ifdef SK_DEBUG
     #define VALIDATE this->validate()
 #else
index 296e052..424d22e 100644 (file)
@@ -10,6 +10,7 @@
 
 #include "GrRectanizer.h"
 #include "SkMathPriv.h"
+#include "SkMemory.h"
 #include "SkPoint.h"
 
 // This Rectanizer quantizes the incoming rects to powers of 2. Each power
index 6ced5bf..fece7f0 100644 (file)
@@ -8,6 +8,7 @@
 #include "SkDiscardableMemory.h"
 #include "SkDiscardableMemoryPool.h"
 #include "SkImageGenerator.h"
+#include "SkMemory.h"
 #include "SkMutex.h"
 #include "SkOnce.h"
 #include "SkTInternalLList.h"
index c2b85fc..af72764 100644 (file)
@@ -9,6 +9,7 @@
 #include "SkData.h"
 #include "SkDeflate.h"
 #include "SkMakeUnique.h"
+#include "SkMemory.h"
 
 #include "zlib.h"
 
index d155308..9e3a7d3 100644 (file)
@@ -18,6 +18,7 @@
 #include "SkMask.h"
 #include "SkMaskGamma.h"
 #include "SkMatrix22.h"
+#include "SkMemory.h"
 #include "SkMutex.h"
 #include "SkOTUtils.h"
 #include "SkPath.h"
index e50a739..17dac84 100644 (file)
@@ -10,6 +10,7 @@
 #include "SkFixed.h"
 #include "SkFontMgr.h"
 #include "SkFontMgr_android_parser.h"
+#include "SkMemory.h"
 #include "SkOSFile.h"
 #include "SkStream.h"
 #include "SkTDArray.h"
index 5574a8a..a303abb 100644 (file)
@@ -4,10 +4,11 @@
  * Use of this source code is governed by a BSD-style license that can be
  * found in the LICENSE file.
  */
+#include "SkMemory.h"
 
 #include "SkTypes.h"
 
-#include <stdlib.h>
+#include <cstdlib>
 
 #define SK_DEBUGFAILF(fmt, ...) \
     SkASSERT((SkDebugf(fmt"\n", __VA_ARGS__), false))
index f8a9963..d665799 100644 (file)
@@ -6,8 +6,9 @@
  * found in the LICENSE file.
  */
 
-#include "SkTypes.h"
+#include "SkMemory.h"
 
+#include "SkTypes.h"
 #include "mozilla/mozalloc.h"
 #include "mozilla/mozalloc_abort.h"
 #include "mozilla/mozalloc_oom.h"
index 9c5ada6..541e06b 100644 (file)
@@ -9,6 +9,7 @@
 #if defined(SK_BUILD_FOR_WIN32)
 
 #include "SkLeanWindows.h"
+#include "SkMemory.h"
 #include "SkOSFile.h"
 #include "SkTFitsIn.h"
 
index d3c0b26..db09018 100644 (file)
@@ -5,10 +5,11 @@
  * found in the LICENSE file.
  */
 
+#include "SkInterpolator.h"
 
 #include "SkFixed.h"
-#include "SkInterpolator.h"
 #include "SkMath.h"
+#include "SkMemory.h"
 #include "SkTSearch.h"
 
 SkInterpolatorBase::SkInterpolatorBase() {
index e878c97..06b717b 100644 (file)
@@ -6,9 +6,11 @@
  */
 
 #include "SkTypes.h"
+
 #if defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_IOS)
 
 #include "SkCGUtils.h"
+#include "SkMemory.h"
 #include "SkStream.h"
 
 // These are used by CGDataProviderCreateWithData