1 /* 2 * Copyright 2017 Google Inc. 3 * 4 * Use of this source code is governed by a BSD-style license that can be 5 * found in the LICENSE file. 6 */ 7 8 #ifndef SkMalloc_DEFINED 9 #define SkMalloc_DEFINED 10 11 #include <cstddef> 12 #include <cstring> 13 14 #include "SkPreConfig.h" 15 16 /* 17 memory wrappers to be implemented by the porting layer (platform) 18 */ 19 20 enum { 21 SK_MALLOC_TEMP = 0x01, //!< hint to sk_malloc that the requested memory will be freed in the scope of the stack frame 22 SK_MALLOC_THROW = 0x02 //!< instructs sk_malloc to call sk_throw if the memory cannot be allocated. 23 }; 24 /** Return a block of memory (at least 4-byte aligned) of at least the 25 specified size. If the requested memory cannot be returned, either 26 return null (if SK_MALLOC_TEMP bit is clear) or throw an exception 27 (if SK_MALLOC_TEMP bit is set). To free the memory, call sk_free(). 28 */ 29 SK_API extern void* sk_malloc_flags(size_t size, unsigned flags); 30 /** Same as sk_malloc(), but hard coded to pass SK_MALLOC_THROW as the flag 31 */ 32 SK_API extern void* sk_malloc_throw(size_t size); 33 /** Same as standard realloc(), but this one never returns null on failure. It will throw 34 an exception if it fails. 35 */ 36 SK_API extern void* sk_realloc_throw(void* buffer, size_t size); 37 /** Free memory returned by sk_malloc(). It is safe to pass null. 38 */ 39 SK_API extern void sk_free(void*); 40 41 /** Much like calloc: returns a pointer to at least size zero bytes, or NULL on failure. 42 */ 43 SK_API extern void* sk_calloc(size_t size); 44 45 /** Same as sk_calloc, but throws an exception instead of returning NULL on failure. 46 */ 47 SK_API extern void* sk_calloc_throw(size_t size); 48 49 /** Called internally if we run out of memory. The platform implementation must 50 not return, but should either throw an exception or otherwise exit. 51 */ 52 SK_API extern void sk_out_of_memory(void); 53 54 // bzero is safer than memset, but we can't rely on it, so... sk_bzero() 55 static inline void sk_bzero(void* buffer, size_t size) { 56 // Please c.f. sk_careful_memcpy. It's undefined behavior to call memset(null, 0, 0). 57 if (size) { 58 memset(buffer, 0, size); 59 } 60 } 61 62 /** 63 * sk_careful_memcpy() is just like memcpy(), but guards against undefined behavior. 64 * 65 * It is undefined behavior to call memcpy() with null dst or src, even if len is 0. 66 * If an optimizer is "smart" enough, it can exploit this to do unexpected things. 67 * memcpy(dst, src, 0); 68 * if (src) { 69 * printf("%x\n", *src); 70 * } 71 * In this code the compiler can assume src is not null and omit the if (src) {...} check, 72 * unconditionally running the printf, crashing the program if src really is null. 73 * Of the compilers we pay attention to only GCC performs this optimization in practice. 74 */ 75 static inline void* sk_careful_memcpy(void* dst, const void* src, size_t len) { 76 // When we pass >0 len we had better already be passing valid pointers. 77 // So we just need to skip calling memcpy when len == 0. 78 if (len) { 79 memcpy(dst,src,len); 80 } 81 return dst; 82 } 83 84 #endif // SkMalloc_DEFINED 85