blob: 4514fb54ad57d9909cc6a41b59dee7cc628275e5 [file] [log] [blame]
Yaowu Xuc27fc142016-08-22 16:08:15 -07001/*
Yaowu Xu9c01aa12016-09-01 14:32:49 -07002 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
Yaowu Xuc27fc142016-08-22 16:08:15 -07003 *
Yaowu Xu9c01aa12016-09-01 14:32:49 -07004 * This source code is subject to the terms of the BSD 2 Clause License and
5 * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6 * was not distributed with this source code in the LICENSE file, you can
7 * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8 * Media Patent License 1.0 was not distributed with this source code in the
9 * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
Yaowu Xuc27fc142016-08-22 16:08:15 -070010 */
11
Yaowu Xuf883b422016-08-30 14:01:10 -070012#define __AOM_MEM_C__
13
14#include "aom_mem.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070015#include <stdio.h>
16#include <stdlib.h>
17#include <string.h>
Yaowu Xuf883b422016-08-30 14:01:10 -070018#include "include/aom_mem_intrnl.h"
19#include "aom/aom_integer.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070020
James Zerncd245162016-09-06 23:24:01 -070021static size_t GetAlignedMallocSize(size_t size, size_t align) {
Urvang Joshi497f27e2016-07-26 12:02:37 -070022 return size + align - 1 + ADDRESS_STORAGE_SIZE;
23}
24
James Zerncd245162016-09-06 23:24:01 -070025static size_t *GetMallocAddressLocation(void *const mem) {
Urvang Joshi73a3fd42016-07-25 17:12:14 -070026 return ((size_t *)mem) - 1;
27}
28
James Zerncd245162016-09-06 23:24:01 -070029static void SetActualMallocAddress(void *const mem,
30 const void *const malloc_addr) {
Urvang Joshi73a3fd42016-07-25 17:12:14 -070031 size_t *const malloc_addr_location = GetMallocAddressLocation(mem);
32 *malloc_addr_location = (size_t)malloc_addr;
33}
34
James Zerncd245162016-09-06 23:24:01 -070035static void *GetActualMallocAddress(void *const mem) {
Urvang Joshi73a3fd42016-07-25 17:12:14 -070036 const size_t *const malloc_addr_location = GetMallocAddressLocation(mem);
37 return (void *)(*malloc_addr_location);
38}
39
Yaowu Xuf883b422016-08-30 14:01:10 -070040void *aom_memalign(size_t align, size_t size) {
Urvang Joshi73a3fd42016-07-25 17:12:14 -070041 void *x = NULL;
Urvang Joshi497f27e2016-07-26 12:02:37 -070042 const size_t aligned_size = GetAlignedMallocSize(size, align);
43 void *const addr = malloc(aligned_size);
Yaowu Xuc27fc142016-08-22 16:08:15 -070044 if (addr) {
James Zern20b85982016-08-27 10:34:40 -070045 x = align_addr((unsigned char *)addr + ADDRESS_STORAGE_SIZE, align);
Urvang Joshi73a3fd42016-07-25 17:12:14 -070046 SetActualMallocAddress(x, addr);
Yaowu Xuc27fc142016-08-22 16:08:15 -070047 }
Yaowu Xuc27fc142016-08-22 16:08:15 -070048 return x;
49}
50
Yaowu Xuf883b422016-08-30 14:01:10 -070051void *aom_malloc(size_t size) { return aom_memalign(DEFAULT_ALIGNMENT, size); }
Yaowu Xuc27fc142016-08-22 16:08:15 -070052
Yaowu Xuf883b422016-08-30 14:01:10 -070053void *aom_calloc(size_t num, size_t size) {
Urvang Joshi73a3fd42016-07-25 17:12:14 -070054 const size_t total_size = num * size;
55 void *const x = aom_malloc(total_size);
56 if (x) memset(x, 0, total_size);
Yaowu Xuc27fc142016-08-22 16:08:15 -070057 return x;
58}
59
Yaowu Xuf883b422016-08-30 14:01:10 -070060void *aom_realloc(void *memblk, size_t size) {
Urvang Joshi73a3fd42016-07-25 17:12:14 -070061 void *new_addr = NULL;
Yaowu Xuc27fc142016-08-22 16:08:15 -070062
63 /*
64 The realloc() function changes the size of the object pointed to by
65 ptr to the size specified by size, and returns a pointer to the
66 possibly moved block. The contents are unchanged up to the lesser
67 of the new and old sizes. If ptr is null, realloc() behaves like
68 malloc() for the specified size. If size is zero (0) and ptr is
69 not a null pointer, the object pointed to is freed.
70 */
71 if (!memblk)
Yaowu Xuf883b422016-08-30 14:01:10 -070072 new_addr = aom_malloc(size);
Yaowu Xuc27fc142016-08-22 16:08:15 -070073 else if (!size)
Yaowu Xuf883b422016-08-30 14:01:10 -070074 aom_free(memblk);
Yaowu Xuc27fc142016-08-22 16:08:15 -070075 else {
Urvang Joshi73a3fd42016-07-25 17:12:14 -070076 void *addr = GetActualMallocAddress(memblk);
Urvang Joshi497f27e2016-07-26 12:02:37 -070077 const size_t aligned_size = GetAlignedMallocSize(size, DEFAULT_ALIGNMENT);
Yaowu Xuc27fc142016-08-22 16:08:15 -070078 memblk = NULL;
Urvang Joshi497f27e2016-07-26 12:02:37 -070079 addr = realloc(addr, aligned_size);
Urvang Joshi73a3fd42016-07-25 17:12:14 -070080 if (addr) {
81 new_addr = align_addr((unsigned char *)addr + ADDRESS_STORAGE_SIZE,
82 DEFAULT_ALIGNMENT);
83 SetActualMallocAddress(new_addr, addr);
Yaowu Xuc27fc142016-08-22 16:08:15 -070084 }
85 }
86
87 return new_addr;
88}
89
Yaowu Xuf883b422016-08-30 14:01:10 -070090void aom_free(void *memblk) {
Yaowu Xuc27fc142016-08-22 16:08:15 -070091 if (memblk) {
Urvang Joshi73a3fd42016-07-25 17:12:14 -070092 void *addr = GetActualMallocAddress(memblk);
Yaowu Xuc27fc142016-08-22 16:08:15 -070093 free(addr);
94 }
95}
96
Yaowu Xuf883b422016-08-30 14:01:10 -070097#if CONFIG_AOM_HIGHBITDEPTH
98void *aom_memset16(void *dest, int val, size_t length) {
Yaowu Xuc27fc142016-08-22 16:08:15 -070099 size_t i;
100 uint16_t *dest16 = (uint16_t *)dest;
101 for (i = 0; i < length; i++) *dest16++ = val;
102 return dest;
103}
Yaowu Xuf883b422016-08-30 14:01:10 -0700104#endif // CONFIG_AOM_HIGHBITDEPTH