CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
hrydgard

CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!

GitHub Repository: hrydgard/ppsspp
Path: blob/master/ext/at3_standalone/mem.cpp
Views: 1401
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 "compat.h"
35
#include "intreadwrite.h"
36
#include "mem.h"
37
38
/**
39
* Multiply two size_t values checking for overflow.
40
* @return 0 if success, AVERROR(EINVAL) if overflow.
41
*/
42
static inline int av_size_mult(size_t a, size_t b, size_t *r)
43
{
44
size_t t = a * b;
45
/* Hack inspired from glibc: only try the division if nelem and elsize
46
* are both greater than sqrt(SIZE_MAX). */
47
if ((a | b) >= ((size_t)1 << (sizeof(size_t) * 4)) && a && t / a != b)
48
return AVERROR(EINVAL);
49
*r = t;
50
return 0;
51
}
52
53
#define ALIGN (HAVE_AVX ? 32 : 16)
54
55
void *av_malloc(size_t size)
56
{
57
void *ptr = NULL;
58
ptr = malloc(size);
59
if(!ptr && !size) {
60
size = 1;
61
ptr= av_malloc(1);
62
}
63
return ptr;
64
}
65
66
void *av_realloc(void *ptr, size_t size)
67
{
68
return realloc(ptr, size + !size);
69
}
70
71
void *av_realloc_f(void *ptr, size_t nelem, size_t elsize)
72
{
73
size_t size;
74
void *r;
75
76
if (av_size_mult(elsize, nelem, &size)) {
77
av_free(ptr);
78
return NULL;
79
}
80
r = av_realloc(ptr, size);
81
if (!r && size)
82
av_free(ptr);
83
return r;
84
}
85
86
void av_free(void *ptr)
87
{
88
free(ptr);
89
}
90
91
void av_freep(void *arg)
92
{
93
void *val;
94
95
memcpy(&val, arg, sizeof(val));
96
memset(arg, 0, sizeof(val));
97
av_free(val);
98
}
99
100
void *av_mallocz(size_t size)
101
{
102
void *ptr = av_malloc(size);
103
if (ptr)
104
memset(ptr, 0, size);
105
return ptr;
106
}
107
108