| rev |
line source |
|
nengel@2
|
1 /*
|
|
nengel@2
|
2 * copyright (c) 2006 Michael Niedermayer <michaelni@gmx.at>
|
|
nengel@2
|
3 *
|
|
nengel@2
|
4 * This file is part of FFmpeg.
|
|
nengel@2
|
5 *
|
|
nengel@2
|
6 * FFmpeg is free software; you can redistribute it and/or
|
|
nengel@2
|
7 * modify it under the terms of the GNU Lesser General Public
|
|
nengel@2
|
8 * License as published by the Free Software Foundation; either
|
|
nengel@2
|
9 * version 2.1 of the License, or (at your option) any later version.
|
|
nengel@2
|
10 *
|
|
nengel@2
|
11 * FFmpeg is distributed in the hope that it will be useful,
|
|
nengel@2
|
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
nengel@2
|
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
nengel@2
|
14 * Lesser General Public License for more details.
|
|
nengel@2
|
15 *
|
|
nengel@2
|
16 * You should have received a copy of the GNU Lesser General Public
|
|
nengel@2
|
17 * License along with FFmpeg; if not, write to the Free Software
|
|
nengel@2
|
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
nengel@2
|
19 */
|
|
nengel@2
|
20
|
|
nengel@2
|
21 /**
|
|
nengel@2
|
22 * @file
|
|
nengel@2
|
23 * memory handling functions
|
|
nengel@2
|
24 */
|
|
nengel@2
|
25
|
|
nengel@2
|
26 #ifndef AVUTIL_MEM_H
|
|
nengel@2
|
27 #define AVUTIL_MEM_H
|
|
nengel@2
|
28
|
|
nengel@2
|
29 #include "attributes.h"
|
|
nengel@2
|
30 #include "config.h"
|
|
nengel@2
|
31
|
|
nengel@2
|
32 #define DECLARE_ALIGNED(n,t,v) t __attribute__ ((aligned (n))) v
|
|
nengel@2
|
33 #define DECLARE_ALIGNED_16(t,v) t __attribute__ ((aligned (16))) v
|
|
nengel@2
|
34 #define DECLARE_ASM_CONST(n,t,v) static const t __attribute__((used)) __attribute__ ((aligned (n))) v
|
|
nengel@2
|
35
|
|
nengel@2
|
36 #if AV_GCC_VERSION_AT_LEAST(3,1)
|
|
nengel@2
|
37 #define av_malloc_attrib __attribute__((__malloc__))
|
|
nengel@2
|
38 #else
|
|
nengel@2
|
39 #define av_malloc_attrib
|
|
nengel@2
|
40 #endif
|
|
nengel@2
|
41
|
|
nengel@2
|
42 /**
|
|
nengel@2
|
43 * Allocates a block of size bytes with alignment suitable for all
|
|
nengel@2
|
44 * memory accesses (including vectors if available on the CPU).
|
|
nengel@2
|
45 * @param size Size in bytes for the memory block to be allocated.
|
|
nengel@2
|
46 * @return Pointer to the allocated block, NULL if the block cannot
|
|
nengel@2
|
47 * be allocated.
|
|
nengel@2
|
48 * @see av_mallocz()
|
|
nengel@2
|
49 */
|
|
nengel@2
|
50 void *av_malloc(unsigned int size) av_malloc_attrib;
|
|
nengel@2
|
51
|
|
nengel@2
|
52 /**
|
|
nengel@2
|
53 * Allocates or reallocates a block of memory.
|
|
nengel@2
|
54 * If ptr is NULL and size > 0, allocates a new block. If
|
|
nengel@2
|
55 * size is zero, frees the memory block pointed to by ptr.
|
|
nengel@2
|
56 * @param size Size in bytes for the memory block to be allocated or
|
|
nengel@2
|
57 * reallocated.
|
|
nengel@2
|
58 * @param ptr Pointer to a memory block already allocated with
|
|
nengel@2
|
59 * av_malloc(z)() or av_realloc() or NULL.
|
|
nengel@2
|
60 * @return Pointer to a newly reallocated block or NULL if the block
|
|
nengel@2
|
61 * cannot be reallocated or the function is used to free the memory block.
|
|
nengel@2
|
62 * @see av_fast_realloc()
|
|
nengel@2
|
63 */
|
|
nengel@2
|
64 void *av_realloc(void *ptr, unsigned int size);
|
|
nengel@2
|
65
|
|
nengel@2
|
66 /**
|
|
nengel@2
|
67 * Reallocates the given block if it is not large enough, otherwise it
|
|
nengel@2
|
68 * does nothing.
|
|
nengel@2
|
69 *
|
|
nengel@2
|
70 * @see av_realloc
|
|
nengel@2
|
71 */
|
|
nengel@2
|
72 void *av_fast_realloc(void *ptr, unsigned int *size, unsigned int min_size);
|
|
nengel@2
|
73
|
|
nengel@2
|
74 /**
|
|
nengel@2
|
75 * Allocates a buffer, reusing the given one if large enough.
|
|
nengel@2
|
76 *
|
|
nengel@2
|
77 * Contrary to av_fast_realloc the current buffer contents might not be
|
|
nengel@2
|
78 * preserved and on error the old buffer is freed, thus no special
|
|
nengel@2
|
79 * handling to avoid memleaks is necessary.
|
|
nengel@2
|
80 *
|
|
nengel@2
|
81 * @param ptr pointer to pointer to already allocated buffer, overwritten with pointer to new buffer
|
|
nengel@2
|
82 * @param size size of the buffer *ptr points to
|
|
nengel@2
|
83 * @param min_size minimum size of *ptr buffer after returning, *ptr will be NULL and
|
|
nengel@2
|
84 * *size 0 if an error occurred.
|
|
nengel@2
|
85 */
|
|
nengel@2
|
86 void av_fast_malloc(void *ptr, unsigned int *size, unsigned int min_size);
|
|
nengel@2
|
87
|
|
nengel@2
|
88 /**
|
|
nengel@2
|
89 * Frees a memory block which has been allocated with av_malloc(z)() or
|
|
nengel@2
|
90 * av_realloc().
|
|
nengel@2
|
91 * @param ptr Pointer to the memory block which should be freed.
|
|
nengel@2
|
92 * @note ptr = NULL is explicitly allowed.
|
|
nengel@2
|
93 * @note It is recommended that you use av_freep() instead.
|
|
nengel@2
|
94 * @see av_freep()
|
|
nengel@2
|
95 */
|
|
nengel@2
|
96
|
|
nengel@2
|
97 void av_free(void *ptr);
|
|
nengel@2
|
98
|
|
nengel@2
|
99 /**
|
|
nengel@2
|
100 * Allocates a block of size bytes with alignment suitable for all
|
|
nengel@2
|
101 * memory accesses (including vectors if available on the CPU) and
|
|
nengel@2
|
102 * zeroes all the bytes of the block.
|
|
nengel@2
|
103 * @param size Size in bytes for the memory block to be allocated.
|
|
nengel@2
|
104 * @return Pointer to the allocated block, NULL if it cannot be allocated.
|
|
nengel@2
|
105 * @see av_malloc()
|
|
nengel@2
|
106 */
|
|
nengel@2
|
107 void *av_mallocz(unsigned int size) av_malloc_attrib;
|
|
nengel@2
|
108
|
|
nengel@2
|
109 /**
|
|
nengel@2
|
110 * Duplicates the string s.
|
|
nengel@2
|
111 * @param s string to be duplicated
|
|
nengel@2
|
112 * @return Pointer to a newly allocated string containing a
|
|
nengel@2
|
113 * copy of s or NULL if the string cannot be allocated.
|
|
nengel@2
|
114 */
|
|
nengel@2
|
115 char *av_strdup(const char *s) av_malloc_attrib;
|
|
nengel@2
|
116
|
|
nengel@2
|
117 /**
|
|
nengel@2
|
118 * Frees a memory block which has been allocated with av_malloc(z)() or
|
|
nengel@2
|
119 * av_realloc() and set the pointer pointing to it to NULL.
|
|
nengel@2
|
120 * @param ptr Pointer to the pointer to the memory block which should
|
|
nengel@2
|
121 * be freed.
|
|
nengel@2
|
122 * @see av_free()
|
|
nengel@2
|
123 */
|
|
nengel@2
|
124 void av_freep(void *ptr);
|
|
nengel@2
|
125
|
|
nengel@2
|
126
|
|
nengel@2
|
127 static av_always_inline uint32_t pack16to32(int a, int b){
|
|
nengel@2
|
128 #if HAVE_BIGENDIAN
|
|
nengel@2
|
129 return (b&0xFFFF) + (a<<16);
|
|
nengel@2
|
130 #else
|
|
nengel@2
|
131 return (a&0xFFFF) + (b<<16);
|
|
nengel@2
|
132 #endif
|
|
nengel@2
|
133 }
|
|
nengel@2
|
134
|
|
nengel@2
|
135 static av_always_inline uint16_t pack8to16(int a, int b){
|
|
nengel@2
|
136 #if HAVE_BIGENDIAN
|
|
nengel@2
|
137 return (b&0xFF) + (a<<8);
|
|
nengel@2
|
138 #else
|
|
nengel@2
|
139 return (a&0xFF) + (b<<8);
|
|
nengel@2
|
140 #endif
|
|
nengel@2
|
141 }
|
|
nengel@2
|
142
|
|
nengel@2
|
143 #endif /* AVUTIL_MEM_H */
|