Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1 | /* |
Yaowu Xu | 9c01aa1 | 2016-09-01 14:32:49 -0700 | [diff] [blame] | 2 | * Copyright (c) 2016, Alliance for Open Media. All rights reserved |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3 | * |
Yaowu Xu | 9c01aa1 | 2016-09-01 14:32:49 -0700 | [diff] [blame] | 4 | * 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 Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10 | */ |
| 11 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 12 | #define __AOM_MEM_C__ |
| 13 | |
| 14 | #include "aom_mem.h" |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 15 | #include <stdio.h> |
| 16 | #include <stdlib.h> |
| 17 | #include <string.h> |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 18 | #include "include/aom_mem_intrnl.h" |
| 19 | #include "aom/aom_integer.h" |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 20 | |
James Zern | cd24516 | 2016-09-06 23:24:01 -0700 | [diff] [blame] | 21 | static size_t GetAlignedMallocSize(size_t size, size_t align) { |
Urvang Joshi | 497f27e | 2016-07-26 12:02:37 -0700 | [diff] [blame] | 22 | return size + align - 1 + ADDRESS_STORAGE_SIZE; |
| 23 | } |
| 24 | |
James Zern | cd24516 | 2016-09-06 23:24:01 -0700 | [diff] [blame] | 25 | static size_t *GetMallocAddressLocation(void *const mem) { |
Urvang Joshi | 73a3fd4 | 2016-07-25 17:12:14 -0700 | [diff] [blame] | 26 | return ((size_t *)mem) - 1; |
| 27 | } |
| 28 | |
James Zern | cd24516 | 2016-09-06 23:24:01 -0700 | [diff] [blame] | 29 | static void SetActualMallocAddress(void *const mem, |
| 30 | const void *const malloc_addr) { |
Urvang Joshi | 73a3fd4 | 2016-07-25 17:12:14 -0700 | [diff] [blame] | 31 | size_t *const malloc_addr_location = GetMallocAddressLocation(mem); |
| 32 | *malloc_addr_location = (size_t)malloc_addr; |
| 33 | } |
| 34 | |
James Zern | cd24516 | 2016-09-06 23:24:01 -0700 | [diff] [blame] | 35 | static void *GetActualMallocAddress(void *const mem) { |
Urvang Joshi | 73a3fd4 | 2016-07-25 17:12:14 -0700 | [diff] [blame] | 36 | const size_t *const malloc_addr_location = GetMallocAddressLocation(mem); |
| 37 | return (void *)(*malloc_addr_location); |
| 38 | } |
| 39 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 40 | void *aom_memalign(size_t align, size_t size) { |
Urvang Joshi | 73a3fd4 | 2016-07-25 17:12:14 -0700 | [diff] [blame] | 41 | void *x = NULL; |
Urvang Joshi | 497f27e | 2016-07-26 12:02:37 -0700 | [diff] [blame] | 42 | const size_t aligned_size = GetAlignedMallocSize(size, align); |
| 43 | void *const addr = malloc(aligned_size); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 44 | if (addr) { |
James Zern | 20b8598 | 2016-08-27 10:34:40 -0700 | [diff] [blame] | 45 | x = align_addr((unsigned char *)addr + ADDRESS_STORAGE_SIZE, align); |
Urvang Joshi | 73a3fd4 | 2016-07-25 17:12:14 -0700 | [diff] [blame] | 46 | SetActualMallocAddress(x, addr); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 47 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 48 | return x; |
| 49 | } |
| 50 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 51 | void *aom_malloc(size_t size) { return aom_memalign(DEFAULT_ALIGNMENT, size); } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 52 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 53 | void *aom_calloc(size_t num, size_t size) { |
Urvang Joshi | 73a3fd4 | 2016-07-25 17:12:14 -0700 | [diff] [blame] | 54 | const size_t total_size = num * size; |
| 55 | void *const x = aom_malloc(total_size); |
| 56 | if (x) memset(x, 0, total_size); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 57 | return x; |
| 58 | } |
| 59 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 60 | void *aom_realloc(void *memblk, size_t size) { |
Urvang Joshi | 73a3fd4 | 2016-07-25 17:12:14 -0700 | [diff] [blame] | 61 | void *new_addr = NULL; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 62 | |
| 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 Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 72 | new_addr = aom_malloc(size); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 73 | else if (!size) |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 74 | aom_free(memblk); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 75 | else { |
Urvang Joshi | 73a3fd4 | 2016-07-25 17:12:14 -0700 | [diff] [blame] | 76 | void *addr = GetActualMallocAddress(memblk); |
Urvang Joshi | 497f27e | 2016-07-26 12:02:37 -0700 | [diff] [blame] | 77 | const size_t aligned_size = GetAlignedMallocSize(size, DEFAULT_ALIGNMENT); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 78 | memblk = NULL; |
Urvang Joshi | 497f27e | 2016-07-26 12:02:37 -0700 | [diff] [blame] | 79 | addr = realloc(addr, aligned_size); |
Urvang Joshi | 73a3fd4 | 2016-07-25 17:12:14 -0700 | [diff] [blame] | 80 | if (addr) { |
| 81 | new_addr = align_addr((unsigned char *)addr + ADDRESS_STORAGE_SIZE, |
| 82 | DEFAULT_ALIGNMENT); |
| 83 | SetActualMallocAddress(new_addr, addr); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 84 | } |
| 85 | } |
| 86 | |
| 87 | return new_addr; |
| 88 | } |
| 89 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 90 | void aom_free(void *memblk) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 91 | if (memblk) { |
Urvang Joshi | 73a3fd4 | 2016-07-25 17:12:14 -0700 | [diff] [blame] | 92 | void *addr = GetActualMallocAddress(memblk); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 93 | free(addr); |
| 94 | } |
| 95 | } |
| 96 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 97 | #if CONFIG_AOM_HIGHBITDEPTH |
| 98 | void *aom_memset16(void *dest, int val, size_t length) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 99 | size_t i; |
| 100 | uint16_t *dest16 = (uint16_t *)dest; |
| 101 | for (i = 0; i < length; i++) *dest16++ = val; |
| 102 | return dest; |
| 103 | } |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 104 | #endif // CONFIG_AOM_HIGHBITDEPTH |