[tremor] [PATCH] eliminate alloca() usage

Nicolas Pitre nico at cam.org
Wed Oct 16 20:38:51 PDT 2002


OK, I felt I could be kind and try helping some people out there so removed
all occurrences of alloca() in the source.  No malloc()/free() were added.

Anybody with another compiler than GCC please confirm if the attached 
patch works for you or not.

If your compiler doesn't support variable-length automatic arrays nor a
proper alloca() implementation then please do yourself a favor by trashing
it and install GCC instead.

Enjoy!

<p>Nicolas

-------------- next part --------------
Index: codebook.c
===================================================================
RCS file: /usr/local/cvsroot/Tremor/codebook.c,v
retrieving revision 1.2
diff -u -r1.2 codebook.c
--- codebook.c	3 Sep 2002 03:15:19 -0000	1.2
+++ codebook.c	17 Oct 2002 02:58:05 -0000
@@ -223,8 +223,8 @@
 long vorbis_book_decodevs_add(codebook *book,ogg_int32_t *a,
 			      oggpack_buffer *b,int n,int point){
   int step=n/book->dim;
-  long *entry = (long *)alloca(sizeof(*entry)*step);
-  ogg_int32_t **t = (ogg_int32_t **)alloca(sizeof(*t)*step);
+  long entry[step];
+  ogg_int32_t *t[step];
   int i,j,o;
   int shift=point-book->binarypoint;
 
Index: configure.in
===================================================================
RCS file: /usr/local/cvsroot/Tremor/configure.in,v
retrieving revision 1.4
diff -u -r1.4 configure.in
--- configure.in	16 Oct 2002 09:07:00 -0000	1.4
+++ configure.in	17 Oct 2002 02:58:05 -0000
@@ -93,7 +93,6 @@
 dnl Check for library functions
 dnl --------------------------------------------------
 
-AC_FUNC_ALLOCA
 AC_FUNC_MEMCMP
 
 dnl --------------------------------------------------
Index: floor0.c
===================================================================
RCS file: /usr/local/cvsroot/Tremor/floor0.c,v
retrieving revision 1.4
diff -u -r1.4 floor0.c
--- floor0.c	16 Oct 2002 09:07:00 -0000	1.4
+++ floor0.c	17 Oct 2002 02:58:06 -0000
@@ -145,7 +145,8 @@
   int i;
   int ampoffseti=ampoffset*4096;
   int ampi=amp;
-  ogg_int32_t *ilsp=(ogg_int32_t *)alloca(m*sizeof(*ilsp));
+  ogg_int32_t ilsp[m];
+
   /* lsp is in 8.24, range 0 to PI; coslook wants it in .16 0 to 1*/
   for(i=0;i<m;i++){
 #ifndef _LOW_ACCURACY_
Index: info.c
===================================================================
RCS file: /usr/local/cvsroot/Tremor/info.c,v
retrieving revision 1.2
diff -u -r1.2 info.c
--- info.c	3 Sep 2002 03:15:19 -0000	1.2
+++ info.c	17 Oct 2002 02:58:07 -0000
@@ -57,7 +57,7 @@
   long i;
   int found = 0;
   int taglen = strlen(tag)+1; /* +1 for the = we append */
-  char *fulltag = (char *)alloca(taglen+ 1);
+  char fulltag[taglen+1];
 
   strcpy(fulltag, tag);
   strcat(fulltag, "=");
@@ -77,7 +77,7 @@
 int vorbis_comment_query_count(vorbis_comment *vc, char *tag){
   int i,count=0;
   int taglen = strlen(tag)+1; /* +1 for the = we append */
-  char *fulltag = (char *)alloca(taglen+1);
+  char fulltag[taglen+1];
   strcpy(fulltag,tag);
   strcat(fulltag, "=");
 
Index: mapping0.c
===================================================================
RCS file: /usr/local/cvsroot/Tremor/mapping0.c,v
retrieving revision 1.3
diff -u -r1.3 mapping0.c
--- mapping0.c	16 Oct 2002 07:39:56 -0000	1.3
+++ mapping0.c	17 Oct 2002 02:58:08 -0000
@@ -188,11 +188,11 @@
   int                   i,j;
   long                  n=vb->pcmend=ci->blocksizes[vb->W];
 
-  ogg_int32_t **pcmbundle=(ogg_int32_t **)alloca(sizeof(*pcmbundle)*vi->channels);
-  int    *zerobundle=(int *)alloca(sizeof(*zerobundle)*vi->channels);
+  ogg_int32_t          *pcmbundle[vi->channels];
+  int                   zerobundle[vi->channels];
   
-  int   *nonzero  =(int *)alloca(sizeof(*nonzero)*vi->channels);
-  void **floormemo=(void **)alloca(sizeof(*floormemo)*vi->channels);
+  int                   nonzero[vi->channels];
+  void                 *floormemo[vi->channels];
   
   /* time domain information decode (note that applying the
      information would have to happen later; we'll probably add a
Index: os.h
===================================================================
RCS file: /usr/local/cvsroot/Tremor/os.h,v
retrieving revision 1.2
diff -u -r1.2 os.h
--- os.h	3 Sep 2002 03:15:19 -0000	1.2
+++ os.h	17 Oct 2002 02:58:08 -0000
@@ -43,10 +43,6 @@
 #  define FAST_HYPOT(a, b) sqrt((a)*(a) + (b)*(b))
 #endif
 
-#ifdef HAVE_ALLOCA_H
-#  include <alloca.h>
-#endif
-
 #ifdef USE_MEMORY_H
 #  include <memory.h>
 #endif
Index: res012.c
===================================================================
RCS file: /usr/local/cvsroot/Tremor/res012.c,v
retrieving revision 1.2
diff -u -r1.2 res012.c
--- res012.c	3 Sep 2002 03:15:19 -0000	1.2
+++ res012.c	17 Oct 2002 02:58:08 -0000
@@ -190,7 +190,7 @@
   
   int partvals=n/samples_per_partition;
   int partwords=(partvals+partitions_per_word-1)/partitions_per_word;
-  int ***partword=(int ***)alloca(ch*sizeof(*partword));
+  int **partword[ch];
 
   for(j=0;j<ch;j++)
     partword[j]=(int **)_vorbis_block_alloc(vb,partwords*sizeof(*partword[j]));
Index: sharedbook.c
===================================================================
RCS file: /usr/local/cvsroot/Tremor/sharedbook.c,v
retrieving revision 1.3
diff -u -r1.3 sharedbook.c
--- sharedbook.c	10 Sep 2002 06:16:12 -0000	1.3
+++ sharedbook.c	17 Oct 2002 02:58:09 -0000
@@ -188,7 +188,9 @@
     ogg_int32_t mindel=_float32_unpack(b->q_min,&minpoint);
     ogg_int32_t delta=_float32_unpack(b->q_delta,&delpoint);
     ogg_int32_t *r=(ogg_int32_t *)_ogg_calloc(n*b->dim,sizeof(*r));
-    int *rp=(int *)_ogg_calloc(n*b->dim,sizeof(*rp));
+    int rp[n*b->dim];
+
+    memset(rp,0,sizeof(rp));
 
     *maxpoint=minpoint;
 
@@ -276,7 +278,6 @@
       if(rp[j]<*maxpoint)
 	r[j]>>=*maxpoint-rp[j];
 	    
-    _ogg_free(rp);
     return(r);
   }
   return(NULL);
@@ -320,16 +321,22 @@
     (**(ogg_uint32_t **)a<**(ogg_uint32_t **)b);
 }
 
-/* decode codebook arrangement is more heavily optimized than encode */
-int vorbis_book_init_decode(codebook *c,const static_codebook *s){
-  int i,j,n=0,tabn;
-  int *sortindex;
-  memset(c,0,sizeof(*c));
-  
+static int _count_used_entries(const static_codebook *s){
+  int i,n=0;
   /* count actually used entries */
   for(i=0;i<s->entries;i++)
     if(s->lengthlist[i]>0)
       n++;
+  return n;
+}
+
+/* decode codebook arrangement is more heavily optimized than encode */
+int vorbis_book_init_decode(codebook *c,const static_codebook *s){
+  int i,j,tabn;
+  int n=_count_used_entries(s);
+  int sortindex[n];
+
+  memset(c,0,sizeof(*c));
 
   c->entries=s->entries;
   c->used_entries=n;
@@ -351,8 +358,8 @@
   {
     /* perform sort */
     ogg_uint32_t *codes=_make_words(s->lengthlist,s->entries,c->used_entries);
-    ogg_uint32_t **codep=(ogg_uint32_t **)alloca(sizeof(*codep)*n);
-    
+    ogg_uint32_t *codep[n];
+
     if(codes==NULL)goto err_out;
 
     for(i=0;i<n;i++){
@@ -362,7 +369,6 @@
 
     qsort(codep,n,sizeof(*codep),sort32a);
 
-    sortindex=(int *)alloca(n*sizeof(*sortindex));
     c->codelist=(ogg_uint32_t *)_ogg_malloc(n*sizeof(*c->codelist));
     /* the index is a reverse index */
     for(i=0;i<n;i++){


More information about the Tremor mailing list