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
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
vpx_mem_tracker.c
|
|
|
|
|
|
|
|
jwz 2003-09-30:
|
|
|
|
Stores a list of addreses, their size, and file and line they came from.
|
|
|
|
All exposed lib functions are prefaced by vpx_ and allow the global list
|
|
|
|
to be thread safe.
|
|
|
|
Current supported platforms are:
|
|
|
|
Linux, Win32, win_ce and vx_works
|
|
|
|
Further support can be added by defining the platform specific mutex
|
|
|
|
in the memory_tracker struct as well as calls to create/destroy/lock/unlock
|
|
|
|
the mutex in vpx_memory_tracker_init/Destroy and memory_tracker_lock_mutex/unlock_mutex
|
|
|
|
*/
|
WebM Experimental Codec Branch Snapshot
This is a code snapshot of experimental work currently ongoing for a
next-generation codec.
The codebase has been cut down considerably from the libvpx baseline.
For example, we are currently only supporting VBR 2-pass rate control
and have removed most of the code relating to coding speed, threading,
error resilience, partitions and various other features. This is in
part to make the codebase easier to work on and experiment with, but
also because we want to have an open discussion about how the bitstream
will be structured and partitioned and not have that conversation
constrained by past work.
Our basic working pattern has been to initially encapsulate experiments
using configure options linked to #IF CONFIG_XXX statements in the
code. Once experiments have matured and we are reasonably happy that
they give benefit and can be merged without breaking other experiments,
we remove the conditional compile statements and merge them in.
Current changes include:
* Temporal coding experiment for segments (though still only 4 max, it
will likely be increased).
* Segment feature experiment - to allow various bits of information to
be coded at the segment level. Features tested so far include mode
and reference frame information, limiting end of block offset and
transform size, alongside Q and loop filter parameters, but this set
is very fluid.
* Support for 8x8 transform - 8x8 dct with 2nd order 2x2 haar is used
in MBs using 16x16 prediction modes within inter frames.
* Compound prediction (combination of signals from existing predictors
to create a new predictor).
* 8 tap interpolation filters and 1/8th pel motion vectors.
* Loop filter modifications.
* Various entropy modifications and changes to how entropy contexts and
updates are handled.
* Extended quantizer range matched to transform precision improvements.
There are also ongoing further experiments that we hope to merge in the
near future: For example, coding of motion and other aspects of the
prediction signal to better support larger image formats, use of larger
block sizes (e.g. 32x32 and up) and lossless non-transform based coding
options (especially for key frames). It is our hope that we will be
able to make regular updates and we will warmly welcome community
contributions.
Please be warned that, at this stage, the codebase is currently slower
than VP8 stable branch as most new code has not been optimized, and
even the 'C' has been deliberately written to be simple and obvious,
not fast.
The following graphs have the initial test results, numbers in the
tables measure the compression improvement in terms of percentage. The
build has the following optional experiments configured:
--enable-experimental --enable-enhanced_interp --enable-uvintra
--enable-high_precision_mv --enable-sixteenth_subpel_uv
CIF Size clips:
http://getwebm.org/tmp/cif/
HD size clips:
http://getwebm.org/tmp/hd/
(stable_20120309 represents encoding results of WebM master branch
build as of commit#7a15907)
They were encoded using the following encode parameters:
--good --cpu-used=0 -t 0 --lag-in-frames=25 --min-q=0 --max-q=63
--end-usage=0 --auto-alt-ref=1 -p 2 --pass=2 --kf-max-dist=9999
--kf-min-dist=0 --drop-frame=0 --static-thresh=0 --bias-pct=50
--minsection-pct=0 --maxsection-pct=800 --sharpness=0
--arnr-maxframes=7 --arnr-strength=3(for HD,6 for CIF)
--arnr-type=3
Change-Id: I5c62ed09cfff5815a2bb34e7820d6a810c23183c
2012-03-10 02:32:50 +01:00
|
|
|
#include "vpx_ports/config.h"
|
2010-05-18 17:58:33 +02:00
|
|
|
|
|
|
|
#if defined(__uClinux__)
|
|
|
|
# include <lddk.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if HAVE_PTHREAD_H
|
|
|
|
# include <pthread.h>
|
|
|
|
#elif defined(WIN32) || defined(_WIN32_WCE)
|
|
|
|
# define WIN32_LEAN_AND_MEAN
|
|
|
|
# include <windows.h>
|
|
|
|
# include <winbase.h>
|
|
|
|
#elif defined(VXWORKS)
|
|
|
|
# include <sem_lib.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2012-07-14 00:21:29 +02:00
|
|
|
#include <string.h> // VXWORKS doesn't have a malloc/memory.h file,
|
|
|
|
// this should pull in malloc,free,etc.
|
2010-05-18 17:58:33 +02:00
|
|
|
#include <stdarg.h>
|
|
|
|
|
|
|
|
#include "include/vpx_mem_tracker.h"
|
|
|
|
|
2012-07-14 00:21:29 +02:00
|
|
|
#undef vpx_malloc // undefine any vpx_mem macros that may affect calls to
|
|
|
|
#undef vpx_free // memory functions in this file
|
2010-05-18 17:58:33 +02:00
|
|
|
#undef vpx_memcpy
|
|
|
|
#undef vpx_memset
|
|
|
|
|
|
|
|
|
|
|
|
#ifndef USE_GLOBAL_FUNCTION_POINTERS
|
2012-07-14 00:21:29 +02:00
|
|
|
# define USE_GLOBAL_FUNCTION_POINTERS 0 // use function pointers instead of compiled functions.
|
2010-05-18 17:58:33 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if USE_GLOBAL_FUNCTION_POINTERS
|
|
|
|
static mem_track_malloc_func g_malloc = malloc;
|
|
|
|
static mem_track_calloc_func g_calloc = calloc;
|
|
|
|
static mem_track_realloc_func g_realloc = realloc;
|
|
|
|
static mem_track_free_func g_free = free;
|
|
|
|
static mem_track_memcpy_func g_memcpy = memcpy;
|
|
|
|
static mem_track_memset_func g_memset = memset;
|
|
|
|
static mem_track_memmove_func g_memmove = memmove;
|
|
|
|
# define MEM_TRACK_MALLOC g_malloc
|
|
|
|
# define MEM_TRACK_FREE g_free
|
|
|
|
# define MEM_TRACK_MEMCPY g_memcpy
|
|
|
|
# define MEM_TRACK_MEMSET g_memset
|
|
|
|
#else
|
|
|
|
# define MEM_TRACK_MALLOC vpx_malloc
|
|
|
|
# define MEM_TRACK_FREE vpx_free
|
|
|
|
# define MEM_TRACK_MEMCPY vpx_memcpy
|
|
|
|
# define MEM_TRACK_MEMSET vpx_memset
|
|
|
|
#endif // USE_GLOBAL_FUNCTION_POINTERS
|
|
|
|
|
|
|
|
/* prototypes for internal library functions */
|
|
|
|
static void memtrack_log(const char *fmt, ...);
|
|
|
|
static void memory_tracker_dump();
|
|
|
|
static void memory_tracker_check_integrity(char *file, unsigned int line);
|
|
|
|
static void memory_tracker_add(size_t addr, unsigned int size,
|
|
|
|
char *file, unsigned int line,
|
|
|
|
int padded);
|
|
|
|
static int memory_tracker_remove(size_t addr);
|
|
|
|
static struct mem_block *memory_tracker_find(size_t addr);
|
|
|
|
|
|
|
|
#if defined(NO_MUTEX)
|
|
|
|
# define memory_tracker_lock_mutex() (!g_b_mem_tracker_inited)
|
|
|
|
# define memory_tracker_unlock_mutex()
|
|
|
|
#else
|
|
|
|
static int memory_tracker_lock_mutex();
|
|
|
|
static int memory_tracker_unlock_mutex();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef VPX_NO_GLOBALS
|
2012-07-14 00:21:29 +02:00
|
|
|
struct memory_tracker {
|
|
|
|
struct mem_block *head,
|
|
|
|
* tail;
|
|
|
|
int len,
|
|
|
|
totalsize;
|
|
|
|
unsigned int current_allocated,
|
|
|
|
max_allocated;
|
2010-05-18 17:58:33 +02:00
|
|
|
|
|
|
|
#if HAVE_PTHREAD_H
|
2012-07-14 00:21:29 +02:00
|
|
|
pthread_mutex_t mutex;
|
2010-05-18 17:58:33 +02:00
|
|
|
#elif defined(WIN32) || defined(_WIN32_WCE)
|
2012-07-14 00:21:29 +02:00
|
|
|
HANDLE mutex;
|
2010-05-18 17:58:33 +02:00
|
|
|
#elif defined(VXWORKS)
|
2012-07-14 00:21:29 +02:00
|
|
|
SEM_ID mutex;
|
2010-05-18 17:58:33 +02:00
|
|
|
#elif defined(NO_MUTEX)
|
|
|
|
#else
|
|
|
|
#error "No mutex type defined for this platform!"
|
|
|
|
#endif
|
|
|
|
|
2012-07-14 00:21:29 +02:00
|
|
|
int padding_size,
|
|
|
|
pad_value;
|
2010-05-18 17:58:33 +02:00
|
|
|
};
|
|
|
|
|
2012-07-14 00:21:29 +02:00
|
|
|
static struct memory_tracker memtrack; // our global memory allocation list
|
|
|
|
static int g_b_mem_tracker_inited = 0; // indicates whether the global list has
|
|
|
|
// been initialized (1:yes/0:no)
|
|
|
|
static struct {
|
|
|
|
FILE *file;
|
|
|
|
int type;
|
|
|
|
void (*func)(void *userdata, const char *fmt, va_list args);
|
|
|
|
void *userdata;
|
2010-05-18 17:58:33 +02:00
|
|
|
} g_logging = {NULL, 0, NULL, NULL};
|
|
|
|
#else
|
|
|
|
# include "vpx_global_handling.h"
|
|
|
|
#define g_b_mem_tracker_inited vpxglobalm(vpxmem,g_b_mem_tracker_inited)
|
|
|
|
#define g_logging vpxglobalm(vpxmem,g_logging)
|
|
|
|
#define memtrack vpxglobalm(vpxmem,memtrack)
|
|
|
|
#endif // #ifndef VPX_NO_GLOBALS
|
|
|
|
|
|
|
|
extern void *vpx_malloc(size_t size);
|
|
|
|
extern void vpx_free(void *memblk);
|
|
|
|
extern void *vpx_memcpy(void *dest, const void *src, size_t length);
|
|
|
|
extern void *vpx_memset(void *dest, int val, size_t length);
|
|
|
|
|
|
|
|
/*
|
|
|
|
*
|
|
|
|
* Exposed library functions
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
vpx_memory_tracker_init(int padding_size, int pad_value)
|
|
|
|
padding_size - the size of the padding before and after each mem addr.
|
|
|
|
Values > 0 indicate that integrity checks can be performed
|
|
|
|
by inspecting these areas.
|
|
|
|
pad_value - the initial value within the padding area before and after
|
|
|
|
each mem addr.
|
|
|
|
|
|
|
|
Initializes global memory tracker structure
|
|
|
|
Allocates the head of the list
|
|
|
|
*/
|
2012-07-14 00:21:29 +02:00
|
|
|
int vpx_memory_tracker_init(int padding_size, int pad_value) {
|
|
|
|
if (!g_b_mem_tracker_inited) {
|
|
|
|
if ((memtrack.head = (struct mem_block *)
|
|
|
|
MEM_TRACK_MALLOC(sizeof(struct mem_block)))) {
|
|
|
|
int ret;
|
2010-05-18 17:58:33 +02:00
|
|
|
|
2012-07-14 00:21:29 +02:00
|
|
|
MEM_TRACK_MEMSET(memtrack.head, 0, sizeof(struct mem_block));
|
2010-05-18 17:58:33 +02:00
|
|
|
|
2012-07-14 00:21:29 +02:00
|
|
|
memtrack.tail = memtrack.head;
|
2010-05-18 17:58:33 +02:00
|
|
|
|
2012-07-14 00:21:29 +02:00
|
|
|
memtrack.current_allocated = 0;
|
|
|
|
memtrack.max_allocated = 0;
|
2010-05-18 17:58:33 +02:00
|
|
|
|
2012-07-14 00:21:29 +02:00
|
|
|
memtrack.padding_size = padding_size;
|
|
|
|
memtrack.pad_value = pad_value;
|
2010-05-18 17:58:33 +02:00
|
|
|
|
|
|
|
#if HAVE_PTHREAD_H
|
2012-07-14 00:21:29 +02:00
|
|
|
ret = pthread_mutex_init(&memtrack.mutex,
|
|
|
|
NULL); /*mutex attributes (NULL=default)*/
|
2010-05-18 17:58:33 +02:00
|
|
|
#elif defined(WIN32) || defined(_WIN32_WCE)
|
2012-07-14 00:21:29 +02:00
|
|
|
memtrack.mutex = CreateMutex(NULL, /*security attributes*/
|
|
|
|
FALSE, /*we don't want initial ownership*/
|
|
|
|
NULL); /*mutex name*/
|
|
|
|
ret = !memtrack.mutex;
|
2010-05-18 17:58:33 +02:00
|
|
|
#elif defined(VXWORKS)
|
2012-07-14 00:21:29 +02:00
|
|
|
memtrack.mutex = sem_bcreate(SEM_Q_FIFO, /*SEM_Q_FIFO non-priority based mutex*/
|
|
|
|
SEM_FULL); /*SEM_FULL initial state is unlocked*/
|
|
|
|
ret = !memtrack.mutex;
|
2010-05-18 17:58:33 +02:00
|
|
|
#elif defined(NO_MUTEX)
|
2012-07-14 00:21:29 +02:00
|
|
|
ret = 0;
|
2010-05-18 17:58:33 +02:00
|
|
|
#endif
|
|
|
|
|
2012-07-14 00:21:29 +02:00
|
|
|
if (ret) {
|
|
|
|
memtrack_log("vpx_memory_tracker_init: Error creating mutex!\n");
|
|
|
|
|
|
|
|
MEM_TRACK_FREE(memtrack.head);
|
|
|
|
memtrack.head = NULL;
|
|
|
|
} else {
|
|
|
|
memtrack_log("Memory Tracker init'd, v."vpx_mem_tracker_version" pad_size:%d pad_val:0x%x %d\n"
|
|
|
|
, padding_size
|
|
|
|
, pad_value
|
|
|
|
, pad_value);
|
|
|
|
g_b_mem_tracker_inited = 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
|
|
|
|
2012-07-14 00:21:29 +02:00
|
|
|
return g_b_mem_tracker_inited;
|
2010-05-18 17:58:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
vpx_memory_tracker_destroy()
|
|
|
|
If our global struct was initialized zeros out all its members,
|
|
|
|
frees memory and destroys it's mutex
|
|
|
|
*/
|
2012-07-14 00:21:29 +02:00
|
|
|
void vpx_memory_tracker_destroy() {
|
|
|
|
if (!memory_tracker_lock_mutex()) {
|
|
|
|
struct mem_block *p = memtrack.head,
|
|
|
|
* p2 = memtrack.head;
|
2010-05-18 17:58:33 +02:00
|
|
|
|
2012-07-14 00:21:29 +02:00
|
|
|
memory_tracker_dump();
|
2010-05-18 17:58:33 +02:00
|
|
|
|
2012-07-14 00:21:29 +02:00
|
|
|
while (p) {
|
|
|
|
p2 = p;
|
|
|
|
p = p->next;
|
2010-05-18 17:58:33 +02:00
|
|
|
|
2012-07-14 00:21:29 +02:00
|
|
|
MEM_TRACK_FREE(p2);
|
|
|
|
}
|
2010-05-18 17:58:33 +02:00
|
|
|
|
2012-07-14 00:21:29 +02:00
|
|
|
memtrack.head = NULL;
|
|
|
|
memtrack.tail = NULL;
|
|
|
|
memtrack.len = 0;
|
|
|
|
memtrack.current_allocated = 0;
|
|
|
|
memtrack.max_allocated = 0;
|
2010-05-18 17:58:33 +02:00
|
|
|
|
2012-07-14 00:21:29 +02:00
|
|
|
if (!g_logging.type && g_logging.file && g_logging.file != stderr) {
|
|
|
|
fclose(g_logging.file);
|
|
|
|
g_logging.file = NULL;
|
|
|
|
}
|
2010-05-18 17:58:33 +02:00
|
|
|
|
2012-07-14 00:21:29 +02:00
|
|
|
memory_tracker_unlock_mutex();
|
2010-05-18 17:58:33 +02:00
|
|
|
|
2012-07-14 00:21:29 +02:00
|
|
|
g_b_mem_tracker_inited = 0;
|
|
|
|
}
|
2010-05-18 17:58:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
vpx_memory_tracker_add(size_t addr, unsigned int size,
|
|
|
|
char * file, unsigned int line)
|
|
|
|
addr - memory address to be added to list
|
|
|
|
size - size of addr
|
|
|
|
file - the file addr was referenced from
|
|
|
|
line - the line in file addr was referenced from
|
|
|
|
Adds memory address addr, it's size, file and line it came from
|
|
|
|
to the global list via the thread safe internal library function
|
|
|
|
*/
|
|
|
|
void vpx_memory_tracker_add(size_t addr, unsigned int size,
|
|
|
|
char *file, unsigned int line,
|
2012-07-14 00:21:29 +02:00
|
|
|
int padded) {
|
|
|
|
memory_tracker_add(addr, size, file, line, padded);
|
2010-05-18 17:58:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
vpx_memory_tracker_remove(size_t addr)
|
|
|
|
addr - memory address to be removed from list
|
|
|
|
Removes addr from the global list via the thread safe
|
|
|
|
internal remove function
|
|
|
|
Return:
|
|
|
|
Same as described for memory_tracker_remove
|
|
|
|
*/
|
2012-07-14 00:21:29 +02:00
|
|
|
int vpx_memory_tracker_remove(size_t addr) {
|
|
|
|
return memory_tracker_remove(addr);
|
2010-05-18 17:58:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
vpx_memory_tracker_find(size_t addr)
|
|
|
|
addr - address to be found in list
|
|
|
|
Return:
|
|
|
|
If found, pointer to the memory block that matches addr
|
|
|
|
NULL otherwise
|
|
|
|
*/
|
2012-07-14 00:21:29 +02:00
|
|
|
struct mem_block *vpx_memory_tracker_find(size_t addr) {
|
|
|
|
struct mem_block *p = NULL;
|
2010-05-18 17:58:33 +02:00
|
|
|
|
2012-07-14 00:21:29 +02:00
|
|
|
if (!memory_tracker_lock_mutex()) {
|
|
|
|
p = memory_tracker_find(addr);
|
|
|
|
memory_tracker_unlock_mutex();
|
|
|
|
}
|
|
|
|
|
|
|
|
return p;
|
2010-05-18 17:58:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
vpx_memory_tracker_dump()
|
|
|
|
Locks the memory tracker's mutex and calls the internal
|
|
|
|
library function to dump the current contents of the
|
|
|
|
global memory allocation list
|
|
|
|
*/
|
2012-07-14 00:21:29 +02:00
|
|
|
void vpx_memory_tracker_dump() {
|
|
|
|
if (!memory_tracker_lock_mutex()) {
|
|
|
|
memory_tracker_dump();
|
|
|
|
memory_tracker_unlock_mutex();
|
|
|
|
}
|
2010-05-18 17:58:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
vpx_memory_tracker_check_integrity(char* file, unsigned int line)
|
|
|
|
file - The file name where the check was placed
|
|
|
|
line - The line in file where the check was placed
|
|
|
|
Locks the memory tracker's mutex and calls the internal
|
|
|
|
integrity check function to inspect every address in the global
|
|
|
|
memory allocation list
|
|
|
|
*/
|
2012-07-14 00:21:29 +02:00
|
|
|
void vpx_memory_tracker_check_integrity(char *file, unsigned int line) {
|
|
|
|
if (!memory_tracker_lock_mutex()) {
|
|
|
|
memory_tracker_check_integrity(file, line);
|
|
|
|
memory_tracker_unlock_mutex();
|
|
|
|
}
|
2010-05-18 17:58:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
vpx_memory_tracker_set_log_type
|
|
|
|
Sets the logging type for the memory tracker. Based on the value it will
|
|
|
|
direct its output to the appropriate place.
|
|
|
|
Return:
|
|
|
|
0: on success
|
|
|
|
-1: if the logging type could not be set, because the value was invalid
|
|
|
|
or because a file could not be opened
|
|
|
|
*/
|
2012-07-14 00:21:29 +02:00
|
|
|
int vpx_memory_tracker_set_log_type(int type, char *option) {
|
|
|
|
int ret = -1;
|
2010-05-18 17:58:33 +02:00
|
|
|
|
2012-07-14 00:21:29 +02:00
|
|
|
switch (type) {
|
2010-05-18 17:58:33 +02:00
|
|
|
case 0:
|
2012-07-14 00:21:29 +02:00
|
|
|
g_logging.type = 0;
|
2010-05-18 17:58:33 +02:00
|
|
|
|
2012-07-14 00:21:29 +02:00
|
|
|
if (!option) {
|
|
|
|
g_logging.file = stderr;
|
|
|
|
ret = 0;
|
|
|
|
} else {
|
|
|
|
if ((g_logging.file = fopen((char *)option, "w")))
|
|
|
|
ret = 0;
|
|
|
|
}
|
2010-05-18 17:58:33 +02:00
|
|
|
|
2012-07-14 00:21:29 +02:00
|
|
|
break;
|
2010-05-18 17:58:33 +02:00
|
|
|
#if defined(WIN32) && !defined(_WIN32_WCE)
|
|
|
|
case 1:
|
2012-07-14 00:21:29 +02:00
|
|
|
g_logging.type = type;
|
|
|
|
ret = 0;
|
|
|
|
break;
|
2010-05-18 17:58:33 +02:00
|
|
|
#endif
|
|
|
|
default:
|
2012-07-14 00:21:29 +02:00
|
|
|
break;
|
|
|
|
}
|
2010-05-18 17:58:33 +02:00
|
|
|
|
2012-07-14 00:21:29 +02:00
|
|
|
// output the version to the new logging destination
|
|
|
|
if (!ret)
|
|
|
|
memtrack_log("Memory Tracker logging initialized, "
|
|
|
|
"Memory Tracker v."vpx_mem_tracker_version"\n");
|
2010-05-18 17:58:33 +02:00
|
|
|
|
2012-07-14 00:21:29 +02:00
|
|
|
return ret;
|
2010-05-18 17:58:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
vpx_memory_tracker_set_log_func
|
|
|
|
Sets a logging function to be used by the memory tracker.
|
|
|
|
Return:
|
|
|
|
0: on success
|
|
|
|
-1: if the logging type could not be set because logfunc was NULL
|
|
|
|
*/
|
|
|
|
int vpx_memory_tracker_set_log_func(void *userdata,
|
|
|
|
void(*logfunc)(void *userdata,
|
2012-07-14 00:21:29 +02:00
|
|
|
const char *fmt, va_list args)) {
|
|
|
|
int ret = -1;
|
|
|
|
|
|
|
|
if (logfunc) {
|
|
|
|
g_logging.type = -1;
|
|
|
|
g_logging.userdata = userdata;
|
|
|
|
g_logging.func = logfunc;
|
|
|
|
ret = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// output the version to the new logging destination
|
|
|
|
if (!ret)
|
|
|
|
memtrack_log("Memory Tracker logging initialized, "
|
|
|
|
"Memory Tracker v."vpx_mem_tracker_version"\n");
|
|
|
|
|
|
|
|
return ret;
|
2010-05-18 17:58:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
*
|
|
|
|
* END - Exposed library functions
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
*
|
|
|
|
* Internal library functions
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2012-07-14 00:21:29 +02:00
|
|
|
static void memtrack_log(const char *fmt, ...) {
|
|
|
|
va_list list;
|
2010-05-18 17:58:33 +02:00
|
|
|
|
2012-07-14 00:21:29 +02:00
|
|
|
va_start(list, fmt);
|
2010-05-18 17:58:33 +02:00
|
|
|
|
2012-07-14 00:21:29 +02:00
|
|
|
switch (g_logging.type) {
|
2010-05-18 17:58:33 +02:00
|
|
|
case -1:
|
|
|
|
|
2012-07-14 00:21:29 +02:00
|
|
|
if (g_logging.func)
|
|
|
|
g_logging.func(g_logging.userdata, fmt, list);
|
2010-05-18 17:58:33 +02:00
|
|
|
|
2012-07-14 00:21:29 +02:00
|
|
|
break;
|
2010-05-18 17:58:33 +02:00
|
|
|
case 0:
|
|
|
|
|
2012-07-14 00:21:29 +02:00
|
|
|
if (g_logging.file) {
|
|
|
|
vfprintf(g_logging.file, fmt, list);
|
|
|
|
fflush(g_logging.file);
|
|
|
|
}
|
2010-05-18 17:58:33 +02:00
|
|
|
|
2012-07-14 00:21:29 +02:00
|
|
|
break;
|
2010-05-18 17:58:33 +02:00
|
|
|
#if defined(WIN32) && !defined(_WIN32_WCE)
|
2012-07-14 00:21:29 +02:00
|
|
|
case 1: {
|
|
|
|
char temp[1024];
|
|
|
|
_vsnprintf(temp, sizeof(temp) / sizeof(char) - 1, fmt, list);
|
|
|
|
OutputDebugString(temp);
|
2010-05-18 17:58:33 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
default:
|
2012-07-14 00:21:29 +02:00
|
|
|
break;
|
|
|
|
}
|
2010-05-18 17:58:33 +02:00
|
|
|
|
2012-07-14 00:21:29 +02:00
|
|
|
va_end(list);
|
2010-05-18 17:58:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
memory_tracker_dump()
|
|
|
|
Dumps the current contents of the global memory allocation list
|
|
|
|
*/
|
2012-07-14 00:21:29 +02:00
|
|
|
static void memory_tracker_dump() {
|
|
|
|
int i = 0;
|
|
|
|
struct mem_block *p = (memtrack.head ? memtrack.head->next : NULL);
|
2010-05-18 17:58:33 +02:00
|
|
|
|
2012-07-14 00:21:29 +02:00
|
|
|
memtrack_log("\n_currently Allocated= %d; Max allocated= %d\n",
|
|
|
|
memtrack.current_allocated, memtrack.max_allocated);
|
2010-05-18 17:58:33 +02:00
|
|
|
|
2012-07-14 00:21:29 +02:00
|
|
|
while (p) {
|
2010-05-18 17:58:33 +02:00
|
|
|
#if defined(WIN32) && !defined(_WIN32_WCE)
|
|
|
|
|
2012-07-14 00:21:29 +02:00
|
|
|
/*when using outputdebugstring, output filenames so they
|
|
|
|
can be clicked to be opened in visual studio*/
|
|
|
|
if (g_logging.type == 1)
|
|
|
|
memtrack_log("memblocks[%d].addr= 0x%.8x, memblocks[%d].size= %d, file:\n"
|
|
|
|
" %s(%d):\n", i,
|
|
|
|
p->addr, i, p->size,
|
|
|
|
p->file, p->line);
|
|
|
|
else
|
2010-05-18 17:58:33 +02:00
|
|
|
#endif
|
2012-07-14 00:21:29 +02:00
|
|
|
memtrack_log("memblocks[%d].addr= 0x%.8x, memblocks[%d].size= %d, file: %s, line: %d\n", i,
|
|
|
|
p->addr, i, p->size,
|
|
|
|
p->file, p->line);
|
2010-05-18 17:58:33 +02:00
|
|
|
|
2012-07-14 00:21:29 +02:00
|
|
|
p = p->next;
|
|
|
|
++i;
|
|
|
|
}
|
2010-05-18 17:58:33 +02:00
|
|
|
|
2012-07-14 00:21:29 +02:00
|
|
|
memtrack_log("\n");
|
2010-05-18 17:58:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
memory_tracker_check_integrity(char* file, unsigned int file)
|
|
|
|
file - the file name where the check was placed
|
|
|
|
line - the line in file where the check was placed
|
|
|
|
If a padding_size was supplied to vpx_memory_tracker_init()
|
|
|
|
this function will check ea. addr in the list verifying that
|
|
|
|
addr-padding_size and addr+padding_size is filled with pad_value
|
|
|
|
*/
|
2012-07-14 00:21:29 +02:00
|
|
|
static void memory_tracker_check_integrity(char *file, unsigned int line) {
|
|
|
|
if (memtrack.padding_size) {
|
|
|
|
int i,
|
|
|
|
index = 0;
|
|
|
|
unsigned char *p_show_me,
|
|
|
|
* p_show_me2;
|
|
|
|
unsigned int tempme = memtrack.pad_value,
|
|
|
|
dead1,
|
|
|
|
dead2;
|
|
|
|
unsigned char *x_bounds;
|
|
|
|
struct mem_block *p = memtrack.head->next;
|
|
|
|
|
|
|
|
while (p) {
|
|
|
|
// x_bounds = (unsigned char*)p->addr;
|
|
|
|
// back up VPX_BYTE_ALIGNMENT
|
|
|
|
// x_bounds -= memtrack.padding_size;
|
|
|
|
|
|
|
|
if (p->padded) { // can the bounds be checked?
|
|
|
|
/*yes, move to the address that was actually allocated
|
|
|
|
by the vpx_* calls*/
|
|
|
|
x_bounds = (unsigned char *)(((size_t *)p->addr)[-1]);
|
|
|
|
|
|
|
|
for (i = 0; i < memtrack.padding_size; i += sizeof(unsigned int)) {
|
|
|
|
p_show_me = (x_bounds + i);
|
|
|
|
p_show_me2 = (unsigned char *)(p->addr + p->size + i);
|
|
|
|
|
|
|
|
MEM_TRACK_MEMCPY(&dead1, p_show_me, sizeof(unsigned int));
|
|
|
|
MEM_TRACK_MEMCPY(&dead2, p_show_me2, sizeof(unsigned int));
|
|
|
|
|
|
|
|
if ((dead1 != tempme) || (dead2 != tempme)) {
|
|
|
|
memtrack_log("\n[vpx_mem integrity check failed]:\n"
|
|
|
|
" index[%d,%d] {%s:%d} addr=0x%x, size=%d,"
|
|
|
|
" file: %s, line: %d c0:0x%x c1:0x%x\n",
|
|
|
|
index, i, file, line, p->addr, p->size, p->file,
|
|
|
|
p->line, dead1, dead2);
|
|
|
|
}
|
2010-05-18 17:58:33 +02:00
|
|
|
}
|
2012-07-14 00:21:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
++index;
|
|
|
|
p = p->next;
|
2010-05-18 17:58:33 +02:00
|
|
|
}
|
2012-07-14 00:21:29 +02:00
|
|
|
}
|
2010-05-18 17:58:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
memory_tracker_add(size_t addr, unsigned int size,
|
|
|
|
char * file, unsigned int line)
|
|
|
|
Adds an address (addr), it's size, file and line number to our list.
|
|
|
|
Adjusts the total bytes allocated and max bytes allocated if necessary.
|
|
|
|
If memory cannot be allocated the list will be destroyed.
|
|
|
|
*/
|
|
|
|
void memory_tracker_add(size_t addr, unsigned int size,
|
|
|
|
char *file, unsigned int line,
|
2012-07-14 00:21:29 +02:00
|
|
|
int padded) {
|
|
|
|
if (!memory_tracker_lock_mutex()) {
|
|
|
|
struct mem_block *p;
|
2010-05-18 17:58:33 +02:00
|
|
|
|
2012-07-14 00:21:29 +02:00
|
|
|
p = MEM_TRACK_MALLOC(sizeof(struct mem_block));
|
2010-05-18 17:58:33 +02:00
|
|
|
|
2012-07-14 00:21:29 +02:00
|
|
|
if (p) {
|
|
|
|
p->prev = memtrack.tail;
|
|
|
|
p->prev->next = p;
|
|
|
|
p->addr = addr;
|
|
|
|
p->size = size;
|
|
|
|
p->line = line;
|
|
|
|
p->file = file;
|
|
|
|
p->padded = padded;
|
|
|
|
p->next = NULL;
|
2010-05-18 17:58:33 +02:00
|
|
|
|
2012-07-14 00:21:29 +02:00
|
|
|
memtrack.tail = p;
|
2010-05-18 17:58:33 +02:00
|
|
|
|
2012-07-14 00:21:29 +02:00
|
|
|
memtrack.current_allocated += size;
|
2010-05-18 17:58:33 +02:00
|
|
|
|
2012-07-14 00:21:29 +02:00
|
|
|
if (memtrack.current_allocated > memtrack.max_allocated)
|
|
|
|
memtrack.max_allocated = memtrack.current_allocated;
|
2010-05-18 17:58:33 +02:00
|
|
|
|
2012-07-14 00:21:29 +02:00
|
|
|
// memtrack_log("memory_tracker_add: added addr=0x%.8x\n", addr);
|
2010-05-18 17:58:33 +02:00
|
|
|
|
2012-07-14 00:21:29 +02:00
|
|
|
memory_tracker_unlock_mutex();
|
|
|
|
} else {
|
|
|
|
memtrack_log("memory_tracker_add: error allocating memory!\n");
|
|
|
|
memory_tracker_unlock_mutex();
|
|
|
|
vpx_memory_tracker_destroy();
|
2010-05-18 17:58:33 +02:00
|
|
|
}
|
2012-07-14 00:21:29 +02:00
|
|
|
}
|
2010-05-18 17:58:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
memory_tracker_remove(size_t addr)
|
|
|
|
Removes an address and its corresponding size (if they exist)
|
|
|
|
from the memory tracker list and adjusts the current number
|
|
|
|
of bytes allocated.
|
|
|
|
Return:
|
|
|
|
0: on success
|
|
|
|
-1: if the mutex could not be locked
|
|
|
|
-2: if the addr was not found in the list
|
|
|
|
*/
|
2012-07-14 00:21:29 +02:00
|
|
|
int memory_tracker_remove(size_t addr) {
|
|
|
|
int ret = -1;
|
2010-05-18 17:58:33 +02:00
|
|
|
|
2012-07-14 00:21:29 +02:00
|
|
|
if (!memory_tracker_lock_mutex()) {
|
|
|
|
struct mem_block *p;
|
2010-05-18 17:58:33 +02:00
|
|
|
|
2012-07-14 00:21:29 +02:00
|
|
|
if ((p = memory_tracker_find(addr))) {
|
|
|
|
memtrack.current_allocated -= p->size;
|
2010-05-18 17:58:33 +02:00
|
|
|
|
2012-07-14 00:21:29 +02:00
|
|
|
p->prev->next = p->next;
|
2010-05-18 17:58:33 +02:00
|
|
|
|
2012-07-14 00:21:29 +02:00
|
|
|
if (p->next)
|
|
|
|
p->next->prev = p->prev;
|
|
|
|
else
|
|
|
|
memtrack.tail = p->prev;
|
2010-05-18 17:58:33 +02:00
|
|
|
|
2012-07-14 00:21:29 +02:00
|
|
|
ret = 0;
|
|
|
|
MEM_TRACK_FREE(p);
|
|
|
|
} else {
|
|
|
|
if (addr)
|
|
|
|
memtrack_log("memory_tracker_remove(): addr not found in list,"
|
|
|
|
" 0x%.8x\n", addr);
|
2010-05-18 17:58:33 +02:00
|
|
|
|
2012-07-14 00:21:29 +02:00
|
|
|
ret = -2;
|
2010-05-18 17:58:33 +02:00
|
|
|
}
|
|
|
|
|
2012-07-14 00:21:29 +02:00
|
|
|
memory_tracker_unlock_mutex();
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
2010-05-18 17:58:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
memory_tracker_find(size_t addr)
|
|
|
|
Finds an address in our addrs list
|
|
|
|
NOTE: the mutex MUST be locked in the other internal
|
|
|
|
functions before calling this one. This avoids
|
|
|
|
the need for repeated locking and unlocking as in Remove
|
|
|
|
Returns: pointer to the mem block if found, NULL otherwise
|
|
|
|
*/
|
2012-07-14 00:21:29 +02:00
|
|
|
static struct mem_block *memory_tracker_find(size_t addr) {
|
|
|
|
struct mem_block *p = NULL;
|
2010-05-18 17:58:33 +02:00
|
|
|
|
2012-07-14 00:21:29 +02:00
|
|
|
if (memtrack.head) {
|
|
|
|
p = memtrack.head->next;
|
2010-05-18 17:58:33 +02:00
|
|
|
|
2012-07-14 00:21:29 +02:00
|
|
|
while (p && (p->addr != addr))
|
|
|
|
p = p->next;
|
|
|
|
}
|
2010-05-18 17:58:33 +02:00
|
|
|
|
2012-07-14 00:21:29 +02:00
|
|
|
return p;
|
2010-05-18 17:58:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#if !defined(NO_MUTEX)
|
|
|
|
/*
|
|
|
|
memory_tracker_lock_mutex()
|
|
|
|
Locks the memory tracker mutex with a platform specific call
|
|
|
|
Returns:
|
|
|
|
0: Success
|
|
|
|
<0: Failure, either the mutex was not initialized
|
|
|
|
or the call to lock the mutex failed
|
|
|
|
*/
|
2012-07-14 00:21:29 +02:00
|
|
|
static int memory_tracker_lock_mutex() {
|
|
|
|
int ret = -1;
|
2010-05-18 17:58:33 +02:00
|
|
|
|
2012-07-14 00:21:29 +02:00
|
|
|
if (g_b_mem_tracker_inited) {
|
2010-05-18 17:58:33 +02:00
|
|
|
|
|
|
|
#if HAVE_PTHREAD_H
|
2012-07-14 00:21:29 +02:00
|
|
|
ret = pthread_mutex_lock(&memtrack.mutex);
|
2010-05-18 17:58:33 +02:00
|
|
|
#elif defined(WIN32) || defined(_WIN32_WCE)
|
2012-07-14 00:21:29 +02:00
|
|
|
ret = WaitForSingleObject(memtrack.mutex, INFINITE);
|
2010-05-18 17:58:33 +02:00
|
|
|
#elif defined(VXWORKS)
|
2012-07-14 00:21:29 +02:00
|
|
|
ret = sem_take(memtrack.mutex, WAIT_FOREVER);
|
2010-05-18 17:58:33 +02:00
|
|
|
#endif
|
|
|
|
|
2012-07-14 00:21:29 +02:00
|
|
|
if (ret) {
|
|
|
|
memtrack_log("memory_tracker_lock_mutex: mutex lock failed\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
|
|
|
|
2012-07-14 00:21:29 +02:00
|
|
|
return ret;
|
2010-05-18 17:58:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
memory_tracker_unlock_mutex()
|
|
|
|
Unlocks the memory tracker mutex with a platform specific call
|
|
|
|
Returns:
|
|
|
|
0: Success
|
|
|
|
<0: Failure, either the mutex was not initialized
|
|
|
|
or the call to unlock the mutex failed
|
|
|
|
*/
|
2012-07-14 00:21:29 +02:00
|
|
|
static int memory_tracker_unlock_mutex() {
|
|
|
|
int ret = -1;
|
2010-05-18 17:58:33 +02:00
|
|
|
|
2012-07-14 00:21:29 +02:00
|
|
|
if (g_b_mem_tracker_inited) {
|
2010-05-18 17:58:33 +02:00
|
|
|
|
|
|
|
#if HAVE_PTHREAD_H
|
2012-07-14 00:21:29 +02:00
|
|
|
ret = pthread_mutex_unlock(&memtrack.mutex);
|
2010-05-18 17:58:33 +02:00
|
|
|
#elif defined(WIN32) || defined(_WIN32_WCE)
|
2012-07-14 00:21:29 +02:00
|
|
|
ret = !ReleaseMutex(memtrack.mutex);
|
2010-05-18 17:58:33 +02:00
|
|
|
#elif defined(VXWORKS)
|
2012-07-14 00:21:29 +02:00
|
|
|
ret = sem_give(memtrack.mutex);
|
2010-05-18 17:58:33 +02:00
|
|
|
#endif
|
|
|
|
|
2012-07-14 00:21:29 +02:00
|
|
|
if (ret) {
|
|
|
|
memtrack_log("memory_tracker_unlock_mutex: mutex unlock failed\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
|
|
|
|
2012-07-14 00:21:29 +02:00
|
|
|
return ret;
|
2010-05-18 17:58:33 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/*
|
|
|
|
vpx_memory_tracker_set_functions
|
|
|
|
|
|
|
|
Sets the function pointers for the standard library functions.
|
|
|
|
|
|
|
|
Return:
|
|
|
|
0: on success
|
|
|
|
-1: if the use global function pointers is not set.
|
|
|
|
*/
|
|
|
|
int vpx_memory_tracker_set_functions(mem_track_malloc_func g_malloc_l
|
2012-07-14 00:21:29 +02:00
|
|
|
, mem_track_calloc_func g_calloc_l
|
|
|
|
, mem_track_realloc_func g_realloc_l
|
|
|
|
, mem_track_free_func g_free_l
|
|
|
|
, mem_track_memcpy_func g_memcpy_l
|
|
|
|
, mem_track_memset_func g_memset_l
|
|
|
|
, mem_track_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 (g_malloc_l)
|
|
|
|
g_malloc = g_malloc_l;
|
2010-05-18 17:58:33 +02:00
|
|
|
|
2012-07-14 00:21:29 +02:00
|
|
|
if (g_calloc_l)
|
|
|
|
g_calloc = g_calloc_l;
|
2010-05-18 17:58:33 +02:00
|
|
|
|
2012-07-14 00:21:29 +02:00
|
|
|
if (g_realloc_l)
|
|
|
|
g_realloc = g_realloc_l;
|
2010-05-18 17:58:33 +02:00
|
|
|
|
2012-07-14 00:21:29 +02:00
|
|
|
if (g_free_l)
|
|
|
|
g_free = g_free_l;
|
2010-05-18 17:58:33 +02:00
|
|
|
|
2012-07-14 00:21:29 +02:00
|
|
|
if (g_memcpy_l)
|
|
|
|
g_memcpy = g_memcpy_l;
|
2010-05-18 17:58:33 +02:00
|
|
|
|
2012-07-14 00:21:29 +02:00
|
|
|
if (g_memset_l)
|
|
|
|
g_memset = g_memset_l;
|
2010-05-18 17:58:33 +02:00
|
|
|
|
2012-07-14 00:21:29 +02:00
|
|
|
if (g_memmove_l)
|
|
|
|
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
|
|
|
|
}
|