Commit 06fe37cb authored by bellard's avatar bellard

use av_malloc() functions - added av_strdup and av_realloc()


git-svn-id: file:///var/local/repositories/ffmpeg/trunk@1505 9553f0bf-9b14-0410-a0b8-cfaf0461ba5b
parent 79806e39
...@@ -1233,7 +1233,9 @@ int avcodec(void* handle, avc_cmd_t cmd, void* pin, void* pout); ...@@ -1233,7 +1233,9 @@ int avcodec(void* handle, avc_cmd_t cmd, void* pin, void* pout);
/* memory */ /* memory */
void *av_malloc(unsigned int size); void *av_malloc(unsigned int size);
void *av_mallocz(unsigned int size); void *av_mallocz(unsigned int size);
void *av_realloc(void *ptr, unsigned int size);
void av_free(void *ptr); void av_free(void *ptr);
char *av_strdup(const char *s);
void __av_freep(void **ptr); void __av_freep(void **ptr);
#define av_freep(p) __av_freep((void **)(p)) #define av_freep(p) __av_freep((void **)(p))
void *av_fast_realloc(void *ptr, int *size, int min_size); void *av_fast_realloc(void *ptr, int *size, int min_size);
......
...@@ -171,8 +171,8 @@ static int alloc_table(VLC *vlc, int size) ...@@ -171,8 +171,8 @@ static int alloc_table(VLC *vlc, int size)
vlc->table_size += size; vlc->table_size += size;
if (vlc->table_size > vlc->table_allocated) { if (vlc->table_size > vlc->table_allocated) {
vlc->table_allocated += (1 << vlc->bits); vlc->table_allocated += (1 << vlc->bits);
vlc->table = realloc(vlc->table, vlc->table = av_realloc(vlc->table,
sizeof(VLC_TYPE) * 2 * vlc->table_allocated); sizeof(VLC_TYPE) * 2 * vlc->table_allocated);
if (!vlc->table) if (!vlc->table)
return -1; return -1;
} }
......
...@@ -931,6 +931,11 @@ if((y)<(x)){\ ...@@ -931,6 +931,11 @@ if((y)<(x)){\
#define CLAMP_TO_8BIT(d) ((d > 0xff) ? 0xff : (d < 0) ? 0 : d) #define CLAMP_TO_8BIT(d) ((d > 0xff) ? 0xff : (d < 0) ? 0 : d)
/* avoid usage of various functions */
#define malloc please_use_av_malloc
#define free please_use_av_free
#define realloc please_use_av_realloc
#endif /* HAVE_AV_CONFIG_H */ #endif /* HAVE_AV_CONFIG_H */
#endif /* COMMON_H */ #endif /* COMMON_H */
...@@ -1071,7 +1071,7 @@ static int avpicture_alloc(AVPicture *picture, ...@@ -1071,7 +1071,7 @@ static int avpicture_alloc(AVPicture *picture,
static void avpicture_free(AVPicture *picture) static void avpicture_free(AVPicture *picture)
{ {
free(picture->data[0]); av_free(picture->data[0]);
} }
/* XXX: always use linesize. Return -1 if not supported */ /* XXX: always use linesize. Return -1 if not supported */
......
...@@ -17,6 +17,12 @@ ...@@ -17,6 +17,12 @@
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/ */
#include "avcodec.h" #include "avcodec.h"
/* here we can use OS dependant allocation functions */
#undef malloc
#undef free
#undef realloc
#ifdef HAVE_MALLOC_H #ifdef HAVE_MALLOC_H
#include <malloc.h> #include <malloc.h>
#endif #endif
...@@ -25,13 +31,15 @@ ...@@ -25,13 +31,15 @@
memory allocator. You do not need to suppress this file because the memory allocator. You do not need to suppress this file because the
linker will do it automatically */ linker will do it automatically */
/* memory alloc */ /**
* Memory allocation of size byte with alignment suitable for all
* memory accesses (including vectors if available on the
* CPU). av_malloc(0) must return a non NULL pointer.
*/
void *av_malloc(unsigned int size) void *av_malloc(unsigned int size)
{ {
void *ptr; void *ptr;
// if(size==0) return NULL;
#if defined (HAVE_MEMALIGN) #if defined (HAVE_MEMALIGN)
ptr = memalign(16,size); ptr = memalign(16,size);
/* Why 64? /* Why 64?
...@@ -63,23 +71,17 @@ void *av_malloc(unsigned int size) ...@@ -63,23 +71,17 @@ void *av_malloc(unsigned int size)
#else #else
ptr = malloc(size); ptr = malloc(size);
#endif #endif
if (!ptr)
return NULL;
//fprintf(stderr, "%X %d\n", (int)ptr, size);
/* NOTE: this memset should not be present */
memset(ptr, 0, size);
return ptr; return ptr;
} }
/** /**
* realloc which does nothing if the block is large enogh * av_realloc semantics (same as glibc): if ptr is NULL and size > 0,
* identical to malloc(size). If size is zero, it is identical to
* free(ptr) and NULL is returned.
*/ */
void *av_fast_realloc(void *ptr, int *size, int min_size){ void *av_realloc(void *ptr, unsigned int size)
if(min_size < *size) return ptr; {
return realloc(ptr, size);
*size= min_size + 10*1024;
return realloc(ptr, *size);
} }
/* NOTE: ptr = NULL is explicetly allowed */ /* NOTE: ptr = NULL is explicetly allowed */
......
...@@ -770,6 +770,7 @@ int MPA_encode_frame(AVCodecContext *avctx, ...@@ -770,6 +770,7 @@ int MPA_encode_frame(AVCodecContext *avctx,
static int MPA_encode_close(AVCodecContext *avctx) static int MPA_encode_close(AVCodecContext *avctx)
{ {
av_freep(&avctx->coded_frame); av_freep(&avctx->coded_frame);
return 0;
} }
AVCodec mp2_encoder = { AVCodec mp2_encoder = {
......
...@@ -751,8 +751,8 @@ static int init_pass2(MpegEncContext *s) ...@@ -751,8 +751,8 @@ static int init_pass2(MpegEncContext *s)
} }
//printf("%lld %lld %lld %lld\n", available_bits[I_TYPE], available_bits[P_TYPE], available_bits[B_TYPE], all_available_bits); //printf("%lld %lld %lld %lld\n", available_bits[I_TYPE], available_bits[P_TYPE], available_bits[B_TYPE], all_available_bits);
qscale= malloc(sizeof(double)*rcc->num_entries); qscale= av_malloc(sizeof(double)*rcc->num_entries);
blured_qscale= malloc(sizeof(double)*rcc->num_entries); blured_qscale= av_malloc(sizeof(double)*rcc->num_entries);
for(step=256*256; step>0.0000001; step*=0.5){ for(step=256*256; step>0.0000001; step*=0.5){
expected_bits=0; expected_bits=0;
...@@ -809,8 +809,8 @@ static int init_pass2(MpegEncContext *s) ...@@ -809,8 +809,8 @@ static int init_pass2(MpegEncContext *s)
// printf("%f %d %f\n", expected_bits, (int)all_available_bits, rate_factor); // printf("%f %d %f\n", expected_bits, (int)all_available_bits, rate_factor);
if(expected_bits > all_available_bits) rate_factor-= step; if(expected_bits > all_available_bits) rate_factor-= step;
} }
free(qscale); av_free(qscale);
free(blured_qscale); av_free(blured_qscale);
if(abs(expected_bits/all_available_bits - 1.0) > 0.01 ){ if(abs(expected_bits/all_available_bits - 1.0) > 0.01 ){
fprintf(stderr, "Error: 2pass curve failed to converge\n"); fprintf(stderr, "Error: 2pass curve failed to converge\n");
......
...@@ -33,6 +33,32 @@ void *av_mallocz(unsigned int size) ...@@ -33,6 +33,32 @@ void *av_mallocz(unsigned int size)
return ptr; return ptr;
} }
char *av_strdup(const char *s)
{
char *ptr;
int len;
len = strlen(s) + 1;
ptr = av_malloc(len);
if (!ptr)
return NULL;
memcpy(ptr, s, len);
return ptr;
}
/**
* realloc which does nothing if the block is large enough
*/
void *av_fast_realloc(void *ptr, int *size, int min_size)
{
if(min_size < *size)
return ptr;
*size= min_size + 10*1024;
return av_realloc(ptr, *size);
}
/* allocation of static arrays - do not use for normal allocation */ /* allocation of static arrays - do not use for normal allocation */
static unsigned int last_static = 0; static unsigned int last_static = 0;
static char*** array_static = NULL; static char*** array_static = NULL;
...@@ -47,7 +73,7 @@ void *__av_mallocz_static(void** location, unsigned int size) ...@@ -47,7 +73,7 @@ void *__av_mallocz_static(void** location, unsigned int size)
if (location) if (location)
{ {
if (l > last_static) if (l > last_static)
array_static = realloc(array_static, l); array_static = av_realloc(array_static, l);
array_static[last_static++] = (char**) location; array_static[last_static++] = (char**) location;
*location = ptr; *location = ptr;
} }
...@@ -61,10 +87,10 @@ void av_free_static() ...@@ -61,10 +87,10 @@ void av_free_static()
unsigned i; unsigned i;
for (i = 0; i < last_static; i++) for (i = 0; i < last_static; i++)
{ {
free(*array_static[i]); av_free(*array_static[i]);
*array_static[i] = NULL; *array_static[i] = NULL;
} }
free(array_static); av_free(array_static);
array_static = 0; array_static = 0;
} }
last_static = 0; last_static = 0;
......
...@@ -92,7 +92,7 @@ typedef struct WMADecodeContext { ...@@ -92,7 +92,7 @@ typedef struct WMADecodeContext {
int16_t coefs1[MAX_CHANNELS][BLOCK_MAX_SIZE]; int16_t coefs1[MAX_CHANNELS][BLOCK_MAX_SIZE];
float coefs[MAX_CHANNELS][BLOCK_MAX_SIZE] __attribute__((aligned(16))); float coefs[MAX_CHANNELS][BLOCK_MAX_SIZE] __attribute__((aligned(16)));
MDCTContext mdct_ctx[BLOCK_NB_SIZES]; MDCTContext mdct_ctx[BLOCK_NB_SIZES];
float *windows[BLOCK_NB_SIZES] __attribute__((aligned(16))); float *windows[BLOCK_NB_SIZES];
FFTSample mdct_tmp[BLOCK_MAX_SIZE] __attribute__((aligned(16))); /* temporary storage for imdct */ FFTSample mdct_tmp[BLOCK_MAX_SIZE] __attribute__((aligned(16))); /* temporary storage for imdct */
/* output buffer for one frame and the last for IMDCT windowing */ /* output buffer for one frame and the last for IMDCT windowing */
float frame_out[MAX_CHANNELS][BLOCK_MAX_SIZE * 2] __attribute__((aligned(16))); float frame_out[MAX_CHANNELS][BLOCK_MAX_SIZE * 2] __attribute__((aligned(16)));
...@@ -212,8 +212,8 @@ static void init_coef_vlc(VLC *vlc, ...@@ -212,8 +212,8 @@ static void init_coef_vlc(VLC *vlc,
init_vlc(vlc, 9, n, table_bits, 1, 1, table_codes, 4, 4); init_vlc(vlc, 9, n, table_bits, 1, 1, table_codes, 4, 4);
run_table = malloc(n * sizeof(uint16_t)); run_table = av_malloc(n * sizeof(uint16_t));
level_table = malloc(n * sizeof(uint16_t)); level_table = av_malloc(n * sizeof(uint16_t));
p = levels_table; p = levels_table;
i = 2; i = 2;
level = 1; level = 1;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment