aom_mem.c 3.19 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
static inline size_t GetAlignedMallocSize(size_t size, size_t align) {
  return size + align - 1 + ADDRESS_STORAGE_SIZE;
}

Urvang Joshi's avatar
Urvang Joshi committed
25 26 27 28 29 30 31 32 33
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
34

Urvang Joshi's avatar
Urvang Joshi committed
35 36 37 38
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
39

Urvang Joshi's avatar
Urvang Joshi committed
40 41
void *aom_memalign(size_t align, size_t size) {
  void *x = NULL;
Urvang Joshi's avatar
Urvang Joshi committed
42 43
  const size_t aligned_size = GetAlignedMallocSize(size, align);
  void *const addr = malloc(aligned_size);
John Koleszar's avatar
John Koleszar committed
44 45
  if (addr) {
    x = align_addr((unsigned char *)addr + ADDRESS_STORAGE_SIZE, (int)align);
Urvang Joshi's avatar
Urvang Joshi committed
46
    SetActualMallocAddress(x, addr);
John Koleszar's avatar
John Koleszar committed
47 48
  }
  return x;
John Koleszar's avatar
John Koleszar committed
49 50
}

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

Yaowu Xu's avatar
Yaowu Xu committed
53
void *aom_calloc(size_t num, size_t size) {
Urvang Joshi's avatar
Urvang Joshi committed
54 55 56
  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
57
  return x;
John Koleszar's avatar
John Koleszar committed
58 59
}

Yaowu Xu's avatar
Yaowu Xu committed
60
void *aom_realloc(void *memblk, size_t size) {
Urvang Joshi's avatar
Urvang Joshi committed
61
  void *new_addr = NULL;
John Koleszar's avatar
John Koleszar committed
62 63 64 65 66 67 68 69 70 71

  /*
  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
72
    new_addr = aom_malloc(size);
John Koleszar's avatar
John Koleszar committed
73
  else if (!size)
Yaowu Xu's avatar
Yaowu Xu committed
74
    aom_free(memblk);
John Koleszar's avatar
John Koleszar committed
75
  else {
Urvang Joshi's avatar
Urvang Joshi committed
76
    void *addr = GetActualMallocAddress(memblk);
Urvang Joshi's avatar
Urvang Joshi committed
77
    const size_t aligned_size = GetAlignedMallocSize(size, DEFAULT_ALIGNMENT);
John Koleszar's avatar
John Koleszar committed
78
    memblk = NULL;
Urvang Joshi's avatar
Urvang Joshi committed
79
    addr = realloc(addr, aligned_size);
Urvang Joshi's avatar
Urvang Joshi committed
80 81 82 83
    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
84
    }
John Koleszar's avatar
John Koleszar committed
85
  }
John Koleszar's avatar
John Koleszar committed
86

John Koleszar's avatar
John Koleszar committed
87
  return new_addr;
John Koleszar's avatar
John Koleszar committed
88 89
}

Yaowu Xu's avatar
Yaowu Xu committed
90
void aom_free(void *memblk) {
John Koleszar's avatar
John Koleszar committed
91
  if (memblk) {
Urvang Joshi's avatar
Urvang Joshi committed
92
    void *addr = GetActualMallocAddress(memblk);
93
    free(addr);
John Koleszar's avatar
John Koleszar committed
94
  }
John Koleszar's avatar
John Koleszar committed
95 96
}

Yaowu Xu's avatar
Yaowu Xu committed
97 98
#if CONFIG_AOM_HIGHBITDEPTH
void *aom_memset16(void *dest, int val, size_t length) {
99
  size_t i;
100
  uint16_t *dest16 = (uint16_t *)dest;
clang-format's avatar
clang-format committed
101
  for (i = 0; i < length; i++) *dest16++ = val;
102
  return dest;
103
}
Yaowu Xu's avatar
Yaowu Xu committed
104
#endif  // CONFIG_AOM_HIGHBITDEPTH