1 /* libs/corecg/SkMemory_stdlib.cpp
2 **
3 ** Copyright 2006, The Android Open Source Project
4 **
5 ** Licensed under the Apache License, Version 2.0 (the "License");
6 ** you may not use this file except in compliance with the License.
7 ** You may obtain a copy of the License at
8 **
9 ** http://www.apache.org/licenses/LICENSE-2.0
10 **
11 ** Unless required by applicable law or agreed to in writing, software
12 ** distributed under the License is distributed on an "AS IS" BASIS,
13 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 ** See the License for the specific language governing permissions and
15 ** limitations under the License.
16 */
17
18 #include "SkTypes.h"
19 #include <stdio.h>
20 #include <stdlib.h>
21
22 #ifdef SK_DEBUG
23 #define SK_TAG_BLOCKS
24 // #define SK_TRACK_ALLOC // enable to see a printf for every alloc/free
25 // #define SK_CHECK_TAGS // enable to double-check debugging link list
26 #endif
27
28 #ifdef SK_TAG_BLOCKS
29
30 #include "SkThread.h"
31
32 // size this (as a multiple of 4) so that the total offset to the internal data
33 // is at least a multiple of 8 (since some clients of our malloc may require
34 // that.
35 static const char kBlockHeaderTag[] = { 's', 'k', 'i', 'a', '1', '2', '3', '4' };
36 static const char kBlockTrailerTag[] = { 'a', 'i', 'k', 's' };
37 #define kByteFill 0xCD
38 #define kDeleteFill 0xEF
39
get_block_mutex()40 static SkMutex& get_block_mutex() {
41 static SkMutex* gBlockMutex;
42 if (NULL == gBlockMutex) {
43 gBlockMutex = new SkMutex;
44 }
45 return *gBlockMutex;
46 }
47
48 static struct SkBlockHeader* gHeader;
49
50 struct SkBlockHeader {
51 SkBlockHeader* fNext;
52 #ifdef SK_CHECK_TAGS
53 SkBlockHeader** fTop; // set to verify in debugger that block was alloc'd / freed with same gHeader
54 SkBlockHeader* fPrevious; // set to see in debugger previous block when corruption happens
55 #endif
56 size_t fSize;
57 char fHeader[sizeof(kBlockHeaderTag)];
58 // data goes here. The offset to this point must be a multiple of 8
59 char fTrailer[sizeof(kBlockTrailerTag)];
60
addSkBlockHeader61 void* add(size_t realSize)
62 {
63 SkAutoMutexAcquire ac(get_block_mutex());
64 InMutexValidate();
65 fNext = gHeader;
66 #ifdef SK_CHECK_TAGS
67 fTop = &gHeader;
68 fPrevious = NULL;
69 if (fNext != NULL)
70 fNext->fPrevious = this;
71 #endif
72 gHeader = this;
73 fSize = realSize;
74 memcpy(fHeader, kBlockHeaderTag, sizeof(kBlockHeaderTag));
75 void* result = fTrailer;
76 void* trailer = (char*)result + realSize;
77 memcpy(trailer, kBlockTrailerTag, sizeof(kBlockTrailerTag));
78 return result;
79 }
80
DumpSkBlockHeader81 static void Dump()
82 {
83 SkAutoMutexAcquire ac(get_block_mutex());
84 InMutexValidate();
85 SkBlockHeader* header = gHeader;
86 int count = 0;
87 size_t size = 0;
88 while (header != NULL) {
89 char scratch[256];
90 char* pos = scratch;
91 size_t size = header->fSize;
92 int* data = (int*)(void*)header->fTrailer;
93 pos += sprintf(pos, "%p 0x%08zx (%7zd) ",
94 data, size, size);
95 size >>= 2;
96 size_t ints = size > 4 ? 4 : size;
97 size_t index;
98 for (index = 0; index < ints; index++)
99 pos += sprintf(pos, "0x%08x ", data[index]);
100 pos += sprintf(pos, " (");
101 for (index = 0; index < ints; index++)
102 pos += sprintf(pos, "%g ", data[index] / 65536.0f);
103 if (ints > 0)
104 --pos;
105 pos += sprintf(pos, ") \"");
106 size_t chars = size > 16 ? 16 : size;
107 char* chPtr = (char*) data;
108 for (index = 0; index < chars; index++) {
109 char ch = chPtr[index];
110 pos += sprintf(pos, "%c", ch >= ' ' && ch < 0x7f ? ch : '?');
111 }
112 pos += sprintf(pos, "\"");
113 SkDebugf("%s\n", scratch);
114 count++;
115 size += header->fSize;
116 header = header->fNext;
117 }
118 SkDebugf("--- count %d size 0x%08x (%zd) ---\n", count, size, size);
119 }
120
removeSkBlockHeader121 void remove() const
122 {
123 SkAutoMutexAcquire ac(get_block_mutex());
124 SkBlockHeader** findPtr = &gHeader;
125 do {
126 SkBlockHeader* find = *findPtr;
127 SkASSERT(find != NULL);
128 if (find == this) {
129 *findPtr = fNext;
130 break;
131 }
132 findPtr = &find->fNext;
133 } while (true);
134 InMutexValidate();
135 SkASSERT(memcmp(fHeader, kBlockHeaderTag, sizeof(kBlockHeaderTag)) == 0);
136 const char* trailer = fTrailer + fSize;
137 SkASSERT(memcmp(trailer, kBlockTrailerTag, sizeof(kBlockTrailerTag)) == 0);
138 }
139
ValidateSkBlockHeader140 static void Validate()
141 {
142 SkAutoMutexAcquire ac(get_block_mutex());
143 InMutexValidate();
144 }
145
146 private:
InMutexValidateSkBlockHeader147 static void InMutexValidate()
148 {
149 SkBlockHeader* header = gHeader;
150 while (header != NULL) {
151 SkASSERT(memcmp(header->fHeader, kBlockHeaderTag, sizeof(kBlockHeaderTag)) == 0);
152 char* trailer = header->fTrailer + header->fSize;
153 SkASSERT(memcmp(trailer, kBlockTrailerTag, sizeof(kBlockTrailerTag)) == 0);
154 header = header->fNext;
155 }
156 }
157 };
158
ValidateHeap()159 void ValidateHeap()
160 {
161 SkBlockHeader::Validate();
162 }
163 #else
ValidateHeap()164 void ValidateHeap() {}
165 #endif
166
sk_throw()167 void sk_throw()
168 {
169 #ifdef ANDROID
170 fprintf(stderr, "throwing...\n");
171 #endif
172 SkASSERT(!"sk_throw");
173 abort();
174 }
175
sk_out_of_memory(void)176 void sk_out_of_memory(void)
177 {
178 #ifdef ANDROID
179 fprintf(stderr,"- out of memory in SGL -\n");
180 #endif
181 SkASSERT(!"sk_out_of_memory");
182 abort();
183 }
184
sk_malloc_throw(size_t size)185 void* sk_malloc_throw(size_t size)
186 {
187 return sk_malloc_flags(size, SK_MALLOC_THROW);
188 }
189
sk_realloc_throw(void * addr,size_t size)190 void* sk_realloc_throw(void* addr, size_t size)
191 {
192 #ifdef SK_TAG_BLOCKS
193 ValidateHeap();
194 if (addr != NULL) {
195 SkBlockHeader* header = (SkBlockHeader*)
196 ((char*)addr - SK_OFFSETOF(SkBlockHeader, fTrailer));
197 header->remove();
198 #ifdef SK_TRACK_ALLOC
199 printf("sk_realloc_throw %p oldSize=%zd\n", addr, header->fSize);
200 #endif
201 addr = header;
202 }
203 size_t realSize = size;
204 if (size)
205 size += sizeof(SkBlockHeader);
206 #endif
207
208 void* p = realloc(addr, size);
209 if (size == 0)
210 {
211 ValidateHeap();
212 return p;
213 }
214
215 if (p == NULL)
216 sk_throw();
217 #ifdef SK_TAG_BLOCKS
218 else
219 {
220 SkBlockHeader* header = (SkBlockHeader*) p;
221 p = header->add(realSize);
222 #ifdef SK_TRACK_ALLOC
223 printf("sk_realloc_throw %p size=%zd\n", p, realSize);
224 #endif
225 }
226 #endif
227 ValidateHeap();
228 return p;
229 }
230
sk_free(void * p)231 void sk_free(void* p)
232 {
233 if (p)
234 {
235 ValidateHeap();
236 #ifdef SK_TAG_BLOCKS
237 SkBlockHeader* header = (SkBlockHeader*)
238 ((char*)p - SK_OFFSETOF(SkBlockHeader, fTrailer));
239 header->remove();
240 #ifdef SK_TRACK_ALLOC
241 printf("sk_free %p size=%zd\n", p, header->fSize);
242 #endif
243 size_t size = header->fSize + sizeof(SkBlockHeader);
244 memset(header, kDeleteFill, size);
245 p = header;
246 #endif
247 ValidateHeap();
248 free(p);
249 ValidateHeap();
250 }
251 }
252
sk_malloc_flags(size_t size,unsigned flags)253 void* sk_malloc_flags(size_t size, unsigned flags)
254 {
255 ValidateHeap();
256 #ifdef SK_TAG_BLOCKS
257 size_t realSize = size;
258 size += sizeof(SkBlockHeader);
259 #endif
260
261 void* p = malloc(size);
262 if (p == NULL)
263 {
264 if (flags & SK_MALLOC_THROW)
265 sk_throw();
266 }
267 #ifdef SK_TAG_BLOCKS
268 else
269 {
270 SkBlockHeader* header = (SkBlockHeader*) p;
271 p = header->add(realSize);
272 memset(p, kByteFill, realSize);
273 #ifdef SK_TRACK_ALLOC
274 printf("sk_malloc_flags %p size=%zd\n", p, realSize);
275 #endif
276 }
277 #endif
278 ValidateHeap();
279 return p;
280 }
281
282