media/libvpx/vpx_mem/vpx_mem.c

Thu, 15 Jan 2015 15:59:08 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 15 Jan 2015 15:59:08 +0100
branch
TOR_BUG_9701
changeset 10
ac0c01689b40
permissions
-rw-r--r--

Implement a real Private Browsing Mode condition by changing the API/ABI;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.

michael@0 1 /*
michael@0 2 * Copyright (c) 2010 The WebM project authors. All Rights Reserved.
michael@0 3 *
michael@0 4 * Use of this source code is governed by a BSD-style license
michael@0 5 * that can be found in the LICENSE file in the root of the source
michael@0 6 * tree. An additional intellectual property rights grant can be found
michael@0 7 * in the file PATENTS. All contributing project authors may
michael@0 8 * be found in the AUTHORS file in the root of the source tree.
michael@0 9 */
michael@0 10
michael@0 11
michael@0 12 #define __VPX_MEM_C__
michael@0 13
michael@0 14 #include "vpx_mem.h"
michael@0 15 #include <stdio.h>
michael@0 16 #include <stdlib.h>
michael@0 17 #include <string.h>
michael@0 18 #include "include/vpx_mem_intrnl.h"
michael@0 19
michael@0 20 #if CONFIG_MEM_TRACKER
michael@0 21 #ifndef VPX_NO_GLOBALS
michael@0 22 static unsigned long g_alloc_count = 0;
michael@0 23 #else
michael@0 24 #include "vpx_global_handling.h"
michael@0 25 #define g_alloc_count vpxglobalm(vpxmem,g_alloc_count)
michael@0 26 #endif
michael@0 27 #endif
michael@0 28
michael@0 29 #if CONFIG_MEM_MANAGER
michael@0 30 # include "heapmm.h"
michael@0 31 # include "hmm_intrnl.h"
michael@0 32
michael@0 33 # define SHIFT_HMM_ADDR_ALIGN_UNIT 5
michael@0 34 # define TOTAL_MEMORY_TO_ALLOCATE 20971520 /* 20 * 1024 * 1024 */
michael@0 35
michael@0 36 # define MM_DYNAMIC_MEMORY 1
michael@0 37 # if MM_DYNAMIC_MEMORY
michael@0 38 static unsigned char *g_p_mng_memory_raw = NULL;
michael@0 39 static unsigned char *g_p_mng_memory = NULL;
michael@0 40 # else
michael@0 41 static unsigned char g_p_mng_memory[TOTAL_MEMORY_TO_ALLOCATE];
michael@0 42 # endif
michael@0 43
michael@0 44 static size_t g_mm_memory_size = TOTAL_MEMORY_TO_ALLOCATE;
michael@0 45
michael@0 46 static hmm_descriptor hmm_d;
michael@0 47 static int g_mng_memory_allocated = 0;
michael@0 48
michael@0 49 static int vpx_mm_create_heap_memory();
michael@0 50 static void *vpx_mm_realloc(void *memblk, size_t size);
michael@0 51 #endif /*CONFIG_MEM_MANAGER*/
michael@0 52
michael@0 53 #if USE_GLOBAL_FUNCTION_POINTERS
michael@0 54 struct GLOBAL_FUNC_POINTERS {
michael@0 55 g_malloc_func g_malloc;
michael@0 56 g_calloc_func g_calloc;
michael@0 57 g_realloc_func g_realloc;
michael@0 58 g_free_func g_free;
michael@0 59 g_memcpy_func g_memcpy;
michael@0 60 g_memset_func g_memset;
michael@0 61 g_memmove_func g_memmove;
michael@0 62 } *g_func = NULL;
michael@0 63
michael@0 64 # define VPX_MALLOC_L g_func->g_malloc
michael@0 65 # define VPX_REALLOC_L g_func->g_realloc
michael@0 66 # define VPX_FREE_L g_func->g_free
michael@0 67 # define VPX_MEMCPY_L g_func->g_memcpy
michael@0 68 # define VPX_MEMSET_L g_func->g_memset
michael@0 69 # define VPX_MEMMOVE_L g_func->g_memmove
michael@0 70 #else
michael@0 71 # define VPX_MALLOC_L malloc
michael@0 72 # define VPX_REALLOC_L realloc
michael@0 73 # define VPX_FREE_L free
michael@0 74 # define VPX_MEMCPY_L memcpy
michael@0 75 # define VPX_MEMSET_L memset
michael@0 76 # define VPX_MEMMOVE_L memmove
michael@0 77 #endif /* USE_GLOBAL_FUNCTION_POINTERS */
michael@0 78
michael@0 79 unsigned int vpx_mem_get_version() {
michael@0 80 unsigned int ver = ((unsigned int)(unsigned char)VPX_MEM_VERSION_CHIEF << 24 |
michael@0 81 (unsigned int)(unsigned char)VPX_MEM_VERSION_MAJOR << 16 |
michael@0 82 (unsigned int)(unsigned char)VPX_MEM_VERSION_MINOR << 8 |
michael@0 83 (unsigned int)(unsigned char)VPX_MEM_VERSION_PATCH);
michael@0 84 return ver;
michael@0 85 }
michael@0 86
michael@0 87 int vpx_mem_set_heap_size(size_t size) {
michael@0 88 int ret = -1;
michael@0 89
michael@0 90 #if CONFIG_MEM_MANAGER
michael@0 91 #if MM_DYNAMIC_MEMORY
michael@0 92
michael@0 93 if (!g_mng_memory_allocated && size) {
michael@0 94 g_mm_memory_size = size;
michael@0 95 ret = 0;
michael@0 96 } else
michael@0 97 ret = -3;
michael@0 98
michael@0 99 #else
michael@0 100 ret = -2;
michael@0 101 #endif
michael@0 102 #else
michael@0 103 (void)size;
michael@0 104 #endif
michael@0 105
michael@0 106 return ret;
michael@0 107 }
michael@0 108
michael@0 109 void *vpx_memalign(size_t align, size_t size) {
michael@0 110 void *addr,
michael@0 111 * x = NULL;
michael@0 112
michael@0 113 #if CONFIG_MEM_MANAGER
michael@0 114 int number_aau;
michael@0 115
michael@0 116 if (vpx_mm_create_heap_memory() < 0) {
michael@0 117 _P(printf("[vpx][mm] ERROR vpx_memalign() Couldn't create memory for Heap.\n");)
michael@0 118 }
michael@0 119
michael@0 120 number_aau = ((size + align - 1 + ADDRESS_STORAGE_SIZE) >>
michael@0 121 SHIFT_HMM_ADDR_ALIGN_UNIT) + 1;
michael@0 122
michael@0 123 addr = hmm_alloc(&hmm_d, number_aau);
michael@0 124 #else
michael@0 125 addr = VPX_MALLOC_L(size + align - 1 + ADDRESS_STORAGE_SIZE);
michael@0 126 #endif /*CONFIG_MEM_MANAGER*/
michael@0 127
michael@0 128 if (addr) {
michael@0 129 x = align_addr((unsigned char *)addr + ADDRESS_STORAGE_SIZE, (int)align);
michael@0 130 /* save the actual malloc address */
michael@0 131 ((size_t *)x)[-1] = (size_t)addr;
michael@0 132 }
michael@0 133
michael@0 134 return x;
michael@0 135 }
michael@0 136
michael@0 137 void *vpx_malloc(size_t size) {
michael@0 138 return vpx_memalign(DEFAULT_ALIGNMENT, size);
michael@0 139 }
michael@0 140
michael@0 141 void *vpx_calloc(size_t num, size_t size) {
michael@0 142 void *x;
michael@0 143
michael@0 144 x = vpx_memalign(DEFAULT_ALIGNMENT, num * size);
michael@0 145
michael@0 146 if (x)
michael@0 147 VPX_MEMSET_L(x, 0, num * size);
michael@0 148
michael@0 149 return x;
michael@0 150 }
michael@0 151
michael@0 152 void *vpx_realloc(void *memblk, size_t size) {
michael@0 153 void *addr,
michael@0 154 * new_addr = NULL;
michael@0 155 int align = DEFAULT_ALIGNMENT;
michael@0 156
michael@0 157 /*
michael@0 158 The realloc() function changes the size of the object pointed to by
michael@0 159 ptr to the size specified by size, and returns a pointer to the
michael@0 160 possibly moved block. The contents are unchanged up to the lesser
michael@0 161 of the new and old sizes. If ptr is null, realloc() behaves like
michael@0 162 malloc() for the specified size. If size is zero (0) and ptr is
michael@0 163 not a null pointer, the object pointed to is freed.
michael@0 164 */
michael@0 165 if (!memblk)
michael@0 166 new_addr = vpx_malloc(size);
michael@0 167 else if (!size)
michael@0 168 vpx_free(memblk);
michael@0 169 else {
michael@0 170 addr = (void *)(((size_t *)memblk)[-1]);
michael@0 171 memblk = NULL;
michael@0 172
michael@0 173 #if CONFIG_MEM_MANAGER
michael@0 174 new_addr = vpx_mm_realloc(addr, size + align + ADDRESS_STORAGE_SIZE);
michael@0 175 #else
michael@0 176 new_addr = VPX_REALLOC_L(addr, size + align + ADDRESS_STORAGE_SIZE);
michael@0 177 #endif
michael@0 178
michael@0 179 if (new_addr) {
michael@0 180 addr = new_addr;
michael@0 181 new_addr = (void *)(((size_t)
michael@0 182 ((unsigned char *)new_addr + ADDRESS_STORAGE_SIZE) + (align - 1)) &
michael@0 183 (size_t) - align);
michael@0 184 /* save the actual malloc address */
michael@0 185 ((size_t *)new_addr)[-1] = (size_t)addr;
michael@0 186 }
michael@0 187 }
michael@0 188
michael@0 189 return new_addr;
michael@0 190 }
michael@0 191
michael@0 192 void vpx_free(void *memblk) {
michael@0 193 if (memblk) {
michael@0 194 void *addr = (void *)(((size_t *)memblk)[-1]);
michael@0 195 #if CONFIG_MEM_MANAGER
michael@0 196 hmm_free(&hmm_d, addr);
michael@0 197 #else
michael@0 198 VPX_FREE_L(addr);
michael@0 199 #endif
michael@0 200 }
michael@0 201 }
michael@0 202
michael@0 203 #if CONFIG_MEM_TRACKER
michael@0 204 void *xvpx_memalign(size_t align, size_t size, char *file, int line) {
michael@0 205 #if TRY_BOUNDS_CHECK
michael@0 206 unsigned char *x_bounds;
michael@0 207 #endif
michael@0 208
michael@0 209 void *x;
michael@0 210
michael@0 211 if (g_alloc_count == 0) {
michael@0 212 #if TRY_BOUNDS_CHECK
michael@0 213 int i_rv = vpx_memory_tracker_init(BOUNDS_CHECK_PAD_SIZE, BOUNDS_CHECK_VALUE);
michael@0 214 #else
michael@0 215 int i_rv = vpx_memory_tracker_init(0, 0);
michael@0 216 #endif
michael@0 217
michael@0 218 if (i_rv < 0) {
michael@0 219 _P(printf("ERROR xvpx_malloc MEM_TRACK_USAGE error vpx_memory_tracker_init().\n");)
michael@0 220 }
michael@0 221 }
michael@0 222
michael@0 223 #if TRY_BOUNDS_CHECK
michael@0 224 {
michael@0 225 int i;
michael@0 226 unsigned int tempme = BOUNDS_CHECK_VALUE;
michael@0 227
michael@0 228 x_bounds = vpx_memalign(align, size + (BOUNDS_CHECK_PAD_SIZE * 2));
michael@0 229
michael@0 230 if (x_bounds) {
michael@0 231 /*we're aligning the address twice here but to keep things
michael@0 232 consistent we want to have the padding come before the stored
michael@0 233 address so no matter what free function gets called we will
michael@0 234 attempt to free the correct address*/
michael@0 235 x_bounds = (unsigned char *)(((size_t *)x_bounds)[-1]);
michael@0 236 x = align_addr(x_bounds + BOUNDS_CHECK_PAD_SIZE + ADDRESS_STORAGE_SIZE,
michael@0 237 (int)align);
michael@0 238 /* save the actual malloc address */
michael@0 239 ((size_t *)x)[-1] = (size_t)x_bounds;
michael@0 240
michael@0 241 for (i = 0; i < BOUNDS_CHECK_PAD_SIZE; i += sizeof(unsigned int)) {
michael@0 242 VPX_MEMCPY_L(x_bounds + i, &tempme, sizeof(unsigned int));
michael@0 243 VPX_MEMCPY_L((unsigned char *)x + size + i,
michael@0 244 &tempme, sizeof(unsigned int));
michael@0 245 }
michael@0 246 } else
michael@0 247 x = NULL;
michael@0 248 }
michael@0 249 #else
michael@0 250 x = vpx_memalign(align, size);
michael@0 251 #endif /*TRY_BOUNDS_CHECK*/
michael@0 252
michael@0 253 g_alloc_count++;
michael@0 254
michael@0 255 vpx_memory_tracker_add((size_t)x, (unsigned int)size, file, line, 1);
michael@0 256
michael@0 257 return x;
michael@0 258 }
michael@0 259
michael@0 260 void *xvpx_malloc(size_t size, char *file, int line) {
michael@0 261 return xvpx_memalign(DEFAULT_ALIGNMENT, size, file, line);
michael@0 262 }
michael@0 263
michael@0 264 void *xvpx_calloc(size_t num, size_t size, char *file, int line) {
michael@0 265 void *x = xvpx_memalign(DEFAULT_ALIGNMENT, num * size, file, line);
michael@0 266
michael@0 267 if (x)
michael@0 268 VPX_MEMSET_L(x, 0, num * size);
michael@0 269
michael@0 270 return x;
michael@0 271 }
michael@0 272
michael@0 273 void *xvpx_realloc(void *memblk, size_t size, char *file, int line) {
michael@0 274 struct mem_block *p = NULL;
michael@0 275 int orig_size = 0,
michael@0 276 orig_line = 0;
michael@0 277 char *orig_file = NULL;
michael@0 278
michael@0 279 #if TRY_BOUNDS_CHECK
michael@0 280 unsigned char *x_bounds = memblk ?
michael@0 281 (unsigned char *)(((size_t *)memblk)[-1]) :
michael@0 282 NULL;
michael@0 283 #endif
michael@0 284
michael@0 285 void *x;
michael@0 286
michael@0 287 if (g_alloc_count == 0) {
michael@0 288 #if TRY_BOUNDS_CHECK
michael@0 289
michael@0 290 if (!vpx_memory_tracker_init(BOUNDS_CHECK_PAD_SIZE, BOUNDS_CHECK_VALUE))
michael@0 291 #else
michael@0 292 if (!vpx_memory_tracker_init(0, 0))
michael@0 293 #endif
michael@0 294 {
michael@0 295 _P(printf("ERROR xvpx_malloc MEM_TRACK_USAGE error vpx_memory_tracker_init().\n");)
michael@0 296 }
michael@0 297 }
michael@0 298
michael@0 299 if ((p = vpx_memory_tracker_find((size_t)memblk))) {
michael@0 300 orig_size = p->size;
michael@0 301 orig_file = p->file;
michael@0 302 orig_line = p->line;
michael@0 303 }
michael@0 304
michael@0 305 #if TRY_BOUNDS_CHECK_ON_FREE
michael@0 306 vpx_memory_tracker_check_integrity(file, line);
michael@0 307 #endif
michael@0 308
michael@0 309 /* have to do this regardless of success, because
michael@0 310 * the memory that does get realloc'd may change
michael@0 311 * the bounds values of this block
michael@0 312 */
michael@0 313 vpx_memory_tracker_remove((size_t)memblk);
michael@0 314
michael@0 315 #if TRY_BOUNDS_CHECK
michael@0 316 {
michael@0 317 int i;
michael@0 318 unsigned int tempme = BOUNDS_CHECK_VALUE;
michael@0 319
michael@0 320 x_bounds = vpx_realloc(memblk, size + (BOUNDS_CHECK_PAD_SIZE * 2));
michael@0 321
michael@0 322 if (x_bounds) {
michael@0 323 x_bounds = (unsigned char *)(((size_t *)x_bounds)[-1]);
michael@0 324 x = align_addr(x_bounds + BOUNDS_CHECK_PAD_SIZE + ADDRESS_STORAGE_SIZE,
michael@0 325 (int)DEFAULT_ALIGNMENT);
michael@0 326 /* save the actual malloc address */
michael@0 327 ((size_t *)x)[-1] = (size_t)x_bounds;
michael@0 328
michael@0 329 for (i = 0; i < BOUNDS_CHECK_PAD_SIZE; i += sizeof(unsigned int)) {
michael@0 330 VPX_MEMCPY_L(x_bounds + i, &tempme, sizeof(unsigned int));
michael@0 331 VPX_MEMCPY_L((unsigned char *)x + size + i,
michael@0 332 &tempme, sizeof(unsigned int));
michael@0 333 }
michael@0 334 } else
michael@0 335 x = NULL;
michael@0 336 }
michael@0 337 #else
michael@0 338 x = vpx_realloc(memblk, size);
michael@0 339 #endif /*TRY_BOUNDS_CHECK*/
michael@0 340
michael@0 341 if (!memblk) ++g_alloc_count;
michael@0 342
michael@0 343 if (x)
michael@0 344 vpx_memory_tracker_add((size_t)x, (unsigned int)size, file, line, 1);
michael@0 345 else
michael@0 346 vpx_memory_tracker_add((size_t)memblk, orig_size, orig_file, orig_line, 1);
michael@0 347
michael@0 348 return x;
michael@0 349 }
michael@0 350
michael@0 351 void xvpx_free(void *p_address, char *file, int line) {
michael@0 352 #if TRY_BOUNDS_CHECK
michael@0 353 unsigned char *p_bounds_address = (unsigned char *)p_address;
michael@0 354 /*p_bounds_address -= BOUNDS_CHECK_PAD_SIZE;*/
michael@0 355 #endif
michael@0 356
michael@0 357 #if !TRY_BOUNDS_CHECK_ON_FREE
michael@0 358 (void)file;
michael@0 359 (void)line;
michael@0 360 #endif
michael@0 361
michael@0 362 if (p_address) {
michael@0 363 #if TRY_BOUNDS_CHECK_ON_FREE
michael@0 364 vpx_memory_tracker_check_integrity(file, line);
michael@0 365 #endif
michael@0 366
michael@0 367 /* if the addr isn't found in the list, assume it was allocated via
michael@0 368 * vpx_ calls not xvpx_, therefore it does not contain any padding
michael@0 369 */
michael@0 370 if (vpx_memory_tracker_remove((size_t)p_address) == -2) {
michael@0 371 p_bounds_address = p_address;
michael@0 372 _P(fprintf(stderr, "[vpx_mem][xvpx_free] addr: %p not found in"
michael@0 373 " list; freed from file:%s"
michael@0 374 " line:%d\n", p_address, file, line));
michael@0 375 } else
michael@0 376 --g_alloc_count;
michael@0 377
michael@0 378 #if TRY_BOUNDS_CHECK
michael@0 379 vpx_free(p_bounds_address);
michael@0 380 #else
michael@0 381 vpx_free(p_address);
michael@0 382 #endif
michael@0 383
michael@0 384 if (!g_alloc_count)
michael@0 385 vpx_memory_tracker_destroy();
michael@0 386 }
michael@0 387 }
michael@0 388
michael@0 389 #endif /*CONFIG_MEM_TRACKER*/
michael@0 390
michael@0 391 #if CONFIG_MEM_CHECKS
michael@0 392 #if defined(VXWORKS)
michael@0 393 #include <task_lib.h> /*for task_delay()*/
michael@0 394 /* This function is only used to get a stack trace of the player
michael@0 395 object so we can se where we are having a problem. */
michael@0 396 static int get_my_tt(int task) {
michael@0 397 tt(task);
michael@0 398
michael@0 399 return 0;
michael@0 400 }
michael@0 401
michael@0 402 static void vx_sleep(int msec) {
michael@0 403 int ticks_to_sleep = 0;
michael@0 404
michael@0 405 if (msec) {
michael@0 406 int msec_per_tick = 1000 / sys_clk_rate_get();
michael@0 407
michael@0 408 if (msec < msec_per_tick)
michael@0 409 ticks_to_sleep++;
michael@0 410 else
michael@0 411 ticks_to_sleep = msec / msec_per_tick;
michael@0 412 }
michael@0 413
michael@0 414 task_delay(ticks_to_sleep);
michael@0 415 }
michael@0 416 #endif
michael@0 417 #endif
michael@0 418
michael@0 419 void *vpx_memcpy(void *dest, const void *source, size_t length) {
michael@0 420 #if CONFIG_MEM_CHECKS
michael@0 421
michael@0 422 if (((int)dest < 0x4000) || ((int)source < 0x4000)) {
michael@0 423 _P(printf("WARNING: vpx_memcpy dest:0x%x source:0x%x len:%d\n", (int)dest, (int)source, length);)
michael@0 424
michael@0 425 #if defined(VXWORKS)
michael@0 426 sp(get_my_tt, task_id_self(), 0, 0, 0, 0, 0, 0, 0, 0);
michael@0 427
michael@0 428 vx_sleep(10000);
michael@0 429 #endif
michael@0 430 }
michael@0 431
michael@0 432 #endif
michael@0 433
michael@0 434 return VPX_MEMCPY_L(dest, source, length);
michael@0 435 }
michael@0 436
michael@0 437 void *vpx_memset(void *dest, int val, size_t length) {
michael@0 438 #if CONFIG_MEM_CHECKS
michael@0 439
michael@0 440 if ((int)dest < 0x4000) {
michael@0 441 _P(printf("WARNING: vpx_memset dest:0x%x val:%d len:%d\n", (int)dest, val, length);)
michael@0 442
michael@0 443 #if defined(VXWORKS)
michael@0 444 sp(get_my_tt, task_id_self(), 0, 0, 0, 0, 0, 0, 0, 0);
michael@0 445
michael@0 446 vx_sleep(10000);
michael@0 447 #endif
michael@0 448 }
michael@0 449
michael@0 450 #endif
michael@0 451
michael@0 452 return VPX_MEMSET_L(dest, val, length);
michael@0 453 }
michael@0 454
michael@0 455 void *vpx_memmove(void *dest, const void *src, size_t count) {
michael@0 456 #if CONFIG_MEM_CHECKS
michael@0 457
michael@0 458 if (((int)dest < 0x4000) || ((int)src < 0x4000)) {
michael@0 459 _P(printf("WARNING: vpx_memmove dest:0x%x src:0x%x count:%d\n", (int)dest, (int)src, count);)
michael@0 460
michael@0 461 #if defined(VXWORKS)
michael@0 462 sp(get_my_tt, task_id_self(), 0, 0, 0, 0, 0, 0, 0, 0);
michael@0 463
michael@0 464 vx_sleep(10000);
michael@0 465 #endif
michael@0 466 }
michael@0 467
michael@0 468 #endif
michael@0 469
michael@0 470 return VPX_MEMMOVE_L(dest, src, count);
michael@0 471 }
michael@0 472
michael@0 473 #if CONFIG_MEM_MANAGER
michael@0 474
michael@0 475 static int vpx_mm_create_heap_memory() {
michael@0 476 int i_rv = 0;
michael@0 477
michael@0 478 if (!g_mng_memory_allocated) {
michael@0 479 #if MM_DYNAMIC_MEMORY
michael@0 480 g_p_mng_memory_raw =
michael@0 481 (unsigned char *)malloc(g_mm_memory_size + HMM_ADDR_ALIGN_UNIT);
michael@0 482
michael@0 483 if (g_p_mng_memory_raw) {
michael@0 484 g_p_mng_memory = (unsigned char *)((((unsigned int)g_p_mng_memory_raw) +
michael@0 485 HMM_ADDR_ALIGN_UNIT - 1) &
michael@0 486 -(int)HMM_ADDR_ALIGN_UNIT);
michael@0 487
michael@0 488 _P(printf("[vpx][mm] total memory size:%d g_p_mng_memory_raw:0x%x g_p_mng_memory:0x%x\n"
michael@0 489 , g_mm_memory_size + HMM_ADDR_ALIGN_UNIT
michael@0 490 , (unsigned int)g_p_mng_memory_raw
michael@0 491 , (unsigned int)g_p_mng_memory);)
michael@0 492 } else {
michael@0 493 _P(printf("[vpx][mm] Couldn't allocate memory:%d for vpx memory manager.\n"
michael@0 494 , g_mm_memory_size);)
michael@0 495
michael@0 496 i_rv = -1;
michael@0 497 }
michael@0 498
michael@0 499 if (g_p_mng_memory)
michael@0 500 #endif
michael@0 501 {
michael@0 502 int chunk_size = 0;
michael@0 503
michael@0 504 g_mng_memory_allocated = 1;
michael@0 505
michael@0 506 hmm_init(&hmm_d);
michael@0 507
michael@0 508 chunk_size = g_mm_memory_size >> SHIFT_HMM_ADDR_ALIGN_UNIT;
michael@0 509
michael@0 510 chunk_size -= DUMMY_END_BLOCK_BAUS;
michael@0 511
michael@0 512 _P(printf("[vpx][mm] memory size:%d for vpx memory manager. g_p_mng_memory:0x%x chunk_size:%d\n"
michael@0 513 , g_mm_memory_size
michael@0 514 , (unsigned int)g_p_mng_memory
michael@0 515 , chunk_size);)
michael@0 516
michael@0 517 hmm_new_chunk(&hmm_d, (void *)g_p_mng_memory, chunk_size);
michael@0 518 }
michael@0 519
michael@0 520 #if MM_DYNAMIC_MEMORY
michael@0 521 else {
michael@0 522 _P(printf("[vpx][mm] Couldn't allocate memory:%d for vpx memory manager.\n"
michael@0 523 , g_mm_memory_size);)
michael@0 524
michael@0 525 i_rv = -1;
michael@0 526 }
michael@0 527
michael@0 528 #endif
michael@0 529 }
michael@0 530
michael@0 531 return i_rv;
michael@0 532 }
michael@0 533
michael@0 534 static void *vpx_mm_realloc(void *memblk, size_t size) {
michael@0 535 void *p_ret = NULL;
michael@0 536
michael@0 537 if (vpx_mm_create_heap_memory() < 0) {
michael@0 538 _P(printf("[vpx][mm] ERROR vpx_mm_realloc() Couldn't create memory for Heap.\n");)
michael@0 539 } else {
michael@0 540 int i_rv = 0;
michael@0 541 int old_num_aaus;
michael@0 542 int new_num_aaus;
michael@0 543
michael@0 544 old_num_aaus = hmm_true_size(memblk);
michael@0 545 new_num_aaus = (size >> SHIFT_HMM_ADDR_ALIGN_UNIT) + 1;
michael@0 546
michael@0 547 if (old_num_aaus == new_num_aaus) {
michael@0 548 p_ret = memblk;
michael@0 549 } else {
michael@0 550 i_rv = hmm_resize(&hmm_d, memblk, new_num_aaus);
michael@0 551
michael@0 552 if (i_rv == 0) {
michael@0 553 p_ret = memblk;
michael@0 554 } else {
michael@0 555 /* Error. Try to malloc and then copy data. */
michael@0 556 void *p_from_malloc;
michael@0 557
michael@0 558 new_num_aaus = (size >> SHIFT_HMM_ADDR_ALIGN_UNIT) + 1;
michael@0 559 p_from_malloc = hmm_alloc(&hmm_d, new_num_aaus);
michael@0 560
michael@0 561 if (p_from_malloc) {
michael@0 562 vpx_memcpy(p_from_malloc, memblk, size);
michael@0 563 hmm_free(&hmm_d, memblk);
michael@0 564
michael@0 565 p_ret = p_from_malloc;
michael@0 566 }
michael@0 567 }
michael@0 568 }
michael@0 569 }
michael@0 570
michael@0 571 return p_ret;
michael@0 572 }
michael@0 573 #endif /*CONFIG_MEM_MANAGER*/
michael@0 574
michael@0 575 #if USE_GLOBAL_FUNCTION_POINTERS
michael@0 576 # if CONFIG_MEM_TRACKER
michael@0 577 extern int vpx_memory_tracker_set_functions(g_malloc_func g_malloc_l
michael@0 578 , g_calloc_func g_calloc_l
michael@0 579 , g_realloc_func g_realloc_l
michael@0 580 , g_free_func g_free_l
michael@0 581 , g_memcpy_func g_memcpy_l
michael@0 582 , g_memset_func g_memset_l
michael@0 583 , g_memmove_func g_memmove_l);
michael@0 584 # endif
michael@0 585 #endif /*USE_GLOBAL_FUNCTION_POINTERS*/
michael@0 586 int vpx_mem_set_functions(g_malloc_func g_malloc_l
michael@0 587 , g_calloc_func g_calloc_l
michael@0 588 , g_realloc_func g_realloc_l
michael@0 589 , g_free_func g_free_l
michael@0 590 , g_memcpy_func g_memcpy_l
michael@0 591 , g_memset_func g_memset_l
michael@0 592 , g_memmove_func g_memmove_l) {
michael@0 593 #if USE_GLOBAL_FUNCTION_POINTERS
michael@0 594
michael@0 595 /* If use global functions is turned on then the
michael@0 596 application must set the global functions before
michael@0 597 it does anything else or vpx_mem will have
michael@0 598 unpredictable results. */
michael@0 599 if (!g_func) {
michael@0 600 g_func = (struct GLOBAL_FUNC_POINTERS *)
michael@0 601 g_malloc_l(sizeof(struct GLOBAL_FUNC_POINTERS));
michael@0 602
michael@0 603 if (!g_func) {
michael@0 604 return -1;
michael@0 605 }
michael@0 606 }
michael@0 607
michael@0 608 #if CONFIG_MEM_TRACKER
michael@0 609 {
michael@0 610 int rv = 0;
michael@0 611 rv = vpx_memory_tracker_set_functions(g_malloc_l
michael@0 612 , g_calloc_l
michael@0 613 , g_realloc_l
michael@0 614 , g_free_l
michael@0 615 , g_memcpy_l
michael@0 616 , g_memset_l
michael@0 617 , g_memmove_l);
michael@0 618
michael@0 619 if (rv < 0) {
michael@0 620 return rv;
michael@0 621 }
michael@0 622 }
michael@0 623 #endif
michael@0 624
michael@0 625 g_func->g_malloc = g_malloc_l;
michael@0 626 g_func->g_calloc = g_calloc_l;
michael@0 627 g_func->g_realloc = g_realloc_l;
michael@0 628 g_func->g_free = g_free_l;
michael@0 629 g_func->g_memcpy = g_memcpy_l;
michael@0 630 g_func->g_memset = g_memset_l;
michael@0 631 g_func->g_memmove = g_memmove_l;
michael@0 632
michael@0 633 return 0;
michael@0 634 #else
michael@0 635 (void)g_malloc_l;
michael@0 636 (void)g_calloc_l;
michael@0 637 (void)g_realloc_l;
michael@0 638 (void)g_free_l;
michael@0 639 (void)g_memcpy_l;
michael@0 640 (void)g_memset_l;
michael@0 641 (void)g_memmove_l;
michael@0 642 return -1;
michael@0 643 #endif
michael@0 644 }
michael@0 645
michael@0 646 int vpx_mem_unset_functions() {
michael@0 647 #if USE_GLOBAL_FUNCTION_POINTERS
michael@0 648
michael@0 649 if (g_func) {
michael@0 650 g_free_func temp_free = g_func->g_free;
michael@0 651 temp_free(g_func);
michael@0 652 g_func = NULL;
michael@0 653 }
michael@0 654
michael@0 655 #endif
michael@0 656 return 0;
michael@0 657 }

mercurial