2 * Copyright (c) 2010 The WebM project authors. All Rights Reserved.
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
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
18 #include "include/vpx_mem_intrnl.h"
20 #if CONFIG_MEM_TRACKER
21 #ifndef VPX_NO_GLOBALS
22 static unsigned long g_alloc_count
= 0;
24 #include "vpx_global_handling.h"
25 #define g_alloc_count vpxglobalm(vpxmem,g_alloc_count)
29 #if CONFIG_MEM_MANAGER
31 # include "hmm_intrnl.h"
33 # define SHIFT_HMM_ADDR_ALIGN_UNIT 5
34 # define TOTAL_MEMORY_TO_ALLOCATE 20971520 /* 20 * 1024 * 1024 */
36 # define MM_DYNAMIC_MEMORY 1
37 # if MM_DYNAMIC_MEMORY
38 static unsigned char *g_p_mng_memory_raw
= NULL
;
39 static unsigned char *g_p_mng_memory
= NULL
;
41 static unsigned char g_p_mng_memory
[TOTAL_MEMORY_TO_ALLOCATE
];
44 static size_t g_mm_memory_size
= TOTAL_MEMORY_TO_ALLOCATE
;
46 static hmm_descriptor hmm_d
;
47 static int g_mng_memory_allocated
= 0;
49 static int vpx_mm_create_heap_memory();
50 static void *vpx_mm_realloc(void *memblk
, size_t size
);
51 #endif /*CONFIG_MEM_MANAGER*/
53 #if USE_GLOBAL_FUNCTION_POINTERS
54 struct GLOBAL_FUNC_POINTERS
56 g_malloc_func g_malloc
;
57 g_calloc_func g_calloc
;
58 g_realloc_func g_realloc
;
60 g_memcpy_func g_memcpy
;
61 g_memset_func g_memset
;
62 g_memmove_func g_memmove
;
65 # define VPX_MALLOC_L g_func->g_malloc
66 # define VPX_REALLOC_L g_func->g_realloc
67 # define VPX_FREE_L g_func->g_free
68 # define VPX_MEMCPY_L g_func->g_memcpy
69 # define VPX_MEMSET_L g_func->g_memset
70 # define VPX_MEMMOVE_L g_func->g_memmove
72 # define VPX_MALLOC_L malloc
73 # define VPX_REALLOC_L realloc
74 # define VPX_FREE_L free
75 # define VPX_MEMCPY_L memcpy
76 # define VPX_MEMSET_L memset
77 # define VPX_MEMMOVE_L memmove
78 #endif /* USE_GLOBAL_FUNCTION_POINTERS */
80 unsigned int vpx_mem_get_version()
82 unsigned int ver
= ((unsigned int)(unsigned char)VPX_MEM_VERSION_CHIEF
<< 24 |
83 (unsigned int)(unsigned char)VPX_MEM_VERSION_MAJOR
<< 16 |
84 (unsigned int)(unsigned char)VPX_MEM_VERSION_MINOR
<< 8 |
85 (unsigned int)(unsigned char)VPX_MEM_VERSION_PATCH
);
89 int vpx_mem_set_heap_size(size_t size
)
93 #if CONFIG_MEM_MANAGER
96 if (!g_mng_memory_allocated
&& size
)
98 g_mm_memory_size
= size
;
114 void *vpx_memalign(size_t align
, size_t size
)
119 #if CONFIG_MEM_MANAGER
122 if (vpx_mm_create_heap_memory() < 0)
124 _P(printf("[vpx][mm] ERROR vpx_memalign() Couldn't create memory for Heap.\n");)
127 number_aau
= ((size
+ align
- 1 + ADDRESS_STORAGE_SIZE
) >>
128 SHIFT_HMM_ADDR_ALIGN_UNIT
) + 1;
130 addr
= hmm_alloc(&hmm_d
, number_aau
);
132 addr
= VPX_MALLOC_L(size
+ align
- 1 + ADDRESS_STORAGE_SIZE
);
133 #endif /*CONFIG_MEM_MANAGER*/
137 x
= align_addr((unsigned char *)addr
+ ADDRESS_STORAGE_SIZE
, (int)align
);
138 /* save the actual malloc address */
139 ((size_t *)x
)[-1] = (size_t)addr
;
145 void *vpx_malloc(size_t size
)
147 return vpx_memalign(DEFAULT_ALIGNMENT
, size
);
150 void *vpx_calloc(size_t num
, size_t size
)
154 x
= vpx_memalign(DEFAULT_ALIGNMENT
, num
* size
);
157 VPX_MEMSET_L(x
, 0, num
* size
);
162 void *vpx_realloc(void *memblk
, size_t size
)
166 int align
= DEFAULT_ALIGNMENT
;
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.
177 new_addr
= vpx_malloc(size
);
182 addr
= (void *)(((size_t *)memblk
)[-1]);
185 #if CONFIG_MEM_MANAGER
186 new_addr
= vpx_mm_realloc(addr
, size
+ align
+ ADDRESS_STORAGE_SIZE
);
188 new_addr
= VPX_REALLOC_L(addr
, size
+ align
+ ADDRESS_STORAGE_SIZE
);
194 new_addr
= (void *)(((size_t)
195 ((unsigned char *)new_addr
+ ADDRESS_STORAGE_SIZE
) + (align
- 1)) &
197 /* save the actual malloc address */
198 ((size_t *)new_addr
)[-1] = (size_t)addr
;
205 void vpx_free(void *memblk
)
209 void *addr
= (void *)(((size_t *)memblk
)[-1]);
210 #if CONFIG_MEM_MANAGER
211 hmm_free(&hmm_d
, addr
);
218 #if CONFIG_MEM_TRACKER
219 void *xvpx_memalign(size_t align
, size_t size
, char *file
, int line
)
222 unsigned char *x_bounds
;
227 if (g_alloc_count
== 0)
230 int i_rv
= vpx_memory_tracker_init(BOUNDS_CHECK_PAD_SIZE
, BOUNDS_CHECK_VALUE
);
232 int i_rv
= vpx_memory_tracker_init(0, 0);
237 _P(printf("ERROR xvpx_malloc MEM_TRACK_USAGE error vpx_memory_tracker_init().\n");)
244 unsigned int tempme
= BOUNDS_CHECK_VALUE
;
246 x_bounds
= vpx_memalign(align
, size
+ (BOUNDS_CHECK_PAD_SIZE
* 2));
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_SIZE
,
257 /* save the actual malloc address */
258 ((size_t *)x
)[-1] = (size_t)x_bounds
;
260 for (i
= 0; i
< BOUNDS_CHECK_PAD_SIZE
; i
+= sizeof(unsigned int))
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));
271 x
= vpx_memalign(align
, size
);
272 #endif /*TRY_BOUNDS_CHECK*/
276 vpx_memory_tracker_add((size_t)x
, (unsigned int)size
, file
, line
, 1);
281 void *xvpx_malloc(size_t size
, char *file
, int line
)
283 return xvpx_memalign(DEFAULT_ALIGNMENT
, size
, file
, line
);
286 void *xvpx_calloc(size_t num
, size_t size
, char *file
, int line
)
288 void *x
= xvpx_memalign(DEFAULT_ALIGNMENT
, num
* size
, file
, line
);
291 VPX_MEMSET_L(x
, 0, num
* size
);
296 void *xvpx_realloc(void *memblk
, size_t size
, char *file
, int line
)
298 struct mem_block
*p
= NULL
;
301 char *orig_file
= NULL
;
304 unsigned char *x_bounds
= memblk
?
305 (unsigned char *)(((size_t *)memblk
)[-1]) :
311 if (g_alloc_count
== 0)
315 if (!vpx_memory_tracker_init(BOUNDS_CHECK_PAD_SIZE
, BOUNDS_CHECK_VALUE
))
317 if (!vpx_memory_tracker_init(0, 0))
320 _P(printf("ERROR xvpx_malloc MEM_TRACK_USAGE error vpx_memory_tracker_init().\n");)
324 if ((p
= vpx_memory_tracker_find((size_t)memblk
)))
331 #if TRY_BOUNDS_CHECK_ON_FREE
332 vpx_memory_tracker_check_integrity(file
, line
);
335 /* have to do this regardless of success, because
336 * the memory that does get realloc'd may change
337 * the bounds values of this block
339 vpx_memory_tracker_remove((size_t)memblk
);
344 unsigned int tempme
= BOUNDS_CHECK_VALUE
;
346 x_bounds
= vpx_realloc(memblk
, size
+ (BOUNDS_CHECK_PAD_SIZE
* 2));
350 x_bounds
= (unsigned char *)(((size_t *)x_bounds
)[-1]);
351 x
= align_addr(x_bounds
+ BOUNDS_CHECK_PAD_SIZE
+ ADDRESS_STORAGE_SIZE
,
352 (int)DEFAULT_ALIGNMENT
);
353 /* save the actual malloc address */
354 ((size_t *)x
)[-1] = (size_t)x_bounds
;
356 for (i
= 0; i
< BOUNDS_CHECK_PAD_SIZE
; i
+= sizeof(unsigned int))
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));
367 x
= vpx_realloc(memblk
, size
);
368 #endif /*TRY_BOUNDS_CHECK*/
370 if (!memblk
) ++g_alloc_count
;
373 vpx_memory_tracker_add((size_t)x
, (unsigned int)size
, file
, line
, 1);
375 vpx_memory_tracker_add((size_t)memblk
, orig_size
, orig_file
, orig_line
, 1);
380 void xvpx_free(void *p_address
, char *file
, int line
)
383 unsigned char *p_bounds_address
= (unsigned char *)p_address
;
384 /*p_bounds_address -= BOUNDS_CHECK_PAD_SIZE;*/
387 #if !TRY_BOUNDS_CHECK_ON_FREE
394 #if TRY_BOUNDS_CHECK_ON_FREE
395 vpx_memory_tracker_check_integrity(file
, line
);
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
401 if (vpx_memory_tracker_remove((size_t)p_address
) == -2)
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
));
412 vpx_free(p_bounds_address
);
418 vpx_memory_tracker_destroy();
422 #endif /*CONFIG_MEM_TRACKER*/
424 #if CONFIG_MEM_CHECKS
426 #include <task_lib.h> /*for task_delay()*/
427 /* 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. */
429 static int get_my_tt(int task
)
436 static void vx_sleep(int msec
)
438 int ticks_to_sleep
= 0;
442 int msec_per_tick
= 1000 / sys_clk_rate_get();
444 if (msec
< msec_per_tick
)
447 ticks_to_sleep
= msec
/ msec_per_tick
;
450 task_delay(ticks_to_sleep
);
455 void *vpx_memcpy(void *dest
, const void *source
, size_t length
)
457 #if CONFIG_MEM_CHECKS
459 if (((int)dest
< 0x4000) || ((int)source
< 0x4000))
461 _P(printf("WARNING: vpx_memcpy dest:0x%x source:0x%x len:%d\n", (int)dest
, (int)source
, length
);)
464 sp(get_my_tt
, task_id_self(), 0, 0, 0, 0, 0, 0, 0, 0);
472 return VPX_MEMCPY_L(dest
, source
, length
);
475 void *vpx_memset(void *dest
, int val
, size_t length
)
477 #if CONFIG_MEM_CHECKS
479 if ((int)dest
< 0x4000)
481 _P(printf("WARNING: vpx_memset dest:0x%x val:%d len:%d\n", (int)dest
, val
, length
);)
484 sp(get_my_tt
, task_id_self(), 0, 0, 0, 0, 0, 0, 0, 0);
492 return VPX_MEMSET_L(dest
, val
, length
);
495 void *vpx_memmove(void *dest
, const void *src
, size_t count
)
497 #if CONFIG_MEM_CHECKS
499 if (((int)dest
< 0x4000) || ((int)src
< 0x4000))
501 _P(printf("WARNING: vpx_memmove dest:0x%x src:0x%x count:%d\n", (int)dest
, (int)src
, count
);)
504 sp(get_my_tt
, task_id_self(), 0, 0, 0, 0, 0, 0, 0, 0);
512 return VPX_MEMMOVE_L(dest
, src
, count
);
515 #if CONFIG_MEM_MANAGER
517 static int vpx_mm_create_heap_memory()
521 if (!g_mng_memory_allocated
)
523 #if MM_DYNAMIC_MEMORY
525 (unsigned char *)malloc(g_mm_memory_size
+ HMM_ADDR_ALIGN_UNIT
);
527 if (g_p_mng_memory_raw
)
529 g_p_mng_memory
= (unsigned char *)((((unsigned int)g_p_mng_memory_raw
) +
530 HMM_ADDR_ALIGN_UNIT
- 1) &
531 -(int)HMM_ADDR_ALIGN_UNIT
);
533 _P(printf("[vpx][mm] total memory size:%d g_p_mng_memory_raw:0x%x g_p_mng_memory:0x%x\n"
534 , g_mm_memory_size
+ HMM_ADDR_ALIGN_UNIT
535 , (unsigned int)g_p_mng_memory_raw
536 , (unsigned int)g_p_mng_memory
);)
540 _P(printf("[vpx][mm] Couldn't allocate memory:%d for vpx memory manager.\n"
541 , g_mm_memory_size
);)
551 g_mng_memory_allocated
= 1;
555 chunk_size
= g_mm_memory_size
>> SHIFT_HMM_ADDR_ALIGN_UNIT
;
557 chunk_size
-= DUMMY_END_BLOCK_BAUS
;
559 _P(printf("[vpx][mm] memory size:%d for vpx memory manager. g_p_mng_memory:0x%x chunk_size:%d\n"
561 , (unsigned int)g_p_mng_memory
564 hmm_new_chunk(&hmm_d
, (void *)g_p_mng_memory
, chunk_size
);
567 #if MM_DYNAMIC_MEMORY
570 _P(printf("[vpx][mm] Couldn't allocate memory:%d for vpx memory manager.\n"
571 , g_mm_memory_size
);)
582 static void *vpx_mm_realloc(void *memblk
, size_t size
)
586 if (vpx_mm_create_heap_memory() < 0)
588 _P(printf("[vpx][mm] ERROR vpx_mm_realloc() Couldn't create memory for Heap.\n");)
596 old_num_aaus
= hmm_true_size(memblk
);
597 new_num_aaus
= (size
>> SHIFT_HMM_ADDR_ALIGN_UNIT
) + 1;
599 if (old_num_aaus
== new_num_aaus
)
605 i_rv
= hmm_resize(&hmm_d
, memblk
, new_num_aaus
);
613 /* Error. Try to malloc and then copy data. */
616 new_num_aaus
= (size
>> SHIFT_HMM_ADDR_ALIGN_UNIT
) + 1;
617 p_from_malloc
= hmm_alloc(&hmm_d
, new_num_aaus
);
621 vpx_memcpy(p_from_malloc
, memblk
, size
);
622 hmm_free(&hmm_d
, memblk
);
624 p_ret
= p_from_malloc
;
632 #endif /*CONFIG_MEM_MANAGER*/
634 #if USE_GLOBAL_FUNCTION_POINTERS
635 # if CONFIG_MEM_TRACKER
636 extern int vpx_memory_tracker_set_functions(g_malloc_func g_malloc_l
637 , g_calloc_func g_calloc_l
638 , g_realloc_func g_realloc_l
639 , g_free_func g_free_l
640 , g_memcpy_func g_memcpy_l
641 , g_memset_func g_memset_l
642 , g_memmove_func g_memmove_l
);
644 #endif /*USE_GLOBAL_FUNCTION_POINTERS*/
645 int vpx_mem_set_functions(g_malloc_func g_malloc_l
646 , g_calloc_func g_calloc_l
647 , g_realloc_func g_realloc_l
648 , g_free_func g_free_l
649 , g_memcpy_func g_memcpy_l
650 , g_memset_func g_memset_l
651 , g_memmove_func g_memmove_l
)
653 #if USE_GLOBAL_FUNCTION_POINTERS
655 /* If use global functions is turned on then the
656 application must set the global functions before
657 it does anything else or vpx_mem will have
658 unpredictable results. */
661 g_func
= (struct GLOBAL_FUNC_POINTERS
*)
662 g_malloc_l(sizeof(struct GLOBAL_FUNC_POINTERS
));
670 #if CONFIG_MEM_TRACKER
673 rv
= vpx_memory_tracker_set_functions(g_malloc_l
688 g_func
->g_malloc
= g_malloc_l
;
689 g_func
->g_calloc
= g_calloc_l
;
690 g_func
->g_realloc
= g_realloc_l
;
691 g_func
->g_free
= g_free_l
;
692 g_func
->g_memcpy
= g_memcpy_l
;
693 g_func
->g_memset
= g_memset_l
;
694 g_func
->g_memmove
= g_memmove_l
;
709 int vpx_mem_unset_functions()
711 #if USE_GLOBAL_FUNCTION_POINTERS
715 g_free_func temp_free
= g_func
->g_free
;