Mercurial > libavcodec.hg
comparison utils.c @ 394:e2cb8a4ee0c5 libavcodec
proper memory handling functions
author | glantau |
---|---|
date | Sat, 18 May 2002 22:59:50 +0000 |
parents | 47c878305986 |
children | b0aed401a756 |
comparison
equal
deleted
inserted
replaced
393:bf164fce2c14 | 394:e2cb8a4ee0c5 |
---|---|
14 * | 14 * |
15 * You should have received a copy of the GNU General Public License | 15 * You should have received a copy of the GNU General Public License |
16 * along with this program; if not, write to the Free Software | 16 * along with this program; if not, write to the Free Software |
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | 17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
18 */ | 18 */ |
19 #include <stdio.h> | 19 #include "avcodec.h" |
20 #include <string.h> | |
21 #include <errno.h> | |
22 #include "common.h" | |
23 #include "dsputil.h" | 20 #include "dsputil.h" |
24 #include "avcodec.h" | |
25 #include "mpegvideo.h" | 21 #include "mpegvideo.h" |
26 #ifdef HAVE_MALLOC_H | 22 #ifdef HAVE_MALLOC_H |
27 #include <malloc.h> | 23 #include <malloc.h> |
28 #else | |
29 #include <stdlib.h> | |
30 #endif | 24 #endif |
31 | 25 |
32 /* memory alloc */ | 26 /* memory alloc */ |
33 void *av_mallocz(int size) | 27 void *av_malloc(int size) |
34 { | 28 { |
35 void *ptr; | 29 void *ptr; |
36 #if defined ( ARCH_X86 ) && defined ( HAVE_MEMALIGN ) | 30 #if defined ( ARCH_X86 ) && defined ( HAVE_MEMALIGN ) |
37 ptr = memalign(64,size); | 31 ptr = memalign(64,size); |
38 /* Why 64? | 32 /* Why 64? |
51 return NULL; | 45 return NULL; |
52 memset(ptr, 0, size); | 46 memset(ptr, 0, size); |
53 return ptr; | 47 return ptr; |
54 } | 48 } |
55 | 49 |
50 void *av_mallocz(int size) | |
51 { | |
52 void *ptr; | |
53 ptr = av_malloc(size); | |
54 if (!ptr) | |
55 return NULL; | |
56 memset(ptr, 0, size); | |
57 return ptr; | |
58 } | |
59 | |
60 /* NOTE: ptr = NULL is explicetly allowed */ | |
61 void av_free(void *ptr) | |
62 { | |
63 /* XXX: this test should not be needed on most libcs */ | |
64 if (ptr) | |
65 free(ptr); | |
66 } | |
67 | |
56 /* encoder management */ | 68 /* encoder management */ |
57 AVCodec *first_avcodec; | 69 AVCodec *first_avcodec; |
58 | 70 |
59 void register_avcodec(AVCodec *format) | 71 void register_avcodec(AVCodec *format) |
60 { | 72 { |
78 } else { | 90 } else { |
79 avctx->priv_data = NULL; | 91 avctx->priv_data = NULL; |
80 } | 92 } |
81 ret = avctx->codec->init(avctx); | 93 ret = avctx->codec->init(avctx); |
82 if (ret < 0) { | 94 if (ret < 0) { |
83 if (avctx->priv_data) | 95 av_freep(&avctx->priv_data); |
84 free(avctx->priv_data); | |
85 avctx->priv_data = NULL; | |
86 return ret; | 96 return ret; |
87 } | 97 } |
88 return 0; | 98 return 0; |
89 } | 99 } |
90 | 100 |
142 | 152 |
143 int avcodec_close(AVCodecContext *avctx) | 153 int avcodec_close(AVCodecContext *avctx) |
144 { | 154 { |
145 if (avctx->codec->close) | 155 if (avctx->codec->close) |
146 avctx->codec->close(avctx); | 156 avctx->codec->close(avctx); |
147 free(avctx->priv_data); | 157 av_freep(&avctx->priv_data); |
148 avctx->priv_data = NULL; | |
149 avctx->codec = NULL; | 158 avctx->codec = NULL; |
150 return 0; | 159 return 0; |
151 } | 160 } |
152 | 161 |
153 AVCodec *avcodec_find_encoder(enum CodecID id) | 162 AVCodec *avcodec_find_encoder(enum CodecID id) |