• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
21 /** Free memory returned by sk_malloc(). It is safe to pass null. */
22 SK_API extern void sk_free(void*);
23 
24 /**
25  *  Called internally if we run out of memory. The platform implementation must
26  *  not return, but should either throw an exception or otherwise exit.
27  */
28 SK_API extern void sk_out_of_memory(void);
29 
30 enum {
31 #ifdef SK_SUPPORT_LEGACY_MALLOC_PORTING_LAYER
32     SK_MALLOC_TEMP = 1,
33 #else
34     /**
35      *  If this bit is set, the returned buffer must be zero-initialized. If this bit is not set
36      *  the buffer can be uninitialized.
37      */
38     SK_MALLOC_ZERO_INITIALIZE   = 1 << 0,
39 #endif
40 
41     /**
42      *  If this bit is set, the implementation must throw/crash/quit if the request cannot
43      *  be fulfilled. If this bit is not set, then it should return nullptr on failure.
44      */
45     SK_MALLOC_THROW             = 1 << 1,
46 };
47 /**
48  *  Return a block of memory (at least 4-byte aligned) of at least the specified size.
49  *  If the requested memory cannot be returned, either return nullptr or throw/exit, depending
50  *  on the SK_MALLOC_THROW bit. If the allocation succeeds, the memory will be zero-initialized
51  *  if the SK_MALLOC_ZERO_INITIALIZE bit was set.
52  *
53  *  To free the memory, call sk_free()
54  */
55 SK_API extern void* sk_malloc_flags(size_t size, unsigned flags);
56 
57 /** Same as standard realloc(), but this one never returns null on failure. It will throw
58  *  an exception if it fails.
59  */
60 SK_API extern void* sk_realloc_throw(void* buffer, size_t size);
61 
62 #ifdef SK_SUPPORT_LEGACY_MALLOC_PORTING_LAYER
63 
64 /** Same as sk_malloc_flags(), but hard coded to pass SK_MALLOC_THROW as the flag */
65 SK_API extern void* sk_malloc_throw(size_t size);
66 
67 /** Much like calloc: returns a pointer to at least size zero bytes, or NULL on failure.
68  */
69 SK_API extern void* sk_calloc(size_t size);
70 
71 /** Same as sk_calloc, but throws an exception instead of returning NULL on failure.
72  */
73 SK_API extern void* sk_calloc_throw(size_t size);
74 
75 #else
sk_malloc_throw(size_t size)76 static inline void* sk_malloc_throw(size_t size) {
77     return sk_malloc_flags(size, SK_MALLOC_THROW);
78 }
79 
sk_calloc_throw(size_t size)80 static inline void* sk_calloc_throw(size_t size) {
81     return sk_malloc_flags(size, SK_MALLOC_THROW | SK_MALLOC_ZERO_INITIALIZE);
82 }
83 #endif
84 
sk_calloc_canfail(size_t size)85 static inline void* sk_calloc_canfail(size_t size) {
86 #ifdef SK_SUPPORT_LEGACY_MALLOC_PORTING_LAYER
87     return sk_calloc(size);
88 #else
89     return sk_malloc_flags(size, SK_MALLOC_ZERO_INITIALIZE);
90 #endif
91 }
92 
93 // Performs a safe multiply count * elemSize, checking for overflow
94 SK_API extern void* sk_calloc_throw(size_t count, size_t elemSize);
95 SK_API extern void* sk_malloc_throw(size_t count, size_t elemSize);
96 SK_API extern void* sk_realloc_throw(void* buffer, size_t count, size_t elemSize);
97 
98 /**
99  *  These variants return nullptr on failure
100  */
sk_malloc_canfail(size_t size)101 static inline void* sk_malloc_canfail(size_t size) {
102     return sk_malloc_flags(size, 0);
103 }
104 SK_API extern void* sk_malloc_canfail(size_t count, size_t elemSize);
105 
106 // bzero is safer than memset, but we can't rely on it, so... sk_bzero()
sk_bzero(void * buffer,size_t size)107 static inline void sk_bzero(void* buffer, size_t size) {
108     // Please c.f. sk_careful_memcpy.  It's undefined behavior to call memset(null, 0, 0).
109     if (size) {
110         memset(buffer, 0, size);
111     }
112 }
113 
114 /**
115  *  sk_careful_memcpy() is just like memcpy(), but guards against undefined behavior.
116  *
117  * It is undefined behavior to call memcpy() with null dst or src, even if len is 0.
118  * If an optimizer is "smart" enough, it can exploit this to do unexpected things.
119  *     memcpy(dst, src, 0);
120  *     if (src) {
121  *         printf("%x\n", *src);
122  *     }
123  * In this code the compiler can assume src is not null and omit the if (src) {...} check,
124  * unconditionally running the printf, crashing the program if src really is null.
125  * Of the compilers we pay attention to only GCC performs this optimization in practice.
126  */
sk_careful_memcpy(void * dst,const void * src,size_t len)127 static inline void* sk_careful_memcpy(void* dst, const void* src, size_t len) {
128     // When we pass >0 len we had better already be passing valid pointers.
129     // So we just need to skip calling memcpy when len == 0.
130     if (len) {
131         memcpy(dst,src,len);
132     }
133     return dst;
134 }
135 
136 #endif  // SkMalloc_DEFINED
137