Line data Source code
1 : /*
2 : * Copyright (c) 2010 The WebM project authors. All Rights Reserved.
3 : *
4 : * Use of this source code is governed by a BSD-style license
5 : * that can be found in the LICENSE file in the root of the source
6 : * tree. An additional intellectual property rights grant can be found
7 : * in the file PATENTS. All contributing project authors may
8 : * be found in the AUTHORS file in the root of the source tree.
9 : */
10 :
11 : #include "vpx_mem.h"
12 : #include <limits.h>
13 : #include <stdio.h>
14 : #include <stdlib.h>
15 : #include <string.h>
16 : #include "include/vpx_mem_intrnl.h"
17 : #include "vpx/vpx_integer.h"
18 :
19 : #if SIZE_MAX > (1ULL << 40)
20 : #define VPX_MAX_ALLOCABLE_MEMORY (1ULL << 40)
21 : #else
22 : // For 32-bit targets keep this below INT_MAX to avoid valgrind warnings.
23 : #define VPX_MAX_ALLOCABLE_MEMORY ((1ULL << 31) - (1 << 16))
24 : #endif
25 :
26 : // Returns 0 in case of overflow of nmemb * size.
27 0 : static int check_size_argument_overflow(uint64_t nmemb, uint64_t size) {
28 0 : const uint64_t total_size = nmemb * size;
29 0 : if (nmemb == 0) return 1;
30 0 : if (size > VPX_MAX_ALLOCABLE_MEMORY / nmemb) return 0;
31 : if (total_size != (size_t)total_size) return 0;
32 :
33 0 : return 1;
34 : }
35 :
36 0 : static size_t *get_malloc_address_location(void *const mem) {
37 0 : return ((size_t *)mem) - 1;
38 : }
39 :
40 0 : static uint64_t get_aligned_malloc_size(size_t size, size_t align) {
41 0 : return (uint64_t)size + align - 1 + ADDRESS_STORAGE_SIZE;
42 : }
43 :
44 0 : static void set_actual_malloc_address(void *const mem,
45 : const void *const malloc_addr) {
46 0 : size_t *const malloc_addr_location = get_malloc_address_location(mem);
47 0 : *malloc_addr_location = (size_t)malloc_addr;
48 0 : }
49 :
50 0 : static void *get_actual_malloc_address(void *const mem) {
51 0 : size_t *const malloc_addr_location = get_malloc_address_location(mem);
52 0 : return (void *)(*malloc_addr_location);
53 : }
54 :
55 0 : void *vpx_memalign(size_t align, size_t size) {
56 0 : void *x = NULL, *addr;
57 0 : const uint64_t aligned_size = get_aligned_malloc_size(size, align);
58 0 : if (!check_size_argument_overflow(1, aligned_size)) return NULL;
59 :
60 0 : addr = malloc((size_t)aligned_size);
61 0 : if (addr) {
62 0 : x = align_addr((unsigned char *)addr + ADDRESS_STORAGE_SIZE, align);
63 0 : set_actual_malloc_address(x, addr);
64 : }
65 0 : return x;
66 : }
67 :
68 0 : void *vpx_malloc(size_t size) { return vpx_memalign(DEFAULT_ALIGNMENT, size); }
69 :
70 0 : void *vpx_calloc(size_t num, size_t size) {
71 : void *x;
72 0 : if (!check_size_argument_overflow(num, size)) return NULL;
73 :
74 0 : x = vpx_malloc(num * size);
75 0 : if (x) memset(x, 0, num * size);
76 0 : return x;
77 : }
78 :
79 0 : void vpx_free(void *memblk) {
80 0 : if (memblk) {
81 0 : void *addr = get_actual_malloc_address(memblk);
82 0 : free(addr);
83 : }
84 0 : }
85 :
86 : #if CONFIG_VP9_HIGHBITDEPTH
87 : void *vpx_memset16(void *dest, int val, size_t length) {
88 : size_t i;
89 : uint16_t *dest16 = (uint16_t *)dest;
90 : for (i = 0; i < length; i++) *dest16++ = val;
91 : return dest;
92 : }
93 : #endif // CONFIG_VP9_HIGHBITDEPTH
|