Repaired 'I must have been boozing' memory management in vorbisfile.a
authorMonty <xiphmont@xiph.org>
Fri, 10 Mar 2000 13:21:18 +0000 (13:21 +0000)
committerMonty <xiphmont@xiph.org>
Fri, 10 Mar 2000 13:21:18 +0000 (13:21 +0000)
Cleaned up every known memory leak (with the help of a malloc tracker,
see lib/misc.c and lib/misc.h for details).

Monty

svn path=/trunk/vorbis/; revision=276

18 files changed:
examples/chaining_example.c
lib/analysis.c
lib/bitwise.c
lib/block.c
lib/codebook.c
lib/envelope.c
lib/floor0.c
lib/framing.c
lib/info.c
lib/lsp.c
lib/mdct.c
lib/misc.h
lib/psy.c
lib/registry.c
lib/smallft.c
lib/time0.c
lib/vorbisfile.c
lib/window.c

index 582ed7f..632eaee 100644 (file)
  ********************************************************************
 
  function: illustrate simple use of chained bitstream and vorbisfile.a
- last mod: $Id: chaining_example.c,v 1.3 2000/01/05 03:10:24 xiphmont Exp $
+ last mod: $Id: chaining_example.c,v 1.4 2000/03/10 13:21:18 xiphmont Exp $
 
  ********************************************************************/
 
 #include "vorbis/codec.h"
 #include "vorbis/vorbisfile.h"
+#include "../lib/misc.h"
 
 int main(){
   OggVorbis_File ov;
index f25f451..ba0c31b 100644 (file)
@@ -12,7 +12,7 @@
  ********************************************************************
 
  function: single-block PCM analysis mode dispatch
- last mod: $Id: analysis.c,v 1.24 2000/02/23 09:24:21 xiphmont Exp $
+ last mod: $Id: analysis.c,v 1.25 2000/03/10 13:21:18 xiphmont Exp $
 
  ********************************************************************/
 
@@ -22,6 +22,7 @@
 #include "vorbis/codec.h"
 #include "bitwise.h"
 #include "registry.h"
+#include "misc.h"
 
 /* decides between modes, dispatches to the appropriate mapping. */
 int vorbis_analysis(vorbis_block *vb,ogg_packet *op){
index 821647f..c11fc46 100644 (file)
@@ -12,7 +12,7 @@
  ********************************************************************
 
   function: packing variable sized words into an octet stream
-  last mod: $Id: bitwise.c,v 1.9 2000/02/23 09:24:22 xiphmont Exp $
+  last mod: $Id: bitwise.c,v 1.10 2000/03/10 13:21:18 xiphmont Exp $
 
  ********************************************************************/
 
@@ -22,6 +22,7 @@
 #include <string.h>
 #include <stdlib.h>
 #include "bitwise.h"
+#include "misc.h"
 
 #define BUFFER_INCREMENT 256
 
index 4728d7f..c53fe72 100644 (file)
@@ -12,7 +12,7 @@
  ********************************************************************
 
  function: PCM data vector blocking, windowing and dis/reassembly
- last mod: $Id: block.c,v 1.27 2000/02/23 11:22:43 xiphmont Exp $
+ last mod: $Id: block.c,v 1.28 2000/03/10 13:21:18 xiphmont Exp $
 
  Handle windowing, overlap-add, etc of the PCM vectors.  This is made
  more amusing by Vorbis' current two allowed block sizes.
@@ -35,6 +35,7 @@
 #include "bitwise.h"
 #include "registry.h"
 #include "bookinternal.h"
+#include "misc.h"
 
 static int ilog2(unsigned int v){
   int ret=0;
@@ -268,14 +269,19 @@ void vorbis_dsp_clear(vorbis_dsp_state *v){
   if(v){
     vorbis_info *vi=v->vi;
 
-    if(v->window[0][0][0])
-      for(i=0;i<VI_WINDOWB;i++){
+    if(v->window[0][0][0]){
+      for(i=0;i<VI_WINDOWB;i++)
        if(v->window[0][0][0][i])free(v->window[0][0][0][i]);
-       for(j=0;j<2;j++)
-         for(k=0;k<2;k++)
+      free(v->window[0][0][0]);
+
+      for(j=0;j<2;j++)
+       for(k=0;k<2;k++){
+         for(i=0;i<VI_WINDOWB;i++)
            if(v->window[1][j][k][i])free(v->window[1][j][k][i]);
-      }
+         free(v->window[1][j][k]);
+       }
+    }
+    
     if(v->pcm){
       for(i=0;i<vi->channels;i++)
        if(v->pcm[i])free(v->pcm[i]);
index cb45348..db61470 100644 (file)
@@ -12,7 +12,7 @@
  ********************************************************************
 
  function: basic codebook pack/unpack/code/decode operations
- last mod: $Id: codebook.c,v 1.11 2000/02/23 09:24:26 xiphmont Exp $
+ last mod: $Id: codebook.c,v 1.12 2000/03/10 13:21:18 xiphmont Exp $
 
  ********************************************************************/
 
@@ -22,6 +22,7 @@
 #include "vorbis/codebook.h"
 #include "bitwise.h"
 #include "bookinternal.h"
+#include "misc.h"
 
 /**** pack/unpack helpers ******************************************/
 static int ilog(unsigned int v){
index 6d07640..a66b23a 100644 (file)
@@ -12,7 +12,7 @@
  ********************************************************************
 
  function: PCM data envelope analysis and manipulation
- last mod: $Id: envelope.c,v 1.15 2000/02/07 20:03:16 xiphmont Exp $
+ last mod: $Id: envelope.c,v 1.16 2000/03/10 13:21:18 xiphmont Exp $
 
  Preecho calculation.
 
@@ -29,6 +29,7 @@
 #include "envelope.h"
 #include "bitwise.h"
 #include "window.h"
+#include "misc.h"
 
 void _ve_envelope_init(envelope_lookup *e,int samples_per){
   int i;
index 95bf482..fb9e1ba 100644 (file)
@@ -12,7 +12,7 @@
  ********************************************************************
 
  function: floor backend 0 implementation
- last mod: $Id: floor0.c,v 1.11 2000/02/23 11:22:44 xiphmont Exp $
+ last mod: $Id: floor0.c,v 1.12 2000/03/10 13:21:18 xiphmont Exp $
 
  ********************************************************************/
 
@@ -26,6 +26,7 @@
 #include "lsp.h"
 #include "bookinternal.h"
 #include "scales.h"
+#include "misc.h"
 
 typedef struct {
   long n;
index 30fe802..b2ca719 100644 (file)
@@ -13,7 +13,7 @@
 
  function: code raw [Vorbis] packets into framed OggSquish stream and
            decode Ogg streams back into raw packets
- last mod: $Id: framing.c,v 1.14 2000/01/12 11:34:40 xiphmont Exp $
+ last mod: $Id: framing.c,v 1.15 2000/03/10 13:21:18 xiphmont Exp $
 
  note: The CRC code is directly derived from public domain code by
  Ross Williams (ross@guest.adelaide.edu.au).  See docs/framing.html
@@ -24,6 +24,7 @@
 #include <stdlib.h>
 #include <string.h>
 #include "vorbis/codec.h"
+#include "misc.h"
 
 /* A complete description of Ogg framing exists in docs/framing.html */
 
index 0b5fd02..c73cb02 100644 (file)
@@ -12,7 +12,7 @@
  ********************************************************************
 
  function: maintain the info structure, info <-> header packets
- last mod: $Id: info.c,v 1.22 2000/02/23 11:50:29 xiphmont Exp $
+ last mod: $Id: info.c,v 1.23 2000/03/10 13:21:18 xiphmont Exp $
 
  ********************************************************************/
 
@@ -28,6 +28,7 @@
 #include "registry.h"
 #include "window.h"
 #include "psy.h"
+#include "misc.h"
 
 /* helpers */
 static int ilog2(unsigned int v){
index f536ee1..a893d32 100644 (file)
--- a/lib/lsp.c
+++ b/lib/lsp.c
@@ -12,7 +12,7 @@
  ********************************************************************
 
   function: LSP (also called LSF) conversion routines
-  last mod: $Id: lsp.c,v 1.4 1999/12/30 07:26:42 xiphmont Exp $
+  last mod: $Id: lsp.c,v 1.5 2000/03/10 13:21:18 xiphmont Exp $
 
   The LSP generation code is taken (with minimal modification) from
   "On the Computation of the LSP Frequencies" by Joseph Rothweiler
@@ -26,6 +26,7 @@
 #include <string.h>
 #include <stdlib.h>
 #include "os.h"
+#include "misc.h"
 
 void vorbis_lsp_to_lpc(double *lsp,double *lpc,int m){ 
   int i,j,m2=m/2;
index a3ccced..1b1d195 100644 (file)
@@ -13,7 +13,7 @@
 
  function: normalized modified discrete cosine transform
            power of two length transform only [16 <= n ]
- last mod: $Id: mdct.c,v 1.15 2000/02/09 22:04:15 xiphmont Exp $
+ last mod: $Id: mdct.c,v 1.16 2000/03/10 13:21:18 xiphmont Exp $
 
  Algorithm adapted from _The use of multirate filter banks for coding
  of high quality digital audio_, by T. Sporer, K. Brandenburg and
@@ -39,6 +39,7 @@
 #include <math.h>
 #include "mdct.h"
 #include "os.h"
+#include "misc.h"
 
 /* build lookups for trig functions; also pre-figure scaling and
    some window function algebra. */
index bae284f..7a4ae1b 100644 (file)
  ********************************************************************
 
  function: miscellaneous prototypes
- last mod: $Id: misc.h,v 1.2 2000/02/23 09:24:30 xiphmont Exp $
+ last mod: $Id: misc.h,v 1.3 2000/03/10 13:21:18 xiphmont Exp $
 
  ********************************************************************/
 
 #ifndef _V_RANDOM_H_
 #define _V_RANDOM_H_
+#include "vorbis/codec.h"
 
 extern void *_vorbis_block_alloc(vorbis_block *vb,long bytes);
 extern void _vorbis_block_ripcord(vorbis_block *vb);
 extern void _analysis_output(char *base,int i,double *v,int n);
 
+#ifdef DEBUG_LEAKS
+extern void *_VDBG_malloc(void *ptr,long bytes,char *file,long line); 
+extern void _VDBG_free(void *ptr,char *file,long line); 
+
+#ifndef MISC_C 
+#undef malloc
+#undef calloc
+#undef realloc
+#undef free
+
+#define malloc(x) _VDBG_malloc(NULL,(x),__FILE__,__LINE__)
+#define calloc(x,y) _VDBG_malloc(NULL,(x)*(y),__FILE__,__LINE__)
+#define realloc(x,y) _VDBG_malloc((x),(y),__FILE__,__LINE__)
+#define free(x) _VDBG_free((x),__FILE__,__LINE__)
+#endif
+#endif
+
 #endif
index dd6a10e..9ba5d21 100644 (file)
--- a/lib/psy.c
+++ b/lib/psy.c
@@ -12,7 +12,7 @@
  ********************************************************************
 
  function: psychoacoustics not including preecho
- last mod: $Id: psy.c,v 1.16 2000/02/12 08:33:07 xiphmont Exp $
+ last mod: $Id: psy.c,v 1.17 2000/03/10 13:21:18 xiphmont Exp $
 
  ********************************************************************/
 
@@ -26,6 +26,7 @@
 #include "lpc.h"
 #include "smallft.h"
 #include "scales.h"
+#include "misc.h"
 
 /* Set up decibel threshhold slopes on a Bark frequency scale */
 
index 4264c79..8e3d661 100644 (file)
  ********************************************************************
 
  function: registry for time, floor, res backends and channel mappings
- last mod: $Id: registry.c,v 1.2 2000/01/22 13:28:29 xiphmont Exp $
+ last mod: $Id: registry.c,v 1.3 2000/03/10 13:21:18 xiphmont Exp $
 
  ********************************************************************/
 
 #include "vorbis/codec.h"
 #include "registry.h"
+#include "misc.h"
 
 /* seems like major overkill now; the backend numbers will grow into
    the infrastructure soon enough */
index dcbd72c..c75e451 100644 (file)
@@ -12,7 +12,7 @@
  ********************************************************************
 
  function: *unnormalized* fft transform
- last mod: $Id: smallft.c,v 1.7 2000/02/09 22:04:16 xiphmont Exp $
+ last mod: $Id: smallft.c,v 1.8 2000/03/10 13:21:18 xiphmont Exp $
 
 ********************************************************************/
 
@@ -33,6 +33,7 @@
 #include <string.h>
 #include <math.h>
 #include "smallft.h"
+#include "misc.h"
 
 static void drfti1(int n, double *wa, int *ifac){
   static int ntryh[4] = { 4,2,3,5 };
index e0ec1f0..6344a48 100644 (file)
@@ -12,7 +12,7 @@
  ********************************************************************
 
  function: time backend 0 (dummy)
- last mod: $Id: time0.c,v 1.4 2000/02/23 09:24:32 xiphmont Exp $
+ last mod: $Id: time0.c,v 1.5 2000/03/10 13:21:18 xiphmont Exp $
 
  ********************************************************************/
 
@@ -20,6 +20,7 @@
 #include <string.h>
 #include "vorbis/codec.h"
 #include "registry.h"
+#include "misc.h"
 
 static void pack (vorbis_info_time *i,oggpack_buffer *opb){
 }
index d602902..d6731c8 100644 (file)
@@ -12,7 +12,7 @@
  ********************************************************************
 
  function: stdio-based convenience library for opening/seeking/decoding
- last mod: $Id: vorbisfile.c,v 1.15 2000/02/23 11:22:47 xiphmont Exp $
+ last mod: $Id: vorbisfile.c,v 1.16 2000/03/10 13:21:18 xiphmont Exp $
 
  ********************************************************************/
 
@@ -23,6 +23,7 @@
 #include "vorbis/vorbisfile.h"
 
 #include "os.h"
+#include "misc.h"
 
 /* A 'chained bitstream' is a Vorbis bitstream that contains more than
    one logical bitstream arranged end to end (the only form of Ogg
@@ -240,7 +241,6 @@ static int _fetch_headers(OggVorbis_File *vf,vorbis_info *vi,vorbis_comment *vc,
        goto bail_header;
       }
   }
-  ogg_stream_clear(&vf->os);
   return 0; 
 
  bail_header:
@@ -280,15 +280,11 @@ static void _prefetch_all_headers(OggVorbis_File *vf,vorbis_info *first_i,
       _seek_helper(vf,vf->offsets[i]);
       if(_fetch_headers(vf,vf->vi+i,vf->vc+i,NULL)==-1){
        fprintf(stderr,"Error opening logical bitstream #%d.\n\n",i+1);
-    
-       ogg_stream_clear(&vf->os); /* clear local storage.  This is not
-                                     done in _fetch_headers, as that may
-                                     be called in a non-seekable stream
-                                     (in which case, we need to preserve
-                                     the stream local storage) */
-       vf->dataoffsets[i]=-1;
-      }else
+       vf->dataoffsets[i]=-1;
+      }else{
        vf->dataoffsets[i]=vf->offset;
+        ogg_stream_clear(&vf->os);
+      }
     }
 
     /* get the serial number and PCM length of this link. To do this,
@@ -317,6 +313,14 @@ static void _prefetch_all_headers(OggVorbis_File *vf,vorbis_info *first_i,
   }
 }
 
+static int _make_decode_ready(OggVorbis_File *vf){
+  if(vf->decode_ready)exit(1);
+  vorbis_synthesis_init(&vf->vd,vf->vi);
+  vorbis_block_init(&vf->vd,&vf->vb);
+  vf->decode_ready=1;
+  return(0);
+}
+
 static int _open_seekable(OggVorbis_File *vf){
   vorbis_info initial_i;
   vorbis_comment initial_c;
@@ -326,8 +330,6 @@ static int _open_seekable(OggVorbis_File *vf){
   ogg_page og;
   
   /* is this even vorbis...? */
-  vorbis_info_init(&initial_i);
-  vorbis_comment_init(&initial_c);
   ret=_fetch_headers(vf,&initial_i,&initial_c,&serialno);
   dataoffset=vf->offset;
   ogg_stream_clear(&vf->os);
@@ -366,10 +368,12 @@ static int _open_nonseekable(OggVorbis_File *vf){
   /* we cannot seek. Set up a 'single' (current) logical bitstream entry  */
   vf->links=1;
   vf->vi=malloc(sizeof(vorbis_info));
+  vf->vc=malloc(sizeof(vorbis_info));
 
   /* Try to fetch the headers, maintaining all the storage */
   if(_fetch_headers(vf,vf->vi,vf->vc,&vf->current_serialno)==-1)return(-1);
-    
+  _make_decode_ready(vf);
+
   return 0;
 }
 
@@ -479,6 +483,10 @@ static int _process_packet(OggVorbis_File *vf,int readp){
                                          leave machine uninitialized */
        
        vf->current_link=link;
+
+       ogg_stream_init(&vf->os,vf->current_serialno);
+       ogg_stream_reset(&vf->os,ogg_page_pageno(&og));
+
       }else{
        /* we're streaming */
        /* fetch the three header packets, build the info struct */
@@ -488,12 +496,7 @@ static int _process_packet(OggVorbis_File *vf,int readp){
        link=0;
       }
       
-      /* reload */
-      ogg_stream_init(&vf->os,vf->current_serialno);
-      ogg_stream_reset(&vf->os,ogg_page_pageno(&og));
-      vorbis_synthesis_init(&vf->vd,vf->vi+link);
-      vorbis_block_init(&vf->vd,&vf->vb);
-      vf->decode_ready=1;
+      _make_decode_ready(vf);
     }
     ogg_stream_pagein(&vf->os,&og);
   }
@@ -516,6 +519,7 @@ int ov_clear(OggVorbis_File *vf){
        vorbis_comment_clear(vf->vc+i);
       }
       free(vf->vi);
+      free(vf->vc);
     }
     if(vf->dataoffsets)free(vf->dataoffsets);
     if(vf->pcmlengths)free(vf->pcmlengths);
@@ -525,6 +529,7 @@ int ov_clear(OggVorbis_File *vf){
     if(vf->f)fclose(vf->f);
     memset(vf,0,sizeof(OggVorbis_File));
   }
+  _VDBG_dump();
   return(0);
 }
 
@@ -565,11 +570,6 @@ int ov_open(FILE *f,OggVorbis_File *vf,char *initial,long ibytes){
   if(ret){
     vf->f=NULL;
     ov_clear(vf);
-  }else{
-    ogg_stream_init(&vf->os,vf->current_serialno);
-    vorbis_synthesis_init(&vf->vd,vf->vi);
-    vorbis_block_init(&vf->vd,&vf->vb);
-    vf->decode_ready=1;
   }
   return(ret);
 }
index e2935f0..9ca08d7 100644 (file)
  ********************************************************************
 
  function: window functions
- last mod: $Id: window.c,v 1.6 2000/02/06 13:39:48 xiphmont Exp $
+ last mod: $Id: window.c,v 1.7 2000/03/10 13:21:18 xiphmont Exp $
 
  ********************************************************************/
 
 #include <stdlib.h>
 #include <math.h>
 #include "os.h"
+#include "misc.h"
 
 double *_vorbis_window(int type, int window,int left,int right){
   double *ret=calloc(window,sizeof(double));