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