Line data Source code
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 : #define __AOM_MEM_C__
13 :
14 : #include "aom_mem.h"
15 : #include <stdio.h>
16 : #include <stdlib.h>
17 : #include <string.h>
18 : #include "include/aom_mem_intrnl.h"
19 : #include "aom/aom_integer.h"
20 :
21 0 : static size_t GetAlignedMallocSize(size_t size, size_t align) {
22 0 : return size + align - 1 + ADDRESS_STORAGE_SIZE;
23 : }
24 :
25 0 : static size_t *GetMallocAddressLocation(void *const mem) {
26 0 : return ((size_t *)mem) - 1;
27 : }
28 :
29 0 : static void SetActualMallocAddress(void *const mem,
30 : const void *const malloc_addr) {
31 0 : size_t *const malloc_addr_location = GetMallocAddressLocation(mem);
32 0 : *malloc_addr_location = (size_t)malloc_addr;
33 0 : }
34 :
35 0 : static void *GetActualMallocAddress(void *const mem) {
36 0 : const size_t *const malloc_addr_location = GetMallocAddressLocation(mem);
37 0 : return (void *)(*malloc_addr_location);
38 : }
39 :
40 0 : void *aom_memalign(size_t align, size_t size) {
41 0 : void *x = NULL;
42 0 : const size_t aligned_size = GetAlignedMallocSize(size, align);
43 0 : void *const addr = malloc(aligned_size);
44 0 : if (addr) {
45 0 : x = align_addr((unsigned char *)addr + ADDRESS_STORAGE_SIZE, align);
46 0 : SetActualMallocAddress(x, addr);
47 : }
48 0 : return x;
49 : }
50 :
51 0 : void *aom_malloc(size_t size) { return aom_memalign(DEFAULT_ALIGNMENT, size); }
52 :
53 0 : void *aom_calloc(size_t num, size_t size) {
54 0 : const size_t total_size = num * size;
55 0 : void *const x = aom_malloc(total_size);
56 0 : if (x) memset(x, 0, total_size);
57 0 : return x;
58 : }
59 :
60 0 : void aom_free(void *memblk) {
61 0 : if (memblk) {
62 0 : void *addr = GetActualMallocAddress(memblk);
63 0 : free(addr);
64 : }
65 0 : }
66 :
67 : #if CONFIG_HIGHBITDEPTH
68 0 : void *aom_memset16(void *dest, int val, size_t length) {
69 : size_t i;
70 0 : uint16_t *dest16 = (uint16_t *)dest;
71 0 : for (i = 0; i < length; i++) *dest16++ = val;
72 0 : return dest;
73 : }
74 : #endif // CONFIG_HIGHBITDEPTH
|