2010-05-18 17:58:33 +02:00
|
|
|
/*
|
2010-09-09 14:16:39 +02:00
|
|
|
* Copyright (c) 2010 The WebM project authors. All Rights Reserved.
|
2010-05-18 17:58:33 +02:00
|
|
|
*
|
2010-06-18 18:39:21 +02:00
|
|
|
* Use of this source code is governed by a BSD-style license
|
2010-06-04 22:19:40 +02:00
|
|
|
* that can be found in the LICENSE file in the root of the source
|
|
|
|
* tree. An additional intellectual property rights grant can be found
|
2010-06-18 18:39:21 +02:00
|
|
|
* in the file PATENTS. All contributing project authors may
|
2010-06-04 22:19:40 +02:00
|
|
|
* be found in the AUTHORS file in the root of the source tree.
|
2010-05-18 17:58:33 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#define __VPX_MEM_C__
|
|
|
|
|
|
|
|
#include "vpx_mem.h"
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include "include/vpx_mem_intrnl.h"
|
2014-08-26 21:35:15 +02:00
|
|
|
#include "vpx/vpx_integer.h"
|
2010-05-18 17:58:33 +02:00
|
|
|
|
|
|
|
#if CONFIG_MEM_TRACKER
|
|
|
|
#ifndef VPX_NO_GLOBALS
|
|
|
|
static unsigned long g_alloc_count = 0;
|
|
|
|
#else
|
|
|
|
#include "vpx_global_handling.h"
|
|
|
|
#define g_alloc_count vpxglobalm(vpxmem,g_alloc_count)
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if CONFIG_MEM_MANAGER
|
|
|
|
# include "heapmm.h"
|
|
|
|
# include "hmm_intrnl.h"
|
|
|
|
|
|
|
|
# define SHIFT_HMM_ADDR_ALIGN_UNIT 5
|
2010-10-28 01:04:02 +02:00
|
|
|
# define TOTAL_MEMORY_TO_ALLOCATE 20971520 /* 20 * 1024 * 1024 */
|
2010-05-18 17:58:33 +02:00
|
|
|
|
|
|
|
# define MM_DYNAMIC_MEMORY 1
|
|
|
|
# if MM_DYNAMIC_MEMORY
|
|
|
|
static unsigned char *g_p_mng_memory_raw = NULL;
|
|
|
|
static unsigned char *g_p_mng_memory = NULL;
|
|
|
|
# else
|
|
|
|
static unsigned char g_p_mng_memory[TOTAL_MEMORY_TO_ALLOCATE];
|
|
|
|
# endif
|
|
|
|
|
|
|
|
static size_t g_mm_memory_size = TOTAL_MEMORY_TO_ALLOCATE;
|
|
|
|
|
|
|
|
static hmm_descriptor hmm_d;
|
|
|
|
static int g_mng_memory_allocated = 0;
|
|
|
|
|
|
|
|
static int vpx_mm_create_heap_memory();
|
|
|
|
static void *vpx_mm_realloc(void *memblk, size_t size);
|
2010-10-28 01:04:02 +02:00
|
|
|
#endif /*CONFIG_MEM_MANAGER*/
|
2010-05-18 17:58:33 +02:00
|
|
|
|
|
|
|
#if USE_GLOBAL_FUNCTION_POINTERS
|
2012-07-14 00:21:29 +02:00
|
|
|
struct GLOBAL_FUNC_POINTERS {
|
|
|
|
g_malloc_func g_malloc;
|
|
|
|
g_calloc_func g_calloc;
|
|
|
|
g_realloc_func g_realloc;
|
|
|
|
g_free_func g_free;
|
|
|
|
g_memcpy_func g_memcpy;
|
|
|
|
g_memset_func g_memset;
|
|
|
|
g_memmove_func g_memmove;
|
2010-05-18 17:58:33 +02:00
|
|
|
} *g_func = NULL;
|
|
|
|
|
|
|
|
# define VPX_MALLOC_L g_func->g_malloc
|
|
|
|
# define VPX_REALLOC_L g_func->g_realloc
|
|
|
|
# define VPX_FREE_L g_func->g_free
|
|
|
|
# define VPX_MEMCPY_L g_func->g_memcpy
|
|
|
|
# define VPX_MEMSET_L g_func->g_memset
|
|
|
|
# define VPX_MEMMOVE_L g_func->g_memmove
|
|
|
|
#else
|
|
|
|
# define VPX_MALLOC_L malloc
|
|
|
|
# define VPX_REALLOC_L realloc
|
|
|
|
# define VPX_FREE_L free
|
|
|
|
# define VPX_MEMCPY_L memcpy
|
|
|
|
# define VPX_MEMSET_L memset
|
|
|
|
# define VPX_MEMMOVE_L memmove
|
2010-10-28 01:04:02 +02:00
|
|
|
#endif /* USE_GLOBAL_FUNCTION_POINTERS */
|
2010-05-18 17:58:33 +02:00
|
|
|
|
2012-07-14 00:21:29 +02:00
|
|
|
unsigned int vpx_mem_get_version() {
|
|
|
|
unsigned int ver = ((unsigned int)(unsigned char)VPX_MEM_VERSION_CHIEF << 24 |
|
|
|
|
(unsigned int)(unsigned char)VPX_MEM_VERSION_MAJOR << 16 |
|
|
|
|
(unsigned int)(unsigned char)VPX_MEM_VERSION_MINOR << 8 |
|
|
|
|
(unsigned int)(unsigned char)VPX_MEM_VERSION_PATCH);
|
|
|
|
return ver;
|
2010-05-18 17:58:33 +02:00
|
|
|
}
|
|
|
|
|
2012-07-14 00:21:29 +02:00
|
|
|
int vpx_mem_set_heap_size(size_t size) {
|
|
|
|
int ret = -1;
|
2010-05-18 17:58:33 +02:00
|
|
|
|
|
|
|
#if CONFIG_MEM_MANAGER
|
|
|
|
#if MM_DYNAMIC_MEMORY
|
|
|
|
|
2012-07-14 00:21:29 +02:00
|
|
|
if (!g_mng_memory_allocated && size) {
|
|
|
|
g_mm_memory_size = size;
|
|
|
|
ret = 0;
|
|
|
|
} else
|
|
|
|
ret = -3;
|
2010-05-18 17:58:33 +02:00
|
|
|
|
|
|
|
#else
|
2012-07-14 00:21:29 +02:00
|
|
|
ret = -2;
|
2010-05-18 17:58:33 +02:00
|
|
|
#endif
|
|
|
|
#else
|
2012-07-14 00:21:29 +02:00
|
|
|
(void)size;
|
2010-05-18 17:58:33 +02:00
|
|
|
#endif
|
|
|
|
|
2012-07-14 00:21:29 +02:00
|
|
|
return ret;
|
2010-05-18 17:58:33 +02:00
|
|
|
}
|
|
|
|
|
2012-07-14 00:21:29 +02:00
|
|
|
void *vpx_memalign(size_t align, size_t size) {
|
|
|
|
void *addr,
|
|
|
|
* x = NULL;
|
2010-05-18 17:58:33 +02:00
|
|
|
|
|
|
|
#if CONFIG_MEM_MANAGER
|
2012-07-14 00:21:29 +02:00
|
|
|
int number_aau;
|
2010-05-18 17:58:33 +02:00
|
|
|
|
2012-07-14 00:21:29 +02:00
|
|
|
if (vpx_mm_create_heap_memory() < 0) {
|
|
|
|
_P(printf("[vpx][mm] ERROR vpx_memalign() Couldn't create memory for Heap.\n");)
|
|
|
|
}
|
2010-05-18 17:58:33 +02:00
|
|
|
|
2012-07-14 00:21:29 +02:00
|
|
|
number_aau = ((size + align - 1 + ADDRESS_STORAGE_SIZE) >>
|
|
|
|
SHIFT_HMM_ADDR_ALIGN_UNIT) + 1;
|
2010-05-18 17:58:33 +02:00
|
|
|
|
2012-07-14 00:21:29 +02:00
|
|
|
addr = hmm_alloc(&hmm_d, number_aau);
|
2010-05-18 17:58:33 +02:00
|
|
|
#else
|
2012-07-14 00:21:29 +02:00
|
|
|
addr = VPX_MALLOC_L(size + align - 1 + ADDRESS_STORAGE_SIZE);
|
2010-10-28 01:04:02 +02:00
|
|
|
#endif /*CONFIG_MEM_MANAGER*/
|
2010-05-18 17:58:33 +02:00
|
|
|
|
2012-07-14 00:21:29 +02:00
|
|
|
if (addr) {
|
|
|
|
x = align_addr((unsigned char *)addr + ADDRESS_STORAGE_SIZE, (int)align);
|
|
|
|
/* save the actual malloc address */
|
|
|
|
((size_t *)x)[-1] = (size_t)addr;
|
|
|
|
}
|
2010-05-18 17:58:33 +02:00
|
|
|
|
2012-07-14 00:21:29 +02:00
|
|
|
return x;
|
2010-05-18 17:58:33 +02:00
|
|
|
}
|
|
|
|
|
2012-07-14 00:21:29 +02:00
|
|
|
void *vpx_malloc(size_t size) {
|
|
|
|
return vpx_memalign(DEFAULT_ALIGNMENT, size);
|
2010-05-18 17:58:33 +02:00
|
|
|
}
|
|
|
|
|
2012-07-14 00:21:29 +02:00
|
|
|
void *vpx_calloc(size_t num, size_t size) {
|
|
|
|
void *x;
|
2010-05-18 17:58:33 +02:00
|
|
|
|
2012-07-14 00:21:29 +02:00
|
|
|
x = vpx_memalign(DEFAULT_ALIGNMENT, num * size);
|
2010-05-18 17:58:33 +02:00
|
|
|
|
2012-07-14 00:21:29 +02:00
|
|
|
if (x)
|
|
|
|
VPX_MEMSET_L(x, 0, num * size);
|
2010-05-18 17:58:33 +02:00
|
|
|
|
2012-07-14 00:21:29 +02:00
|
|
|
return x;
|
2010-05-18 17:58:33 +02:00
|
|
|
}
|
|
|
|
|
2012-07-14 00:21:29 +02:00
|
|
|
void *vpx_realloc(void *memblk, size_t size) {
|
|
|
|
void *addr,
|
|
|
|
* new_addr = NULL;
|
|
|
|
int align = DEFAULT_ALIGNMENT;
|
|
|
|
|
|
|
|
/*
|
|
|
|
The realloc() function changes the size of the object pointed to by
|
|
|
|
ptr to the size specified by size, and returns a pointer to the
|
|
|
|
possibly moved block. The contents are unchanged up to the lesser
|
|
|
|
of the new and old sizes. If ptr is null, realloc() behaves like
|
|
|
|
malloc() for the specified size. If size is zero (0) and ptr is
|
|
|
|
not a null pointer, the object pointed to is freed.
|
|
|
|
*/
|
|
|
|
if (!memblk)
|
|
|
|
new_addr = vpx_malloc(size);
|
|
|
|
else if (!size)
|
|
|
|
vpx_free(memblk);
|
|
|
|
else {
|
|
|
|
addr = (void *)(((size_t *)memblk)[-1]);
|
|
|
|
memblk = NULL;
|
2010-05-18 17:58:33 +02:00
|
|
|
|
|
|
|
#if CONFIG_MEM_MANAGER
|
2012-07-14 00:21:29 +02:00
|
|
|
new_addr = vpx_mm_realloc(addr, size + align + ADDRESS_STORAGE_SIZE);
|
2010-05-18 17:58:33 +02:00
|
|
|
#else
|
2012-07-14 00:21:29 +02:00
|
|
|
new_addr = VPX_REALLOC_L(addr, size + align + ADDRESS_STORAGE_SIZE);
|
2010-05-18 17:58:33 +02:00
|
|
|
#endif
|
|
|
|
|
2012-07-14 00:21:29 +02:00
|
|
|
if (new_addr) {
|
|
|
|
addr = new_addr;
|
|
|
|
new_addr = (void *)(((size_t)
|
|
|
|
((unsigned char *)new_addr + ADDRESS_STORAGE_SIZE) + (align - 1)) &
|
|
|
|
(size_t) - align);
|
|
|
|
/* save the actual malloc address */
|
|
|
|
((size_t *)new_addr)[-1] = (size_t)addr;
|
2010-05-18 17:58:33 +02:00
|
|
|
}
|
2012-07-14 00:21:29 +02:00
|
|
|
}
|
2010-05-18 17:58:33 +02:00
|
|
|
|
2012-07-14 00:21:29 +02:00
|
|
|
return new_addr;
|
2010-05-18 17:58:33 +02:00
|
|
|
}
|
|
|
|
|
2012-07-14 00:21:29 +02:00
|
|
|
void vpx_free(void *memblk) {
|
|
|
|
if (memblk) {
|
|
|
|
void *addr = (void *)(((size_t *)memblk)[-1]);
|
2010-05-18 17:58:33 +02:00
|
|
|
#if CONFIG_MEM_MANAGER
|
2012-07-14 00:21:29 +02:00
|
|
|
hmm_free(&hmm_d, addr);
|
2010-05-18 17:58:33 +02:00
|
|
|
#else
|
2012-07-14 00:21:29 +02:00
|
|
|
VPX_FREE_L(addr);
|
2010-05-18 17:58:33 +02:00
|
|
|
#endif
|
2012-07-14 00:21:29 +02:00
|
|
|
}
|
2010-05-18 17:58:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#if CONFIG_MEM_TRACKER
|
2012-07-14 00:21:29 +02:00
|
|
|
void *xvpx_memalign(size_t align, size_t size, char *file, int line) {
|
2010-05-18 17:58:33 +02:00
|
|
|
#if TRY_BOUNDS_CHECK
|
2012-07-14 00:21:29 +02:00
|
|
|
unsigned char *x_bounds;
|
2010-05-18 17:58:33 +02:00
|
|
|
#endif
|
|
|
|
|
2012-07-14 00:21:29 +02:00
|
|
|
void *x;
|
2010-05-18 17:58:33 +02:00
|
|
|
|
2012-07-14 00:21:29 +02:00
|
|
|
if (g_alloc_count == 0) {
|
2010-05-18 17:58:33 +02:00
|
|
|
#if TRY_BOUNDS_CHECK
|
2012-07-14 00:21:29 +02:00
|
|
|
int i_rv = vpx_memory_tracker_init(BOUNDS_CHECK_PAD_SIZE, BOUNDS_CHECK_VALUE);
|
2010-05-18 17:58:33 +02:00
|
|
|
#else
|
2012-07-14 00:21:29 +02:00
|
|
|
int i_rv = vpx_memory_tracker_init(0, 0);
|
2010-05-18 17:58:33 +02:00
|
|
|
#endif
|
|
|
|
|
2012-07-14 00:21:29 +02:00
|
|
|
if (i_rv < 0) {
|
|
|
|
_P(printf("ERROR xvpx_malloc MEM_TRACK_USAGE error vpx_memory_tracker_init().\n");)
|
2010-05-18 17:58:33 +02:00
|
|
|
}
|
2012-07-14 00:21:29 +02:00
|
|
|
}
|
2010-05-18 17:58:33 +02:00
|
|
|
|
|
|
|
#if TRY_BOUNDS_CHECK
|
2012-07-14 00:21:29 +02:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
unsigned int tempme = BOUNDS_CHECK_VALUE;
|
|
|
|
|
|
|
|
x_bounds = vpx_memalign(align, size + (BOUNDS_CHECK_PAD_SIZE * 2));
|
|
|
|
|
|
|
|
if (x_bounds) {
|
|
|
|
/*we're aligning the address twice here but to keep things
|
|
|
|
consistent we want to have the padding come before the stored
|
|
|
|
address so no matter what free function gets called we will
|
|
|
|
attempt to free the correct address*/
|
|
|
|
x_bounds = (unsigned char *)(((size_t *)x_bounds)[-1]);
|
|
|
|
x = align_addr(x_bounds + BOUNDS_CHECK_PAD_SIZE + ADDRESS_STORAGE_SIZE,
|
|
|
|
(int)align);
|
|
|
|
/* save the actual malloc address */
|
|
|
|
((size_t *)x)[-1] = (size_t)x_bounds;
|
|
|
|
|
|
|
|
for (i = 0; i < BOUNDS_CHECK_PAD_SIZE; i += sizeof(unsigned int)) {
|
|
|
|
VPX_MEMCPY_L(x_bounds + i, &tempme, sizeof(unsigned int));
|
|
|
|
VPX_MEMCPY_L((unsigned char *)x + size + i,
|
|
|
|
&tempme, sizeof(unsigned int));
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
x = NULL;
|
|
|
|
}
|
2010-05-18 17:58:33 +02:00
|
|
|
#else
|
2012-07-14 00:21:29 +02:00
|
|
|
x = vpx_memalign(align, size);
|
2010-10-28 01:04:02 +02:00
|
|
|
#endif /*TRY_BOUNDS_CHECK*/
|
2010-05-18 17:58:33 +02:00
|
|
|
|
2012-07-14 00:21:29 +02:00
|
|
|
g_alloc_count++;
|
2010-05-18 17:58:33 +02:00
|
|
|
|
2012-07-14 00:21:29 +02:00
|
|
|
vpx_memory_tracker_add((size_t)x, (unsigned int)size, file, line, 1);
|
2010-05-18 17:58:33 +02:00
|
|
|
|
2012-07-14 00:21:29 +02:00
|
|
|
return x;
|
2010-05-18 17:58:33 +02:00
|
|
|
}
|
|
|
|
|
2012-07-14 00:21:29 +02:00
|
|
|
void *xvpx_malloc(size_t size, char *file, int line) {
|
|
|
|
return xvpx_memalign(DEFAULT_ALIGNMENT, size, file, line);
|
2010-05-18 17:58:33 +02:00
|
|
|
}
|
|
|
|
|
2012-07-14 00:21:29 +02:00
|
|
|
void *xvpx_calloc(size_t num, size_t size, char *file, int line) {
|
|
|
|
void *x = xvpx_memalign(DEFAULT_ALIGNMENT, num * size, file, line);
|
2010-05-18 17:58:33 +02:00
|
|
|
|
2012-07-14 00:21:29 +02:00
|
|
|
if (x)
|
|
|
|
VPX_MEMSET_L(x, 0, num * size);
|
2010-05-18 17:58:33 +02:00
|
|
|
|
2012-07-14 00:21:29 +02:00
|
|
|
return x;
|
2010-05-18 17:58:33 +02:00
|
|
|
}
|
|
|
|
|
2012-07-14 00:21:29 +02:00
|
|
|
void *xvpx_realloc(void *memblk, size_t size, char *file, int line) {
|
|
|
|
struct mem_block *p = NULL;
|
|
|
|
int orig_size = 0,
|
|
|
|
orig_line = 0;
|
|
|
|
char *orig_file = NULL;
|
2010-05-18 17:58:33 +02:00
|
|
|
|
|
|
|
#if TRY_BOUNDS_CHECK
|
2012-07-14 00:21:29 +02:00
|
|
|
unsigned char *x_bounds = memblk ?
|
|
|
|
(unsigned char *)(((size_t *)memblk)[-1]) :
|
|
|
|
NULL;
|
2010-05-18 17:58:33 +02:00
|
|
|
#endif
|
|
|
|
|
2012-07-14 00:21:29 +02:00
|
|
|
void *x;
|
2010-05-18 17:58:33 +02:00
|
|
|
|
2012-07-14 00:21:29 +02:00
|
|
|
if (g_alloc_count == 0) {
|
2010-05-18 17:58:33 +02:00
|
|
|
#if TRY_BOUNDS_CHECK
|
|
|
|
|
2012-07-14 00:21:29 +02:00
|
|
|
if (!vpx_memory_tracker_init(BOUNDS_CHECK_PAD_SIZE, BOUNDS_CHECK_VALUE))
|
2010-05-18 17:58:33 +02:00
|
|
|
#else
|
2012-07-14 00:21:29 +02:00
|
|
|
if (!vpx_memory_tracker_init(0, 0))
|
2010-05-18 17:58:33 +02:00
|
|
|
#endif
|
|
|
|
{
|
2012-07-14 00:21:29 +02:00
|
|
|
_P(printf("ERROR xvpx_malloc MEM_TRACK_USAGE error vpx_memory_tracker_init().\n");)
|
2010-05-18 17:58:33 +02:00
|
|
|
}
|
2012-07-14 00:21:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if ((p = vpx_memory_tracker_find((size_t)memblk))) {
|
|
|
|
orig_size = p->size;
|
|
|
|
orig_file = p->file;
|
|
|
|
orig_line = p->line;
|
|
|
|
}
|
2010-05-18 17:58:33 +02:00
|
|
|
|
|
|
|
#if TRY_BOUNDS_CHECK_ON_FREE
|
2012-07-14 00:21:29 +02:00
|
|
|
vpx_memory_tracker_check_integrity(file, line);
|
2010-05-18 17:58:33 +02:00
|
|
|
#endif
|
|
|
|
|
2012-07-14 00:21:29 +02:00
|
|
|
/* have to do this regardless of success, because
|
|
|
|
* the memory that does get realloc'd may change
|
|
|
|
* the bounds values of this block
|
|
|
|
*/
|
|
|
|
vpx_memory_tracker_remove((size_t)memblk);
|
2010-05-18 17:58:33 +02:00
|
|
|
|
|
|
|
#if TRY_BOUNDS_CHECK
|
2012-07-14 00:21:29 +02:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
unsigned int tempme = BOUNDS_CHECK_VALUE;
|
|
|
|
|
|
|
|
x_bounds = vpx_realloc(memblk, size + (BOUNDS_CHECK_PAD_SIZE * 2));
|
|
|
|
|
|
|
|
if (x_bounds) {
|
|
|
|
x_bounds = (unsigned char *)(((size_t *)x_bounds)[-1]);
|
|
|
|
x = align_addr(x_bounds + BOUNDS_CHECK_PAD_SIZE + ADDRESS_STORAGE_SIZE,
|
|
|
|
(int)DEFAULT_ALIGNMENT);
|
|
|
|
/* save the actual malloc address */
|
|
|
|
((size_t *)x)[-1] = (size_t)x_bounds;
|
|
|
|
|
|
|
|
for (i = 0; i < BOUNDS_CHECK_PAD_SIZE; i += sizeof(unsigned int)) {
|
|
|
|
VPX_MEMCPY_L(x_bounds + i, &tempme, sizeof(unsigned int));
|
|
|
|
VPX_MEMCPY_L((unsigned char *)x + size + i,
|
|
|
|
&tempme, sizeof(unsigned int));
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
x = NULL;
|
|
|
|
}
|
2010-05-18 17:58:33 +02:00
|
|
|
#else
|
2012-07-14 00:21:29 +02:00
|
|
|
x = vpx_realloc(memblk, size);
|
2010-10-28 01:04:02 +02:00
|
|
|
#endif /*TRY_BOUNDS_CHECK*/
|
2010-05-18 17:58:33 +02:00
|
|
|
|
2012-07-14 00:21:29 +02:00
|
|
|
if (!memblk) ++g_alloc_count;
|
2010-05-18 17:58:33 +02:00
|
|
|
|
2012-07-14 00:21:29 +02:00
|
|
|
if (x)
|
|
|
|
vpx_memory_tracker_add((size_t)x, (unsigned int)size, file, line, 1);
|
|
|
|
else
|
|
|
|
vpx_memory_tracker_add((size_t)memblk, orig_size, orig_file, orig_line, 1);
|
2010-05-18 17:58:33 +02:00
|
|
|
|
2012-07-14 00:21:29 +02:00
|
|
|
return x;
|
2010-05-18 17:58:33 +02:00
|
|
|
}
|
|
|
|
|
2012-07-14 00:21:29 +02:00
|
|
|
void xvpx_free(void *p_address, char *file, int line) {
|
2010-05-18 17:58:33 +02:00
|
|
|
#if TRY_BOUNDS_CHECK
|
2012-07-14 00:21:29 +02:00
|
|
|
unsigned char *p_bounds_address = (unsigned char *)p_address;
|
|
|
|
/*p_bounds_address -= BOUNDS_CHECK_PAD_SIZE;*/
|
2010-05-18 17:58:33 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if !TRY_BOUNDS_CHECK_ON_FREE
|
2012-07-14 00:21:29 +02:00
|
|
|
(void)file;
|
|
|
|
(void)line;
|
2010-05-18 17:58:33 +02:00
|
|
|
#endif
|
|
|
|
|
2012-07-14 00:21:29 +02:00
|
|
|
if (p_address) {
|
2010-05-18 17:58:33 +02:00
|
|
|
#if TRY_BOUNDS_CHECK_ON_FREE
|
2012-07-14 00:21:29 +02:00
|
|
|
vpx_memory_tracker_check_integrity(file, line);
|
2010-05-18 17:58:33 +02:00
|
|
|
#endif
|
|
|
|
|
2012-07-14 00:21:29 +02:00
|
|
|
/* if the addr isn't found in the list, assume it was allocated via
|
|
|
|
* vpx_ calls not xvpx_, therefore it does not contain any padding
|
|
|
|
*/
|
|
|
|
if (vpx_memory_tracker_remove((size_t)p_address) == -2) {
|
|
|
|
p_bounds_address = p_address;
|
|
|
|
_P(fprintf(stderr, "[vpx_mem][xvpx_free] addr: %p not found in"
|
|
|
|
" list; freed from file:%s"
|
|
|
|
" line:%d\n", p_address, file, line));
|
|
|
|
} else
|
|
|
|
--g_alloc_count;
|
2010-05-18 17:58:33 +02:00
|
|
|
|
|
|
|
#if TRY_BOUNDS_CHECK
|
2012-07-14 00:21:29 +02:00
|
|
|
vpx_free(p_bounds_address);
|
2010-05-18 17:58:33 +02:00
|
|
|
#else
|
2012-07-14 00:21:29 +02:00
|
|
|
vpx_free(p_address);
|
2010-05-18 17:58:33 +02:00
|
|
|
#endif
|
|
|
|
|
2012-07-14 00:21:29 +02:00
|
|
|
if (!g_alloc_count)
|
|
|
|
vpx_memory_tracker_destroy();
|
|
|
|
}
|
2010-05-18 17:58:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif /*CONFIG_MEM_TRACKER*/
|
|
|
|
|
|
|
|
#if CONFIG_MEM_CHECKS
|
|
|
|
#if defined(VXWORKS)
|
2010-10-28 01:04:02 +02:00
|
|
|
#include <task_lib.h> /*for task_delay()*/
|
2010-05-18 17:58:33 +02:00
|
|
|
/* This function is only used to get a stack trace of the player
|
|
|
|
object so we can se where we are having a problem. */
|
2012-07-14 00:21:29 +02:00
|
|
|
static int get_my_tt(int task) {
|
|
|
|
tt(task);
|
2010-05-18 17:58:33 +02:00
|
|
|
|
2012-07-14 00:21:29 +02:00
|
|
|
return 0;
|
2010-05-18 17:58:33 +02:00
|
|
|
}
|
|
|
|
|
2012-07-14 00:21:29 +02:00
|
|
|
static void vx_sleep(int msec) {
|
|
|
|
int ticks_to_sleep = 0;
|
2010-05-18 17:58:33 +02:00
|
|
|
|
2012-07-14 00:21:29 +02:00
|
|
|
if (msec) {
|
|
|
|
int msec_per_tick = 1000 / sys_clk_rate_get();
|
2010-05-18 17:58:33 +02:00
|
|
|
|
2012-07-14 00:21:29 +02:00
|
|
|
if (msec < msec_per_tick)
|
|
|
|
ticks_to_sleep++;
|
|
|
|
else
|
|
|
|
ticks_to_sleep = msec / msec_per_tick;
|
|
|
|
}
|
2010-05-18 17:58:33 +02:00
|
|
|
|
2012-07-14 00:21:29 +02:00
|
|
|
task_delay(ticks_to_sleep);
|
2010-05-18 17:58:33 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
2012-07-14 00:21:29 +02:00
|
|
|
void *vpx_memcpy(void *dest, const void *source, size_t length) {
|
2010-05-18 17:58:33 +02:00
|
|
|
#if CONFIG_MEM_CHECKS
|
|
|
|
|
2012-07-14 00:21:29 +02:00
|
|
|
if (((int)dest < 0x4000) || ((int)source < 0x4000)) {
|
|
|
|
_P(printf("WARNING: vpx_memcpy dest:0x%x source:0x%x len:%d\n", (int)dest, (int)source, length);)
|
2010-05-18 17:58:33 +02:00
|
|
|
|
|
|
|
#if defined(VXWORKS)
|
2012-07-14 00:21:29 +02:00
|
|
|
sp(get_my_tt, task_id_self(), 0, 0, 0, 0, 0, 0, 0, 0);
|
2010-05-18 17:58:33 +02:00
|
|
|
|
2012-07-14 00:21:29 +02:00
|
|
|
vx_sleep(10000);
|
2010-05-18 17:58:33 +02:00
|
|
|
#endif
|
2012-07-14 00:21:29 +02:00
|
|
|
}
|
2010-05-18 17:58:33 +02:00
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2012-07-14 00:21:29 +02:00
|
|
|
return VPX_MEMCPY_L(dest, source, length);
|
2010-05-18 17:58:33 +02:00
|
|
|
}
|
|
|
|
|
2012-07-14 00:21:29 +02:00
|
|
|
void *vpx_memset(void *dest, int val, size_t length) {
|
2010-05-18 17:58:33 +02:00
|
|
|
#if CONFIG_MEM_CHECKS
|
|
|
|
|
2012-07-14 00:21:29 +02:00
|
|
|
if ((int)dest < 0x4000) {
|
|
|
|
_P(printf("WARNING: vpx_memset dest:0x%x val:%d len:%d\n", (int)dest, val, length);)
|
2010-05-18 17:58:33 +02:00
|
|
|
|
|
|
|
#if defined(VXWORKS)
|
2012-07-14 00:21:29 +02:00
|
|
|
sp(get_my_tt, task_id_self(), 0, 0, 0, 0, 0, 0, 0, 0);
|
2010-05-18 17:58:33 +02:00
|
|
|
|
2012-07-14 00:21:29 +02:00
|
|
|
vx_sleep(10000);
|
2010-05-18 17:58:33 +02:00
|
|
|
#endif
|
2012-07-14 00:21:29 +02:00
|
|
|
}
|
2010-05-18 17:58:33 +02:00
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2012-07-14 00:21:29 +02:00
|
|
|
return VPX_MEMSET_L(dest, val, length);
|
2010-05-18 17:58:33 +02:00
|
|
|
}
|
|
|
|
|
2014-08-26 21:35:15 +02:00
|
|
|
#if CONFIG_VP9 && CONFIG_VP9_HIGHBITDEPTH
|
|
|
|
void *vpx_memset16(void *dest, int val, size_t length) {
|
|
|
|
#if CONFIG_MEM_CHECKS
|
|
|
|
if ((int)dest < 0x4000) {
|
|
|
|
_P(printf("WARNING: vpx_memset dest:0x%x val:%d len:%d\n",
|
|
|
|
(int)dest, val, length);)
|
|
|
|
|
|
|
|
#if defined(VXWORKS)
|
|
|
|
sp(get_my_tt, task_id_self(), 0, 0, 0, 0, 0, 0, 0, 0);
|
|
|
|
|
|
|
|
vx_sleep(10000);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
int i;
|
|
|
|
void *orig = dest;
|
|
|
|
uint16_t *dest16 = dest;
|
|
|
|
for (i = 0; i < length; i++)
|
|
|
|
*dest16++ = val;
|
|
|
|
return orig;
|
|
|
|
}
|
|
|
|
#endif // CONFIG_VP9 && CONFIG_VP9_HIGHBITDEPTH
|
|
|
|
|
2012-07-14 00:21:29 +02:00
|
|
|
void *vpx_memmove(void *dest, const void *src, size_t count) {
|
2010-05-18 17:58:33 +02:00
|
|
|
#if CONFIG_MEM_CHECKS
|
|
|
|
|
2012-07-14 00:21:29 +02:00
|
|
|
if (((int)dest < 0x4000) || ((int)src < 0x4000)) {
|
|
|
|
_P(printf("WARNING: vpx_memmove dest:0x%x src:0x%x count:%d\n", (int)dest, (int)src, count);)
|
2010-05-18 17:58:33 +02:00
|
|
|
|
|
|
|
#if defined(VXWORKS)
|
2012-07-14 00:21:29 +02:00
|
|
|
sp(get_my_tt, task_id_self(), 0, 0, 0, 0, 0, 0, 0, 0);
|
2010-05-18 17:58:33 +02:00
|
|
|
|
2012-07-14 00:21:29 +02:00
|
|
|
vx_sleep(10000);
|
2010-05-18 17:58:33 +02:00
|
|
|
#endif
|
2012-07-14 00:21:29 +02:00
|
|
|
}
|
2010-05-18 17:58:33 +02:00
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2012-07-14 00:21:29 +02:00
|
|
|
return VPX_MEMMOVE_L(dest, src, count);
|
2010-05-18 17:58:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#if CONFIG_MEM_MANAGER
|
|
|
|
|
2012-07-14 00:21:29 +02:00
|
|
|
static int vpx_mm_create_heap_memory() {
|
|
|
|
int i_rv = 0;
|
2010-05-18 17:58:33 +02:00
|
|
|
|
2012-07-14 00:21:29 +02:00
|
|
|
if (!g_mng_memory_allocated) {
|
2010-05-18 17:58:33 +02:00
|
|
|
#if MM_DYNAMIC_MEMORY
|
2012-07-14 00:21:29 +02:00
|
|
|
g_p_mng_memory_raw =
|
|
|
|
(unsigned char *)malloc(g_mm_memory_size + HMM_ADDR_ALIGN_UNIT);
|
|
|
|
|
|
|
|
if (g_p_mng_memory_raw) {
|
|
|
|
g_p_mng_memory = (unsigned char *)((((unsigned int)g_p_mng_memory_raw) +
|
|
|
|
HMM_ADDR_ALIGN_UNIT - 1) &
|
|
|
|
-(int)HMM_ADDR_ALIGN_UNIT);
|
|
|
|
|
|
|
|
_P(printf("[vpx][mm] total memory size:%d g_p_mng_memory_raw:0x%x g_p_mng_memory:0x%x\n"
|
|
|
|
, g_mm_memory_size + HMM_ADDR_ALIGN_UNIT
|
|
|
|
, (unsigned int)g_p_mng_memory_raw
|
|
|
|
, (unsigned int)g_p_mng_memory);)
|
|
|
|
} else {
|
|
|
|
_P(printf("[vpx][mm] Couldn't allocate memory:%d for vpx memory manager.\n"
|
|
|
|
, g_mm_memory_size);)
|
|
|
|
|
|
|
|
i_rv = -1;
|
|
|
|
}
|
2010-05-18 17:58:33 +02:00
|
|
|
|
2012-07-14 00:21:29 +02:00
|
|
|
if (g_p_mng_memory)
|
2010-05-18 17:58:33 +02:00
|
|
|
#endif
|
2012-07-14 00:21:29 +02:00
|
|
|
{
|
|
|
|
int chunk_size = 0;
|
2010-05-18 17:58:33 +02:00
|
|
|
|
2012-07-14 00:21:29 +02:00
|
|
|
g_mng_memory_allocated = 1;
|
2010-05-18 17:58:33 +02:00
|
|
|
|
2012-07-14 00:21:29 +02:00
|
|
|
hmm_init(&hmm_d);
|
2010-05-18 17:58:33 +02:00
|
|
|
|
2012-07-14 00:21:29 +02:00
|
|
|
chunk_size = g_mm_memory_size >> SHIFT_HMM_ADDR_ALIGN_UNIT;
|
2010-05-18 17:58:33 +02:00
|
|
|
|
2012-07-14 00:21:29 +02:00
|
|
|
chunk_size -= DUMMY_END_BLOCK_BAUS;
|
2010-05-18 17:58:33 +02:00
|
|
|
|
2012-07-14 00:21:29 +02:00
|
|
|
_P(printf("[vpx][mm] memory size:%d for vpx memory manager. g_p_mng_memory:0x%x chunk_size:%d\n"
|
|
|
|
, g_mm_memory_size
|
|
|
|
, (unsigned int)g_p_mng_memory
|
|
|
|
, chunk_size);)
|
2010-05-18 17:58:33 +02:00
|
|
|
|
2012-07-14 00:21:29 +02:00
|
|
|
hmm_new_chunk(&hmm_d, (void *)g_p_mng_memory, chunk_size);
|
|
|
|
}
|
2010-05-18 17:58:33 +02:00
|
|
|
|
|
|
|
#if MM_DYNAMIC_MEMORY
|
2012-07-14 00:21:29 +02:00
|
|
|
else {
|
|
|
|
_P(printf("[vpx][mm] Couldn't allocate memory:%d for vpx memory manager.\n"
|
|
|
|
, g_mm_memory_size);)
|
2010-05-18 17:58:33 +02:00
|
|
|
|
2012-07-14 00:21:29 +02:00
|
|
|
i_rv = -1;
|
|
|
|
}
|
2010-05-18 17:58:33 +02:00
|
|
|
|
|
|
|
#endif
|
2012-07-14 00:21:29 +02:00
|
|
|
}
|
2010-05-18 17:58:33 +02:00
|
|
|
|
2012-07-14 00:21:29 +02:00
|
|
|
return i_rv;
|
2010-05-18 17:58:33 +02:00
|
|
|
}
|
|
|
|
|
2012-07-14 00:21:29 +02:00
|
|
|
static void *vpx_mm_realloc(void *memblk, size_t size) {
|
|
|
|
void *p_ret = NULL;
|
2010-05-18 17:58:33 +02:00
|
|
|
|
2012-07-14 00:21:29 +02:00
|
|
|
if (vpx_mm_create_heap_memory() < 0) {
|
|
|
|
_P(printf("[vpx][mm] ERROR vpx_mm_realloc() Couldn't create memory for Heap.\n");)
|
|
|
|
} else {
|
|
|
|
int i_rv = 0;
|
|
|
|
int old_num_aaus;
|
|
|
|
int new_num_aaus;
|
|
|
|
|
|
|
|
old_num_aaus = hmm_true_size(memblk);
|
|
|
|
new_num_aaus = (size >> SHIFT_HMM_ADDR_ALIGN_UNIT) + 1;
|
|
|
|
|
|
|
|
if (old_num_aaus == new_num_aaus) {
|
|
|
|
p_ret = memblk;
|
|
|
|
} else {
|
|
|
|
i_rv = hmm_resize(&hmm_d, memblk, new_num_aaus);
|
|
|
|
|
|
|
|
if (i_rv == 0) {
|
|
|
|
p_ret = memblk;
|
|
|
|
} else {
|
|
|
|
/* Error. Try to malloc and then copy data. */
|
|
|
|
void *p_from_malloc;
|
2010-05-18 17:58:33 +02:00
|
|
|
|
|
|
|
new_num_aaus = (size >> SHIFT_HMM_ADDR_ALIGN_UNIT) + 1;
|
2012-07-14 00:21:29 +02:00
|
|
|
p_from_malloc = hmm_alloc(&hmm_d, new_num_aaus);
|
2010-05-18 17:58:33 +02:00
|
|
|
|
2012-07-14 00:21:29 +02:00
|
|
|
if (p_from_malloc) {
|
|
|
|
vpx_memcpy(p_from_malloc, memblk, size);
|
|
|
|
hmm_free(&hmm_d, memblk);
|
|
|
|
|
|
|
|
p_ret = p_from_malloc;
|
2010-05-18 17:58:33 +02:00
|
|
|
}
|
2012-07-14 00:21:29 +02:00
|
|
|
}
|
2010-05-18 17:58:33 +02:00
|
|
|
}
|
2012-07-14 00:21:29 +02:00
|
|
|
}
|
2010-05-18 17:58:33 +02:00
|
|
|
|
2012-07-14 00:21:29 +02:00
|
|
|
return p_ret;
|
2010-05-18 17:58:33 +02:00
|
|
|
}
|
2010-10-28 01:04:02 +02:00
|
|
|
#endif /*CONFIG_MEM_MANAGER*/
|
2010-05-18 17:58:33 +02:00
|
|
|
|
|
|
|
#if USE_GLOBAL_FUNCTION_POINTERS
|
|
|
|
# if CONFIG_MEM_TRACKER
|
|
|
|
extern int vpx_memory_tracker_set_functions(g_malloc_func g_malloc_l
|
2012-07-14 00:21:29 +02:00
|
|
|
, g_calloc_func g_calloc_l
|
|
|
|
, g_realloc_func g_realloc_l
|
|
|
|
, g_free_func g_free_l
|
|
|
|
, g_memcpy_func g_memcpy_l
|
|
|
|
, g_memset_func g_memset_l
|
|
|
|
, g_memmove_func g_memmove_l);
|
2010-05-18 17:58:33 +02:00
|
|
|
# endif
|
2010-10-28 01:04:02 +02:00
|
|
|
#endif /*USE_GLOBAL_FUNCTION_POINTERS*/
|
2010-05-18 17:58:33 +02:00
|
|
|
int vpx_mem_set_functions(g_malloc_func g_malloc_l
|
2012-07-14 00:21:29 +02:00
|
|
|
, g_calloc_func g_calloc_l
|
|
|
|
, g_realloc_func g_realloc_l
|
|
|
|
, g_free_func g_free_l
|
|
|
|
, g_memcpy_func g_memcpy_l
|
|
|
|
, g_memset_func g_memset_l
|
|
|
|
, g_memmove_func g_memmove_l) {
|
2010-05-18 17:58:33 +02:00
|
|
|
#if USE_GLOBAL_FUNCTION_POINTERS
|
|
|
|
|
2012-07-14 00:21:29 +02:00
|
|
|
/* If use global functions is turned on then the
|
|
|
|
application must set the global functions before
|
|
|
|
it does anything else or vpx_mem will have
|
|
|
|
unpredictable results. */
|
|
|
|
if (!g_func) {
|
|
|
|
g_func = (struct GLOBAL_FUNC_POINTERS *)
|
|
|
|
g_malloc_l(sizeof(struct GLOBAL_FUNC_POINTERS));
|
2010-05-18 17:58:33 +02:00
|
|
|
|
2012-07-14 00:21:29 +02:00
|
|
|
if (!g_func) {
|
|
|
|
return -1;
|
2010-05-18 17:58:33 +02:00
|
|
|
}
|
2012-07-14 00:21:29 +02:00
|
|
|
}
|
2010-05-18 17:58:33 +02:00
|
|
|
|
|
|
|
#if CONFIG_MEM_TRACKER
|
2012-07-14 00:21:29 +02:00
|
|
|
{
|
|
|
|
int rv = 0;
|
|
|
|
rv = vpx_memory_tracker_set_functions(g_malloc_l
|
|
|
|
, g_calloc_l
|
|
|
|
, g_realloc_l
|
|
|
|
, g_free_l
|
|
|
|
, g_memcpy_l
|
|
|
|
, g_memset_l
|
|
|
|
, g_memmove_l);
|
|
|
|
|
|
|
|
if (rv < 0) {
|
|
|
|
return rv;
|
2010-05-18 17:58:33 +02:00
|
|
|
}
|
2012-07-14 00:21:29 +02:00
|
|
|
}
|
2010-05-18 17:58:33 +02:00
|
|
|
#endif
|
|
|
|
|
2012-07-14 00:21:29 +02:00
|
|
|
g_func->g_malloc = g_malloc_l;
|
|
|
|
g_func->g_calloc = g_calloc_l;
|
|
|
|
g_func->g_realloc = g_realloc_l;
|
|
|
|
g_func->g_free = g_free_l;
|
|
|
|
g_func->g_memcpy = g_memcpy_l;
|
|
|
|
g_func->g_memset = g_memset_l;
|
|
|
|
g_func->g_memmove = g_memmove_l;
|
2010-05-18 17:58:33 +02:00
|
|
|
|
2012-07-14 00:21:29 +02:00
|
|
|
return 0;
|
2010-05-18 17:58:33 +02:00
|
|
|
#else
|
2012-07-14 00:21:29 +02:00
|
|
|
(void)g_malloc_l;
|
|
|
|
(void)g_calloc_l;
|
|
|
|
(void)g_realloc_l;
|
|
|
|
(void)g_free_l;
|
|
|
|
(void)g_memcpy_l;
|
|
|
|
(void)g_memset_l;
|
|
|
|
(void)g_memmove_l;
|
|
|
|
return -1;
|
2010-05-18 17:58:33 +02:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2012-07-14 00:21:29 +02:00
|
|
|
int vpx_mem_unset_functions() {
|
2010-05-18 17:58:33 +02:00
|
|
|
#if USE_GLOBAL_FUNCTION_POINTERS
|
|
|
|
|
2012-07-14 00:21:29 +02:00
|
|
|
if (g_func) {
|
|
|
|
g_free_func temp_free = g_func->g_free;
|
|
|
|
temp_free(g_func);
|
|
|
|
g_func = NULL;
|
|
|
|
}
|
2010-05-18 17:58:33 +02:00
|
|
|
|
|
|
|
#endif
|
2012-07-14 00:21:29 +02:00
|
|
|
return 0;
|
2010-05-18 17:58:33 +02:00
|
|
|
}
|