• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2016, Alliance for Open Media. All rights reserved
3  *
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.
10  */
11 
12 #include "aom_mem.h"
13 #include <assert.h>
14 #include <stdlib.h>
15 #include <string.h>
16 #include "include/aom_mem_intrnl.h"
17 #include "aom/aom_integer.h"
18 
GetAllocationPaddingSize(size_t align)19 static size_t GetAllocationPaddingSize(size_t align) {
20   assert(align > 0);
21   assert(align < SIZE_MAX - ADDRESS_STORAGE_SIZE);
22   return align - 1 + ADDRESS_STORAGE_SIZE;
23 }
24 
25 // Returns 0 in case of overflow of nmemb * size.
check_size_argument_overflow(size_t nmemb,size_t size,size_t align)26 static int check_size_argument_overflow(size_t nmemb, size_t size,
27                                         size_t align) {
28   if (nmemb == 0) return 1;
29   const size_t alloc_padding = GetAllocationPaddingSize(align);
30 #if defined(AOM_MAX_ALLOCABLE_MEMORY)
31   assert(AOM_MAX_ALLOCABLE_MEMORY >= alloc_padding);
32   assert(AOM_MAX_ALLOCABLE_MEMORY <= SIZE_MAX);
33   if (size > (AOM_MAX_ALLOCABLE_MEMORY - alloc_padding) / nmemb) return 0;
34 #else
35   if (size > (SIZE_MAX - alloc_padding) / nmemb) return 0;
36 #endif
37   return 1;
38 }
39 
GetMallocAddressLocation(void * const mem)40 static size_t *GetMallocAddressLocation(void *const mem) {
41   return ((size_t *)mem) - 1;
42 }
43 
SetActualMallocAddress(void * const mem,const void * const malloc_addr)44 static void SetActualMallocAddress(void *const mem,
45                                    const void *const malloc_addr) {
46   size_t *const malloc_addr_location = GetMallocAddressLocation(mem);
47   *malloc_addr_location = (size_t)malloc_addr;
48 }
49 
GetActualMallocAddress(void * const mem)50 static void *GetActualMallocAddress(void *const mem) {
51   const size_t *const malloc_addr_location = GetMallocAddressLocation(mem);
52   return (void *)(*malloc_addr_location);
53 }
54 
aom_memalign(size_t align,size_t size)55 void *aom_memalign(size_t align, size_t size) {
56   void *x = NULL;
57   if (!check_size_argument_overflow(1, size, align)) return NULL;
58   const size_t aligned_size = size + GetAllocationPaddingSize(align);
59   void *const addr = malloc(aligned_size);
60   if (addr) {
61     x = aom_align_addr((unsigned char *)addr + ADDRESS_STORAGE_SIZE, align);
62     SetActualMallocAddress(x, addr);
63   }
64   return x;
65 }
66 
aom_malloc(size_t size)67 void *aom_malloc(size_t size) { return aom_memalign(DEFAULT_ALIGNMENT, size); }
68 
aom_calloc(size_t num,size_t size)69 void *aom_calloc(size_t num, size_t size) {
70   if (!check_size_argument_overflow(num, size, DEFAULT_ALIGNMENT)) return NULL;
71   const size_t total_size = num * size;
72   void *const x = aom_malloc(total_size);
73   if (x) memset(x, 0, total_size);
74   return x;
75 }
76 
aom_free(void * memblk)77 void aom_free(void *memblk) {
78   if (memblk) {
79     void *addr = GetActualMallocAddress(memblk);
80     free(addr);
81   }
82 }
83