shithub: opus

Download patch

ref: 07f884042eccd913c1e96c1503cc15dfe6af9d2b
parent: 4a4a54644078f34ce0f9c86a01ec1159579afb48
author: Jean-Marc Valin <[email protected]>
date: Mon Aug 29 11:08:51 EDT 2011

Wrapping all allocation within opus_alloc() and opus_free()

--- a/libcelt/celt.c
+++ b/libcelt/celt.c
@@ -199,7 +199,7 @@
 CELTEncoder *celt_encoder_create(int sampling_rate, int channels, int *error)
 {
    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)
    {
       celt_encoder_destroy(st);
@@ -210,7 +210,7 @@
 
 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)
    {
       celt_encoder_destroy(st);
@@ -280,7 +280,7 @@
 
 void celt_encoder_destroy(CELTEncoder *st)
 {
-   celt_free(st);
+   opus_free(st);
 }
 
 static inline opus_val16 SIG2WORD16(celt_sig x)
@@ -1923,7 +1923,7 @@
 CELTDecoder *celt_decoder_create(int sampling_rate, int channels, int *error)
 {
    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)
    {
       celt_decoder_destroy(st);
@@ -1934,7 +1934,7 @@
 
 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)
    {
       celt_decoder_destroy(st);
@@ -1992,7 +1992,7 @@
 
 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)
--- a/libcelt/kiss_fft.c
+++ b/libcelt/kiss_fft.c
@@ -40,6 +40,7 @@
 #include "os_support.h"
 #include "mathops.h"
 #include "stack_alloc.h"
+#include "os_support.h"
 
 /* The guts header contains all the multiplication and addition macros that are defined for
    complex numbers.  It also delares the kf_ internal functions.
@@ -600,10 +601,10 @@
 {
    if (cfg)
    {
-      celt_free((opus_int16*)cfg->bitrev);
+      opus_free((opus_int16*)cfg->bitrev);
       if (cfg->shift < 0)
-         celt_free((kiss_twiddle_cpx*)cfg->twiddles);
-      celt_free((kiss_fft_state*)cfg);
+         opus_free((kiss_twiddle_cpx*)cfg->twiddles);
+      opus_free((kiss_fft_state*)cfg);
    }
 }
 
--- a/libcelt/kiss_fft.h
+++ b/libcelt/kiss_fft.h
@@ -55,7 +55,7 @@
 # define kiss_fft_scalar __m128
 #define KISS_FFT_MALLOC(nbytes) memalign(16,nbytes)
 #else	
-#define KISS_FFT_MALLOC celt_alloc
+#define KISS_FFT_MALLOC opus_alloc
 #endif	
 
 #ifdef FIXED_POINT
--- a/libcelt/mdct.c
+++ b/libcelt/mdct.c
@@ -75,7 +75,7 @@
          return 0;
 #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)
      return 0;
    /* We have enough points that sine isn't necessary */
@@ -94,7 +94,7 @@
    int i;
    for (i=0;i<=l->maxshift;i++)
       opus_fft_free(l->kfft[i]);
-   celt_free((kiss_twiddle_scalar*)l->trig);
+   opus_free((kiss_twiddle_scalar*)l->trig);
 }
 
 #endif /* CUSTOM_MODES */
--- a/libcelt/modes.c
+++ b/libcelt/modes.c
@@ -95,7 +95,7 @@
    if (Fs == 400*(opus_int32)frame_size)
    {
       *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++)
          eBands[i] = eband5ms[i];
       return eBands;
@@ -113,7 +113,7 @@
    low = (bark_freq[lin]+res/2)/res;
    high = nBark-lin;
    *nbEBands = low+high;
-   eBands = celt_alloc(sizeof(opus_int16)*(*nbEBands+2));
+   eBands = opus_alloc(sizeof(opus_int16)*(*nbEBands+2));
 
    if (eBands==NULL)
       return NULL;
@@ -170,7 +170,7 @@
    int maxBands = sizeof(eband5ms)/sizeof(eband5ms[0])-1;
 
    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)
       return;
 
@@ -303,7 +303,7 @@
       return NULL;
    }
 
-   mode = celt_alloc(sizeof(CELTMode));
+   mode = opus_alloc(sizeof(CELTMode));
    if (mode==NULL)
       goto failure;
    mode->Fs = Fs;
@@ -357,7 +357,7 @@
    if (mode->allocVectors==NULL)
       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)
       goto failure;
 
@@ -370,7 +370,7 @@
 #endif
    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)
       goto failure;
 
@@ -412,17 +412,17 @@
       }
    }
 #endif /* CUSTOM_MODES_ONLY */
-   celt_free((opus_int16*)mode->eBands);
-   celt_free((opus_int16*)mode->allocVectors);
+   opus_free((opus_int16*)mode->eBands);
+   opus_free((opus_int16*)mode->allocVectors);
 
-   celt_free((opus_val16*)mode->window);
-   celt_free((opus_int16*)mode->logN);
+   opus_free((opus_val16*)mode->window);
+   opus_free((opus_int16*)mode->logN);
 
-   celt_free((opus_int16*)mode->cache.index);
-   celt_free((unsigned char*)mode->cache.bits);
-   celt_free((unsigned char*)mode->cache.caps);
+   opus_free((opus_int16*)mode->cache.index);
+   opus_free((unsigned char*)mode->cache.bits);
+   opus_free((unsigned char*)mode->cache.caps);
    clt_mdct_clear(&mode->mdct);
 
-   celt_free((CELTMode *)mode);
+   opus_free((CELTMode *)mode);
 #endif
 }
--- a/libcelt/os_support.h
+++ b/libcelt/os_support.h
@@ -39,10 +39,9 @@
 #include <stdio.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
-    NOTE: celt_alloc needs to CLEAR THE MEMORY */
+/** Opus wrapper for malloc(). To do your own dynamic allocation, all you need to do is replace this function and opus_free */
 #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()
       or your own allocator, YOU NEED TO CLEAR THE MEMORY ALLOCATED. Otherwise
@@ -53,18 +52,18 @@
 
 /** 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
-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 */
-   return malloc(size);
+   return opus_alloc(size);
 }
 #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
-static inline void celt_free (void *ptr)
+static inline void opus_free (void *ptr)
 {
-   free(ptr);
+   opus_free(ptr);
 }
 #endif
 
--- a/libcelt/stack_alloc.h
+++ b/libcelt/stack_alloc.h
@@ -124,7 +124,7 @@
 #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 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
 
@@ -131,7 +131,7 @@
 #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 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*/
 
--- a/src/opus_decoder.c
+++ b/src/opus_decoder.c
@@ -41,8 +41,8 @@
 #include "stack_alloc.h"
 #include "float_cast.h"
 #include "opus_private.h"
+#include "os_support.h"
 
-
 #ifdef FIXED_POINT
 #define celt_decode_native celt_decode
 #else
@@ -125,7 +125,7 @@
 	st->frame_size = Fs/400;
 	return OPUS_OK;
 failure:
-    free(st);
+    opus_free(st);
     return OPUS_INTERNAL_ERROR;
 }
 
@@ -132,7 +132,7 @@
 OpusDecoder *opus_decoder_create(int Fs, int channels, int *error)
 {
    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 (error)
@@ -142,7 +142,7 @@
    ret = opus_decoder_init((OpusDecoder*)raw_state, Fs, channels);
    if (ret != OPUS_OK)
    {
-      free(raw_state);
+      opus_free(raw_state);
       raw_state = NULL;
    }
    return (OpusDecoder*)raw_state;
@@ -772,7 +772,7 @@
 
 void opus_decoder_destroy(OpusDecoder *st)
 {
-	free(st);
+	opus_free(st);
 }
 
 
--- a/src/opus_encoder.c
+++ b/src/opus_encoder.c
@@ -42,6 +42,7 @@
 #include "opus.h"
 #include "arch.h"
 #include "opus_private.h"
+#include "os_support.h"
 
 #ifdef FIXED_POINT
 #define celt_encode_native celt_encode
@@ -193,7 +194,7 @@
     return OPUS_OK;
 
 failure:
-    free(st);
+    opus_free(st);
     return OPUS_INTERNAL_ERROR;
 }
 
@@ -231,7 +232,7 @@
 OpusEncoder *opus_encoder_create(int Fs, int channels, int mode, int *error)
 {
    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 (error)
@@ -243,7 +244,7 @@
       *error = ret;
    if (ret != OPUS_OK)
    {
-      free(raw_state);
+      opus_free(raw_state);
       raw_state = NULL;
    }
    return (OpusEncoder*)raw_state;
@@ -1005,5 +1006,5 @@
 
 void opus_encoder_destroy(OpusEncoder *st)
 {
-    free(st);
+    opus_free(st);
 }
--- a/src/opus_multistream.c
+++ b/src/opus_multistream.c
@@ -200,7 +200,7 @@
 )
 {
    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 (error)
@@ -210,7 +210,7 @@
    ret = opus_multistream_encoder_init(st, Fs, channels, streams, coupled_streams, mapping, application);
    if (ret != OPUS_OK)
    {
-      free(st);
+      opus_free(st);
       st = NULL;
    }
    if (error)
@@ -440,7 +440,7 @@
 
 void opus_multistream_encoder_destroy(OpusMSEncoder *st)
 {
-    free(st);
+    opus_free(st);
 }
 
 
@@ -509,7 +509,7 @@
 )
 {
    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 (error)
@@ -521,7 +521,7 @@
       *error = ret;
    if (ret != OPUS_OK)
    {
-      free(st);
+      opus_free(st);
       st = NULL;
    }
    return st;
@@ -739,5 +739,5 @@
 
 void opus_multistream_decoder_destroy(OpusMSDecoder *st)
 {
-    free(st);
+    opus_free(st);
 }
--- a/src/repacketizer.c
+++ b/src/repacketizer.c
@@ -35,6 +35,7 @@
 
 #include "opus.h"
 #include "opus_private.h"
+#include "os_support.h"
 
 struct OpusRepacketizer {
    unsigned char toc;
@@ -59,12 +60,12 @@
 
 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)
 {
-   free(rp);
+   opus_free(rp);
 }
 
 int opus_repacketizer_cat(OpusRepacketizer *rp, const unsigned char *data, int len)