Wrapping all allocation within opus_alloc() and opus_free()

This commit is contained in:
Jean-Marc Valin 2011-08-29 15:08:51 -04:00
parent 4a4a546440
commit 07f884042e
11 changed files with 55 additions and 53 deletions

View file

@ -199,7 +199,7 @@ int celt_encoder_get_size_custom(const CELTMode *mode, int channels)
CELTEncoder *celt_encoder_create(int sampling_rate, int channels, int *error) CELTEncoder *celt_encoder_create(int sampling_rate, int channels, int *error)
{ {
CELTEncoder *st; CELTEncoder *st;
st = (CELTEncoder *)celt_alloc(celt_encoder_get_size(channels)); st = (CELTEncoder *)opus_alloc(celt_encoder_get_size(channels));
if (st!=NULL && celt_encoder_init(st, sampling_rate, channels, error)==NULL) if (st!=NULL && celt_encoder_init(st, sampling_rate, channels, error)==NULL)
{ {
celt_encoder_destroy(st); celt_encoder_destroy(st);
@ -210,7 +210,7 @@ CELTEncoder *celt_encoder_create(int sampling_rate, int channels, int *error)
CELTEncoder *celt_encoder_create_custom(const CELTMode *mode, int channels, int *error) CELTEncoder *celt_encoder_create_custom(const CELTMode *mode, int channels, int *error)
{ {
CELTEncoder *st = (CELTEncoder *)celt_alloc(celt_encoder_get_size_custom(mode, channels)); CELTEncoder *st = (CELTEncoder *)opus_alloc(celt_encoder_get_size_custom(mode, channels));
if (st!=NULL && celt_encoder_init_custom(st, mode, channels, error)==NULL) if (st!=NULL && celt_encoder_init_custom(st, mode, channels, error)==NULL)
{ {
celt_encoder_destroy(st); celt_encoder_destroy(st);
@ -280,7 +280,7 @@ CELTEncoder *celt_encoder_init_custom(CELTEncoder *st, const CELTMode *mode, int
void celt_encoder_destroy(CELTEncoder *st) void celt_encoder_destroy(CELTEncoder *st)
{ {
celt_free(st); opus_free(st);
} }
static inline opus_val16 SIG2WORD16(celt_sig x) static inline opus_val16 SIG2WORD16(celt_sig x)
@ -1923,7 +1923,7 @@ int celt_decoder_get_size_custom(const CELTMode *mode, int channels)
CELTDecoder *celt_decoder_create(int sampling_rate, int channels, int *error) CELTDecoder *celt_decoder_create(int sampling_rate, int channels, int *error)
{ {
CELTDecoder *st; CELTDecoder *st;
st = (CELTDecoder *)celt_alloc(celt_decoder_get_size(channels)); st = (CELTDecoder *)opus_alloc(celt_decoder_get_size(channels));
if (st!=NULL && celt_decoder_init(st, sampling_rate, channels, error)==NULL) if (st!=NULL && celt_decoder_init(st, sampling_rate, channels, error)==NULL)
{ {
celt_decoder_destroy(st); celt_decoder_destroy(st);
@ -1934,7 +1934,7 @@ CELTDecoder *celt_decoder_create(int sampling_rate, int channels, int *error)
CELTDecoder *celt_decoder_create_custom(const CELTMode *mode, int channels, int *error) CELTDecoder *celt_decoder_create_custom(const CELTMode *mode, int channels, int *error)
{ {
CELTDecoder *st = (CELTDecoder *)celt_alloc(celt_decoder_get_size_custom(mode, channels)); CELTDecoder *st = (CELTDecoder *)opus_alloc(celt_decoder_get_size_custom(mode, channels));
if (st!=NULL && celt_decoder_init_custom(st, mode, channels, error)==NULL) if (st!=NULL && celt_decoder_init_custom(st, mode, channels, error)==NULL)
{ {
celt_decoder_destroy(st); celt_decoder_destroy(st);
@ -1992,7 +1992,7 @@ CELTDecoder *celt_decoder_init_custom(CELTDecoder *st, const CELTMode *mode, int
void celt_decoder_destroy(CELTDecoder *st) void celt_decoder_destroy(CELTDecoder *st)
{ {
celt_free(st); opus_free(st);
} }
static void celt_decode_lost(CELTDecoder * restrict st, opus_val16 * restrict pcm, int N, int LM) static void celt_decode_lost(CELTDecoder * restrict st, opus_val16 * restrict pcm, int N, int LM)

View file

@ -40,6 +40,7 @@
#include "os_support.h" #include "os_support.h"
#include "mathops.h" #include "mathops.h"
#include "stack_alloc.h" #include "stack_alloc.h"
#include "os_support.h"
/* The guts header contains all the multiplication and addition macros that are defined for /* The guts header contains all the multiplication and addition macros that are defined for
complex numbers. It also delares the kf_ internal functions. complex numbers. It also delares the kf_ internal functions.
@ -600,10 +601,10 @@ void opus_fft_free(const kiss_fft_state *cfg)
{ {
if (cfg) if (cfg)
{ {
celt_free((opus_int16*)cfg->bitrev); opus_free((opus_int16*)cfg->bitrev);
if (cfg->shift < 0) if (cfg->shift < 0)
celt_free((kiss_twiddle_cpx*)cfg->twiddles); opus_free((kiss_twiddle_cpx*)cfg->twiddles);
celt_free((kiss_fft_state*)cfg); opus_free((kiss_fft_state*)cfg);
} }
} }

View file

@ -55,7 +55,7 @@ extern "C" {
# define kiss_fft_scalar __m128 # define kiss_fft_scalar __m128
#define KISS_FFT_MALLOC(nbytes) memalign(16,nbytes) #define KISS_FFT_MALLOC(nbytes) memalign(16,nbytes)
#else #else
#define KISS_FFT_MALLOC celt_alloc #define KISS_FFT_MALLOC opus_alloc
#endif #endif
#ifdef FIXED_POINT #ifdef FIXED_POINT

View file

@ -75,7 +75,7 @@ int clt_mdct_init(mdct_lookup *l,int N, int maxshift)
return 0; return 0;
#endif #endif
} }
l->trig = trig = (kiss_twiddle_scalar*)celt_alloc((N4+1)*sizeof(kiss_twiddle_scalar)); l->trig = trig = (kiss_twiddle_scalar*)opus_alloc((N4+1)*sizeof(kiss_twiddle_scalar));
if (l->trig==NULL) if (l->trig==NULL)
return 0; return 0;
/* We have enough points that sine isn't necessary */ /* We have enough points that sine isn't necessary */
@ -94,7 +94,7 @@ void clt_mdct_clear(mdct_lookup *l)
int i; int i;
for (i=0;i<=l->maxshift;i++) for (i=0;i<=l->maxshift;i++)
opus_fft_free(l->kfft[i]); opus_fft_free(l->kfft[i]);
celt_free((kiss_twiddle_scalar*)l->trig); opus_free((kiss_twiddle_scalar*)l->trig);
} }
#endif /* CUSTOM_MODES */ #endif /* CUSTOM_MODES */

View file

@ -95,7 +95,7 @@ static opus_int16 *compute_ebands(opus_int32 Fs, int frame_size, int res, int *n
if (Fs == 400*(opus_int32)frame_size) if (Fs == 400*(opus_int32)frame_size)
{ {
*nbEBands = sizeof(eband5ms)/sizeof(eband5ms[0])-1; *nbEBands = sizeof(eband5ms)/sizeof(eband5ms[0])-1;
eBands = celt_alloc(sizeof(opus_int16)*(*nbEBands+1)); eBands = opus_alloc(sizeof(opus_int16)*(*nbEBands+1));
for (i=0;i<*nbEBands+1;i++) for (i=0;i<*nbEBands+1;i++)
eBands[i] = eband5ms[i]; eBands[i] = eband5ms[i];
return eBands; return eBands;
@ -113,7 +113,7 @@ static opus_int16 *compute_ebands(opus_int32 Fs, int frame_size, int res, int *n
low = (bark_freq[lin]+res/2)/res; low = (bark_freq[lin]+res/2)/res;
high = nBark-lin; high = nBark-lin;
*nbEBands = low+high; *nbEBands = low+high;
eBands = celt_alloc(sizeof(opus_int16)*(*nbEBands+2)); eBands = opus_alloc(sizeof(opus_int16)*(*nbEBands+2));
if (eBands==NULL) if (eBands==NULL)
return NULL; return NULL;
@ -170,7 +170,7 @@ static void compute_allocation_table(CELTMode *mode)
int maxBands = sizeof(eband5ms)/sizeof(eband5ms[0])-1; int maxBands = sizeof(eband5ms)/sizeof(eband5ms[0])-1;
mode->nbAllocVectors = BITALLOC_SIZE; mode->nbAllocVectors = BITALLOC_SIZE;
allocVectors = celt_alloc(sizeof(unsigned char)*(BITALLOC_SIZE*mode->nbEBands)); allocVectors = opus_alloc(sizeof(unsigned char)*(BITALLOC_SIZE*mode->nbEBands));
if (allocVectors==NULL) if (allocVectors==NULL)
return; return;
@ -303,7 +303,7 @@ CELTMode *celt_mode_create(opus_int32 Fs, int frame_size, int *error)
return NULL; return NULL;
} }
mode = celt_alloc(sizeof(CELTMode)); mode = opus_alloc(sizeof(CELTMode));
if (mode==NULL) if (mode==NULL)
goto failure; goto failure;
mode->Fs = Fs; mode->Fs = Fs;
@ -357,7 +357,7 @@ CELTMode *celt_mode_create(opus_int32 Fs, int frame_size, int *error)
if (mode->allocVectors==NULL) if (mode->allocVectors==NULL)
goto failure; goto failure;
window = (opus_val16*)celt_alloc(mode->overlap*sizeof(opus_val16)); window = (opus_val16*)opus_alloc(mode->overlap*sizeof(opus_val16));
if (window==NULL) if (window==NULL)
goto failure; goto failure;
@ -370,7 +370,7 @@ CELTMode *celt_mode_create(opus_int32 Fs, int frame_size, int *error)
#endif #endif
mode->window = window; mode->window = window;
logN = (opus_int16*)celt_alloc(mode->nbEBands*sizeof(opus_int16)); logN = (opus_int16*)opus_alloc(mode->nbEBands*sizeof(opus_int16));
if (logN==NULL) if (logN==NULL)
goto failure; goto failure;
@ -412,17 +412,17 @@ void celt_mode_destroy(CELTMode *mode)
} }
} }
#endif /* CUSTOM_MODES_ONLY */ #endif /* CUSTOM_MODES_ONLY */
celt_free((opus_int16*)mode->eBands); opus_free((opus_int16*)mode->eBands);
celt_free((opus_int16*)mode->allocVectors); opus_free((opus_int16*)mode->allocVectors);
celt_free((opus_val16*)mode->window); opus_free((opus_val16*)mode->window);
celt_free((opus_int16*)mode->logN); opus_free((opus_int16*)mode->logN);
celt_free((opus_int16*)mode->cache.index); opus_free((opus_int16*)mode->cache.index);
celt_free((unsigned char*)mode->cache.bits); opus_free((unsigned char*)mode->cache.bits);
celt_free((unsigned char*)mode->cache.caps); opus_free((unsigned char*)mode->cache.caps);
clt_mdct_clear(&mode->mdct); clt_mdct_clear(&mode->mdct);
celt_free((CELTMode *)mode); opus_free((CELTMode *)mode);
#endif #endif
} }

View file

@ -39,10 +39,9 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
/** CELT wrapper for calloc(). To do your own dynamic allocation, all you need to do is replace this function, celt_realloc and celt_free /** Opus wrapper for malloc(). To do your own dynamic allocation, all you need to do is replace this function and opus_free */
NOTE: celt_alloc needs to CLEAR THE MEMORY */
#ifndef OVERRIDE_CELT_ALLOC #ifndef OVERRIDE_CELT_ALLOC
static inline void *celt_alloc (size_t size) static inline void *opus_alloc (size_t size)
{ {
/* WARNING: this is not equivalent to malloc(). If you want to use malloc() /* WARNING: this is not equivalent to malloc(). If you want to use malloc()
or your own allocator, YOU NEED TO CLEAR THE MEMORY ALLOCATED. Otherwise or your own allocator, YOU NEED TO CLEAR THE MEMORY ALLOCATED. Otherwise
@ -53,18 +52,18 @@ static inline void *celt_alloc (size_t size)
/** Same as celt_alloc(), except that the area is only needed inside a CELT call (might cause problem with wideband though) */ /** Same as celt_alloc(), except that the area is only needed inside a CELT call (might cause problem with wideband though) */
#ifndef OVERRIDE_CELT_ALLOC_SCRATCH #ifndef OVERRIDE_CELT_ALLOC_SCRATCH
static inline void *celt_alloc_scratch (size_t size) static inline void *opus_alloc_scratch (size_t size)
{ {
/* Scratch space doesn't need to be cleared */ /* Scratch space doesn't need to be cleared */
return malloc(size); return opus_alloc(size);
} }
#endif #endif
/** CELT wrapper for free(). To do your own dynamic allocation, all you need to do is replace this function, celt_realloc and celt_alloc */ /** Opus wrapper for free(). To do your own dynamic allocation, all you need to do is replace this function and opus_alloc */
#ifndef OVERRIDE_CELT_FREE #ifndef OVERRIDE_CELT_FREE
static inline void celt_free (void *ptr) static inline void opus_free (void *ptr)
{ {
free(ptr); opus_free(ptr);
} }
#endif #endif

View file

@ -124,14 +124,14 @@ extern char *global_stack_top;
#define ALIGN(stack, size) ((stack) += ((size) - (long)(stack)) & ((size) - 1)) #define ALIGN(stack, size) ((stack) += ((size) - (long)(stack)) & ((size) - 1))
#define PUSH(stack, size, type) (VALGRIND_MAKE_MEM_NOACCESS(stack, global_stack_top-stack),ALIGN((stack),sizeof(type)/sizeof(char)),VALGRIND_MAKE_MEM_UNDEFINED(stack, ((size)*sizeof(type)/sizeof(char))),(stack)+=(2*(size)*sizeof(type)/sizeof(char)),(type*)((stack)-(2*(size)*sizeof(type)/sizeof(char)))) #define PUSH(stack, size, type) (VALGRIND_MAKE_MEM_NOACCESS(stack, global_stack_top-stack),ALIGN((stack),sizeof(type)/sizeof(char)),VALGRIND_MAKE_MEM_UNDEFINED(stack, ((size)*sizeof(type)/sizeof(char))),(stack)+=(2*(size)*sizeof(type)/sizeof(char)),(type*)((stack)-(2*(size)*sizeof(type)/sizeof(char))))
#define RESTORE_STACK ((global_stack = _saved_stack),VALGRIND_MAKE_MEM_NOACCESS(global_stack, global_stack_top-global_stack)) #define RESTORE_STACK ((global_stack = _saved_stack),VALGRIND_MAKE_MEM_NOACCESS(global_stack, global_stack_top-global_stack))
#define ALLOC_STACK char *_saved_stack; ((global_stack = (global_stack==0) ? ((global_stack_top=celt_alloc_scratch(GLOBAL_STACK_SIZE*2)+(GLOBAL_STACK_SIZE*2))-(GLOBAL_STACK_SIZE*2)) : global_stack),VALGRIND_MAKE_MEM_NOACCESS(global_stack, global_stack_top-global_stack)); _saved_stack = global_stack; #define ALLOC_STACK char *_saved_stack; ((global_stack = (global_stack==0) ? ((global_stack_top=opus_alloc_scratch(GLOBAL_STACK_SIZE*2)+(GLOBAL_STACK_SIZE*2))-(GLOBAL_STACK_SIZE*2)) : global_stack),VALGRIND_MAKE_MEM_NOACCESS(global_stack, global_stack_top-global_stack)); _saved_stack = global_stack;
#else #else
#define ALIGN(stack, size) ((stack) += ((size) - (long)(stack)) & ((size) - 1)) #define ALIGN(stack, size) ((stack) += ((size) - (long)(stack)) & ((size) - 1))
#define PUSH(stack, size, type) (ALIGN((stack),sizeof(type)/sizeof(char)),(stack)+=(size)*(sizeof(type)/sizeof(char)),(type*)((stack)-(size)*(sizeof(type)/sizeof(char)))) #define PUSH(stack, size, type) (ALIGN((stack),sizeof(type)/sizeof(char)),(stack)+=(size)*(sizeof(type)/sizeof(char)),(type*)((stack)-(size)*(sizeof(type)/sizeof(char))))
#define RESTORE_STACK (global_stack = _saved_stack) #define RESTORE_STACK (global_stack = _saved_stack)
#define ALLOC_STACK char *_saved_stack; (global_stack = (global_stack==0) ? celt_alloc_scratch(GLOBAL_STACK_SIZE) : global_stack); _saved_stack = global_stack; #define ALLOC_STACK char *_saved_stack; (global_stack = (global_stack==0) ? opus_alloc_scratch(GLOBAL_STACK_SIZE) : global_stack); _saved_stack = global_stack;
#endif /*ENABLE_VALGRIND*/ #endif /*ENABLE_VALGRIND*/

View file

@ -41,7 +41,7 @@
#include "stack_alloc.h" #include "stack_alloc.h"
#include "float_cast.h" #include "float_cast.h"
#include "opus_private.h" #include "opus_private.h"
#include "os_support.h"
#ifdef FIXED_POINT #ifdef FIXED_POINT
#define celt_decode_native celt_decode #define celt_decode_native celt_decode
@ -125,14 +125,14 @@ int opus_decoder_init(OpusDecoder *st, int Fs, int channels)
st->frame_size = Fs/400; st->frame_size = Fs/400;
return OPUS_OK; return OPUS_OK;
failure: failure:
free(st); opus_free(st);
return OPUS_INTERNAL_ERROR; return OPUS_INTERNAL_ERROR;
} }
OpusDecoder *opus_decoder_create(int Fs, int channels, int *error) OpusDecoder *opus_decoder_create(int Fs, int channels, int *error)
{ {
int ret; int ret;
char *raw_state = (char*)malloc(opus_decoder_get_size(channels)); char *raw_state = (char*)opus_alloc(opus_decoder_get_size(channels));
if (raw_state == NULL) if (raw_state == NULL)
{ {
if (error) if (error)
@ -142,7 +142,7 @@ OpusDecoder *opus_decoder_create(int Fs, int channels, int *error)
ret = opus_decoder_init((OpusDecoder*)raw_state, Fs, channels); ret = opus_decoder_init((OpusDecoder*)raw_state, Fs, channels);
if (ret != OPUS_OK) if (ret != OPUS_OK)
{ {
free(raw_state); opus_free(raw_state);
raw_state = NULL; raw_state = NULL;
} }
return (OpusDecoder*)raw_state; return (OpusDecoder*)raw_state;
@ -772,7 +772,7 @@ int opus_decoder_ctl(OpusDecoder *st, int request, ...)
void opus_decoder_destroy(OpusDecoder *st) void opus_decoder_destroy(OpusDecoder *st)
{ {
free(st); opus_free(st);
} }

View file

@ -42,6 +42,7 @@
#include "opus.h" #include "opus.h"
#include "arch.h" #include "arch.h"
#include "opus_private.h" #include "opus_private.h"
#include "os_support.h"
#ifdef FIXED_POINT #ifdef FIXED_POINT
#define celt_encode_native celt_encode #define celt_encode_native celt_encode
@ -193,7 +194,7 @@ int opus_encoder_init(OpusEncoder* st, int Fs, int channels, int application)
return OPUS_OK; return OPUS_OK;
failure: failure:
free(st); opus_free(st);
return OPUS_INTERNAL_ERROR; return OPUS_INTERNAL_ERROR;
} }
@ -231,7 +232,7 @@ static unsigned char gen_toc(int mode, int framerate, int bandwidth, int channel
OpusEncoder *opus_encoder_create(int Fs, int channels, int mode, int *error) OpusEncoder *opus_encoder_create(int Fs, int channels, int mode, int *error)
{ {
int ret; int ret;
char *raw_state = (char *)malloc(opus_encoder_get_size(channels)); char *raw_state = (char *)opus_alloc(opus_encoder_get_size(channels));
if (raw_state == NULL) if (raw_state == NULL)
{ {
if (error) if (error)
@ -243,7 +244,7 @@ OpusEncoder *opus_encoder_create(int Fs, int channels, int mode, int *error)
*error = ret; *error = ret;
if (ret != OPUS_OK) if (ret != OPUS_OK)
{ {
free(raw_state); opus_free(raw_state);
raw_state = NULL; raw_state = NULL;
} }
return (OpusEncoder*)raw_state; return (OpusEncoder*)raw_state;
@ -1005,5 +1006,5 @@ bad_arg:
void opus_encoder_destroy(OpusEncoder *st) void opus_encoder_destroy(OpusEncoder *st)
{ {
free(st); opus_free(st);
} }

View file

@ -200,7 +200,7 @@ OpusMSEncoder *opus_multistream_encoder_create(
) )
{ {
int ret; int ret;
OpusMSEncoder *st = malloc(opus_multistream_encoder_get_size(streams, coupled_streams)); OpusMSEncoder *st = opus_alloc(opus_multistream_encoder_get_size(streams, coupled_streams));
if (st==NULL) if (st==NULL)
{ {
if (error) if (error)
@ -210,7 +210,7 @@ OpusMSEncoder *opus_multistream_encoder_create(
ret = opus_multistream_encoder_init(st, Fs, channels, streams, coupled_streams, mapping, application); ret = opus_multistream_encoder_init(st, Fs, channels, streams, coupled_streams, mapping, application);
if (ret != OPUS_OK) if (ret != OPUS_OK)
{ {
free(st); opus_free(st);
st = NULL; st = NULL;
} }
if (error) if (error)
@ -440,7 +440,7 @@ int opus_multistream_encoder_ctl(OpusMSEncoder *st, int request, ...)
void opus_multistream_encoder_destroy(OpusMSEncoder *st) void opus_multistream_encoder_destroy(OpusMSEncoder *st)
{ {
free(st); opus_free(st);
} }
@ -509,7 +509,7 @@ OpusMSDecoder *opus_multistream_decoder_create(
) )
{ {
int ret; int ret;
OpusMSDecoder *st = malloc(opus_multistream_decoder_get_size(streams, coupled_streams)); OpusMSDecoder *st = opus_alloc(opus_multistream_decoder_get_size(streams, coupled_streams));
if (st==NULL) if (st==NULL)
{ {
if (error) if (error)
@ -521,7 +521,7 @@ OpusMSDecoder *opus_multistream_decoder_create(
*error = ret; *error = ret;
if (ret != OPUS_OK) if (ret != OPUS_OK)
{ {
free(st); opus_free(st);
st = NULL; st = NULL;
} }
return st; return st;
@ -739,5 +739,5 @@ int opus_multistream_decoder_ctl(OpusMSDecoder *st, int request, ...)
void opus_multistream_decoder_destroy(OpusMSDecoder *st) void opus_multistream_decoder_destroy(OpusMSDecoder *st)
{ {
free(st); opus_free(st);
} }

View file

@ -35,6 +35,7 @@
#include "opus.h" #include "opus.h"
#include "opus_private.h" #include "opus_private.h"
#include "os_support.h"
struct OpusRepacketizer { struct OpusRepacketizer {
unsigned char toc; unsigned char toc;
@ -59,12 +60,12 @@ OpusRepacketizer *opus_repacketizer_init(OpusRepacketizer *rp)
OpusRepacketizer *opus_repacketizer_create(void) OpusRepacketizer *opus_repacketizer_create(void)
{ {
return opus_repacketizer_init(malloc(opus_repacketizer_get_size())); return opus_repacketizer_init(opus_alloc(opus_repacketizer_get_size()));
} }
void opus_repacketizer_destroy(OpusRepacketizer *rp) void opus_repacketizer_destroy(OpusRepacketizer *rp)
{ {
free(rp); opus_free(rp);
} }
int opus_repacketizer_cat(OpusRepacketizer *rp, const unsigned char *data, int len) int opus_repacketizer_cat(OpusRepacketizer *rp, const unsigned char *data, int len)