media/libvpx/vpx_mem/vpx_mem.c

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/media/libvpx/vpx_mem/vpx_mem.c	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,657 @@
     1.4 +/*
     1.5 + *  Copyright (c) 2010 The WebM project authors. All Rights Reserved.
     1.6 + *
     1.7 + *  Use of this source code is governed by a BSD-style license
     1.8 + *  that can be found in the LICENSE file in the root of the source
     1.9 + *  tree. An additional intellectual property rights grant can be found
    1.10 + *  in the file PATENTS.  All contributing project authors may
    1.11 + *  be found in the AUTHORS file in the root of the source tree.
    1.12 + */
    1.13 +
    1.14 +
    1.15 +#define __VPX_MEM_C__
    1.16 +
    1.17 +#include "vpx_mem.h"
    1.18 +#include <stdio.h>
    1.19 +#include <stdlib.h>
    1.20 +#include <string.h>
    1.21 +#include "include/vpx_mem_intrnl.h"
    1.22 +
    1.23 +#if CONFIG_MEM_TRACKER
    1.24 +#ifndef VPX_NO_GLOBALS
    1.25 +static unsigned long g_alloc_count = 0;
    1.26 +#else
    1.27 +#include "vpx_global_handling.h"
    1.28 +#define g_alloc_count vpxglobalm(vpxmem,g_alloc_count)
    1.29 +#endif
    1.30 +#endif
    1.31 +
    1.32 +#if CONFIG_MEM_MANAGER
    1.33 +# include "heapmm.h"
    1.34 +# include "hmm_intrnl.h"
    1.35 +
    1.36 +# define SHIFT_HMM_ADDR_ALIGN_UNIT 5
    1.37 +# define TOTAL_MEMORY_TO_ALLOCATE  20971520 /* 20 * 1024 * 1024 */
    1.38 +
    1.39 +# define MM_DYNAMIC_MEMORY 1
    1.40 +# if MM_DYNAMIC_MEMORY
    1.41 +static unsigned char *g_p_mng_memory_raw = NULL;
    1.42 +static unsigned char *g_p_mng_memory     = NULL;
    1.43 +# else
    1.44 +static unsigned char g_p_mng_memory[TOTAL_MEMORY_TO_ALLOCATE];
    1.45 +# endif
    1.46 +
    1.47 +static size_t g_mm_memory_size = TOTAL_MEMORY_TO_ALLOCATE;
    1.48 +
    1.49 +static hmm_descriptor hmm_d;
    1.50 +static int g_mng_memory_allocated = 0;
    1.51 +
    1.52 +static int vpx_mm_create_heap_memory();
    1.53 +static void *vpx_mm_realloc(void *memblk, size_t size);
    1.54 +#endif /*CONFIG_MEM_MANAGER*/
    1.55 +
    1.56 +#if USE_GLOBAL_FUNCTION_POINTERS
    1.57 +struct GLOBAL_FUNC_POINTERS {
    1.58 +  g_malloc_func g_malloc;
    1.59 +  g_calloc_func g_calloc;
    1.60 +  g_realloc_func g_realloc;
    1.61 +  g_free_func g_free;
    1.62 +  g_memcpy_func g_memcpy;
    1.63 +  g_memset_func g_memset;
    1.64 +  g_memmove_func g_memmove;
    1.65 +} *g_func = NULL;
    1.66 +
    1.67 +# define VPX_MALLOC_L  g_func->g_malloc
    1.68 +# define VPX_REALLOC_L g_func->g_realloc
    1.69 +# define VPX_FREE_L    g_func->g_free
    1.70 +# define VPX_MEMCPY_L  g_func->g_memcpy
    1.71 +# define VPX_MEMSET_L  g_func->g_memset
    1.72 +# define VPX_MEMMOVE_L g_func->g_memmove
    1.73 +#else
    1.74 +# define VPX_MALLOC_L  malloc
    1.75 +# define VPX_REALLOC_L realloc
    1.76 +# define VPX_FREE_L    free
    1.77 +# define VPX_MEMCPY_L  memcpy
    1.78 +# define VPX_MEMSET_L  memset
    1.79 +# define VPX_MEMMOVE_L memmove
    1.80 +#endif /* USE_GLOBAL_FUNCTION_POINTERS */
    1.81 +
    1.82 +unsigned int vpx_mem_get_version() {
    1.83 +  unsigned int ver = ((unsigned int)(unsigned char)VPX_MEM_VERSION_CHIEF << 24 |
    1.84 +                      (unsigned int)(unsigned char)VPX_MEM_VERSION_MAJOR << 16 |
    1.85 +                      (unsigned int)(unsigned char)VPX_MEM_VERSION_MINOR << 8  |
    1.86 +                      (unsigned int)(unsigned char)VPX_MEM_VERSION_PATCH);
    1.87 +  return ver;
    1.88 +}
    1.89 +
    1.90 +int vpx_mem_set_heap_size(size_t size) {
    1.91 +  int ret = -1;
    1.92 +
    1.93 +#if CONFIG_MEM_MANAGER
    1.94 +#if MM_DYNAMIC_MEMORY
    1.95 +
    1.96 +  if (!g_mng_memory_allocated && size) {
    1.97 +    g_mm_memory_size = size;
    1.98 +    ret = 0;
    1.99 +  } else
   1.100 +    ret = -3;
   1.101 +
   1.102 +#else
   1.103 +  ret = -2;
   1.104 +#endif
   1.105 +#else
   1.106 +  (void)size;
   1.107 +#endif
   1.108 +
   1.109 +  return ret;
   1.110 +}
   1.111 +
   1.112 +void *vpx_memalign(size_t align, size_t size) {
   1.113 +  void *addr,
   1.114 +       * x = NULL;
   1.115 +
   1.116 +#if CONFIG_MEM_MANAGER
   1.117 +  int number_aau;
   1.118 +
   1.119 +  if (vpx_mm_create_heap_memory() < 0) {
   1.120 +    _P(printf("[vpx][mm] ERROR vpx_memalign() Couldn't create memory for Heap.\n");)
   1.121 +  }
   1.122 +
   1.123 +  number_aau = ((size + align - 1 + ADDRESS_STORAGE_SIZE) >>
   1.124 +                SHIFT_HMM_ADDR_ALIGN_UNIT) + 1;
   1.125 +
   1.126 +  addr = hmm_alloc(&hmm_d, number_aau);
   1.127 +#else
   1.128 +  addr = VPX_MALLOC_L(size + align - 1 + ADDRESS_STORAGE_SIZE);
   1.129 +#endif /*CONFIG_MEM_MANAGER*/
   1.130 +
   1.131 +  if (addr) {
   1.132 +    x = align_addr((unsigned char *)addr + ADDRESS_STORAGE_SIZE, (int)align);
   1.133 +    /* save the actual malloc address */
   1.134 +    ((size_t *)x)[-1] = (size_t)addr;
   1.135 +  }
   1.136 +
   1.137 +  return x;
   1.138 +}
   1.139 +
   1.140 +void *vpx_malloc(size_t size) {
   1.141 +  return vpx_memalign(DEFAULT_ALIGNMENT, size);
   1.142 +}
   1.143 +
   1.144 +void *vpx_calloc(size_t num, size_t size) {
   1.145 +  void *x;
   1.146 +
   1.147 +  x = vpx_memalign(DEFAULT_ALIGNMENT, num * size);
   1.148 +
   1.149 +  if (x)
   1.150 +    VPX_MEMSET_L(x, 0, num * size);
   1.151 +
   1.152 +  return x;
   1.153 +}
   1.154 +
   1.155 +void *vpx_realloc(void *memblk, size_t size) {
   1.156 +  void *addr,
   1.157 +       * new_addr = NULL;
   1.158 +  int align = DEFAULT_ALIGNMENT;
   1.159 +
   1.160 +  /*
   1.161 +  The realloc() function changes the size of the object pointed to by
   1.162 +  ptr to the size specified by size, and returns a pointer to the
   1.163 +  possibly moved block. The contents are unchanged up to the lesser
   1.164 +  of the new and old sizes. If ptr is null, realloc() behaves like
   1.165 +  malloc() for the specified size. If size is zero (0) and ptr is
   1.166 +  not a null pointer, the object pointed to is freed.
   1.167 +  */
   1.168 +  if (!memblk)
   1.169 +    new_addr = vpx_malloc(size);
   1.170 +  else if (!size)
   1.171 +    vpx_free(memblk);
   1.172 +  else {
   1.173 +    addr   = (void *)(((size_t *)memblk)[-1]);
   1.174 +    memblk = NULL;
   1.175 +
   1.176 +#if CONFIG_MEM_MANAGER
   1.177 +    new_addr = vpx_mm_realloc(addr, size + align + ADDRESS_STORAGE_SIZE);
   1.178 +#else
   1.179 +    new_addr = VPX_REALLOC_L(addr, size + align + ADDRESS_STORAGE_SIZE);
   1.180 +#endif
   1.181 +
   1.182 +    if (new_addr) {
   1.183 +      addr = new_addr;
   1.184 +      new_addr = (void *)(((size_t)
   1.185 +                           ((unsigned char *)new_addr + ADDRESS_STORAGE_SIZE) + (align - 1)) &
   1.186 +                          (size_t) - align);
   1.187 +      /* save the actual malloc address */
   1.188 +      ((size_t *)new_addr)[-1] = (size_t)addr;
   1.189 +    }
   1.190 +  }
   1.191 +
   1.192 +  return new_addr;
   1.193 +}
   1.194 +
   1.195 +void vpx_free(void *memblk) {
   1.196 +  if (memblk) {
   1.197 +    void *addr = (void *)(((size_t *)memblk)[-1]);
   1.198 +#if CONFIG_MEM_MANAGER
   1.199 +    hmm_free(&hmm_d, addr);
   1.200 +#else
   1.201 +    VPX_FREE_L(addr);
   1.202 +#endif
   1.203 +  }
   1.204 +}
   1.205 +
   1.206 +#if CONFIG_MEM_TRACKER
   1.207 +void *xvpx_memalign(size_t align, size_t size, char *file, int line) {
   1.208 +#if TRY_BOUNDS_CHECK
   1.209 +  unsigned char *x_bounds;
   1.210 +#endif
   1.211 +
   1.212 +  void *x;
   1.213 +
   1.214 +  if (g_alloc_count == 0) {
   1.215 +#if TRY_BOUNDS_CHECK
   1.216 +    int i_rv = vpx_memory_tracker_init(BOUNDS_CHECK_PAD_SIZE, BOUNDS_CHECK_VALUE);
   1.217 +#else
   1.218 +    int i_rv = vpx_memory_tracker_init(0, 0);
   1.219 +#endif
   1.220 +
   1.221 +    if (i_rv < 0) {
   1.222 +      _P(printf("ERROR xvpx_malloc MEM_TRACK_USAGE error vpx_memory_tracker_init().\n");)
   1.223 +    }
   1.224 +  }
   1.225 +
   1.226 +#if TRY_BOUNDS_CHECK
   1.227 +  {
   1.228 +    int i;
   1.229 +    unsigned int tempme = BOUNDS_CHECK_VALUE;
   1.230 +
   1.231 +    x_bounds = vpx_memalign(align, size + (BOUNDS_CHECK_PAD_SIZE * 2));
   1.232 +
   1.233 +    if (x_bounds) {
   1.234 +      /*we're aligning the address twice here but to keep things
   1.235 +        consistent we want to have the padding come before the stored
   1.236 +        address so no matter what free function gets called we will
   1.237 +        attempt to free the correct address*/
   1.238 +      x_bounds = (unsigned char *)(((size_t *)x_bounds)[-1]);
   1.239 +      x = align_addr(x_bounds + BOUNDS_CHECK_PAD_SIZE + ADDRESS_STORAGE_SIZE,
   1.240 +                     (int)align);
   1.241 +      /* save the actual malloc address */
   1.242 +      ((size_t *)x)[-1] = (size_t)x_bounds;
   1.243 +
   1.244 +      for (i = 0; i < BOUNDS_CHECK_PAD_SIZE; i += sizeof(unsigned int)) {
   1.245 +        VPX_MEMCPY_L(x_bounds + i, &tempme, sizeof(unsigned int));
   1.246 +        VPX_MEMCPY_L((unsigned char *)x + size + i,
   1.247 +                     &tempme, sizeof(unsigned int));
   1.248 +      }
   1.249 +    } else
   1.250 +      x = NULL;
   1.251 +  }
   1.252 +#else
   1.253 +  x = vpx_memalign(align, size);
   1.254 +#endif /*TRY_BOUNDS_CHECK*/
   1.255 +
   1.256 +  g_alloc_count++;
   1.257 +
   1.258 +  vpx_memory_tracker_add((size_t)x, (unsigned int)size, file, line, 1);
   1.259 +
   1.260 +  return x;
   1.261 +}
   1.262 +
   1.263 +void *xvpx_malloc(size_t size, char *file, int line) {
   1.264 +  return xvpx_memalign(DEFAULT_ALIGNMENT, size, file, line);
   1.265 +}
   1.266 +
   1.267 +void *xvpx_calloc(size_t num, size_t size, char *file, int line) {
   1.268 +  void *x = xvpx_memalign(DEFAULT_ALIGNMENT, num * size, file, line);
   1.269 +
   1.270 +  if (x)
   1.271 +    VPX_MEMSET_L(x, 0, num * size);
   1.272 +
   1.273 +  return x;
   1.274 +}
   1.275 +
   1.276 +void *xvpx_realloc(void *memblk, size_t size, char *file, int line) {
   1.277 +  struct mem_block *p = NULL;
   1.278 +  int orig_size = 0,
   1.279 +      orig_line = 0;
   1.280 +  char *orig_file = NULL;
   1.281 +
   1.282 +#if TRY_BOUNDS_CHECK
   1.283 +  unsigned char *x_bounds = memblk ?
   1.284 +                            (unsigned char *)(((size_t *)memblk)[-1]) :
   1.285 +                            NULL;
   1.286 +#endif
   1.287 +
   1.288 +  void *x;
   1.289 +
   1.290 +  if (g_alloc_count == 0) {
   1.291 +#if TRY_BOUNDS_CHECK
   1.292 +
   1.293 +    if (!vpx_memory_tracker_init(BOUNDS_CHECK_PAD_SIZE, BOUNDS_CHECK_VALUE))
   1.294 +#else
   1.295 +    if (!vpx_memory_tracker_init(0, 0))
   1.296 +#endif
   1.297 +    {
   1.298 +      _P(printf("ERROR xvpx_malloc MEM_TRACK_USAGE error vpx_memory_tracker_init().\n");)
   1.299 +    }
   1.300 +  }
   1.301 +
   1.302 +  if ((p = vpx_memory_tracker_find((size_t)memblk))) {
   1.303 +    orig_size = p->size;
   1.304 +    orig_file = p->file;
   1.305 +    orig_line = p->line;
   1.306 +  }
   1.307 +
   1.308 +#if TRY_BOUNDS_CHECK_ON_FREE
   1.309 +  vpx_memory_tracker_check_integrity(file, line);
   1.310 +#endif
   1.311 +
   1.312 +  /* have to do this regardless of success, because
   1.313 +   * the memory that does get realloc'd may change
   1.314 +   * the bounds values of this block
   1.315 +   */
   1.316 +  vpx_memory_tracker_remove((size_t)memblk);
   1.317 +
   1.318 +#if TRY_BOUNDS_CHECK
   1.319 +  {
   1.320 +    int i;
   1.321 +    unsigned int tempme = BOUNDS_CHECK_VALUE;
   1.322 +
   1.323 +    x_bounds = vpx_realloc(memblk, size + (BOUNDS_CHECK_PAD_SIZE * 2));
   1.324 +
   1.325 +    if (x_bounds) {
   1.326 +      x_bounds = (unsigned char *)(((size_t *)x_bounds)[-1]);
   1.327 +      x = align_addr(x_bounds + BOUNDS_CHECK_PAD_SIZE + ADDRESS_STORAGE_SIZE,
   1.328 +                     (int)DEFAULT_ALIGNMENT);
   1.329 +      /* save the actual malloc address */
   1.330 +      ((size_t *)x)[-1] = (size_t)x_bounds;
   1.331 +
   1.332 +      for (i = 0; i < BOUNDS_CHECK_PAD_SIZE; i += sizeof(unsigned int)) {
   1.333 +        VPX_MEMCPY_L(x_bounds + i, &tempme, sizeof(unsigned int));
   1.334 +        VPX_MEMCPY_L((unsigned char *)x + size + i,
   1.335 +                     &tempme, sizeof(unsigned int));
   1.336 +      }
   1.337 +    } else
   1.338 +      x = NULL;
   1.339 +  }
   1.340 +#else
   1.341 +  x = vpx_realloc(memblk, size);
   1.342 +#endif /*TRY_BOUNDS_CHECK*/
   1.343 +
   1.344 +  if (!memblk) ++g_alloc_count;
   1.345 +
   1.346 +  if (x)
   1.347 +    vpx_memory_tracker_add((size_t)x, (unsigned int)size, file, line, 1);
   1.348 +  else
   1.349 +    vpx_memory_tracker_add((size_t)memblk, orig_size, orig_file, orig_line, 1);
   1.350 +
   1.351 +  return x;
   1.352 +}
   1.353 +
   1.354 +void xvpx_free(void *p_address, char *file, int line) {
   1.355 +#if TRY_BOUNDS_CHECK
   1.356 +  unsigned char *p_bounds_address = (unsigned char *)p_address;
   1.357 +  /*p_bounds_address -= BOUNDS_CHECK_PAD_SIZE;*/
   1.358 +#endif
   1.359 +
   1.360 +#if !TRY_BOUNDS_CHECK_ON_FREE
   1.361 +  (void)file;
   1.362 +  (void)line;
   1.363 +#endif
   1.364 +
   1.365 +  if (p_address) {
   1.366 +#if TRY_BOUNDS_CHECK_ON_FREE
   1.367 +    vpx_memory_tracker_check_integrity(file, line);
   1.368 +#endif
   1.369 +
   1.370 +    /* if the addr isn't found in the list, assume it was allocated via
   1.371 +     * vpx_ calls not xvpx_, therefore it does not contain any padding
   1.372 +     */
   1.373 +    if (vpx_memory_tracker_remove((size_t)p_address) == -2) {
   1.374 +      p_bounds_address = p_address;
   1.375 +      _P(fprintf(stderr, "[vpx_mem][xvpx_free] addr: %p not found in"
   1.376 +                 " list; freed from file:%s"
   1.377 +                 " line:%d\n", p_address, file, line));
   1.378 +    } else
   1.379 +      --g_alloc_count;
   1.380 +
   1.381 +#if TRY_BOUNDS_CHECK
   1.382 +    vpx_free(p_bounds_address);
   1.383 +#else
   1.384 +    vpx_free(p_address);
   1.385 +#endif
   1.386 +
   1.387 +    if (!g_alloc_count)
   1.388 +      vpx_memory_tracker_destroy();
   1.389 +  }
   1.390 +}
   1.391 +
   1.392 +#endif /*CONFIG_MEM_TRACKER*/
   1.393 +
   1.394 +#if CONFIG_MEM_CHECKS
   1.395 +#if defined(VXWORKS)
   1.396 +#include <task_lib.h> /*for task_delay()*/
   1.397 +/* This function is only used to get a stack trace of the player
   1.398 +object so we can se where we are having a problem. */
   1.399 +static int get_my_tt(int task) {
   1.400 +  tt(task);
   1.401 +
   1.402 +  return 0;
   1.403 +}
   1.404 +
   1.405 +static void vx_sleep(int msec) {
   1.406 +  int ticks_to_sleep = 0;
   1.407 +
   1.408 +  if (msec) {
   1.409 +    int msec_per_tick = 1000 / sys_clk_rate_get();
   1.410 +
   1.411 +    if (msec < msec_per_tick)
   1.412 +      ticks_to_sleep++;
   1.413 +    else
   1.414 +      ticks_to_sleep = msec / msec_per_tick;
   1.415 +  }
   1.416 +
   1.417 +  task_delay(ticks_to_sleep);
   1.418 +}
   1.419 +#endif
   1.420 +#endif
   1.421 +
   1.422 +void *vpx_memcpy(void *dest, const void *source, size_t length) {
   1.423 +#if CONFIG_MEM_CHECKS
   1.424 +
   1.425 +  if (((int)dest < 0x4000) || ((int)source < 0x4000)) {
   1.426 +    _P(printf("WARNING: vpx_memcpy dest:0x%x source:0x%x len:%d\n", (int)dest, (int)source, length);)
   1.427 +
   1.428 +#if defined(VXWORKS)
   1.429 +    sp(get_my_tt, task_id_self(), 0, 0, 0, 0, 0, 0, 0, 0);
   1.430 +
   1.431 +    vx_sleep(10000);
   1.432 +#endif
   1.433 +  }
   1.434 +
   1.435 +#endif
   1.436 +
   1.437 +  return VPX_MEMCPY_L(dest, source, length);
   1.438 +}
   1.439 +
   1.440 +void *vpx_memset(void *dest, int val, size_t length) {
   1.441 +#if CONFIG_MEM_CHECKS
   1.442 +
   1.443 +  if ((int)dest < 0x4000) {
   1.444 +    _P(printf("WARNING: vpx_memset dest:0x%x val:%d len:%d\n", (int)dest, val, length);)
   1.445 +
   1.446 +#if defined(VXWORKS)
   1.447 +    sp(get_my_tt, task_id_self(), 0, 0, 0, 0, 0, 0, 0, 0);
   1.448 +
   1.449 +    vx_sleep(10000);
   1.450 +#endif
   1.451 +  }
   1.452 +
   1.453 +#endif
   1.454 +
   1.455 +  return VPX_MEMSET_L(dest, val, length);
   1.456 +}
   1.457 +
   1.458 +void *vpx_memmove(void *dest, const void *src, size_t count) {
   1.459 +#if CONFIG_MEM_CHECKS
   1.460 +
   1.461 +  if (((int)dest < 0x4000) || ((int)src < 0x4000)) {
   1.462 +    _P(printf("WARNING: vpx_memmove dest:0x%x src:0x%x count:%d\n", (int)dest, (int)src, count);)
   1.463 +
   1.464 +#if defined(VXWORKS)
   1.465 +    sp(get_my_tt, task_id_self(), 0, 0, 0, 0, 0, 0, 0, 0);
   1.466 +
   1.467 +    vx_sleep(10000);
   1.468 +#endif
   1.469 +  }
   1.470 +
   1.471 +#endif
   1.472 +
   1.473 +  return VPX_MEMMOVE_L(dest, src, count);
   1.474 +}
   1.475 +
   1.476 +#if CONFIG_MEM_MANAGER
   1.477 +
   1.478 +static int vpx_mm_create_heap_memory() {
   1.479 +  int i_rv = 0;
   1.480 +
   1.481 +  if (!g_mng_memory_allocated) {
   1.482 +#if MM_DYNAMIC_MEMORY
   1.483 +    g_p_mng_memory_raw =
   1.484 +      (unsigned char *)malloc(g_mm_memory_size + HMM_ADDR_ALIGN_UNIT);
   1.485 +
   1.486 +    if (g_p_mng_memory_raw) {
   1.487 +      g_p_mng_memory = (unsigned char *)((((unsigned int)g_p_mng_memory_raw) +
   1.488 +                                          HMM_ADDR_ALIGN_UNIT - 1) &
   1.489 +                                         -(int)HMM_ADDR_ALIGN_UNIT);
   1.490 +
   1.491 +      _P(printf("[vpx][mm] total memory size:%d g_p_mng_memory_raw:0x%x g_p_mng_memory:0x%x\n"
   1.492 +, g_mm_memory_size + HMM_ADDR_ALIGN_UNIT
   1.493 +, (unsigned int)g_p_mng_memory_raw
   1.494 +, (unsigned int)g_p_mng_memory);)
   1.495 +    } else {
   1.496 +      _P(printf("[vpx][mm] Couldn't allocate memory:%d for vpx memory manager.\n"
   1.497 +, g_mm_memory_size);)
   1.498 +
   1.499 +      i_rv = -1;
   1.500 +    }
   1.501 +
   1.502 +    if (g_p_mng_memory)
   1.503 +#endif
   1.504 +    {
   1.505 +      int chunk_size = 0;
   1.506 +
   1.507 +      g_mng_memory_allocated = 1;
   1.508 +
   1.509 +      hmm_init(&hmm_d);
   1.510 +
   1.511 +      chunk_size = g_mm_memory_size >> SHIFT_HMM_ADDR_ALIGN_UNIT;
   1.512 +
   1.513 +      chunk_size -= DUMMY_END_BLOCK_BAUS;
   1.514 +
   1.515 +      _P(printf("[vpx][mm] memory size:%d for vpx memory manager. g_p_mng_memory:0x%x  chunk_size:%d\n"
   1.516 +, g_mm_memory_size
   1.517 +, (unsigned int)g_p_mng_memory
   1.518 +, chunk_size);)
   1.519 +
   1.520 +      hmm_new_chunk(&hmm_d, (void *)g_p_mng_memory, chunk_size);
   1.521 +    }
   1.522 +
   1.523 +#if MM_DYNAMIC_MEMORY
   1.524 +    else {
   1.525 +      _P(printf("[vpx][mm] Couldn't allocate memory:%d for vpx memory manager.\n"
   1.526 +, g_mm_memory_size);)
   1.527 +
   1.528 +      i_rv = -1;
   1.529 +    }
   1.530 +
   1.531 +#endif
   1.532 +  }
   1.533 +
   1.534 +  return i_rv;
   1.535 +}
   1.536 +
   1.537 +static void *vpx_mm_realloc(void *memblk, size_t size) {
   1.538 +  void *p_ret = NULL;
   1.539 +
   1.540 +  if (vpx_mm_create_heap_memory() < 0) {
   1.541 +    _P(printf("[vpx][mm] ERROR vpx_mm_realloc() Couldn't create memory for Heap.\n");)
   1.542 +  } else {
   1.543 +    int i_rv = 0;
   1.544 +    int old_num_aaus;
   1.545 +    int new_num_aaus;
   1.546 +
   1.547 +    old_num_aaus = hmm_true_size(memblk);
   1.548 +    new_num_aaus = (size >> SHIFT_HMM_ADDR_ALIGN_UNIT) + 1;
   1.549 +
   1.550 +    if (old_num_aaus == new_num_aaus) {
   1.551 +      p_ret = memblk;
   1.552 +    } else {
   1.553 +      i_rv = hmm_resize(&hmm_d, memblk, new_num_aaus);
   1.554 +
   1.555 +      if (i_rv == 0) {
   1.556 +        p_ret = memblk;
   1.557 +      } else {
   1.558 +        /* Error. Try to malloc and then copy data. */
   1.559 +        void *p_from_malloc;
   1.560 +
   1.561 +        new_num_aaus = (size >> SHIFT_HMM_ADDR_ALIGN_UNIT) + 1;
   1.562 +        p_from_malloc  = hmm_alloc(&hmm_d, new_num_aaus);
   1.563 +
   1.564 +        if (p_from_malloc) {
   1.565 +          vpx_memcpy(p_from_malloc, memblk, size);
   1.566 +          hmm_free(&hmm_d, memblk);
   1.567 +
   1.568 +          p_ret = p_from_malloc;
   1.569 +        }
   1.570 +      }
   1.571 +    }
   1.572 +  }
   1.573 +
   1.574 +  return p_ret;
   1.575 +}
   1.576 +#endif /*CONFIG_MEM_MANAGER*/
   1.577 +
   1.578 +#if USE_GLOBAL_FUNCTION_POINTERS
   1.579 +# if CONFIG_MEM_TRACKER
   1.580 +extern int vpx_memory_tracker_set_functions(g_malloc_func g_malloc_l
   1.581 +, g_calloc_func g_calloc_l
   1.582 +, g_realloc_func g_realloc_l
   1.583 +, g_free_func g_free_l
   1.584 +, g_memcpy_func g_memcpy_l
   1.585 +, g_memset_func g_memset_l
   1.586 +, g_memmove_func g_memmove_l);
   1.587 +# endif
   1.588 +#endif /*USE_GLOBAL_FUNCTION_POINTERS*/
   1.589 +int vpx_mem_set_functions(g_malloc_func g_malloc_l
   1.590 +, g_calloc_func g_calloc_l
   1.591 +, g_realloc_func g_realloc_l
   1.592 +, g_free_func g_free_l
   1.593 +, g_memcpy_func g_memcpy_l
   1.594 +, g_memset_func g_memset_l
   1.595 +, g_memmove_func g_memmove_l) {
   1.596 +#if USE_GLOBAL_FUNCTION_POINTERS
   1.597 +
   1.598 +  /* If use global functions is turned on then the
   1.599 +  application must set the global functions before
   1.600 +  it does anything else or vpx_mem will have
   1.601 +  unpredictable results. */
   1.602 +  if (!g_func) {
   1.603 +    g_func = (struct GLOBAL_FUNC_POINTERS *)
   1.604 +             g_malloc_l(sizeof(struct GLOBAL_FUNC_POINTERS));
   1.605 +
   1.606 +    if (!g_func) {
   1.607 +      return -1;
   1.608 +    }
   1.609 +  }
   1.610 +
   1.611 +#if CONFIG_MEM_TRACKER
   1.612 +  {
   1.613 +    int rv = 0;
   1.614 +    rv = vpx_memory_tracker_set_functions(g_malloc_l
   1.615 +, g_calloc_l
   1.616 +, g_realloc_l
   1.617 +, g_free_l
   1.618 +, g_memcpy_l
   1.619 +, g_memset_l
   1.620 +, g_memmove_l);
   1.621 +
   1.622 +    if (rv < 0) {
   1.623 +      return rv;
   1.624 +    }
   1.625 +  }
   1.626 +#endif
   1.627 +
   1.628 +  g_func->g_malloc  = g_malloc_l;
   1.629 +  g_func->g_calloc  = g_calloc_l;
   1.630 +  g_func->g_realloc = g_realloc_l;
   1.631 +  g_func->g_free    = g_free_l;
   1.632 +  g_func->g_memcpy  = g_memcpy_l;
   1.633 +  g_func->g_memset  = g_memset_l;
   1.634 +  g_func->g_memmove = g_memmove_l;
   1.635 +
   1.636 +  return 0;
   1.637 +#else
   1.638 +  (void)g_malloc_l;
   1.639 +  (void)g_calloc_l;
   1.640 +  (void)g_realloc_l;
   1.641 +  (void)g_free_l;
   1.642 +  (void)g_memcpy_l;
   1.643 +  (void)g_memset_l;
   1.644 +  (void)g_memmove_l;
   1.645 +  return -1;
   1.646 +#endif
   1.647 +}
   1.648 +
   1.649 +int vpx_mem_unset_functions() {
   1.650 +#if USE_GLOBAL_FUNCTION_POINTERS
   1.651 +
   1.652 +  if (g_func) {
   1.653 +    g_free_func temp_free = g_func->g_free;
   1.654 +    temp_free(g_func);
   1.655 +    g_func = NULL;
   1.656 +  }
   1.657 +
   1.658 +#endif
   1.659 +  return 0;
   1.660 +}

mercurial