Coverage Report

Created: 2025-06-22 08:04

/src/aom/aom_mem/aom_mem.c
Line
Count
Source
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
19
14.9M
static size_t GetAllocationPaddingSize(size_t align) {
20
14.9M
  assert(align > 0);
21
14.9M
  assert(align < SIZE_MAX - ADDRESS_STORAGE_SIZE);
22
14.9M
  return align - 1 + ADDRESS_STORAGE_SIZE;
23
14.9M
}
24
25
// Returns 0 in case of overflow of nmemb * size.
26
static int check_size_argument_overflow(size_t nmemb, size_t size,
27
8.09M
                                        size_t align) {
28
8.09M
  if (nmemb == 0) return 1;
29
8.09M
  const size_t alloc_padding = GetAllocationPaddingSize(align);
30
8.09M
#if defined(AOM_MAX_ALLOCABLE_MEMORY)
31
8.09M
  assert(AOM_MAX_ALLOCABLE_MEMORY >= alloc_padding);
32
8.09M
  assert(AOM_MAX_ALLOCABLE_MEMORY <= SIZE_MAX);
33
8.09M
  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
8.09M
  return 1;
38
8.09M
}
39
40
13.7M
static size_t *GetMallocAddressLocation(void *const mem) {
41
13.7M
  return ((size_t *)mem) - 1;
42
13.7M
}
43
44
static void SetActualMallocAddress(void *const mem,
45
6.86M
                                   const void *const malloc_addr) {
46
6.86M
  size_t *const malloc_addr_location = GetMallocAddressLocation(mem);
47
6.86M
  *malloc_addr_location = (size_t)malloc_addr;
48
6.86M
}
49
50
6.86M
static void *GetActualMallocAddress(void *const mem) {
51
6.86M
  const size_t *const malloc_addr_location = GetMallocAddressLocation(mem);
52
6.86M
  return (void *)(*malloc_addr_location);
53
6.86M
}
54
55
6.86M
void *aom_memalign(size_t align, size_t size) {
56
6.86M
  void *x = NULL;
57
6.86M
  if (!check_size_argument_overflow(1, size, align)) return NULL;
58
6.86M
  const size_t aligned_size = size + GetAllocationPaddingSize(align);
59
6.86M
  void *const addr = malloc(aligned_size);
60
6.86M
  if (addr) {
61
6.86M
    x = aom_align_addr((unsigned char *)addr + ADDRESS_STORAGE_SIZE, align);
62
6.86M
    SetActualMallocAddress(x, addr);
63
6.86M
  }
64
6.86M
  return x;
65
6.86M
}
66
67
2.15M
void *aom_malloc(size_t size) { return aom_memalign(DEFAULT_ALIGNMENT, size); }
68
69
1.22M
void *aom_calloc(size_t num, size_t size) {
70
1.22M
  if (!check_size_argument_overflow(num, size, DEFAULT_ALIGNMENT)) return NULL;
71
1.22M
  const size_t total_size = num * size;
72
1.22M
  void *const x = aom_malloc(total_size);
73
1.22M
  if (x) memset(x, 0, total_size);
74
1.22M
  return x;
75
1.22M
}
76
77
12.9M
void aom_free(void *memblk) {
78
12.9M
  if (memblk) {
79
6.86M
    void *addr = GetActualMallocAddress(memblk);
80
6.86M
    free(addr);
81
6.86M
  }
82
12.9M
}