Coverage Report

Created: 2025-06-13 07:07

/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
8.20M
static size_t GetAllocationPaddingSize(size_t align) {
20
8.20M
  assert(align > 0);
21
8.20M
  assert(align < SIZE_MAX - ADDRESS_STORAGE_SIZE);
22
8.20M
  return align - 1 + ADDRESS_STORAGE_SIZE;
23
8.20M
}
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
4.54M
                                        size_t align) {
28
4.54M
  if (nmemb == 0) return 1;
29
4.53M
  const size_t alloc_padding = GetAllocationPaddingSize(align);
30
4.53M
#if defined(AOM_MAX_ALLOCABLE_MEMORY)
31
4.53M
  assert(AOM_MAX_ALLOCABLE_MEMORY >= alloc_padding);
32
4.53M
  assert(AOM_MAX_ALLOCABLE_MEMORY <= SIZE_MAX);
33
4.53M
  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
4.53M
  return 1;
38
4.53M
}
39
40
7.34M
static size_t *GetMallocAddressLocation(void *const mem) {
41
7.34M
  return ((size_t *)mem) - 1;
42
7.34M
}
43
44
static void SetActualMallocAddress(void *const mem,
45
3.67M
                                   const void *const malloc_addr) {
46
3.67M
  size_t *const malloc_addr_location = GetMallocAddressLocation(mem);
47
3.67M
  *malloc_addr_location = (size_t)malloc_addr;
48
3.67M
}
49
50
3.67M
static void *GetActualMallocAddress(void *const mem) {
51
3.67M
  const size_t *const malloc_addr_location = GetMallocAddressLocation(mem);
52
3.67M
  return (void *)(*malloc_addr_location);
53
3.67M
}
54
55
3.67M
void *aom_memalign(size_t align, size_t size) {
56
3.67M
  void *x = NULL;
57
3.67M
  if (!check_size_argument_overflow(1, size, align)) return NULL;
58
3.67M
  const size_t aligned_size = size + GetAllocationPaddingSize(align);
59
3.67M
  void *const addr = malloc(aligned_size);
60
3.67M
  if (addr) {
61
3.67M
    x = aom_align_addr((unsigned char *)addr + ADDRESS_STORAGE_SIZE, align);
62
3.67M
    SetActualMallocAddress(x, addr);
63
3.67M
  }
64
3.67M
  return x;
65
3.67M
}
66
67
1.60M
void *aom_malloc(size_t size) { return aom_memalign(DEFAULT_ALIGNMENT, size); }
68
69
870k
void *aom_calloc(size_t num, size_t size) {
70
870k
  if (!check_size_argument_overflow(num, size, DEFAULT_ALIGNMENT)) return NULL;
71
869k
  const size_t total_size = num * size;
72
869k
  void *const x = aom_malloc(total_size);
73
869k
  if (x) memset(x, 0, total_size);
74
869k
  return x;
75
870k
}
76
77
6.00M
void aom_free(void *memblk) {
78
6.00M
  if (memblk) {
79
3.67M
    void *addr = GetActualMallocAddress(memblk);
80
3.67M
    free(addr);
81
3.67M
  }
82
6.00M
}