2 * Copyright (c) 2010 The VP8 project authors. All Rights Reserved.
4 * Use of this source code is governed by a BSD-style license and patent
5 * grant that can be found in the LICENSE file in the root of the source
6 * tree. All contributing project authors may be found in the AUTHORS
7 * file in the root of the source tree.
17 #include "include/vpx_mem_intrnl.h"
19 #if CONFIG_MEM_TRACKER
20 #ifndef VPX_NO_GLOBALS
21 static unsigned long g_alloc_count
= 0;
23 #include "vpx_global_handling.h"
24 #define g_alloc_count vpxglobalm(vpxmem,g_alloc_count)
28 #if CONFIG_MEM_MANAGER
30 # include "hmm_intrnl.h"
32 # define SHIFT_HMM_ADDR_ALIGN_UNIT 5
33 # define TOTAL_MEMORY_TO_ALLOCATE 20971520 // 20 * 1024 * 1024
35 # define MM_DYNAMIC_MEMORY 1
36 # if MM_DYNAMIC_MEMORY
37 static unsigned char *g_p_mng_memory_raw
= NULL
;
38 static unsigned char *g_p_mng_memory
= NULL
;
40 static unsigned char g_p_mng_memory
[TOTAL_MEMORY_TO_ALLOCATE
];
43 static size_t g_mm_memory_size
= TOTAL_MEMORY_TO_ALLOCATE
;
45 static hmm_descriptor hmm_d
;
46 static int g_mng_memory_allocated
= 0;
48 static int vpx_mm_create_heap_memory();
49 static void *vpx_mm_realloc(void *memblk
, size_t size
);
50 #endif //CONFIG_MEM_MANAGER
52 #if USE_GLOBAL_FUNCTION_POINTERS
53 struct GLOBAL_FUNC_POINTERS
55 g_malloc_func g_malloc
;
56 g_calloc_func g_calloc
;
57 g_realloc_func g_realloc
;
59 g_memcpy_func g_memcpy
;
60 g_memset_func g_memset
;
61 g_memmove_func g_memmove
;
64 # define VPX_MALLOC_L g_func->g_malloc
65 # define VPX_REALLOC_L g_func->g_realloc
66 # define VPX_FREE_L g_func->g_free
67 # define VPX_MEMCPY_L g_func->g_memcpy
68 # define VPX_MEMSET_L g_func->g_memset
69 # define VPX_MEMMOVE_L g_func->g_memmove
71 # define VPX_MALLOC_L malloc
72 # define VPX_REALLOC_L realloc
73 # define VPX_FREE_L free
74 # define VPX_MEMCPY_L memcpy
75 # define VPX_MEMSET_L memset
76 # define VPX_MEMMOVE_L memmove
77 #endif // USE_GLOBAL_FUNCTION_POINTERS
79 unsigned int vpx_mem_get_version()
81 unsigned int ver
= ((unsigned int)(unsigned char)VPX_MEM_VERSION_CHIEF
<< 24 |
82 (unsigned int)(unsigned char)VPX_MEM_VERSION_MAJOR
<< 16 |
83 (unsigned int)(unsigned char)VPX_MEM_VERSION_MINOR
<< 8 |
84 (unsigned int)(unsigned char)VPX_MEM_VERSION_PATCH
);
88 int vpx_mem_set_heap_size(size_t size
)
92 #if CONFIG_MEM_MANAGER
95 if (!g_mng_memory_allocated
&& size
)
97 g_mm_memory_size
= size
;
113 void *vpx_memalign(size_t align
, size_t size
)
118 #if CONFIG_MEM_MANAGER
121 if (vpx_mm_create_heap_memory() < 0)
123 _P(printf("[vpx][mm] ERROR vpx_memalign() Couldn't create memory for Heap.\n");)
126 number_aau
= ((size
+ align
- 1 + ADDRESS_STORAGE_SIZE
) >>
127 SHIFT_HMM_ADDR_ALIGN_UNIT
) + 1;
129 addr
= hmm_alloc(&hmm_d
, number_aau
);
131 addr
= VPX_MALLOC_L(size
+ align
- 1 + ADDRESS_STORAGE_SIZE
);
132 #endif //CONFIG_MEM_MANAGER
136 x
= align_addr((unsigned char *)addr
+ ADDRESS_STORAGE_SIZE
, (int)align
);
137 /* save the actual malloc address */
138 ((size_t *)x
)[-1] = (size_t)addr
;
144 void *vpx_malloc(size_t size
)
146 return vpx_memalign(DEFAULT_ALIGNMENT
, size
);
149 void *vpx_calloc(size_t num
, size_t size
)
153 x
= vpx_memalign(DEFAULT_ALIGNMENT
, num
* size
);
156 VPX_MEMSET_L(x
, 0, num
* size
);
161 void *vpx_realloc(void *memblk
, size_t size
)
165 int align
= DEFAULT_ALIGNMENT
;
168 The realloc() function changes the size of the object pointed to by
169 ptr to the size specified by size, and returns a pointer to the
170 possibly moved block. The contents are unchanged up to the lesser
171 of the new and old sizes. If ptr is null, realloc() behaves like
172 malloc() for the specified size. If size is zero (0) and ptr is
173 not a null pointer, the object pointed to is freed.
176 new_addr
= vpx_malloc(size
);
181 addr
= (void *)(((size_t *)memblk
)[-1]);
184 #if CONFIG_MEM_MANAGER
185 new_addr
= vpx_mm_realloc(addr
, size
+ align
+ ADDRESS_STORAGE_SIZE
);
187 new_addr
= VPX_REALLOC_L(addr
, size
+ align
+ ADDRESS_STORAGE_SIZE
);
193 new_addr
= (void *)(((size_t)
194 ((unsigned char *)new_addr
+ ADDRESS_STORAGE_SIZE
) + (align
- 1)) &
196 /* save the actual malloc address */
197 ((size_t *)new_addr
)[-1] = (size_t)addr
;
204 void vpx_free(void *memblk
)
208 void *addr
= (void *)(((size_t *)memblk
)[-1]);
209 #if CONFIG_MEM_MANAGER
210 hmm_free(&hmm_d
, addr
);
217 #if CONFIG_MEM_TRACKER
218 void *xvpx_memalign(size_t align
, size_t size
, char *file
, int line
)
221 unsigned char *x_bounds
;
226 if (g_alloc_count
== 0)
229 int i_rv
= vpx_memory_tracker_init(BOUNDS_CHECK_PAD_SIZE
, BOUNDS_CHECK_VALUE
);
231 int i_rv
= vpx_memory_tracker_init(0, 0);
236 _P(printf("ERROR xvpx_malloc MEM_TRACK_USAGE error vpx_memory_tracker_init().\n");)
243 unsigned int tempme
= BOUNDS_CHECK_VALUE
;
245 x_bounds
= vpx_memalign(align
, size
+ (BOUNDS_CHECK_PAD_SIZE
* 2));
249 /*we're aligning the address twice here but to keep things
250 consistent we want to have the padding come before the stored
251 address so no matter what free function gets called we will
252 attempt to free the correct address*/
253 x_bounds
= (unsigned char *)(((size_t *)x_bounds
)[-1]);
254 x
= align_addr(x_bounds
+ BOUNDS_CHECK_PAD_SIZE
+ ADDRESS_STORAGE_SIZE
,
256 /* save the actual malloc address */
257 ((size_t *)x
)[-1] = (size_t)x_bounds
;
259 for (i
= 0; i
< BOUNDS_CHECK_PAD_SIZE
; i
+= sizeof(unsigned int))
261 VPX_MEMCPY_L(x_bounds
+ i
, &tempme
, sizeof(unsigned int));
262 VPX_MEMCPY_L((unsigned char *)x
+ size
+ i
,
263 &tempme
, sizeof(unsigned int));
270 x
= vpx_memalign(align
, size
);
271 #endif //TRY_BOUNDS_CHECK
275 vpx_memory_tracker_add((size_t)x
, (unsigned int)size
, file
, line
, 1);
280 void *xvpx_malloc(size_t size
, char *file
, int line
)
282 return xvpx_memalign(DEFAULT_ALIGNMENT
, size
, file
, line
);
285 void *xvpx_calloc(size_t num
, size_t size
, char *file
, int line
)
287 void *x
= xvpx_memalign(DEFAULT_ALIGNMENT
, num
* size
, file
, line
);
290 VPX_MEMSET_L(x
, 0, num
* size
);
295 void *xvpx_realloc(void *memblk
, size_t size
, char *file
, int line
)
297 struct mem_block
*p
= NULL
;
300 char *orig_file
= NULL
;
303 unsigned char *x_bounds
= memblk
?
304 (unsigned char *)(((size_t *)memblk
)[-1]) :
310 if (g_alloc_count
== 0)
314 if (!vpx_memory_tracker_init(BOUNDS_CHECK_PAD_SIZE
, BOUNDS_CHECK_VALUE
))
316 if (!vpx_memory_tracker_init(0, 0))
319 _P(printf("ERROR xvpx_malloc MEM_TRACK_USAGE error vpx_memory_tracker_init().\n");)
323 if ((p
= vpx_memory_tracker_find((size_t)memblk
)))
330 #if TRY_BOUNDS_CHECK_ON_FREE
331 vpx_memory_tracker_check_integrity(file
, line
);
334 //have to do this regardless of success, because
335 //the memory that does get realloc'd may change
336 //the bounds values of this block
337 vpx_memory_tracker_remove((size_t)memblk
);
342 unsigned int tempme
= BOUNDS_CHECK_VALUE
;
344 x_bounds
= vpx_realloc(memblk
, size
+ (BOUNDS_CHECK_PAD_SIZE
* 2));
348 x_bounds
= (unsigned char *)(((size_t *)x_bounds
)[-1]);
349 x
= align_addr(x_bounds
+ BOUNDS_CHECK_PAD_SIZE
+ ADDRESS_STORAGE_SIZE
,
350 (int)DEFAULT_ALIGNMENT
);
351 /* save the actual malloc address */
352 ((size_t *)x
)[-1] = (size_t)x_bounds
;
354 for (i
= 0; i
< BOUNDS_CHECK_PAD_SIZE
; i
+= sizeof(unsigned int))
356 VPX_MEMCPY_L(x_bounds
+ i
, &tempme
, sizeof(unsigned int));
357 VPX_MEMCPY_L((unsigned char *)x
+ size
+ i
,
358 &tempme
, sizeof(unsigned int));
365 x
= vpx_realloc(memblk
, size
);
366 #endif //TRY_BOUNDS_CHECK
368 if (!memblk
) ++g_alloc_count
;
371 vpx_memory_tracker_add((size_t)x
, (unsigned int)size
, file
, line
, 1);
373 vpx_memory_tracker_add((size_t)memblk
, orig_size
, orig_file
, orig_line
, 1);
378 void xvpx_free(void *p_address
, char *file
, int line
)
381 unsigned char *p_bounds_address
= (unsigned char *)p_address
;
382 //p_bounds_address -= BOUNDS_CHECK_PAD_SIZE;
385 #if !TRY_BOUNDS_CHECK_ON_FREE
392 #if TRY_BOUNDS_CHECK_ON_FREE
393 vpx_memory_tracker_check_integrity(file
, line
);
396 //if the addr isn't found in the list, assume it was allocated via
397 //vpx_ calls not xvpx_, therefore it does not contain any padding
398 if (vpx_memory_tracker_remove((size_t)p_address
) == -2)
400 p_bounds_address
= p_address
;
401 _P(fprintf(stderr
, "[vpx_mem][xvpx_free] addr: %p not found in"
402 " list; freed from file:%s"
403 " line:%d\n", p_address
, file
, line
));
409 vpx_free(p_bounds_address
);
415 vpx_memory_tracker_destroy();
419 #endif /*CONFIG_MEM_TRACKER*/
421 #if CONFIG_MEM_CHECKS
423 #include <task_lib.h> //for task_delay()
424 /* This function is only used to get a stack trace of the player
425 object so we can se where we are having a problem. */
426 static int get_my_tt(int task
)
433 static void vx_sleep(int msec
)
435 int ticks_to_sleep
= 0;
439 int msec_per_tick
= 1000 / sys_clk_rate_get();
441 if (msec
< msec_per_tick
)
444 ticks_to_sleep
= msec
/ msec_per_tick
;
447 task_delay(ticks_to_sleep
);
452 void *vpx_memcpy(void *dest
, const void *source
, size_t length
)
454 #if CONFIG_MEM_CHECKS
456 if (((int)dest
< 0x4000) || ((int)source
< 0x4000))
458 _P(printf("WARNING: vpx_memcpy dest:0x%x source:0x%x len:%d\n", (int)dest
, (int)source
, length
);)
461 sp(get_my_tt
, task_id_self(), 0, 0, 0, 0, 0, 0, 0, 0);
469 return VPX_MEMCPY_L(dest
, source
, length
);
472 void *vpx_memset(void *dest
, int val
, size_t length
)
474 #if CONFIG_MEM_CHECKS
476 if ((int)dest
< 0x4000)
478 _P(printf("WARNING: vpx_memset dest:0x%x val:%d len:%d\n", (int)dest
, val
, length
);)
481 sp(get_my_tt
, task_id_self(), 0, 0, 0, 0, 0, 0, 0, 0);
489 return VPX_MEMSET_L(dest
, val
, length
);
492 void *vpx_memmove(void *dest
, const void *src
, size_t count
)
494 #if CONFIG_MEM_CHECKS
496 if (((int)dest
< 0x4000) || ((int)src
< 0x4000))
498 _P(printf("WARNING: vpx_memmove dest:0x%x src:0x%x count:%d\n", (int)dest
, (int)src
, count
);)
501 sp(get_my_tt
, task_id_self(), 0, 0, 0, 0, 0, 0, 0, 0);
509 return VPX_MEMMOVE_L(dest
, src
, count
);
512 #if CONFIG_MEM_MANAGER
514 static int vpx_mm_create_heap_memory()
518 if (!g_mng_memory_allocated
)
520 #if MM_DYNAMIC_MEMORY
522 (unsigned char *)malloc(g_mm_memory_size
+ HMM_ADDR_ALIGN_UNIT
);
524 if (g_p_mng_memory_raw
)
526 g_p_mng_memory
= (unsigned char *)((((unsigned int)g_p_mng_memory_raw
) +
527 HMM_ADDR_ALIGN_UNIT
- 1) &
528 -(int)HMM_ADDR_ALIGN_UNIT
);
530 _P(printf("[vpx][mm] total memory size:%d g_p_mng_memory_raw:0x%x g_p_mng_memory:0x%x\n"
531 , g_mm_memory_size
+ HMM_ADDR_ALIGN_UNIT
532 , (unsigned int)g_p_mng_memory_raw
533 , (unsigned int)g_p_mng_memory
);)
537 _P(printf("[vpx][mm] Couldn't allocate memory:%d for vpx memory manager.\n"
538 , g_mm_memory_size
);)
548 g_mng_memory_allocated
= 1;
552 chunk_size
= g_mm_memory_size
>> SHIFT_HMM_ADDR_ALIGN_UNIT
;
554 chunk_size
-= DUMMY_END_BLOCK_BAUS
;
556 _P(printf("[vpx][mm] memory size:%d for vpx memory manager. g_p_mng_memory:0x%x chunk_size:%d\n"
558 , (unsigned int)g_p_mng_memory
561 hmm_new_chunk(&hmm_d
, (void *)g_p_mng_memory
, chunk_size
);
564 #if MM_DYNAMIC_MEMORY
567 _P(printf("[vpx][mm] Couldn't allocate memory:%d for vpx memory manager.\n"
568 , g_mm_memory_size
);)
579 static void *vpx_mm_realloc(void *memblk
, size_t size
)
583 if (vpx_mm_create_heap_memory() < 0)
585 _P(printf("[vpx][mm] ERROR vpx_mm_realloc() Couldn't create memory for Heap.\n");)
593 old_num_aaus
= hmm_true_size(memblk
);
594 new_num_aaus
= (size
>> SHIFT_HMM_ADDR_ALIGN_UNIT
) + 1;
596 if (old_num_aaus
== new_num_aaus
)
602 i_rv
= hmm_resize(&hmm_d
, memblk
, new_num_aaus
);
610 /* Error. Try to malloc and then copy data. */
613 new_num_aaus
= (size
>> SHIFT_HMM_ADDR_ALIGN_UNIT
) + 1;
614 p_from_malloc
= hmm_alloc(&hmm_d
, new_num_aaus
);
618 vpx_memcpy(p_from_malloc
, memblk
, size
);
619 hmm_free(&hmm_d
, memblk
);
621 p_ret
= p_from_malloc
;
629 #endif //CONFIG_MEM_MANAGER
631 #if USE_GLOBAL_FUNCTION_POINTERS
632 # if CONFIG_MEM_TRACKER
633 extern int vpx_memory_tracker_set_functions(g_malloc_func g_malloc_l
634 , g_calloc_func g_calloc_l
635 , g_realloc_func g_realloc_l
636 , g_free_func g_free_l
637 , g_memcpy_func g_memcpy_l
638 , g_memset_func g_memset_l
639 , g_memmove_func g_memmove_l
);
641 #endif //USE_GLOBAL_FUNCTION_POINTERS
642 int vpx_mem_set_functions(g_malloc_func g_malloc_l
643 , g_calloc_func g_calloc_l
644 , g_realloc_func g_realloc_l
645 , g_free_func g_free_l
646 , g_memcpy_func g_memcpy_l
647 , g_memset_func g_memset_l
648 , g_memmove_func g_memmove_l
)
650 #if USE_GLOBAL_FUNCTION_POINTERS
652 /* If use global functions is turned on then the
653 application must set the global functions before
654 it does anything else or vpx_mem will have
655 unpredictable results. */
658 g_func
= (struct GLOBAL_FUNC_POINTERS
*)
659 g_malloc_l(sizeof(struct GLOBAL_FUNC_POINTERS
));
667 #if CONFIG_MEM_TRACKER
670 rv
= vpx_memory_tracker_set_functions(g_malloc_l
685 g_func
->g_malloc
= g_malloc_l
;
686 g_func
->g_calloc
= g_calloc_l
;
687 g_func
->g_realloc
= g_realloc_l
;
688 g_func
->g_free
= g_free_l
;
689 g_func
->g_memcpy
= g_memcpy_l
;
690 g_func
->g_memset
= g_memset_l
;
691 g_func
->g_memmove
= g_memmove_l
;
706 int vpx_mem_unset_functions()
708 #if USE_GLOBAL_FUNCTION_POINTERS
712 g_free_func temp_free
= g_func
->g_free
;