Coverage Report

Created: 2022-08-24 06:17

/src/aom/aom_mem/aom_mem.c
Line
Count
Source (jump to first uncovered line)
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
182k
static size_t GetAllocationPaddingSize(size_t align) {
20
182k
  assert(align > 0);
21
182k
  assert(align < SIZE_MAX - ADDRESS_STORAGE_SIZE);
22
182k
  return align - 1 + ADDRESS_STORAGE_SIZE;
23
182k
}
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
111k
                                        size_t align) {
28
111k
  if (nmemb == 0) return 1;
29
111k
  const size_t alloc_padding = GetAllocationPaddingSize(align);
30
111k
#if defined(AOM_MAX_ALLOCABLE_MEMORY)
31
111k
  assert(AOM_MAX_ALLOCABLE_MEMORY >= alloc_padding);
32
111k
  assert(AOM_MAX_ALLOCABLE_MEMORY <= SIZE_MAX);
33
111k
  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
111k
  return 1;
38
111k
}
39
40
142k
static size_t *GetMallocAddressLocation(void *const mem) {
41
142k
  return ((size_t *)mem) - 1;
42
142k
}
43
44
static void SetActualMallocAddress(void *const mem,
45
71.0k
                                   const void *const malloc_addr) {
46
71.0k
  size_t *const malloc_addr_location = GetMallocAddressLocation(mem);
47
71.0k
  *malloc_addr_location = (size_t)malloc_addr;
48
71.0k
}
49
50
71.0k
static void *GetActualMallocAddress(void *const mem) {
51
71.0k
  const size_t *const malloc_addr_location = GetMallocAddressLocation(mem);
52
71.0k
  return (void *)(*malloc_addr_location);
53
71.0k
}
54
55
71.0k
void *aom_memalign(size_t align, size_t size) {
56
71.0k
  void *x = NULL;
57
71.0k
  if (!check_size_argument_overflow(1, size, align)) return NULL;
58
71.0k
  const size_t aligned_size = size + GetAllocationPaddingSize(align);
59
71.0k
  void *const addr = malloc(aligned_size);
60
71.0k
  if (addr) {
61
71.0k
    x = aom_align_addr((unsigned char *)addr + ADDRESS_STORAGE_SIZE, align);
62
71.0k
    SetActualMallocAddress(x, addr);
63
71.0k
  }
64
71.0k
  return x;
65
71.0k
}
66
67
43.0k
void *aom_malloc(size_t size) { return aom_memalign(DEFAULT_ALIGNMENT, size); }
68
69
40.5k
void *aom_calloc(size_t num, size_t size) {
70
40.5k
  if (!check_size_argument_overflow(num, size, DEFAULT_ALIGNMENT)) return NULL;
71
40.5k
  const size_t total_size = num * size;
72
40.5k
  void *const x = aom_malloc(total_size);
73
40.5k
  if (x) memset(x, 0, total_size);
74
40.5k
  return x;
75
40.5k
}
76
77
237k
void aom_free(void *memblk) {
78
237k
  if (memblk) {
79
71.0k
    void *addr = GetActualMallocAddress(memblk);
80
71.0k
    free(addr);
81
71.0k
  }
82
237k
}
83
84
32.6M
void *aom_memset16(void *dest, int val, size_t length) {
85
32.6M
  size_t i;
86
32.6M
  uint16_t *dest16 = (uint16_t *)dest;
87
2.13G
  for (i = 0; i < length; i++) *dest16++ = val;
88
32.6M
  return dest;
89
32.6M
}