blob: e603fc5bfb7ed3061ac42527d52a1152e1616f0b [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#include "aom_mem.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070013#include <stdio.h>
14#include <stdlib.h>
15#include <string.h>
Yaowu Xuf883b422016-08-30 14:01:10 -070016#include "include/aom_mem_intrnl.h"
17#include "aom/aom_integer.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070018
Hui Subaf09d82017-11-03 14:54:38 -070019#if defined(AOM_MAX_ALLOCABLE_MEMORY)
20// Returns 0 in case of overflow of nmemb * size.
21static int check_size_argument_overflow(uint64_t nmemb, uint64_t size) {
22 const uint64_t total_size = nmemb * size;
23 if (nmemb == 0) return 1;
24 if (size > AOM_MAX_ALLOCABLE_MEMORY / nmemb) return 0;
25 if (total_size != (size_t)total_size) return 0;
26 return 1;
27}
28#endif
29
James Zerncd245162016-09-06 23:24:01 -070030static size_t GetAlignedMallocSize(size_t size, size_t align) {
Urvang Joshi497f27e2016-07-26 12:02:37 -070031 return size + align - 1 + ADDRESS_STORAGE_SIZE;
32}
33
James Zerncd245162016-09-06 23:24:01 -070034static size_t *GetMallocAddressLocation(void *const mem) {
Urvang Joshi73a3fd42016-07-25 17:12:14 -070035 return ((size_t *)mem) - 1;
36}
37
James Zerncd245162016-09-06 23:24:01 -070038static void SetActualMallocAddress(void *const mem,
39 const void *const malloc_addr) {
Urvang Joshi73a3fd42016-07-25 17:12:14 -070040 size_t *const malloc_addr_location = GetMallocAddressLocation(mem);
41 *malloc_addr_location = (size_t)malloc_addr;
42}
43
James Zerncd245162016-09-06 23:24:01 -070044static void *GetActualMallocAddress(void *const mem) {
Urvang Joshi73a3fd42016-07-25 17:12:14 -070045 const size_t *const malloc_addr_location = GetMallocAddressLocation(mem);
46 return (void *)(*malloc_addr_location);
47}
48
Yaowu Xuf883b422016-08-30 14:01:10 -070049void *aom_memalign(size_t align, size_t size) {
Urvang Joshi73a3fd42016-07-25 17:12:14 -070050 void *x = NULL;
Urvang Joshi497f27e2016-07-26 12:02:37 -070051 const size_t aligned_size = GetAlignedMallocSize(size, align);
Hui Subaf09d82017-11-03 14:54:38 -070052#if defined(AOM_MAX_ALLOCABLE_MEMORY)
53 if (!check_size_argument_overflow(1, aligned_size)) return NULL;
54#endif
Urvang Joshi497f27e2016-07-26 12:02:37 -070055 void *const addr = malloc(aligned_size);
Yaowu Xuc27fc142016-08-22 16:08:15 -070056 if (addr) {
James Zern20b85982016-08-27 10:34:40 -070057 x = align_addr((unsigned char *)addr + ADDRESS_STORAGE_SIZE, align);
Urvang Joshi73a3fd42016-07-25 17:12:14 -070058 SetActualMallocAddress(x, addr);
Yaowu Xuc27fc142016-08-22 16:08:15 -070059 }
Yaowu Xuc27fc142016-08-22 16:08:15 -070060 return x;
61}
62
Yaowu Xuf883b422016-08-30 14:01:10 -070063void *aom_malloc(size_t size) { return aom_memalign(DEFAULT_ALIGNMENT, size); }
Yaowu Xuc27fc142016-08-22 16:08:15 -070064
Yaowu Xuf883b422016-08-30 14:01:10 -070065void *aom_calloc(size_t num, size_t size) {
Urvang Joshi73a3fd42016-07-25 17:12:14 -070066 const size_t total_size = num * size;
67 void *const x = aom_malloc(total_size);
68 if (x) memset(x, 0, total_size);
Yaowu Xuc27fc142016-08-22 16:08:15 -070069 return x;
70}
71
Yaowu Xuf883b422016-08-30 14:01:10 -070072void aom_free(void *memblk) {
Yaowu Xuc27fc142016-08-22 16:08:15 -070073 if (memblk) {
Urvang Joshi73a3fd42016-07-25 17:12:14 -070074 void *addr = GetActualMallocAddress(memblk);
Yaowu Xuc27fc142016-08-22 16:08:15 -070075 free(addr);
76 }
77}
78
Yaowu Xuf883b422016-08-30 14:01:10 -070079void *aom_memset16(void *dest, int val, size_t length) {
Yaowu Xuc27fc142016-08-22 16:08:15 -070080 size_t i;
81 uint16_t *dest16 = (uint16_t *)dest;
82 for (i = 0; i < length; i++) *dest16++ = val;
83 return dest;
84}