aom_mem.c 2.99 KB
Newer Older
John Koleszar's avatar
John Koleszar committed
1
/*
Yaowu Xu's avatar
Yaowu Xu committed
2
 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
John Koleszar's avatar
John Koleszar committed
3
 *
Yaowu Xu's avatar
Yaowu Xu committed
4 5 6 7 8 9
 * This source code is subject to the terms of the BSD 2 Clause License and
 * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
 * was not distributed with this source code in the LICENSE file, you can
 * obtain it at www.aomedia.org/license/software. If the Alliance for Open
 * Media Patent License 1.0 was not distributed with this source code in the
 * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
John Koleszar's avatar
John Koleszar committed
10 11
 */

Yaowu Xu's avatar
Yaowu Xu committed
12 13 14
#define __AOM_MEM_C__

#include "aom_mem.h"
John Koleszar's avatar
John Koleszar committed
15 16 17
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Yaowu Xu's avatar
Yaowu Xu committed
18 19
#include "include/aom_mem_intrnl.h"
#include "aom/aom_integer.h"
John Koleszar's avatar
John Koleszar committed
20

Urvang Joshi's avatar
Urvang Joshi committed
21 22 23 24 25 26 27 28 29
static inline size_t *GetMallocAddressLocation(void *const mem) {
  return ((size_t *)mem) - 1;
}

static inline void SetActualMallocAddress(void *const mem,
                                          const void *const malloc_addr) {
  size_t *const malloc_addr_location = GetMallocAddressLocation(mem);
  *malloc_addr_location = (size_t)malloc_addr;
}
John Koleszar's avatar
John Koleszar committed
30

Urvang Joshi's avatar
Urvang Joshi committed
31 32 33 34
static inline void *GetActualMallocAddress(void *const mem) {
  const size_t *const malloc_addr_location = GetMallocAddressLocation(mem);
  return (void *)(*malloc_addr_location);
}
John Koleszar's avatar
John Koleszar committed
35

Urvang Joshi's avatar
Urvang Joshi committed
36 37 38
void *aom_memalign(size_t align, size_t size) {
  void *x = NULL;
  void *const addr = malloc(size + align - 1 + ADDRESS_STORAGE_SIZE);
John Koleszar's avatar
John Koleszar committed
39 40
  if (addr) {
    x = align_addr((unsigned char *)addr + ADDRESS_STORAGE_SIZE, (int)align);
Urvang Joshi's avatar
Urvang Joshi committed
41
    SetActualMallocAddress(x, addr);
John Koleszar's avatar
John Koleszar committed
42 43
  }
  return x;
John Koleszar's avatar
John Koleszar committed
44 45
}

Yaowu Xu's avatar
Yaowu Xu committed
46
void *aom_malloc(size_t size) { return aom_memalign(DEFAULT_ALIGNMENT, size); }
John Koleszar's avatar
John Koleszar committed
47

Yaowu Xu's avatar
Yaowu Xu committed
48
void *aom_calloc(size_t num, size_t size) {
Urvang Joshi's avatar
Urvang Joshi committed
49 50 51
  const size_t total_size = num * size;
  void *const x = aom_malloc(total_size);
  if (x) memset(x, 0, total_size);
John Koleszar's avatar
John Koleszar committed
52
  return x;
John Koleszar's avatar
John Koleszar committed
53 54
}

Yaowu Xu's avatar
Yaowu Xu committed
55
void *aom_realloc(void *memblk, size_t size) {
Urvang Joshi's avatar
Urvang Joshi committed
56
  void *new_addr = NULL;
John Koleszar's avatar
John Koleszar committed
57 58 59 60 61 62 63 64 65 66

  /*
  The realloc() function changes the size of the object pointed to by
  ptr to the size specified by size, and returns a pointer to the
  possibly moved block. The contents are unchanged up to the lesser
  of the new and old sizes. If ptr is null, realloc() behaves like
  malloc() for the specified size. If size is zero (0) and ptr is
  not a null pointer, the object pointed to is freed.
  */
  if (!memblk)
Yaowu Xu's avatar
Yaowu Xu committed
67
    new_addr = aom_malloc(size);
John Koleszar's avatar
John Koleszar committed
68
  else if (!size)
Yaowu Xu's avatar
Yaowu Xu committed
69
    aom_free(memblk);
John Koleszar's avatar
John Koleszar committed
70
  else {
Urvang Joshi's avatar
Urvang Joshi committed
71
    void *addr = GetActualMallocAddress(memblk);
John Koleszar's avatar
John Koleszar committed
72
    memblk = NULL;
Urvang Joshi's avatar
Urvang Joshi committed
73 74 75 76 77
    addr = realloc(addr, size + DEFAULT_ALIGNMENT + ADDRESS_STORAGE_SIZE);
    if (addr) {
      new_addr = align_addr((unsigned char *)addr + ADDRESS_STORAGE_SIZE,
                            DEFAULT_ALIGNMENT);
      SetActualMallocAddress(new_addr, addr);
John Koleszar's avatar
John Koleszar committed
78
    }
John Koleszar's avatar
John Koleszar committed
79
  }
John Koleszar's avatar
John Koleszar committed
80

John Koleszar's avatar
John Koleszar committed
81
  return new_addr;
John Koleszar's avatar
John Koleszar committed
82 83
}

Yaowu Xu's avatar
Yaowu Xu committed
84
void aom_free(void *memblk) {
John Koleszar's avatar
John Koleszar committed
85
  if (memblk) {
Urvang Joshi's avatar
Urvang Joshi committed
86
    void *addr = GetActualMallocAddress(memblk);
87
    free(addr);
John Koleszar's avatar
John Koleszar committed
88
  }
John Koleszar's avatar
John Koleszar committed
89 90
}

Yaowu Xu's avatar
Yaowu Xu committed
91 92
#if CONFIG_AOM_HIGHBITDEPTH
void *aom_memset16(void *dest, int val, size_t length) {
93
  size_t i;
94
  uint16_t *dest16 = (uint16_t *)dest;
clang-format's avatar
clang-format committed
95
  for (i = 0; i < length; i++) *dest16++ = val;
96
  return dest;
97
}
Yaowu Xu's avatar
Yaowu Xu committed
98
#endif  // CONFIG_AOM_HIGHBITDEPTH