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.h
Views: 1401
1
/*
2
* copyright (c) 2006 Michael Niedermayer <[email protected]>
3
*
4
* This file is part of FFmpeg.
5
*
6
* FFmpeg is free software; you can redistribute it and/or
7
* modify it under the terms of the GNU Lesser General Public
8
* License as published by the Free Software Foundation; either
9
* version 2.1 of the License, or (at your option) any later version.
10
*
11
* FFmpeg is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14
* Lesser General Public License for more details.
15
*
16
* You should have received a copy of the GNU Lesser General Public
17
* License along with FFmpeg; if not, write to the Free Software
18
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
*/
20
21
#pragma once
22
23
#include <limits.h>
24
#include <stdint.h>
25
#include <errno.h>
26
#include <stddef.h>
27
28
#include "compat.h"
29
30
/**
31
* Allocate a block of size bytes with alignment suitable for all
32
* memory accesses (including vectors if available on the CPU).
33
* @param size Size in bytes for the memory block to be allocated.
34
* @return Pointer to the allocated block, NULL if the block cannot
35
* be allocated.
36
* @see av_mallocz()
37
*/
38
void *av_malloc(size_t size);
39
40
/**
41
* Allocate a block of size * nmemb bytes with av_malloc().
42
* @param nmemb Number of elements
43
* @param size Size of the single element
44
* @return Pointer to the allocated block, NULL if the block cannot
45
* be allocated.
46
* @see av_malloc()
47
*/
48
static inline void *av_malloc_array(size_t nmemb, size_t size)
49
{
50
if (!size || nmemb >= INT_MAX / size)
51
return NULL;
52
return av_malloc(nmemb * size);
53
}
54
55
/**
56
* Allocate or reallocate a block of memory.
57
* If ptr is NULL and size > 0, allocate a new block. If
58
* size is zero, free the memory block pointed to by ptr.
59
* @param ptr Pointer to a memory block already allocated with
60
* av_realloc() or NULL.
61
* @param size Size in bytes of the memory block to be allocated or
62
* reallocated.
63
* @return Pointer to a newly-reallocated block or NULL if the block
64
* cannot be reallocated or the function is used to free the memory block.
65
* @warning Pointers originating from the av_malloc() family of functions must
66
* not be passed to av_realloc(). The former can be implemented using
67
* memalign() (or other functions), and there is no guarantee that
68
* pointers from such functions can be passed to realloc() at all.
69
* The situation is undefined according to POSIX and may crash with
70
* some libc implementations.
71
*/
72
void *av_realloc(void *ptr, size_t size);
73
74
/**
75
* Allocate or reallocate a block of memory.
76
* This function does the same thing as av_realloc, except:
77
* - It takes two arguments and checks the result of the multiplication for
78
* integer overflow.
79
* - It frees the input block in case of failure, thus avoiding the memory
80
* leak with the classic "buf = realloc(buf); if (!buf) return -1;".
81
*/
82
void *av_realloc_f(void *ptr, size_t nelem, size_t elsize);
83
84
/**
85
* Free a memory block which has been allocated with av_malloc(z)() or
86
* av_realloc().
87
* @param ptr Pointer to the memory block which should be freed.
88
* @note ptr = NULL is explicitly allowed.
89
* @note It is recommended that you use av_freep() instead.
90
* @see av_freep()
91
*/
92
void av_free(void *ptr);
93
94
/**
95
* Allocate a block of size bytes with alignment suitable for all
96
* memory accesses (including vectors if available on the CPU) and
97
* zero all the bytes of the block.
98
* @param size Size in bytes for the memory block to be allocated.
99
* @return Pointer to the allocated block, NULL if it cannot be allocated.
100
* @see av_malloc()
101
*/
102
void *av_mallocz(size_t size);
103
104
/**
105
* Allocate a block of size * nmemb bytes with av_mallocz().
106
* @param nmemb Number of elements
107
* @param size Size of the single element
108
* @return Pointer to the allocated block, NULL if the block cannot
109
* be allocated.
110
* @see av_mallocz()
111
* @see av_malloc_array()
112
*/
113
static inline void *av_mallocz_array(size_t nmemb, size_t size)
114
{
115
if (!size || nmemb >= INT_MAX / size)
116
return NULL;
117
return av_mallocz(nmemb * size);
118
}
119
120
/**
121
* Free a memory block which has been allocated with av_malloc(z)() or
122
* av_realloc() and set the pointer pointing to it to NULL.
123
* @param ptr Pointer to the pointer to the memory block which should
124
* be freed.
125
* @note passing a pointer to a NULL pointer is safe and leads to no action.
126
* @see av_free()
127
*/
128
void av_freep(void *ptr);
129
130
/**
131
* @}
132
*/
133
134