Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(101)

Side by Side Diff: source/libvpx/vpx_mem/vpx_mem.c

Issue 11555023: libvpx: Add VP9 decoder. (Closed) Base URL: svn://chrome-svn/chrome/trunk/deps/third_party/libvpx/
Patch Set: Created 8 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 /* 1 /*
2 * Copyright (c) 2010 The WebM project authors. All Rights Reserved. 2 * Copyright (c) 2010 The WebM project authors. All Rights Reserved.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license 4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source 5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found 6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may 7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree. 8 * be found in the AUTHORS file in the root of the source tree.
9 */ 9 */
10 10
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
44 static size_t g_mm_memory_size = TOTAL_MEMORY_TO_ALLOCATE; 44 static size_t g_mm_memory_size = TOTAL_MEMORY_TO_ALLOCATE;
45 45
46 static hmm_descriptor hmm_d; 46 static hmm_descriptor hmm_d;
47 static int g_mng_memory_allocated = 0; 47 static int g_mng_memory_allocated = 0;
48 48
49 static int vpx_mm_create_heap_memory(); 49 static int vpx_mm_create_heap_memory();
50 static void *vpx_mm_realloc(void *memblk, size_t size); 50 static void *vpx_mm_realloc(void *memblk, size_t size);
51 #endif /*CONFIG_MEM_MANAGER*/ 51 #endif /*CONFIG_MEM_MANAGER*/
52 52
53 #if USE_GLOBAL_FUNCTION_POINTERS 53 #if USE_GLOBAL_FUNCTION_POINTERS
54 struct GLOBAL_FUNC_POINTERS 54 struct GLOBAL_FUNC_POINTERS {
55 { 55 g_malloc_func g_malloc;
56 g_malloc_func g_malloc; 56 g_calloc_func g_calloc;
57 g_calloc_func g_calloc; 57 g_realloc_func g_realloc;
58 g_realloc_func g_realloc; 58 g_free_func g_free;
59 g_free_func g_free; 59 g_memcpy_func g_memcpy;
60 g_memcpy_func g_memcpy; 60 g_memset_func g_memset;
61 g_memset_func g_memset; 61 g_memmove_func g_memmove;
62 g_memmove_func g_memmove;
63 } *g_func = NULL; 62 } *g_func = NULL;
64 63
65 # define VPX_MALLOC_L g_func->g_malloc 64 # define VPX_MALLOC_L g_func->g_malloc
66 # define VPX_REALLOC_L g_func->g_realloc 65 # define VPX_REALLOC_L g_func->g_realloc
67 # define VPX_FREE_L g_func->g_free 66 # define VPX_FREE_L g_func->g_free
68 # define VPX_MEMCPY_L g_func->g_memcpy 67 # define VPX_MEMCPY_L g_func->g_memcpy
69 # define VPX_MEMSET_L g_func->g_memset 68 # define VPX_MEMSET_L g_func->g_memset
70 # define VPX_MEMMOVE_L g_func->g_memmove 69 # define VPX_MEMMOVE_L g_func->g_memmove
71 #else 70 #else
72 # define VPX_MALLOC_L malloc 71 # define VPX_MALLOC_L malloc
73 # define VPX_REALLOC_L realloc 72 # define VPX_REALLOC_L realloc
74 # define VPX_FREE_L free 73 # define VPX_FREE_L free
75 # define VPX_MEMCPY_L memcpy 74 # define VPX_MEMCPY_L memcpy
76 # define VPX_MEMSET_L memset 75 # define VPX_MEMSET_L memset
77 # define VPX_MEMMOVE_L memmove 76 # define VPX_MEMMOVE_L memmove
78 #endif /* USE_GLOBAL_FUNCTION_POINTERS */ 77 #endif /* USE_GLOBAL_FUNCTION_POINTERS */
79 78
80 unsigned int vpx_mem_get_version() 79 unsigned int vpx_mem_get_version() {
81 { 80 unsigned int ver = ((unsigned int)(unsigned char)VPX_MEM_VERSION_CHIEF << 24 |
82 unsigned int ver = ((unsigned int)(unsigned char)VPX_MEM_VERSION_CHIEF << 24 | 81 (unsigned int)(unsigned char)VPX_MEM_VERSION_MAJOR << 16 |
83 (unsigned int)(unsigned char)VPX_MEM_VERSION_MAJOR << 16 | 82 (unsigned int)(unsigned char)VPX_MEM_VERSION_MINOR << 8 |
84 (unsigned int)(unsigned char)VPX_MEM_VERSION_MINOR << 8 | 83 (unsigned int)(unsigned char)VPX_MEM_VERSION_PATCH);
85 (unsigned int)(unsigned char)VPX_MEM_VERSION_PATCH); 84 return ver;
86 return ver; 85 }
87 } 86
88 87 int vpx_mem_set_heap_size(size_t size) {
89 int vpx_mem_set_heap_size(size_t size) 88 int ret = -1;
90 {
91 int ret = -1;
92 89
93 #if CONFIG_MEM_MANAGER 90 #if CONFIG_MEM_MANAGER
94 #if MM_DYNAMIC_MEMORY 91 #if MM_DYNAMIC_MEMORY
95 92
96 if (!g_mng_memory_allocated && size) 93 if (!g_mng_memory_allocated && size) {
94 g_mm_memory_size = size;
95 ret = 0;
96 } else
97 ret = -3;
98
99 #else
100 ret = -2;
101 #endif
102 #else
103 (void)size;
104 #endif
105
106 return ret;
107 }
108
109 void *vpx_memalign(size_t align, size_t size) {
110 void *addr,
111 * x = NULL;
112
113 #if CONFIG_MEM_MANAGER
114 int number_aau;
115
116 if (vpx_mm_create_heap_memory() < 0) {
117 _P(printf("[vpx][mm] ERROR vpx_memalign() Couldn't create memory for Heap.\n ");)
118 }
119
120 number_aau = ((size + align - 1 + ADDRESS_STORAGE_SIZE) >>
121 SHIFT_HMM_ADDR_ALIGN_UNIT) + 1;
122
123 addr = hmm_alloc(&hmm_d, number_aau);
124 #else
125 addr = VPX_MALLOC_L(size + align - 1 + ADDRESS_STORAGE_SIZE);
126 #endif /*CONFIG_MEM_MANAGER*/
127
128 if (addr) {
129 x = align_addr((unsigned char *)addr + ADDRESS_STORAGE_SIZE, (int)align);
130 /* save the actual malloc address */
131 ((size_t *)x)[-1] = (size_t)addr;
132 }
133
134 return x;
135 }
136
137 void *vpx_malloc(size_t size) {
138 return vpx_memalign(DEFAULT_ALIGNMENT, size);
139 }
140
141 void *vpx_calloc(size_t num, size_t size) {
142 void *x;
143
144 x = vpx_memalign(DEFAULT_ALIGNMENT, num * size);
145
146 if (x)
147 VPX_MEMSET_L(x, 0, num * size);
148
149 return x;
150 }
151
152 void *vpx_realloc(void *memblk, size_t size) {
153 void *addr,
154 * new_addr = NULL;
155 int align = DEFAULT_ALIGNMENT;
156
157 /*
158 The realloc() function changes the size of the object pointed to by
159 ptr to the size specified by size, and returns a pointer to the
160 possibly moved block. The contents are unchanged up to the lesser
161 of the new and old sizes. If ptr is null, realloc() behaves like
162 malloc() for the specified size. If size is zero (0) and ptr is
163 not a null pointer, the object pointed to is freed.
164 */
165 if (!memblk)
166 new_addr = vpx_malloc(size);
167 else if (!size)
168 vpx_free(memblk);
169 else {
170 addr = (void *)(((size_t *)memblk)[-1]);
171 memblk = NULL;
172
173 #if CONFIG_MEM_MANAGER
174 new_addr = vpx_mm_realloc(addr, size + align + ADDRESS_STORAGE_SIZE);
175 #else
176 new_addr = VPX_REALLOC_L(addr, size + align + ADDRESS_STORAGE_SIZE);
177 #endif
178
179 if (new_addr) {
180 addr = new_addr;
181 new_addr = (void *)(((size_t)
182 ((unsigned char *)new_addr + ADDRESS_STORAGE_SIZE) + (align - 1)) &
183 (size_t) - align);
184 /* save the actual malloc address */
185 ((size_t *)new_addr)[-1] = (size_t)addr;
186 }
187 }
188
189 return new_addr;
190 }
191
192 void vpx_free(void *memblk) {
193 if (memblk) {
194 void *addr = (void *)(((size_t *)memblk)[-1]);
195 #if CONFIG_MEM_MANAGER
196 hmm_free(&hmm_d, addr);
197 #else
198 VPX_FREE_L(addr);
199 #endif
200 }
201 }
202
203 #if CONFIG_MEM_TRACKER
204 void *xvpx_memalign(size_t align, size_t size, char *file, int line) {
205 #if TRY_BOUNDS_CHECK
206 unsigned char *x_bounds;
207 #endif
208
209 void *x;
210
211 if (g_alloc_count == 0) {
212 #if TRY_BOUNDS_CHECK
213 int i_rv = vpx_memory_tracker_init(BOUNDS_CHECK_PAD_SIZE, BOUNDS_CHECK_VALUE );
214 #else
215 int i_rv = vpx_memory_tracker_init(0, 0);
216 #endif
217
218 if (i_rv < 0) {
219 _P(printf("ERROR xvpx_malloc MEM_TRACK_USAGE error vpx_memory_tracker_init ().\n");)
220 }
221 }
222
223 #if TRY_BOUNDS_CHECK
224 {
225 int i;
226 unsigned int tempme = BOUNDS_CHECK_VALUE;
227
228 x_bounds = vpx_memalign(align, size + (BOUNDS_CHECK_PAD_SIZE * 2));
229
230 if (x_bounds) {
231 /*we're aligning the address twice here but to keep things
232 consistent we want to have the padding come before the stored
233 address so no matter what free function gets called we will
234 attempt to free the correct address*/
235 x_bounds = (unsigned char *)(((size_t *)x_bounds)[-1]);
236 x = align_addr(x_bounds + BOUNDS_CHECK_PAD_SIZE + ADDRESS_STORAGE_SIZE,
237 (int)align);
238 /* save the actual malloc address */
239 ((size_t *)x)[-1] = (size_t)x_bounds;
240
241 for (i = 0; i < BOUNDS_CHECK_PAD_SIZE; i += sizeof(unsigned int)) {
242 VPX_MEMCPY_L(x_bounds + i, &tempme, sizeof(unsigned int));
243 VPX_MEMCPY_L((unsigned char *)x + size + i,
244 &tempme, sizeof(unsigned int));
245 }
246 } else
247 x = NULL;
248 }
249 #else
250 x = vpx_memalign(align, size);
251 #endif /*TRY_BOUNDS_CHECK*/
252
253 g_alloc_count++;
254
255 vpx_memory_tracker_add((size_t)x, (unsigned int)size, file, line, 1);
256
257 return x;
258 }
259
260 void *xvpx_malloc(size_t size, char *file, int line) {
261 return xvpx_memalign(DEFAULT_ALIGNMENT, size, file, line);
262 }
263
264 void *xvpx_calloc(size_t num, size_t size, char *file, int line) {
265 void *x = xvpx_memalign(DEFAULT_ALIGNMENT, num * size, file, line);
266
267 if (x)
268 VPX_MEMSET_L(x, 0, num * size);
269
270 return x;
271 }
272
273 void *xvpx_realloc(void *memblk, size_t size, char *file, int line) {
274 struct mem_block *p = NULL;
275 int orig_size = 0,
276 orig_line = 0;
277 char *orig_file = NULL;
278
279 #if TRY_BOUNDS_CHECK
280 unsigned char *x_bounds = memblk ?
281 (unsigned char *)(((size_t *)memblk)[-1]) :
282 NULL;
283 #endif
284
285 void *x;
286
287 if (g_alloc_count == 0) {
288 #if TRY_BOUNDS_CHECK
289
290 if (!vpx_memory_tracker_init(BOUNDS_CHECK_PAD_SIZE, BOUNDS_CHECK_VALUE))
291 #else
292 if (!vpx_memory_tracker_init(0, 0))
293 #endif
97 { 294 {
98 g_mm_memory_size = size; 295 _P(printf("ERROR xvpx_malloc MEM_TRACK_USAGE error vpx_memory_tracker_init ().\n");)
99 ret = 0;
100 } 296 }
101 else 297 }
102 ret = -3; 298
103 299 if ((p = vpx_memory_tracker_find((size_t)memblk))) {
104 #else 300 orig_size = p->size;
105 ret = -2; 301 orig_file = p->file;
106 #endif 302 orig_line = p->line;
107 #else 303 }
108 (void)size; 304
109 #endif 305 #if TRY_BOUNDS_CHECK_ON_FREE
110 306 vpx_memory_tracker_check_integrity(file, line);
111 return ret; 307 #endif
112 } 308
113 309 /* have to do this regardless of success, because
114 void *vpx_memalign(size_t align, size_t size) 310 * the memory that does get realloc'd may change
115 { 311 * the bounds values of this block
116 void *addr, 312 */
117 * x = NULL; 313 vpx_memory_tracker_remove((size_t)memblk);
118 314
119 #if CONFIG_MEM_MANAGER 315 #if TRY_BOUNDS_CHECK
120 int number_aau; 316 {
121 317 int i;
122 if (vpx_mm_create_heap_memory() < 0) 318 unsigned int tempme = BOUNDS_CHECK_VALUE;
123 { 319
124 _P(printf("[vpx][mm] ERROR vpx_memalign() Couldn't create memory for Hea p.\n");) 320 x_bounds = vpx_realloc(memblk, size + (BOUNDS_CHECK_PAD_SIZE * 2));
125 } 321
126 322 if (x_bounds) {
127 number_aau = ((size + align - 1 + ADDRESS_STORAGE_SIZE) >> 323 x_bounds = (unsigned char *)(((size_t *)x_bounds)[-1]);
128 SHIFT_HMM_ADDR_ALIGN_UNIT) + 1; 324 x = align_addr(x_bounds + BOUNDS_CHECK_PAD_SIZE + ADDRESS_STORAGE_SIZE,
129 325 (int)DEFAULT_ALIGNMENT);
130 addr = hmm_alloc(&hmm_d, number_aau); 326 /* save the actual malloc address */
131 #else 327 ((size_t *)x)[-1] = (size_t)x_bounds;
132 addr = VPX_MALLOC_L(size + align - 1 + ADDRESS_STORAGE_SIZE); 328
133 #endif /*CONFIG_MEM_MANAGER*/ 329 for (i = 0; i < BOUNDS_CHECK_PAD_SIZE; i += sizeof(unsigned int)) {
134 330 VPX_MEMCPY_L(x_bounds + i, &tempme, sizeof(unsigned int));
135 if (addr) 331 VPX_MEMCPY_L((unsigned char *)x + size + i,
136 { 332 &tempme, sizeof(unsigned int));
137 x = align_addr((unsigned char *)addr + ADDRESS_STORAGE_SIZE, (int)align) ; 333 }
138 /* save the actual malloc address */ 334 } else
139 ((size_t *)x)[-1] = (size_t)addr; 335 x = NULL;
140 } 336 }
141 337 #else
142 return x; 338 x = vpx_realloc(memblk, size);
143 }
144
145 void *vpx_malloc(size_t size)
146 {
147 return vpx_memalign(DEFAULT_ALIGNMENT, size);
148 }
149
150 void *vpx_calloc(size_t num, size_t size)
151 {
152 void *x;
153
154 x = vpx_memalign(DEFAULT_ALIGNMENT, num * size);
155
156 if (x)
157 VPX_MEMSET_L(x, 0, num * size);
158
159 return x;
160 }
161
162 void *vpx_realloc(void *memblk, size_t size)
163 {
164 void *addr,
165 * new_addr = NULL;
166 int align = DEFAULT_ALIGNMENT;
167
168 /*
169 The realloc() function changes the size of the object pointed to by
170 ptr to the size specified by size, and returns a pointer to the
171 possibly moved block. The contents are unchanged up to the lesser
172 of the new and old sizes. If ptr is null, realloc() behaves like
173 malloc() for the specified size. If size is zero (0) and ptr is
174 not a null pointer, the object pointed to is freed.
175 */
176 if (!memblk)
177 new_addr = vpx_malloc(size);
178 else if (!size)
179 vpx_free(memblk);
180 else
181 {
182 addr = (void *)(((size_t *)memblk)[-1]);
183 memblk = NULL;
184
185 #if CONFIG_MEM_MANAGER
186 new_addr = vpx_mm_realloc(addr, size + align + ADDRESS_STORAGE_SIZE);
187 #else
188 new_addr = VPX_REALLOC_L(addr, size + align + ADDRESS_STORAGE_SIZE);
189 #endif
190
191 if (new_addr)
192 {
193 addr = new_addr;
194 new_addr = (void *)(((size_t)
195 ((unsigned char *)new_addr + ADDRESS_STORAGE_SI ZE) + (align - 1)) &
196 (size_t) - align);
197 /* save the actual malloc address */
198 ((size_t *)new_addr)[-1] = (size_t)addr;
199 }
200 }
201
202 return new_addr;
203 }
204
205 void vpx_free(void *memblk)
206 {
207 if (memblk)
208 {
209 void *addr = (void *)(((size_t *)memblk)[-1]);
210 #if CONFIG_MEM_MANAGER
211 hmm_free(&hmm_d, addr);
212 #else
213 VPX_FREE_L(addr);
214 #endif
215 }
216 }
217
218 #if CONFIG_MEM_TRACKER
219 void *xvpx_memalign(size_t align, size_t size, char *file, int line)
220 {
221 #if TRY_BOUNDS_CHECK
222 unsigned char *x_bounds;
223 #endif
224
225 void *x;
226
227 if (g_alloc_count == 0)
228 {
229 #if TRY_BOUNDS_CHECK
230 int i_rv = vpx_memory_tracker_init(BOUNDS_CHECK_PAD_SIZE, BOUNDS_CHECK_V ALUE);
231 #else
232 int i_rv = vpx_memory_tracker_init(0, 0);
233 #endif
234
235 if (i_rv < 0)
236 {
237 _P(printf("ERROR xvpx_malloc MEM_TRACK_USAGE error vpx_memory_tracke r_init().\n");)
238 }
239 }
240
241 #if TRY_BOUNDS_CHECK
242 {
243 int i;
244 unsigned int tempme = BOUNDS_CHECK_VALUE;
245
246 x_bounds = vpx_memalign(align, size + (BOUNDS_CHECK_PAD_SIZE * 2));
247
248 if (x_bounds)
249 {
250 /*we're aligning the address twice here but to keep things
251 consistent we want to have the padding come before the stored
252 address so no matter what free function gets called we will
253 attempt to free the correct address*/
254 x_bounds = (unsigned char *)(((size_t *)x_bounds)[-1]);
255 x = align_addr(x_bounds + BOUNDS_CHECK_PAD_SIZE + ADDRESS_STORAGE_SI ZE,
256 (int)align);
257 /* save the actual malloc address */
258 ((size_t *)x)[-1] = (size_t)x_bounds;
259
260 for (i = 0; i < BOUNDS_CHECK_PAD_SIZE; i += sizeof(unsigned int))
261 {
262 VPX_MEMCPY_L(x_bounds + i, &tempme, sizeof(unsigned int));
263 VPX_MEMCPY_L((unsigned char *)x + size + i,
264 &tempme, sizeof(unsigned int));
265 }
266 }
267 else
268 x = NULL;
269 }
270 #else
271 x = vpx_memalign(align, size);
272 #endif /*TRY_BOUNDS_CHECK*/ 339 #endif /*TRY_BOUNDS_CHECK*/
273 340
274 g_alloc_count++; 341 if (!memblk) ++g_alloc_count;
275 342
343 if (x)
276 vpx_memory_tracker_add((size_t)x, (unsigned int)size, file, line, 1); 344 vpx_memory_tracker_add((size_t)x, (unsigned int)size, file, line, 1);
277 345 else
278 return x; 346 vpx_memory_tracker_add((size_t)memblk, orig_size, orig_file, orig_line, 1);
279 } 347
280 348 return x;
281 void *xvpx_malloc(size_t size, char *file, int line) 349 }
282 { 350
283 return xvpx_memalign(DEFAULT_ALIGNMENT, size, file, line); 351 void xvpx_free(void *p_address, char *file, int line) {
284 } 352 #if TRY_BOUNDS_CHECK
285 353 unsigned char *p_bounds_address = (unsigned char *)p_address;
286 void *xvpx_calloc(size_t num, size_t size, char *file, int line) 354 /*p_bounds_address -= BOUNDS_CHECK_PAD_SIZE;*/
287 { 355 #endif
288 void *x = xvpx_memalign(DEFAULT_ALIGNMENT, num * size, file, line); 356
289 357 #if !TRY_BOUNDS_CHECK_ON_FREE
290 if (x) 358 (void)file;
291 VPX_MEMSET_L(x, 0, num * size); 359 (void)line;
292 360 #endif
293 return x; 361
294 } 362 if (p_address) {
295
296 void *xvpx_realloc(void *memblk, size_t size, char *file, int line)
297 {
298 struct mem_block *p = NULL;
299 int orig_size = 0,
300 orig_line = 0;
301 char *orig_file = NULL;
302
303 #if TRY_BOUNDS_CHECK
304 unsigned char *x_bounds = memblk ?
305 (unsigned char *)(((size_t *)memblk)[-1]) :
306 NULL;
307 #endif
308
309 void *x;
310
311 if (g_alloc_count == 0)
312 {
313 #if TRY_BOUNDS_CHECK
314
315 if (!vpx_memory_tracker_init(BOUNDS_CHECK_PAD_SIZE, BOUNDS_CHECK_VALUE))
316 #else
317 if (!vpx_memory_tracker_init(0, 0))
318 #endif
319 {
320 _P(printf("ERROR xvpx_malloc MEM_TRACK_USAGE error vpx_memory_tracke r_init().\n");)
321 }
322 }
323
324 if ((p = vpx_memory_tracker_find((size_t)memblk)))
325 {
326 orig_size = p->size;
327 orig_file = p->file;
328 orig_line = p->line;
329 }
330
331 #if TRY_BOUNDS_CHECK_ON_FREE 363 #if TRY_BOUNDS_CHECK_ON_FREE
332 vpx_memory_tracker_check_integrity(file, line); 364 vpx_memory_tracker_check_integrity(file, line);
333 #endif 365 #endif
334 366
335 /* have to do this regardless of success, because 367 /* if the addr isn't found in the list, assume it was allocated via
336 * the memory that does get realloc'd may change 368 * vpx_ calls not xvpx_, therefore it does not contain any padding
337 * the bounds values of this block
338 */ 369 */
339 vpx_memory_tracker_remove((size_t)memblk); 370 if (vpx_memory_tracker_remove((size_t)p_address) == -2) {
340 371 p_bounds_address = p_address;
341 #if TRY_BOUNDS_CHECK 372 _P(fprintf(stderr, "[vpx_mem][xvpx_free] addr: %p not found in"
342 { 373 " list; freed from file:%s"
343 int i; 374 " line:%d\n", p_address, file, line));
344 unsigned int tempme = BOUNDS_CHECK_VALUE; 375 } else
345 376 --g_alloc_count;
346 x_bounds = vpx_realloc(memblk, size + (BOUNDS_CHECK_PAD_SIZE * 2)); 377
347 378 #if TRY_BOUNDS_CHECK
348 if (x_bounds) 379 vpx_free(p_bounds_address);
349 { 380 #else
350 x_bounds = (unsigned char *)(((size_t *)x_bounds)[-1]); 381 vpx_free(p_address);
351 x = align_addr(x_bounds + BOUNDS_CHECK_PAD_SIZE + ADDRESS_STORAGE_SI ZE, 382 #endif
352 (int)DEFAULT_ALIGNMENT); 383
353 /* save the actual malloc address */ 384 if (!g_alloc_count)
354 ((size_t *)x)[-1] = (size_t)x_bounds; 385 vpx_memory_tracker_destroy();
355 386 }
356 for (i = 0; i < BOUNDS_CHECK_PAD_SIZE; i += sizeof(unsigned int))
357 {
358 VPX_MEMCPY_L(x_bounds + i, &tempme, sizeof(unsigned int));
359 VPX_MEMCPY_L((unsigned char *)x + size + i,
360 &tempme, sizeof(unsigned int));
361 }
362 }
363 else
364 x = NULL;
365 }
366 #else
367 x = vpx_realloc(memblk, size);
368 #endif /*TRY_BOUNDS_CHECK*/
369
370 if (!memblk) ++g_alloc_count;
371
372 if (x)
373 vpx_memory_tracker_add((size_t)x, (unsigned int)size, file, line, 1);
374 else
375 vpx_memory_tracker_add((size_t)memblk, orig_size, orig_file, orig_line, 1);
376
377 return x;
378 }
379
380 void xvpx_free(void *p_address, char *file, int line)
381 {
382 #if TRY_BOUNDS_CHECK
383 unsigned char *p_bounds_address = (unsigned char *)p_address;
384 /*p_bounds_address -= BOUNDS_CHECK_PAD_SIZE;*/
385 #endif
386
387 #if !TRY_BOUNDS_CHECK_ON_FREE
388 (void)file;
389 (void)line;
390 #endif
391
392 if (p_address)
393 {
394 #if TRY_BOUNDS_CHECK_ON_FREE
395 vpx_memory_tracker_check_integrity(file, line);
396 #endif
397
398 /* if the addr isn't found in the list, assume it was allocated via
399 * vpx_ calls not xvpx_, therefore it does not contain any padding
400 */
401 if (vpx_memory_tracker_remove((size_t)p_address) == -2)
402 {
403 p_bounds_address = p_address;
404 _P(fprintf(stderr, "[vpx_mem][xvpx_free] addr: %p not found in"
405 " list; freed from file:%s"
406 " line:%d\n", p_address, file, line));
407 }
408 else
409 --g_alloc_count;
410
411 #if TRY_BOUNDS_CHECK
412 vpx_free(p_bounds_address);
413 #else
414 vpx_free(p_address);
415 #endif
416
417 if (!g_alloc_count)
418 vpx_memory_tracker_destroy();
419 }
420 } 387 }
421 388
422 #endif /*CONFIG_MEM_TRACKER*/ 389 #endif /*CONFIG_MEM_TRACKER*/
423 390
424 #if CONFIG_MEM_CHECKS 391 #if CONFIG_MEM_CHECKS
425 #if defined(VXWORKS) 392 #if defined(VXWORKS)
426 #include <task_lib.h> /*for task_delay()*/ 393 #include <task_lib.h> /*for task_delay()*/
427 /* This function is only used to get a stack trace of the player 394 /* This function is only used to get a stack trace of the player
428 object so we can se where we are having a problem. */ 395 object so we can se where we are having a problem. */
429 static int get_my_tt(int task) 396 static int get_my_tt(int task) {
430 { 397 tt(task);
431 tt(task); 398
432 399 return 0;
433 return 0; 400 }
434 } 401
435 402 static void vx_sleep(int msec) {
436 static void vx_sleep(int msec) 403 int ticks_to_sleep = 0;
437 { 404
438 int ticks_to_sleep = 0; 405 if (msec) {
439 406 int msec_per_tick = 1000 / sys_clk_rate_get();
440 if (msec) 407
408 if (msec < msec_per_tick)
409 ticks_to_sleep++;
410 else
411 ticks_to_sleep = msec / msec_per_tick;
412 }
413
414 task_delay(ticks_to_sleep);
415 }
416 #endif
417 #endif
418
419 void *vpx_memcpy(void *dest, const void *source, size_t length) {
420 #if CONFIG_MEM_CHECKS
421
422 if (((int)dest < 0x4000) || ((int)source < 0x4000)) {
423 _P(printf("WARNING: vpx_memcpy dest:0x%x source:0x%x len:%d\n", (int)dest, ( int)source, length);)
424
425 #if defined(VXWORKS)
426 sp(get_my_tt, task_id_self(), 0, 0, 0, 0, 0, 0, 0, 0);
427
428 vx_sleep(10000);
429 #endif
430 }
431
432 #endif
433
434 return VPX_MEMCPY_L(dest, source, length);
435 }
436
437 void *vpx_memset(void *dest, int val, size_t length) {
438 #if CONFIG_MEM_CHECKS
439
440 if ((int)dest < 0x4000) {
441 _P(printf("WARNING: vpx_memset dest:0x%x val:%d len:%d\n", (int)dest, val, l ength);)
442
443 #if defined(VXWORKS)
444 sp(get_my_tt, task_id_self(), 0, 0, 0, 0, 0, 0, 0, 0);
445
446 vx_sleep(10000);
447 #endif
448 }
449
450 #endif
451
452 return VPX_MEMSET_L(dest, val, length);
453 }
454
455 void *vpx_memmove(void *dest, const void *src, size_t count) {
456 #if CONFIG_MEM_CHECKS
457
458 if (((int)dest < 0x4000) || ((int)src < 0x4000)) {
459 _P(printf("WARNING: vpx_memmove dest:0x%x src:0x%x count:%d\n", (int)dest, ( int)src, count);)
460
461 #if defined(VXWORKS)
462 sp(get_my_tt, task_id_self(), 0, 0, 0, 0, 0, 0, 0, 0);
463
464 vx_sleep(10000);
465 #endif
466 }
467
468 #endif
469
470 return VPX_MEMMOVE_L(dest, src, count);
471 }
472
473 #if CONFIG_MEM_MANAGER
474
475 static int vpx_mm_create_heap_memory() {
476 int i_rv = 0;
477
478 if (!g_mng_memory_allocated) {
479 #if MM_DYNAMIC_MEMORY
480 g_p_mng_memory_raw =
481 (unsigned char *)malloc(g_mm_memory_size + HMM_ADDR_ALIGN_UNIT);
482
483 if (g_p_mng_memory_raw) {
484 g_p_mng_memory = (unsigned char *)((((unsigned int)g_p_mng_memory_raw) +
485 HMM_ADDR_ALIGN_UNIT - 1) &
486 -(int)HMM_ADDR_ALIGN_UNIT);
487
488 _P(printf("[vpx][mm] total memory size:%d g_p_mng_memory_raw:0x%x g_p_mng_ memory:0x%x\n"
489 , g_mm_memory_size + HMM_ADDR_ALIGN_UNIT
490 , (unsigned int)g_p_mng_memory_raw
491 , (unsigned int)g_p_mng_memory);)
492 } else {
493 _P(printf("[vpx][mm] Couldn't allocate memory:%d for vpx memory manager.\n "
494 , g_mm_memory_size);)
495
496 i_rv = -1;
497 }
498
499 if (g_p_mng_memory)
500 #endif
441 { 501 {
442 int msec_per_tick = 1000 / sys_clk_rate_get(); 502 int chunk_size = 0;
443 503
444 if (msec < msec_per_tick) 504 g_mng_memory_allocated = 1;
445 ticks_to_sleep++; 505
446 else 506 hmm_init(&hmm_d);
447 ticks_to_sleep = msec / msec_per_tick; 507
448 } 508 chunk_size = g_mm_memory_size >> SHIFT_HMM_ADDR_ALIGN_UNIT;
449 509
450 task_delay(ticks_to_sleep); 510 chunk_size -= DUMMY_END_BLOCK_BAUS;
451 } 511
452 #endif 512 _P(printf("[vpx][mm] memory size:%d for vpx memory manager. g_p_mng_memory :0x%x chunk_size:%d\n"
453 #endif 513 , g_mm_memory_size
454 514 , (unsigned int)g_p_mng_memory
455 void *vpx_memcpy(void *dest, const void *source, size_t length) 515 , chunk_size);)
456 { 516
457 #if CONFIG_MEM_CHECKS 517 hmm_new_chunk(&hmm_d, (void *)g_p_mng_memory, chunk_size);
458 518 }
459 if (((int)dest < 0x4000) || ((int)source < 0x4000)) 519
460 { 520 #if MM_DYNAMIC_MEMORY
461 _P(printf("WARNING: vpx_memcpy dest:0x%x source:0x%x len:%d\n", (int)des t, (int)source, length);) 521 else {
462 522 _P(printf("[vpx][mm] Couldn't allocate memory:%d for vpx memory manager.\n "
463 #if defined(VXWORKS) 523 , g_mm_memory_size);)
464 sp(get_my_tt, task_id_self(), 0, 0, 0, 0, 0, 0, 0, 0); 524
465 525 i_rv = -1;
466 vx_sleep(10000); 526 }
467 #endif 527
468 } 528 #endif
469 529 }
470 #endif 530
471 531 return i_rv;
472 return VPX_MEMCPY_L(dest, source, length); 532 }
473 } 533
474 534 static void *vpx_mm_realloc(void *memblk, size_t size) {
475 void *vpx_memset(void *dest, int val, size_t length) 535 void *p_ret = NULL;
476 { 536
477 #if CONFIG_MEM_CHECKS 537 if (vpx_mm_create_heap_memory() < 0) {
478 538 _P(printf("[vpx][mm] ERROR vpx_mm_realloc() Couldn't create memory for Heap. \n");)
479 if ((int)dest < 0x4000) 539 } else {
480 {
481 _P(printf("WARNING: vpx_memset dest:0x%x val:%d len:%d\n", (int)dest, va l, length);)
482
483 #if defined(VXWORKS)
484 sp(get_my_tt, task_id_self(), 0, 0, 0, 0, 0, 0, 0, 0);
485
486 vx_sleep(10000);
487 #endif
488 }
489
490 #endif
491
492 return VPX_MEMSET_L(dest, val, length);
493 }
494
495 void *vpx_memmove(void *dest, const void *src, size_t count)
496 {
497 #if CONFIG_MEM_CHECKS
498
499 if (((int)dest < 0x4000) || ((int)src < 0x4000))
500 {
501 _P(printf("WARNING: vpx_memmove dest:0x%x src:0x%x count:%d\n", (int)des t, (int)src, count);)
502
503 #if defined(VXWORKS)
504 sp(get_my_tt, task_id_self(), 0, 0, 0, 0, 0, 0, 0, 0);
505
506 vx_sleep(10000);
507 #endif
508 }
509
510 #endif
511
512 return VPX_MEMMOVE_L(dest, src, count);
513 }
514
515 #if CONFIG_MEM_MANAGER
516
517 static int vpx_mm_create_heap_memory()
518 {
519 int i_rv = 0; 540 int i_rv = 0;
520 541 int old_num_aaus;
521 if (!g_mng_memory_allocated) 542 int new_num_aaus;
522 { 543
523 #if MM_DYNAMIC_MEMORY 544 old_num_aaus = hmm_true_size(memblk);
524 g_p_mng_memory_raw = 545 new_num_aaus = (size >> SHIFT_HMM_ADDR_ALIGN_UNIT) + 1;
525 (unsigned char *)malloc(g_mm_memory_size + HMM_ADDR_ALIGN_UNIT); 546
526 547 if (old_num_aaus == new_num_aaus) {
527 if (g_p_mng_memory_raw) 548 p_ret = memblk;
528 { 549 } else {
529 g_p_mng_memory = (unsigned char *)((((unsigned int)g_p_mng_memory_ra w) + 550 i_rv = hmm_resize(&hmm_d, memblk, new_num_aaus);
530 HMM_ADDR_ALIGN_UNIT - 1) & 551
531 -(int)HMM_ADDR_ALIGN_UNIT); 552 if (i_rv == 0) {
532 553 p_ret = memblk;
533 _P(printf("[vpx][mm] total memory size:%d g_p_mng_memory_raw:0x%x g_ p_mng_memory:0x%x\n" 554 } else {
534 , g_mm_memory_size + HMM_ADDR_ALIGN_UNIT 555 /* Error. Try to malloc and then copy data. */
535 , (unsigned int)g_p_mng_memory_raw 556 void *p_from_malloc;
536 , (unsigned int)g_p_mng_memory);) 557
558 new_num_aaus = (size >> SHIFT_HMM_ADDR_ALIGN_UNIT) + 1;
559 p_from_malloc = hmm_alloc(&hmm_d, new_num_aaus);
560
561 if (p_from_malloc) {
562 vpx_memcpy(p_from_malloc, memblk, size);
563 hmm_free(&hmm_d, memblk);
564
565 p_ret = p_from_malloc;
537 } 566 }
538 else 567 }
539 { 568 }
540 _P(printf("[vpx][mm] Couldn't allocate memory:%d for vpx memory mana ger.\n" 569 }
541 , g_mm_memory_size);) 570
542 571 return p_ret;
543 i_rv = -1;
544 }
545
546 if (g_p_mng_memory)
547 #endif
548 {
549 int chunk_size = 0;
550
551 g_mng_memory_allocated = 1;
552
553 hmm_init(&hmm_d);
554
555 chunk_size = g_mm_memory_size >> SHIFT_HMM_ADDR_ALIGN_UNIT;
556
557 chunk_size -= DUMMY_END_BLOCK_BAUS;
558
559 _P(printf("[vpx][mm] memory size:%d for vpx memory manager. g_p_mng_ memory:0x%x chunk_size:%d\n"
560 , g_mm_memory_size
561 , (unsigned int)g_p_mng_memory
562 , chunk_size);)
563
564 hmm_new_chunk(&hmm_d, (void *)g_p_mng_memory, chunk_size);
565 }
566
567 #if MM_DYNAMIC_MEMORY
568 else
569 {
570 _P(printf("[vpx][mm] Couldn't allocate memory:%d for vpx memory mana ger.\n"
571 , g_mm_memory_size);)
572
573 i_rv = -1;
574 }
575
576 #endif
577 }
578
579 return i_rv;
580 }
581
582 static void *vpx_mm_realloc(void *memblk, size_t size)
583 {
584 void *p_ret = NULL;
585
586 if (vpx_mm_create_heap_memory() < 0)
587 {
588 _P(printf("[vpx][mm] ERROR vpx_mm_realloc() Couldn't create memory for H eap.\n");)
589 }
590 else
591 {
592 int i_rv = 0;
593 int old_num_aaus;
594 int new_num_aaus;
595
596 old_num_aaus = hmm_true_size(memblk);
597 new_num_aaus = (size >> SHIFT_HMM_ADDR_ALIGN_UNIT) + 1;
598
599 if (old_num_aaus == new_num_aaus)
600 {
601 p_ret = memblk;
602 }
603 else
604 {
605 i_rv = hmm_resize(&hmm_d, memblk, new_num_aaus);
606
607 if (i_rv == 0)
608 {
609 p_ret = memblk;
610 }
611 else
612 {
613 /* Error. Try to malloc and then copy data. */
614 void *p_from_malloc;
615
616 new_num_aaus = (size >> SHIFT_HMM_ADDR_ALIGN_UNIT) + 1;
617 p_from_malloc = hmm_alloc(&hmm_d, new_num_aaus);
618
619 if (p_from_malloc)
620 {
621 vpx_memcpy(p_from_malloc, memblk, size);
622 hmm_free(&hmm_d, memblk);
623
624 p_ret = p_from_malloc;
625 }
626 }
627 }
628 }
629
630 return p_ret;
631 } 572 }
632 #endif /*CONFIG_MEM_MANAGER*/ 573 #endif /*CONFIG_MEM_MANAGER*/
633 574
634 #if USE_GLOBAL_FUNCTION_POINTERS 575 #if USE_GLOBAL_FUNCTION_POINTERS
635 # if CONFIG_MEM_TRACKER 576 # if CONFIG_MEM_TRACKER
636 extern int vpx_memory_tracker_set_functions(g_malloc_func g_malloc_l 577 extern int vpx_memory_tracker_set_functions(g_malloc_func g_malloc_l
637 , g_calloc_func g_calloc_l 578 , g_calloc_func g_calloc_l
638 , g_realloc_func g_realloc_l 579 , g_realloc_func g_realloc_l
639 , g_free_func g_free_l 580 , g_free_func g_free_l
640 , g_memcpy_func g_memcpy_l 581 , g_memcpy_func g_memcpy_l
641 , g_memset_func g_memset_l 582 , g_memset_func g_memset_l
642 , g_memmove_func g_memmove_l); 583 , g_memmove_func g_memmove_l);
643 # endif 584 # endif
644 #endif /*USE_GLOBAL_FUNCTION_POINTERS*/ 585 #endif /*USE_GLOBAL_FUNCTION_POINTERS*/
645 int vpx_mem_set_functions(g_malloc_func g_malloc_l 586 int vpx_mem_set_functions(g_malloc_func g_malloc_l
646 , g_calloc_func g_calloc_l 587 , g_calloc_func g_calloc_l
647 , g_realloc_func g_realloc_l 588 , g_realloc_func g_realloc_l
648 , g_free_func g_free_l 589 , g_free_func g_free_l
649 , g_memcpy_func g_memcpy_l 590 , g_memcpy_func g_memcpy_l
650 , g_memset_func g_memset_l 591 , g_memset_func g_memset_l
651 , g_memmove_func g_memmove_l) 592 , g_memmove_func g_memmove_l) {
652 {
653 #if USE_GLOBAL_FUNCTION_POINTERS 593 #if USE_GLOBAL_FUNCTION_POINTERS
654 594
655 /* If use global functions is turned on then the 595 /* If use global functions is turned on then the
656 application must set the global functions before 596 application must set the global functions before
657 it does anything else or vpx_mem will have 597 it does anything else or vpx_mem will have
658 unpredictable results. */ 598 unpredictable results. */
659 if (!g_func) 599 if (!g_func) {
660 { 600 g_func = (struct GLOBAL_FUNC_POINTERS *)
661 g_func = (struct GLOBAL_FUNC_POINTERS *) 601 g_malloc_l(sizeof(struct GLOBAL_FUNC_POINTERS));
662 g_malloc_l(sizeof(struct GLOBAL_FUNC_POINTERS)); 602
663 603 if (!g_func) {
664 if (!g_func) 604 return -1;
665 { 605 }
666 return -1; 606 }
667 }
668 }
669 607
670 #if CONFIG_MEM_TRACKER 608 #if CONFIG_MEM_TRACKER
671 { 609 {
672 int rv = 0; 610 int rv = 0;
673 rv = vpx_memory_tracker_set_functions(g_malloc_l 611 rv = vpx_memory_tracker_set_functions(g_malloc_l
674 , g_calloc_l 612 , g_calloc_l
675 , g_realloc_l 613 , g_realloc_l
676 , g_free_l 614 , g_free_l
677 , g_memcpy_l 615 , g_memcpy_l
678 , g_memset_l 616 , g_memset_l
679 , g_memmove_l); 617 , g_memmove_l);
680 618
681 if (rv < 0) 619 if (rv < 0) {
682 { 620 return rv;
683 return rv; 621 }
684 } 622 }
685 } 623 #endif
686 #endif 624
687 625 g_func->g_malloc = g_malloc_l;
688 g_func->g_malloc = g_malloc_l; 626 g_func->g_calloc = g_calloc_l;
689 g_func->g_calloc = g_calloc_l; 627 g_func->g_realloc = g_realloc_l;
690 g_func->g_realloc = g_realloc_l; 628 g_func->g_free = g_free_l;
691 g_func->g_free = g_free_l; 629 g_func->g_memcpy = g_memcpy_l;
692 g_func->g_memcpy = g_memcpy_l; 630 g_func->g_memset = g_memset_l;
693 g_func->g_memset = g_memset_l; 631 g_func->g_memmove = g_memmove_l;
694 g_func->g_memmove = g_memmove_l; 632
695 633 return 0;
696 return 0;
697 #else 634 #else
698 (void)g_malloc_l; 635 (void)g_malloc_l;
699 (void)g_calloc_l; 636 (void)g_calloc_l;
700 (void)g_realloc_l; 637 (void)g_realloc_l;
701 (void)g_free_l; 638 (void)g_free_l;
702 (void)g_memcpy_l; 639 (void)g_memcpy_l;
703 (void)g_memset_l; 640 (void)g_memset_l;
704 (void)g_memmove_l; 641 (void)g_memmove_l;
705 return -1; 642 return -1;
706 #endif 643 #endif
707 } 644 }
708 645
709 int vpx_mem_unset_functions() 646 int vpx_mem_unset_functions() {
710 {
711 #if USE_GLOBAL_FUNCTION_POINTERS 647 #if USE_GLOBAL_FUNCTION_POINTERS
712 648
713 if (g_func) 649 if (g_func) {
714 { 650 g_free_func temp_free = g_func->g_free;
715 g_free_func temp_free = g_func->g_free; 651 temp_free(g_func);
716 temp_free(g_func); 652 g_func = NULL;
717 g_func = NULL; 653 }
718 } 654
719 655 #endif
720 #endif 656 return 0;
721 return 0; 657 }
722 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698