diff options
author | Herb Derby <herb@google.com> | 2017-03-20 11:19:23 -0400 |
---|---|---|
committer | Skia Commit-Bot <skia-commit-bot@chromium.org> | 2017-03-20 18:40:49 +0000 |
commit | d7b34a5ca0a9183e70737125c688d2a8b5310d81 (patch) | |
tree | 94fe551b5fe1f868fbe316a7ae4316000ac589ff /include | |
parent | 628999dd99a10a737470c5c89c44ae28b6a764bf (diff) |
Make SkMemory.h and adjust all files for usage.
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>
Diffstat (limited to 'include')
-rw-r--r-- | include/core/SkTypes.h | 69 | ||||
-rw-r--r-- | include/private/SkMemory.h | 84 | ||||
-rw-r--r-- | include/private/SkTDArray.h | 3 | ||||
-rw-r--r-- | include/private/SkTemplates.h | 1 |
4 files changed, 90 insertions, 67 deletions
diff --git a/include/core/SkTypes.h b/include/core/SkTypes.h index 8baf246d34..8f32bfbc28 100644 --- a/include/core/SkTypes.h +++ b/include/core/SkTypes.h @@ -34,32 +34,13 @@ // 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 index 0000000000..02ad0ce52e --- /dev/null +++ b/include/private/SkMemory.h @@ -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 diff --git a/include/private/SkTDArray.h b/include/private/SkTDArray.h index 16f8474bd4..acf69b58b4 100644 --- a/include/private/SkTDArray.h +++ b/include/private/SkTDArray.h @@ -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; diff --git a/include/private/SkTemplates.h b/include/private/SkTemplates.h index 6f29aa14f9..e8dd04e6c5 100644 --- a/include/private/SkTemplates.h +++ b/include/private/SkTemplates.h @@ -11,6 +11,7 @@ #define SkTemplates_DEFINED #include "SkMath.h" +#include "SkMemory.h" #include "SkTLogic.h" #include "SkTypes.h" #include <limits.h> |