Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
hrydgard
GitHub Repository: hrydgard/ppsspp
Path: blob/master/ext/at3_standalone/mem.cpp
3186 views
1
/*
2
* default memory allocator for libavutil
3
* Copyright (c) 2002 Fabrice Bellard
4
*
5
* This file is part of FFmpeg.
6
*
7
* FFmpeg is free software; you can redistribute it and/or
8
* modify it under the terms of the GNU Lesser General Public
9
* License as published by the Free Software Foundation; either
10
* version 2.1 of the License, or (at your option) any later version.
11
*
12
* FFmpeg is distributed in the hope that it will be useful,
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15
* Lesser General Public License for more details.
16
*
17
* You should have received a copy of the GNU Lesser General Public
18
* License along with FFmpeg; if not, write to the Free Software
19
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20
*/
21
22
/**
23
* @file
24
* default memory allocator for libavutil
25
*/
26
27
#define _XOPEN_SOURCE 600
28
29
#include <limits.h>
30
#include <stdint.h>
31
#include <stdlib.h>
32
#include <string.h>
33
34
#include "Common/MemoryUtil.h"
35
36
#include "compat.h"
37
#include "intreadwrite.h"
38
#include "mem.h"
39
40
/**
41
* Multiply two size_t values checking for overflow.
42
* @return 0 if success, AVERROR(EINVAL) if overflow.
43
*/
44
static inline int av_size_mult(size_t a, size_t b, size_t *r) {
45
size_t t = a * b;
46
/* Hack inspired from glibc: only try the division if nelem and elsize
47
* are both greater than sqrt(SIZE_MAX). */
48
if ((a | b) >= ((size_t)1 << (sizeof(size_t) * 4)) && a && t / a != b)
49
return AVERROR(EINVAL);
50
*r = t;
51
return 0;
52
}
53
54
void *av_malloc(size_t size) {
55
void *ptr = NULL;
56
57
// Some code requires av malloc to have an alignment of 32 at least. See #20155
58
ptr = AllocateAlignedMemory(size, 32);
59
if (!ptr && !size) {
60
// Compensate for platforms that don't allow zero-size allocations (not sure if this is actually an issue)
61
return av_malloc(1);
62
}
63
return ptr;
64
}
65
66
void av_free(void *ptr) {
67
FreeAlignedMemory(ptr);
68
}
69
70
void av_freep(void *arg) {
71
void *val;
72
memcpy(&val, arg, sizeof(val));
73
memset(arg, 0, sizeof(val));
74
av_free(val);
75
}
76
77
void *av_mallocz(size_t size) {
78
void *ptr = av_malloc(size);
79
if (ptr)
80
memset(ptr, 0, size);
81
return ptr;
82
}
83
84