shithub: sox

Download patch

ref: bf4f2ae85985b8931d6ed4994503e9cebe9f84a0
parent: 7a41ebe99da12b58622df6112624669a013e7659
author: rrt <rrt>
date: Wed Dec 13 09:58:14 EST 2006

Add x{m,c,re}alloc, and stop checking the return code of
{m,c,realloc}, as it was not always done, when it was done it wasn't
clear that the code could cope with the consequences, and sometimes it
was done wrong, e.g.:

if (malloc(...) == NULL || malloc(...) == NULL || ...)
  st_fail(...)

which leaks if any malloc after the first fails.

Also, tidy up some code, e.g. code that was calling malloc and then
zeroing the result now calls xcalloc (with the exception of code
allocating floats or doubles: although on almost all architectures all
zero bits == 0.0, it's not universally true).

I've tagged the tree add-xmalloc in case anyone is horrified by this
change. In case you're worried about library-style use of libst,
don't: in sox, aborting on out of memory is never a problem, and in a
long-running application xmalloc can be redefined to call longjmp
instead of exit. In any case, as I said, the existing code was often
wrong.

--- a/src/8svx.c
+++ b/src/8svx.c
@@ -96,12 +96,7 @@
                         st_readdw(ft, &chunksize);
                         if (chunksize & 1)
                                 chunksize++;
-                        chunk_buf = (char *) malloc(chunksize + 2);
-                        if (chunk_buf == 0)
-                        {
-                            st_fail_errno(ft, ST_ENOMEM, "Unable to alloc memory");
-                            return(ST_EOF);
-                        }
+                        chunk_buf = (char *) xmalloc(chunksize + 2);
                         if (st_readbuf(ft, chunk_buf,1,(size_t)chunksize)
                                         != chunksize)
                         {
@@ -119,12 +114,7 @@
                         st_readdw(ft, &chunksize);
                         if (chunksize & 1)
                                 chunksize++;
-                        chunk_buf = (char *) malloc(chunksize + 1);
-                        if (chunk_buf == 0)
-                        {
-                            st_fail_errno(ft, ST_ENOMEM, "Unable to alloc memory");
-                            return(ST_EOF);
-                        }
+                        chunk_buf = (char *) xmalloc(chunksize + 1);
                         if (st_readbuf(ft, chunk_buf,1,(size_t)chunksize)
                                         != chunksize)
                         {
--- a/src/FFT.c
+++ b/src/FFT.c
@@ -58,13 +58,7 @@
 int **gFFTBitTable = NULL;
 const int MaxFastBits = 16;
 
-/* Declare Static functions */
-static int IsPowerOfTwo(int x);
-static int NumberOfBitsNeeded(int PowerOfTwo);
-static int ReverseBits(int index, int NumBits);
-static void InitFFT(void);
-
-int IsPowerOfTwo(int x)
+static int IsPowerOfTwo(int x)
 {
    if (x < 2)
       return 0;
@@ -72,7 +66,7 @@
    return !(x & (x-1));         /* Thanks to 'byang' for this cute trick! */
 }
 
-int NumberOfBitsNeeded(int PowerOfTwo)
+static int NumberOfBitsNeeded(int PowerOfTwo)
 {
    int i;
 
@@ -86,7 +80,7 @@
          return i;
 }
 
-int ReverseBits(int index, int NumBits)
+static int ReverseBits(int index, int NumBits)
 {
    int i, rev;
 
@@ -99,25 +93,19 @@
 }
 
 
-/* This function allocates about 250k (actually (2**16)-2 ints) which is never
- * freed, to use as a lookup table for bit-reversal. The good news is that
- * we bascially need this until the very end, so the fact that it's not freed
- * is OK. */
-void InitFFT(void)
+/* This function permanently allocates about 250Kb (actually (2**16)-2 ints). */
+static void InitFFT(void)
 {
    int len, b;
-
-   gFFTBitTable = (int**)calloc(MaxFastBits, sizeof(*gFFTBitTable));
    
-   len = 2;
-   for (b = 1; b <= MaxFastBits; b++) {
+   gFFTBitTable = (int**)xcalloc(MaxFastBits, sizeof(*gFFTBitTable));
+   
+   for (b = 1, len = 2; b <= MaxFastBits; b++) {
       int i;
 
-      gFFTBitTable[b - 1] = (int*)calloc(len, sizeof(**gFFTBitTable));
-
-      for (i = 0; i < len; i++) {
+      gFFTBitTable[b - 1] = (int*)xcalloc(len, sizeof(**gFFTBitTable));
+      for (i = 0; i < len; i++)
         gFFTBitTable[b - 1][i] = ReverseBits(i, b);
-      }
 
       len <<= 1;
    }
@@ -129,7 +117,6 @@
 /*
  * Complex Fast Fourier Transform
  */
-
 void FFT(int NumSamples,
          int InverseTransform,
          const float *RealIn, float *ImagIn, float *RealOut, float *ImagOut)
@@ -243,8 +230,7 @@
 void RealFFT(int NumSamples, const float *RealIn, float *RealOut, float *ImagOut)
 {
    int Half = NumSamples / 2;
-   int i;
-
+   int i, i3;
    float theta = M_PI / Half;
    float wtemp = (float) sin(0.5 * theta);
    float wpr = -2.0 * wtemp * wtemp;
@@ -251,13 +237,11 @@
    float wpi = (float) sin(theta);
    float wr = 1.0 + wpr;
    float wi = wpi;
-
-   int i3;
-
    float h1r, h1i, h2r, h2i;
+   float *tmpReal, *tmpImag;
 
-   float *tmpReal = (float*)calloc(Half, sizeof(float));
-   float *tmpImag = (float*)calloc(Half, sizeof(float));
+   tmpReal = (float*)xcalloc(NumSamples, sizeof(float));
+   tmpImag = tmpReal + Half;
 
    for (i = 0; i < Half; i++) {
       tmpReal[i] = RealIn[2 * i];
@@ -267,29 +251,28 @@
    FFT(Half, 0, tmpReal, tmpImag, RealOut, ImagOut);
 
    for (i = 1; i < Half / 2; i++) {
-      i3 = Half - i;
-
-      h1r = 0.5 * (RealOut[i] + RealOut[i3]);
-      h1i = 0.5 * (ImagOut[i] - ImagOut[i3]);
-      h2r = 0.5 * (ImagOut[i] + ImagOut[i3]);
-      h2i = -0.5 * (RealOut[i] - RealOut[i3]);
-
-      RealOut[i] = h1r + wr * h2r - wi * h2i;
-      ImagOut[i] = h1i + wr * h2i + wi * h2r;
-      RealOut[i3] = h1r - wr * h2r + wi * h2i;
-      ImagOut[i3] = -h1i + wr * h2i + wi * h2r;
-
-      wtemp = wr;
-      wr = wr * wpr - wi * wpi + wr;
-      wi = wi * wpr + wtemp * wpi + wi;
+     i3 = Half - i;
+       
+     h1r = 0.5 * (RealOut[i] + RealOut[i3]);
+     h1i = 0.5 * (ImagOut[i] - ImagOut[i3]);
+     h2r = 0.5 * (ImagOut[i] + ImagOut[i3]);
+     h2i = -0.5 * (RealOut[i] - RealOut[i3]);
+       
+     RealOut[i] = h1r + wr * h2r - wi * h2i;
+     ImagOut[i] = h1i + wr * h2i + wi * h2r;
+     RealOut[i3] = h1r - wr * h2r + wi * h2i;
+     ImagOut[i3] = -h1i + wr * h2i + wi * h2r;
+       
+     wtemp = wr;
+     wr = wr * wpr - wi * wpi + wr;
+     wi = wi * wpr + wtemp * wpi + wi;
    }
-
+     
    h1r = RealOut[0];
    RealOut[0] += ImagOut[0];
    ImagOut[0] = h1r - ImagOut[0];
 
    free(tmpReal);
-   free(tmpImag);
 }
 
 /*
@@ -318,10 +301,10 @@
 
   theta = M_PI / Half;
 
-  tmpReal = (float*)calloc(Half, sizeof(float));
-  tmpImag = (float*)calloc(Half, sizeof(float));
-  RealOut = (float*)calloc(Half, sizeof(float));
-  ImagOut = (float*)calloc(Half, sizeof(float));
+  tmpReal = (float*)xcalloc(Half * 4, sizeof(float));
+  tmpImag = tmpReal + Half;
+  RealOut = tmpImag + Half;
+  ImagOut = RealOut + Half;
 
   for (i = 0; i < Half; i++) {
     tmpReal[i] = In[2 * i];
@@ -329,7 +312,7 @@
   }
 
   FFT(Half, 0, tmpReal, tmpImag, RealOut, ImagOut);
-
+  
   wtemp = (float) sin(0.5 * theta);
 
   wpr = -2.0 * wtemp * wtemp;
@@ -336,9 +319,9 @@
   wpi = (float) sin(theta);
   wr = 1.0 + wpr;
   wi = wpi;
-
+    
   for (i = 1; i < Half / 2; i++) {
-
+      
     i3 = Half - i;
     
     h1r = 0.5 * (RealOut[i] + RealOut[i3]);
@@ -369,9 +352,6 @@
   Out[Half / 2] = rt * rt + it * it;
   
   free(tmpReal);
-  free(tmpImag);
-  free(RealOut);
-  free(ImagOut);
 }
 
 /*
--- a/src/FFT.h
+++ b/src/FFT.h
@@ -1,5 +1,4 @@
 /*
- *
  * FFT.h
  *
  * Based on FFT.h from Audacity, with the following permission from
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -17,7 +17,8 @@
 	  reverb.c reverse.c silence.c speed.c stat.c \
 	  stretch.c swap.c synth.c tone.c trim.c vibro.c vol.c
 
-libst_a_SOURCES = $(formats) $(effects) alsa.c oss.c sunaudio.c handlers.c misc.c stio.c util.c getopt.c getopt1.c
+libst_a_SOURCES = $(formats) $(effects) alsa.c oss.c sunaudio.c handlers.c misc.c \
+	  stio.c util.c xmalloc.c getopt.c getopt1.c
 
 sox_SOURCES = sox.c
 sox_LDADD = libst.a libgsm/libgsm.a
--- a/src/Makefile.in
+++ b/src/Makefile.in
@@ -89,8 +89,8 @@
 	vol.$(OBJEXT)
 am_libst_a_OBJECTS = $(am__objects_1) $(am__objects_2) alsa.$(OBJEXT) \
 	oss.$(OBJEXT) sunaudio.$(OBJEXT) handlers.$(OBJEXT) \
-	misc.$(OBJEXT) stio.$(OBJEXT) util.$(OBJEXT) getopt.$(OBJEXT) \
-	getopt1.$(OBJEXT)
+	misc.$(OBJEXT) stio.$(OBJEXT) util.$(OBJEXT) xmalloc.$(OBJEXT) \
+	getopt.$(OBJEXT) getopt1.$(OBJEXT)
 libst_a_OBJECTS = $(am_libst_a_OBJECTS)
 binPROGRAMS_INSTALL = $(INSTALL_PROGRAM)
 PROGRAMS = $(bin_PROGRAMS)
@@ -217,7 +217,9 @@
 	  reverb.c reverse.c silence.c speed.c stat.c \
 	  stretch.c swap.c synth.c tone.c trim.c vibro.c vol.c
 
-libst_a_SOURCES = $(formats) $(effects) alsa.c oss.c sunaudio.c handlers.c misc.c stio.c util.c getopt.c getopt1.c
+libst_a_SOURCES = $(formats) $(effects) alsa.c oss.c sunaudio.c handlers.c misc.c \
+	  stio.c util.c xmalloc.c getopt.c getopt1.c
+
 sox_SOURCES = sox.c
 sox_LDADD = libst.a libgsm/libgsm.a
 sox_LDFLAGS = @SAMPLERATE_LIBS@
@@ -434,6 +436,7 @@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/wav.Po@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/wve.Po@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/xa.Po@am__quote@
+@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/xmalloc.Po@am__quote@
 
 .c.o:
 @am__fastdepCC_TRUE@	if $(COMPILE) -MT $@ -MD -MP -MF "$(DEPDIR)/$*.Tpo" -c -o $@ $<; \
--- a/src/aiff.c
+++ b/src/aiff.c
@@ -554,12 +554,7 @@
   uint32_t chunksize;
   st_readdw(ft, &chunksize);
   /* allocate enough memory to hold the text including a terminating \0 */
-  *text = (char *) malloc((size_t) chunksize + 1);
-  if (*text == NULL)
-  {
-    st_fail_errno(ft,ST_ENOMEM,"AIFF: Couldn't allocate %s header", chunkDescription);
-    return(ST_EOF);
-  }
+  *text = (char *) xmalloc((size_t) chunksize + 1);
   if (st_readbuf(ft, *text, 1, chunksize) != chunksize)
   {
     st_fail_errno(ft,ST_EOF,"AIFF: Unexpected EOF in %s header", chunkDescription);
@@ -605,16 +600,12 @@
     totalCommentLength += commentLength;
     /* allocate enough memory to hold the text including a terminating \0 */
     if(commentIndex == 0) {
-      *text = (char *) malloc((size_t) totalCommentLength + 1);
+      *text = (char *) xmalloc((size_t) totalCommentLength + 1);
     }
     else {
-      *text = realloc(*text, (size_t) totalCommentLength + 1);
+      *text = xrealloc(*text, (size_t) totalCommentLength + 1);
     }
 
-    if (*text == NULL) {
-        st_fail_errno(ft,ST_ENOMEM,"AIFF: Couldn't allocate %s header", chunkDescription);
-        return(ST_EOF);
-    }
     if (st_readbuf(ft, *text + totalCommentLength - commentLength, 1, commentLength) != commentLength) {
         st_fail_errno(ft,ST_EOF,"AIFF: Unexpected EOF in %s header", chunkDescription);
         return(ST_EOF);
--- a/src/alsa.c
+++ b/src/alsa.c
@@ -91,7 +91,8 @@
         else if (ft->info.channels < min_chan) 
             ft->info.channels = min_chan;
 
-    snd_pcm_format_mask_malloc(&fmask);
+    if (snd_pcm_format_mask_malloc(&fmask) < 0)
+        goto open_error;
     snd_pcm_hw_params_get_format_mask(hw_params, fmask);
 
     if (get_format(ft, fmask, &fmt) < 0)
@@ -224,14 +225,7 @@
     }
 
     alsa->buf_size = buffer_size * ft->info.size * ft->info.channels;
-
-    if ((alsa->buf = malloc(alsa->buf_size)) == NULL) 
-    {
-        st_fail_errno(ft,ST_ENOMEM,
-                      "unable to allocate output buffer of size %d", 
-                      alsa->buf_size);
-        return(ST_EOF);
-    }
+    alsa->buf = xmalloc(alsa->buf_size);
 
     return (ST_SUCCESS);
 
--- a/src/au.c
+++ b/src/au.c
@@ -255,7 +255,8 @@
         hdr_size -= SUN_HDRSIZE; /* #bytes already read */
         if (hdr_size > 0) {
                 /* Allocate comment buffer */
-                buf = (char *) malloc(hdr_size+1);              
+                buf = (char *) xmalloc(hdr_size+1);
+                
                 for(i = 0; i < hdr_size; i++) {
                         st_readb(ft, (unsigned char *)&(buf[i]));
                         if (st_eof(ft))
@@ -265,7 +266,7 @@
                         }
                 }
                 /* Buffer should already be null terminated but
-                 * just in case we malloced an extra byte and 
+                 * just in case we xmalloced an extra byte and 
                  * force the last byte to be 0 anyways.
                  * This should help work with a greater array of
                  * software.
--- a/src/chorus.c
+++ b/src/chorus.c
@@ -206,25 +206,20 @@
                         return (ST_EOF);
                 }
                 chorus->length[i] = effp->ininfo.rate / chorus->speed[i];
-                if (! (chorus->lookup_tab[i] = 
-                        (int *) malloc(sizeof (int) * chorus->length[i])))
-                {
-                        st_fail("chorus: Cannot malloc %d bytes!", 
-                                sizeof(int) * chorus->length[i]);
-                        return (ST_EOF);
-                }
-    if (chorus->modulation[i] == MOD_SINE)
-      st_generate_wave_table(ST_WAVE_SINE, ST_INT, chorus->lookup_tab[i],
-          chorus->length[i], 0, chorus->depth_samples[i], 0);
-    else
-      st_generate_wave_table(ST_WAVE_TRIANGLE, ST_INT, chorus->lookup_tab[i], 
-          chorus->length[i],
-          chorus->samples[i] - 1 - 2 * chorus->depth_samples[i],
-          chorus->samples[i] - 1, 3 * M_PI_2);
+                chorus->lookup_tab[i] = (int *) xmalloc(sizeof (int) * chorus->length[i]);
+
+                if (chorus->modulation[i] == MOD_SINE)
+                  st_generate_wave_table(ST_WAVE_SINE, ST_INT, chorus->lookup_tab[i],
+                                         chorus->length[i], 0, chorus->depth_samples[i], 0);
+                else
+                  st_generate_wave_table(ST_WAVE_TRIANGLE, ST_INT, chorus->lookup_tab[i], 
+                                         chorus->length[i],
+                                         chorus->samples[i] - 1 - 2 * chorus->depth_samples[i],
+                                         chorus->samples[i] - 1, 3 * M_PI_2);
                 chorus->phase[i] = 0;
-
+                
                 if ( chorus->samples[i] > chorus->maxsamples )
-                        chorus->maxsamples = chorus->samples[i];
+                  chorus->maxsamples = chorus->samples[i];
         }
 
         /* Be nice and check the hint with warning, if... */
@@ -235,13 +230,7 @@
         st_warn("chorus: warning >>> gain-out can cause saturation or clipping of output <<<");
 
 
-        if (! (chorus->chorusbuf = 
-                (float *) malloc(sizeof (float) * chorus->maxsamples)))
-        {
-                st_fail("chorus: Cannot malloc %d bytes!", 
-                        sizeof(float) * chorus->maxsamples);
-                return (ST_EOF);
-        }
+        chorus->chorusbuf = (float *) xmalloc(sizeof (float) * chorus->maxsamples);
         for ( i = 0; i < chorus->maxsamples; i++ )
                 chorus->chorusbuf[i] = 0.0;
 
--- a/src/compand.c
+++ b/src/compand.c
@@ -96,13 +96,9 @@
       }
 
       rates = 1 + commas/2;
-      if ((l->attackRate = (double *)malloc(sizeof(double) * rates)) == NULL ||
-          (l->decayRate  = (double *)malloc(sizeof(double) * rates)) == NULL ||
-          (l->volume     = (double *)malloc(sizeof(double) * rates)) == NULL)
-      {
-        st_fail("Out of memory");
-        return (ST_EOF);
-      }
+      l->attackRate = (double *)xmalloc(sizeof(double) * rates);
+      l->decayRate = (double *)xmalloc(sizeof(double) * rates);
+      l->volume = (double *)xmalloc(sizeof(double) * rates);
       l->expectedChannels = rates;
       l->delay_buf = NULL;
 
@@ -130,12 +126,8 @@
       }
 
       tfers = 3 + commas/2; /* 0, 0 at start; 1, 1 at end */
-      if ((l->transferIns  = (double *)malloc(sizeof(double) * tfers)) == NULL ||
-          (l->transferOuts = (double *)malloc(sizeof(double) * tfers)) == NULL)
-      {
-        st_fail("Out of memory");
-        return (ST_EOF);
-      }
+      l->transferIns = (double *)xmalloc(sizeof(double) * tfers);
+      l->transferOuts = (double *)xmalloc(sizeof(double) * tfers);
       l->transferPoints = tfers;
       l->transferIns[0] = 0.0; l->transferOuts[0] = 0.0;
       l->transferIns[tfers-1] = 1.0; l->transferOuts[tfers-1] = 1.0;
@@ -230,11 +222,8 @@
 
   /* Allocate the delay buffer */
   l->delay_buf_size = l->delay * effp->outinfo.rate * effp->outinfo.channels;
-  if (l->delay_buf_size > 0
-   && (l->delay_buf = (st_sample_t *)malloc(sizeof(long) * l->delay_buf_size)) == NULL) {
-    st_fail("Out of memory");
-    return (ST_EOF);
-  }
+  if (l->delay_buf_size > 0)
+    l->delay_buf = (st_sample_t *)xmalloc(sizeof(long) * l->delay_buf_size);
   for (i = 0;  i < l->delay_buf_size;  i++)
     l->delay_buf[i] = 0;
   l->delay_buf_ptr = 0;
--- a/src/earwax.c
+++ b/src/earwax.c
@@ -89,12 +89,7 @@
   }
 
   /* allocate tap memory */
-  earwax->tap = (st_sample_t*)malloc( sizeof(st_sample_t) * EARWAX_NUMTAPS );
-  if( !earwax->tap ){
-    st_fail("earwax: Cannot malloc %d bytes!", 
-            sizeof(st_sample_t) * EARWAX_NUMTAPS );
-    return (ST_EOF);
-  }
+  earwax->tap = (st_sample_t*)xmalloc( sizeof(st_sample_t) * EARWAX_NUMTAPS );
 
   /* zero out the delayed taps */
   for(i=0; i < EARWAX_NUMTAPS; i++ ){
--- a/src/echo.c
+++ b/src/echo.c
@@ -9,7 +9,7 @@
 
 /*
  * This is the "echo.c" while the old "echo.c" from version 12 moves to
- * "reverb.c" satisfying the defintions made in the Guitar FX FAQ.
+ * "reverb.c" satisfying the definitions made in the Guitar FX FAQ.
  *
  *
  * Echo effect for dsp.
@@ -162,12 +162,7 @@
                 if ( echo->samples[i] > echo->maxsamples )
                         echo->maxsamples = echo->samples[i];
         }
-        if (! (echo->delay_buf = (double *) malloc(sizeof (double) * echo->maxsamples)))
-        {
-                st_fail("echo: Cannot malloc %d bytes!", 
-                        sizeof(long) * echo->maxsamples);
-                return (ST_EOF);
-        }
+        echo->delay_buf = (double *) xmalloc(sizeof (double) * echo->maxsamples);
         for ( j = 0; j < echo->maxsamples; ++j )
                 echo->delay_buf[j] = 0.0;
         /* Be nice and check the hint with warning, if... */
--- a/src/echos.c
+++ b/src/echos.c
@@ -156,12 +156,7 @@
                 echos->pointer[i] = echos->sumsamples;
                 echos->sumsamples += echos->samples[i];
         }
-        if (! (echos->delay_buf = (double *) malloc(sizeof (double) * echos->sumsamples)))
-        {
-                st_fail("echos: Cannot malloc %d bytes!", 
-                        sizeof(double) * echos->sumsamples);
-                return(ST_EOF);
-        }
+        echos->delay_buf = (double *) xmalloc(sizeof (double) * echos->sumsamples);
         for ( j = 0; j < echos->sumsamples; ++j )
                 echos->delay_buf[j] = 0.0;
         /* Be nice and check the hint with warning, if... */
--- a/src/fade.c
+++ b/src/fade.c
@@ -78,12 +78,7 @@
         fade->out_fadetype = 'l';
     }
 
-    fade->in_stop_str = (char *)malloc(strlen(argv[0])+1);
-    if (!fade->in_stop_str)
-    {
-        st_fail("Could not allocate memory");
-        return (ST_EOF);
-    }
+    fade->in_stop_str = (char *)xmalloc(strlen(argv[0])+1);
     strcpy(fade->in_stop_str,argv[0]);
     /* Do a dummy parse to see if it will fail */
     if (st_parsesamples(0, fade->in_stop_str, &fade->in_stop, 't') !=
@@ -100,39 +95,27 @@
         /* See if there is fade-in/fade-out times/curves specified. */
         if(t_argno == 1)
         {
-            fade->out_stop_str = (char *)malloc(strlen(argv[t_argno])+1);
-            if (!fade->out_stop_str)
-            {
-                st_fail("Could not allocate memory");
-                return (ST_EOF);
-            }
-             strcpy(fade->out_stop_str,argv[t_argno]);
+            fade->out_stop_str = (char *)xmalloc(strlen(argv[t_argno])+1);
+            strcpy(fade->out_stop_str,argv[t_argno]);
 
-             /* Do a dummy parse to see if it will fail */
-             if (st_parsesamples(0, fade->out_stop_str, 
-                         &fade->out_stop, 't') != ST_SUCCESS)
-             {
-                 st_fail(st_fade_effect.usage);
-                 return(ST_EOF);
-             }
+            /* Do a dummy parse to see if it will fail */
+            if (st_parsesamples(0, fade->out_stop_str, 
+                                &fade->out_stop, 't') != ST_SUCCESS) {
+              st_fail(st_fade_effect.usage);
+              return(ST_EOF);
+            }
         }
         else
         {
-            fade->out_start_str = (char *)malloc(strlen(argv[t_argno])+1);
-            if (!fade->out_start_str)
-            {
-                st_fail("Could not allocate memory");
-                return (ST_EOF);
-            }
-             strcpy(fade->out_start_str,argv[t_argno]);
+            fade->out_start_str = (char *)xmalloc(strlen(argv[t_argno])+1);
+            strcpy(fade->out_start_str,argv[t_argno]);
 
-             /* Do a dummy parse to see if it will fail */
-             if (st_parsesamples(0, fade->out_start_str, 
-                         &fade->out_start, 't') != ST_SUCCESS)
-             {
-                 st_fail(st_fade_effect.usage);
-                 return(ST_EOF);
-             }
+            /* Do a dummy parse to see if it will fail */
+            if (st_parsesamples(0, fade->out_start_str, 
+                                &fade->out_start, 't') != ST_SUCCESS) {
+              st_fail(st_fade_effect.usage);
+              return(ST_EOF);
+            }
         }
     } /* End for(t_argno) */
 
--- a/src/filter.c
+++ b/src/filter.c
@@ -125,7 +125,7 @@
         }
         
         Xh = f->Nwin/2;
-        Fp0 = (double *) malloc(sizeof(double) * (Xh + 2)) + 1;
+        Fp0 = (double *) xmalloc(sizeof(double) * (Xh + 2)) + 1;
         if (f->freq0 > (st_sample_t)f->rate/200) {
                 Xh0 = makeFilter(Fp0, Xh, 2.0*(double)f->freq0/f->rate, f->beta, 1, 0);
                 if (Xh0 <= 1)
@@ -136,7 +136,7 @@
         } else {
                 Xh0 = 0;
         }
-        Fp1 = (double *) malloc(sizeof(double) * (Xh + 2)) + 1;
+        Fp1 = (double *) xmalloc(sizeof(double) * (Xh + 2)) + 1;
         /* need Fp[-1] and Fp[Xh] for makeFilter */
         if (f->freq1 < (st_sample_t)f->rate/2) {
                 Xh1 = makeFilter(Fp1, Xh, 2.0*(double)f->freq1/f->rate, f->beta, 1, 0);
@@ -169,7 +169,7 @@
         f->Xh = Xh;
         f->Xt = Xh;
 
-        f->X = (double *) malloc(sizeof(double) * (2*BUFFSIZE + 2*Xh));
+        f->X = (double *) xmalloc(sizeof(double) * (2*BUFFSIZE + 2*Xh));
         f->Y = f->X + BUFFSIZE + 2*Xh;
 
         /* Need Xh zeros at beginning of X */
--- a/src/flac.c
+++ b/src/flac.c
@@ -76,12 +76,7 @@
       comment_size += metadata->data.vorbis_comment.comments[i].length + 1;
     }
 
-    if ((format->comment = (char *) calloc(comment_size, sizeof(char))) == NULL)
-    {
-      st_fail_errno(format, ST_ENOMEM, "FLAC: Could not allocate memory");
-      decoder->eof = true;
-      return;
-    }
+    format->comment = (char *) xcalloc(comment_size, sizeof(char));
 
     for (i = 0; i < metadata->data.vorbis_comment.num_comments; ++i)
     {
@@ -271,12 +266,12 @@
 
   memset(encoder, 0, sizeof(*encoder));
   encoder->flac = FLAC__stream_encoder_new();
-  encoder->decoded_samples = malloc(ST_BUFSIZ * sizeof(FLAC__int32));
-  if (encoder->flac == NULL || encoder->decoded_samples == NULL)
+  if (encoder->flac == NULL)
   {
     st_fail_errno(format, ST_ENOMEM, "FLAC ERROR creating the encoder instance");
     return ST_EOF;
   }
+  encoder->decoded_samples = xmalloc(ST_BUFSIZ * sizeof(FLAC__int32));
 
   {     /* Select and set FLAC encoder options: */
     static struct
@@ -371,12 +366,9 @@
     if (strchr(format->comment, '=') == NULL) 
     {
       static const char prepend[] = "COMMENT=";
-      comments = malloc(strlen(format->comment) + sizeof(prepend));
-      if (comments != NULL)
-      {
-        strcpy(comments, prepend);
-        strcat(comments, format->comment);
-      }
+      comments = xmalloc(strlen(format->comment) + sizeof(prepend));
+      strcpy(comments, prepend);
+      strcat(comments, format->comment);
     }
     else
     {
--- a/src/flanger.c
+++ b/src/flanger.c
@@ -200,22 +200,12 @@
     (f->delay_min + f->delay_depth) / 1000 * effp->ininfo.rate + 0.5;
   ++f->delay_buf_length;  /* Need 0 to n, i.e. n + 1. */
   ++f->delay_buf_length;  /* Quadratic interpolator needs one more. */
-  for (c = 0; c < channels; ++c) {
-    f->delay_bufs[c] = calloc(f->delay_buf_length, sizeof(*f->delay_bufs[0]));
-    if (f->delay_bufs[c] == NULL)
-    {
-      st_fail("Cannot allocate memory for delay_bufs");
-      return ST_EOF;
-    }
-  }
+  for (c = 0; c < channels; ++c)
+    f->delay_bufs[c] = xcalloc(f->delay_buf_length, sizeof(*f->delay_bufs[0]));
 
   /* Create the LFO lookup table: */
   f->lfo_length = effp->ininfo.rate / f->speed;
-  f->lfo = calloc(f->lfo_length, sizeof(*f->lfo));
-  if (f->lfo == NULL) {
-    st_fail("Cannot allocate memory for lfo");
-    return ST_EOF;
-  }
+  f->lfo = xcalloc(f->lfo_length, sizeof(*f->lfo));
   st_generate_wave_table(
       f->wave_shape,
       ST_FLOAT,
--- a/src/getopt.c
+++ b/src/getopt.c
@@ -264,7 +264,7 @@
 #ifdef _LIBC
 /* Stored original parameters.
    XXX This is no good solution.  We should rather copy the args so
-   that we can compare them later.  But we must not use malloc(3).  */
+   that we can compare them later.  But we must not use xmalloc(3).  */
 extern int __libc_argc;
 extern char **__libc_argv;
 
@@ -329,7 +329,7 @@
     {
       /* We must extend the array.  The user plays games with us and
          presents new arguments.  */
-      char *new_str = malloc (top + 1);
+      char *new_str = xmalloc (top + 1);
       if (new_str == NULL)
         nonoption_flags_len = nonoption_flags_max_len = 0;
       else
@@ -441,7 +441,7 @@
               if (nonoption_flags_max_len < argc)
                 nonoption_flags_max_len = argc;
               __getopt_nonoption_flags =
-                (char *) malloc (nonoption_flags_max_len);
+                (char *) xmalloc (nonoption_flags_max_len);
               if (__getopt_nonoption_flags == NULL)
                 nonoption_flags_max_len = -1;
               else
--- a/src/gsm.c
+++ b/src/gsm.c
@@ -81,8 +81,8 @@
                         return (ST_EOF);
                 }
         }
-        p->frames = (gsm_byte*) malloc(p->channels*FRAMESIZE);
-        p->samples = (gsm_signal*) malloc(BLOCKSIZE * (p->channels+1) * sizeof(gsm_signal));
+        p->frames = (gsm_byte*) xmalloc(p->channels*FRAMESIZE);
+        p->samples = (gsm_signal*) xmalloc(BLOCKSIZE * (p->channels+1) * sizeof(gsm_signal));
         p->sampleTop = p->samples + BLOCKSIZE*p->channels;
         p->samplePtr = (w)? p->samples : p->sampleTop;
         return (ST_SUCCESS);
--- a/src/hcom.c
+++ b/src/hcom.c
@@ -132,12 +132,7 @@
         ft->info.channels = 1;
 
         /* Allocate memory for the dictionary */
-        p->dictionary = (dictent *) malloc(511 * sizeof(dictent));
-        if (p->dictionary == NULL)
-        {
-                st_fail_errno(ft,ST_ENOMEM,"can't malloc memory for Huffman dictionary");
-                return (ST_EOF);
-        }
+        p->dictionary = (dictent *) xmalloc(511 * sizeof(dictent));
 
         /* Read dictionary */
         for(i = 0; i < dictsize; i++) {
@@ -250,7 +245,7 @@
 }
 
 struct writepriv {
-        unsigned char *data;    /* Buffer allocated with malloc */
+        unsigned char *data;    /* Buffer allocated with xmalloc */
         unsigned int size;      /* Size of allocated buffer */
         unsigned int pos;       /* Where next byte goes */
 };
@@ -285,12 +280,7 @@
 
         p->size = BUFINCR;
         p->pos = 0;
-        p->data = (unsigned char *) malloc(p->size);
-        if (p->data == NULL)
-        {
-                st_fail_errno(ft,ST_ENOMEM,"can't malloc buffer for uncompressed HCOM data");
-                return (ST_EOF);
-        }
+        p->data = (unsigned char *) xmalloc(p->size);
         return (ST_SUCCESS);
 }
 
@@ -305,12 +295,7 @@
 
         if (p->pos + len > p->size) {
                 p->size = ((p->pos + len) / BUFINCR + 1) * BUFINCR;
-                p->data = (unsigned char *) realloc(p->data, p->size);
-                if (p->data == NULL)
-                {
-                    st_fail_errno(ft,ST_ENOMEM,"can't realloc buffer for uncompressed HCOM data");
-                    return (0);
-                }
+                p->data = (unsigned char *) xrealloc(p->data, p->size);
         }
 
         while (len-- > 0) {
@@ -361,7 +346,7 @@
   }
 }
 
-static int compress(ft_t ft, unsigned char **df, int32_t *dl, float fr)
+static void compress(ft_t ft, unsigned char **df, int32_t *dl, float fr)
 {
   struct readpriv *p = (struct readpriv *) ft->priv;
   int32_t samplerate;
@@ -434,10 +419,7 @@
   l = (((l + 31) >> 5) << 2) + 24 + dictsize * 4;
   st_debug("  Original size: %6d bytes", *dl);
   st_debug("Compressed size: %6d bytes", l);
-  if((datafork = (unsigned char *)malloc((unsigned)l)) == NULL)
-  {
-    return (ST_ENOMEM);
-  }
+  datafork = (unsigned char *)xmalloc((unsigned)l);
   ddf = datafork + 22;
   for(i = 0; i < dictsize; i++) {
     put16_be(&ddf, p->dictionary[i].dict_leftson);
@@ -465,8 +447,6 @@
   put16_be(&dfp, dictsize);
   *df = datafork;               /* reassign passed pointer to new datafork */
   *dl = l;                      /* and its compressed length */
-
-  return (ST_SUCCESS);
 }
 
 /* End of hcom utility routines */
@@ -479,13 +459,8 @@
         int rc;
 
         /* Compress it all at once */
-        rc = compress(ft, &compressed_data, (int32_t *)&compressed_len, (double) ft->info.rate);
+        compress(ft, &compressed_data, (int32_t *)&compressed_len, (double) ft->info.rate);
         free((char *) p->data);
-
-        if (rc){
-        st_fail_errno(ft, rc,"can't malloc buffer for compressed HCOM data");
-            return 0;
-        }
 
         /* Write the header */
         st_writebuf(ft, (void *)"\000\001A", 1, 3); /* Dummy file name "A" */
--- a/src/maud.c
+++ b/src/maud.c
@@ -174,12 +174,7 @@
                         st_readdw(ft, &chunksize);
                         if (chunksize & 1)
                                 chunksize++;
-                        chunk_buf = (char *) malloc(chunksize + 1);
-                        if (!chunk_buf)
-                        {
-                            st_fail_errno(ft,ST_ENOMEM,"Couldn't alloc resources");
-                            return(ST_EOF);
-                        }
+                        chunk_buf = (char *) xmalloc(chunksize + 1);
                         if (st_readbuf(ft, chunk_buf, 1, (int)chunksize) 
                             != chunksize)
                         {
--- a/src/mcompand.c
+++ b/src/mcompand.c
@@ -94,16 +94,8 @@
 static int lowpass_setup (butterworth_crossover_t butterworth, double frequency, st_rate_t rate, int nchan) {
   double c;
 
-  if (! (butterworth->xy_low = (struct xy *)malloc(nchan * sizeof(struct xy)))) {
-    st_fail("Out of memory");
-    return (ST_EOF);
-  }
-  memset(butterworth->xy_low,0,nchan * sizeof(struct xy));
-  if (! (butterworth->xy_high = (struct xy *)malloc(nchan * sizeof(struct xy)))) {
-    st_fail("Out of memory");
-    return (ST_EOF);
-  }
-  memset(butterworth->xy_high,0,nchan * sizeof(struct xy));
+  butterworth->xy_low = (struct xy *)xcalloc(nchan, sizeof(struct xy));
+  butterworth->xy_high = (struct xy *)xcalloc(nchan, sizeof(struct xy));
 
   /* lowpass setup */
   butterworth->frequency_low = frequency/1.3;
@@ -250,21 +242,10 @@
       }
 
       rates = 1 + commas/2;
-      if ((l->attackRate = (double *)malloc(sizeof(double) * rates)) == NULL ||
-          (l->decayRate  = (double *)malloc(sizeof(double) * rates)) == NULL)
-      {
-        st_fail("Out of memory");
-        return (ST_EOF);
-      }
-
-      if ((l->volume = (double *)malloc(sizeof(double) * rates)) == NULL)
-      {
-        st_fail("Out of memory");
-        return (ST_EOF);
-      }
-
+      l->attackRate = (double *)xmalloc(sizeof(double) * rates);
+      l->decayRate  = (double *)xmalloc(sizeof(double) * rates);
+      l->volume = (double *)xmalloc(sizeof(double) * rates);
       l->expectedChannels = rates;
-
       l->delay_buf = NULL;
 
       /* Now tokenise the rates string and set up these arrays.  Keep
@@ -291,12 +272,8 @@
       }
 
       tfers = 3 + commas/2; /* 0, 0 at start; 1, 1 at end */
-      if ((l->transferIns  = (double *)malloc(sizeof(double) * tfers)) == NULL ||
-          (l->transferOuts = (double *)malloc(sizeof(double) * tfers)) == NULL)
-      {
-        st_fail("Out of memory");
-        return (ST_EOF);
-      }
+      l->transferIns  = (double *)xmalloc(sizeof(double) * tfers);
+      l->transferOuts = (double *)xmalloc(sizeof(double) * tfers);
       l->transferPoints = tfers;
       l->transferIns[0] = 0.0; l->transferOuts[0] = 0.0;
       l->transferIns[tfers-1] = 1.0; l->transferOuts[tfers-1] = 1.0;
@@ -397,11 +374,7 @@
   }
   c->nBands = (n+1)>>1;
 
-  if (! (c->bands = (struct comp_band *)malloc(c->nBands * sizeof(struct comp_band)))) {
-    st_fail("Out of memory");
-    return ST_EOF;
-  }
-  memset(c->bands,0,c->nBands * sizeof(struct comp_band));
+  c->bands = (struct comp_band *)xcalloc(c->nBands, sizeof(struct comp_band));
 
   for (i=0;i<c->nBands;++i) {
     len = strlen(argv[i<<1]);
@@ -463,15 +436,8 @@
     }
 
     /* Allocate the delay buffer */
-    if (c->delay_buf_size > 0) {
-      if ((l->delay_buf = (st_sample_t *)malloc(sizeof(long) * c->delay_buf_size)) == NULL) {
-        st_fail("Out of memory");
-        return (ST_EOF);
-      }
-      for (i = 0;  i < c->delay_buf_size;  i++)
-        l->delay_buf[i] = 0;
-
-    }
+    if (c->delay_buf_size > 0)
+      l->delay_buf = (st_sample_t *)xcalloc(sizeof(long), c->delay_buf_size);
     l->delay_buf_ptr = 0;
     l->delay_buf_cnt = 0;
 
@@ -584,16 +550,13 @@
   double out;
 
   if (c->band_buf_len < len) {
-    if ((! (c->band_buf1 = (st_sample_t *)realloc(c->band_buf1,len*sizeof(st_sample_t)))) ||
-        (! (c->band_buf2 = (st_sample_t *)realloc(c->band_buf2,len*sizeof(st_sample_t)))) ||
-        (! (c->band_buf3 = (st_sample_t *)realloc(c->band_buf3,len*sizeof(st_sample_t))))) {
-      st_fail("Out of memory");
-      return (ST_EOF);
-    }
+    c->band_buf1 = (st_sample_t *)xrealloc(c->band_buf1,len*sizeof(st_sample_t));
+    c->band_buf2 = (st_sample_t *)xrealloc(c->band_buf2,len*sizeof(st_sample_t));
+    c->band_buf3 = (st_sample_t *)xrealloc(c->band_buf3,len*sizeof(st_sample_t));
     c->band_buf_len = len;
   }
 
-  ibuf_copy = (st_sample_t *)malloc(*isamp * sizeof(st_sample_t));
+  ibuf_copy = (st_sample_t *)xmalloc(*isamp * sizeof(st_sample_t));
   memcpy(ibuf_copy, ibuf, *isamp * sizeof(st_sample_t));
 
   /* split ibuf into bands using butterworths, pipe each band through st_mcompand_flow_1, then add back together and write to obuf */
--- a/src/misc.c
+++ b/src/misc.c
@@ -493,11 +493,7 @@
  */
 char *strdup(const char *s)
 {
-    char *dups;
-
-    dups = (char *)malloc(strlen(s)+1);
-    strcpy(dups, s);
-    return dups;
+    return strcpy((char *)xmalloc(strlen(s) + 1), s);
 }
 #endif
 
--- a/src/mp3.c
+++ b/src/mp3.c
@@ -170,19 +170,12 @@
     p->Timer = NULL;
     p->InputBuffer = NULL;
 
-    p->Stream=(struct mad_stream *)malloc(sizeof(struct mad_stream));
-    p->Frame=(struct mad_frame *)malloc(sizeof(struct mad_frame));
-    p->Synth=(struct mad_synth *)malloc(sizeof(struct mad_synth));
-    p->Timer=(mad_timer_t *)malloc(sizeof(mad_timer_t));
-    p->InputBuffer=(unsigned char *)malloc(INPUT_BUFFER_SIZE);
+    p->Stream=(struct mad_stream *)xmalloc(sizeof(struct mad_stream));
+    p->Frame=(struct mad_frame *)xmalloc(sizeof(struct mad_frame));
+    p->Synth=(struct mad_synth *)xmalloc(sizeof(struct mad_synth));
+    p->Timer=(mad_timer_t *)xmalloc(sizeof(mad_timer_t));
+    p->InputBuffer=(unsigned char *)xmalloc(INPUT_BUFFER_SIZE);
 
-    if (!p->Stream || !p->Frame || !p->Synth || 
-        !p->Timer || !p->InputBuffer)
-    {
-        st_fail_errno(ft, ST_ENOMEM, "Could not allocate memory");
-        goto error;
-    }
-
     mad_stream_init(p->Stream);
     mad_frame_init(p->Frame);
     mad_synth_init(p->Synth);
@@ -262,15 +255,6 @@
     p->cursamp = 0;
 
     return ST_SUCCESS;
-
-error:
-    if (p->Stream) free(p->Stream);
-    if (p->Frame) free(p->Frame);
-    if (p->Synth) free(p->Synth);
-    if (p->Timer) free(p->Timer);
-    if (p->InputBuffer) free(p->InputBuffer);
-
-    return ST_EOF;
 }
 
 /*
@@ -456,16 +440,10 @@
      * different scalling between 32-bit and 64-bit CPU's.
      *
      * We might as well scale it ourselfs to 16-bit to allow
-     * malloc()'ing a smaller buffer and call a consistent
+     * xmalloc()'ing a smaller buffer and call a consistent
      * interface.
      */
-    if ((buffer_l = 
-         (short signed int *)malloc(nsamples*
-                                    sizeof(short signed int))) == NULL)
-    {
-        st_fail_errno(ft, ST_ENOMEM, "Memory allocation failed");
-        goto end4;
-    }
+    buffer_l = (short signed int *)xmalloc(nsamples * sizeof(short signed int));
 
     if (ft->info.channels == 2)
     {
@@ -473,7 +451,7 @@
          * them out into seperate buffers.
          */
         if ((buffer_r = 
-             (short signed int *)malloc(nsamples*
+             (short signed int *)xmalloc(nsamples*
                                           sizeof(short signed int))) == NULL)
         {
             st_fail_errno(ft,ST_ENOMEM,"Memory allocation failed");
@@ -497,7 +475,7 @@
     }
 
     mp3buffer_size = 1.25 * nsamples + 7200;
-    if ((mp3buffer=(char *)malloc(mp3buffer_size)) == NULL)
+    if ((mp3buffer=(char *)xmalloc(mp3buffer_size)) == NULL)
     {
         st_fail_errno(ft,ST_ENOMEM,"Memory allocation failed");
         goto end2;
@@ -525,7 +503,7 @@
         free(buffer_r);
 end3:
     free(buffer_l);
-end4:
+
     return done;
 }
 
--- a/src/noiseprof.c
+++ b/src/noiseprof.c
@@ -74,23 +74,20 @@
         data->output_file = stderr;
     }
 
-    data->chandata = (chandata_t*)calloc(channels, sizeof(*(data->chandata)));
+    data->chandata = (chandata_t*)xcalloc(channels, sizeof(*(data->chandata)));
+    data->bufdata = 0;
     for (i = 0; i < channels; i ++) {
-        data->chandata[i].sum          =
-            (float*)calloc(FREQCOUNT, sizeof(float));
-        data->chandata[i].profilecount =
-            (int*)calloc(FREQCOUNT, sizeof(int));
-        data->chandata[i].window       =
-            (float*)calloc(WINDOWSIZE, sizeof(float));
+        data->chandata[i].sum = (float*)xcalloc(FREQCOUNT, sizeof(float));
+        data->chandata[i].profilecount = (int*)xcalloc(FREQCOUNT, sizeof(int));
+        data->chandata[i].window = (float*)xcalloc(WINDOWSIZE, sizeof(float));
     }
-    data->bufdata = 0;
-    return (ST_SUCCESS);
+
+    return ST_SUCCESS;
 }
 
 /* Collect statistics from the complete window on channel chan. */
 static void collect_data(chandata_t* chan) {
-    float *out = (float*)calloc(FREQCOUNT, sizeof(float));
-
+    float *out = (float*)xcalloc(FREQCOUNT, sizeof(float));
     int i;
 
     PowerSpectrum(WINDOWSIZE, chan->window, out);
@@ -132,9 +129,8 @@
             chan->window[j+data->bufdata] =
                 ST_SAMPLE_TO_FLOAT_DWORD(ibuf[i+j*tracks], effp->clippedCount);
         }
-        if (ncopy + data->bufdata == WINDOWSIZE) {
+        if (ncopy + data->bufdata == WINDOWSIZE)
             collect_data(chan);
-        }
     }
 
     data->bufdata += ncopy;
--- a/src/noisered.c
+++ b/src/noisered.c
@@ -75,13 +75,13 @@
     int i;
     FILE* ifd;
 
-    data->chandata = (chandata_t*)calloc(channels, sizeof(*(data->chandata)));
+    data->chandata = (chandata_t*)xcalloc(channels, sizeof(*(data->chandata)));
+    data->bufdata = 0;
     for (i = 0; i < channels; i ++) {
-        data->chandata[i].noisegate = (float*)calloc(FREQCOUNT, sizeof(float));
-        data->chandata[i].smoothing = (float*)calloc(FREQCOUNT, sizeof(float));
+        data->chandata[i].noisegate = (float*)xcalloc(FREQCOUNT, sizeof(float));
+        data->chandata[i].smoothing = (float*)xcalloc(FREQCOUNT, sizeof(float));
         data->chandata[i].lastwindow = NULL;
     }
-    data->bufdata = 0;
 
     /* Here we actually open the input file. */
     ifd = fopen(data->profile_filename, "r");
@@ -128,20 +128,18 @@
  * due to overlapping windows. */
 static void reduce_noise(chandata_t* chan, float* window, float level)
 {
-    float *inr   = (float*)calloc(WINDOWSIZE, sizeof(float));
-    float *ini   = (float*)calloc(WINDOWSIZE, sizeof(float));
-    float *outr  = (float*)calloc(WINDOWSIZE, sizeof(float));
-    float *outi  = (float*)calloc(WINDOWSIZE, sizeof(float));
-    float *power = (float*)calloc(WINDOWSIZE, sizeof(float));
+    float *inr, *ini, *outr, *outi, *power;
     float *smoothing = chan->smoothing;
-    static int callnum = 0;
     int i;
 
-    callnum ++;
-
-    for (i = 0; i < FREQCOUNT; i ++) {
+    inr = (float*)xcalloc(WINDOWSIZE * 5, sizeof(float));
+    ini = inr + WINDOWSIZE;
+    outr = ini + WINDOWSIZE;
+    outi = outr + WINDOWSIZE;
+    power = outi + WINDOWSIZE;
+    
+    for (i = 0; i < FREQCOUNT; i ++)
         assert(smoothing[i] >= 0 && smoothing[i] <= 1);
-    }
 
     memcpy(inr, window, WINDOWSIZE*sizeof(float));
 
@@ -151,7 +149,7 @@
     WindowFunc(HANNING, WINDOWSIZE, inr);
     PowerSpectrum(WINDOWSIZE, inr, power);
 
-    for(i = 0; i < FREQCOUNT; i ++) {
+    for (i = 0; i < FREQCOUNT; i ++) {
         float smooth;
         float plog;
         plog = log(power[i]);
@@ -159,10 +157,10 @@
             smooth = 0.0;
         else
             smooth = 1.0;
-
+        
         smoothing[i] = smooth * 0.5 + smoothing[i] * 0.5;
     }
-
+    
     /* Audacity says this code will eliminate tinkle bells.
      * I have no idea what that means. */
     for (i = 2; i < FREQCOUNT - 2; i ++) {
@@ -174,7 +172,7 @@
             smoothing[i+2]<0.1)
             smoothing[i] = 0.0;
     }
-
+    
     outr[0] *= smoothing[0];
     outi[0] *= smoothing[0];
     outr[FREQCOUNT-1] *= smoothing[FREQCOUNT-1];
@@ -183,27 +181,22 @@
     for (i = 1; i < FREQCOUNT-1; i ++) {
         int j = WINDOWSIZE - i;
         float smooth = smoothing[i];
-
+        
         outr[i] *= smooth;
         outi[i] *= smooth;
         outr[j] *= smooth;
         outi[j] *= smooth;
     }
-
+    
     FFT(WINDOWSIZE, 1, outr, outi, inr, ini);
     WindowFunc(HANNING, WINDOWSIZE, inr);
-
+    
     memcpy(window, inr, WINDOWSIZE*sizeof(float));
 
-    free(inr);
-    free(ini);
-    free(outr);
-    free(outi);
-    free(power);
-
-    for (i = 0; i < FREQCOUNT; i ++) {
+    for (i = 0; i < FREQCOUNT; i ++)
         assert(smoothing[i] >= 0 && smoothing[i] <= 1);
-    }
+
+    free(inr);
 }
 
 /* Do window management once we have a complete window, including mangling
@@ -216,12 +209,13 @@
     chandata_t *chan = &(data->chandata[chan_num]);
     int first = (chan->lastwindow == NULL);
 
-    nextwindow = (float*)calloc(WINDOWSIZE, sizeof(float));
+    if ((nextwindow = (float*)xcalloc(WINDOWSIZE, sizeof(float))) == NULL)
+        return ST_EOF;
+    
     memcpy(nextwindow, chan->window+WINDOWSIZE/2,
            sizeof(float)*(WINDOWSIZE/2));
 
     reduce_noise(chan, chan->window, data->threshold);
-        
     if (!first) {
         for (j = 0; j < use; j ++) {
             float s = chan->window[j] + chan->lastwindow[WINDOWSIZE/2 + j];
@@ -238,7 +232,7 @@
     }
     chan->lastwindow = chan->window;
     chan->window = nextwindow;
-
+    
     return use;
 }
 
@@ -256,42 +250,39 @@
     st_size_t whole_window = (ncopy + data->bufdata == WINDOWSIZE);
     int oldbuf = data->bufdata;
     st_size_t i;
+
     assert(effp->ininfo.channels == effp->outinfo.channels);
 
-    if (whole_window) {
+    if (whole_window)
         data->bufdata = WINDOWSIZE/2;
-    } else {
+    else
         data->bufdata += ncopy;
-    }
 
     /* Reduce noise on every channel. */
     for (i = 0; i < tracks; i ++) {
         chandata_t* chan = &(data->chandata[i]);
         st_size_t j;
-        if (chan->window == NULL) {
-            chan->window = (float*)calloc(WINDOWSIZE, sizeof(float));
-        }
+
+        if (chan->window == NULL)
+            chan->window = (float*)xcalloc(WINDOWSIZE, sizeof(float));
         
-        for (j = 0; j < ncopy; j ++) {
+        for (j = 0; j < ncopy; j ++)
             chan->window[oldbuf + j] =
                 ST_SAMPLE_TO_FLOAT_DWORD(ibuf[i + tracks * j], effp->clippedCount);
-        }
 
         if (!whole_window)
             continue;
-        else {
+        else
             process_window(effp, data, i, tracks, obuf, oldbuf + ncopy);
-        }
     }
     
     *isamp = tracks*ncopy;
-    if (whole_window) {
+    if (whole_window)
         *osamp = tracks*(WINDOWSIZE/2);
-    } else {
+    else
         *osamp = 0;
-    }
-    
-    return (ST_SUCCESS);
+
+    return ST_SUCCESS;
 }
 
 /*
@@ -303,9 +294,9 @@
     reddata_t data = (reddata_t)effp->priv;
     int i;
     int tracks = effp->ininfo.channels;
-    for (i = 0; i < tracks; i ++) {
+    for (i = 0; i < tracks; i ++)
         *osamp = process_window(effp, data, i, tracks, obuf, data->bufdata);
-    }
+
     /* FIXME: This is very picky.  osamp needs to be big enough to get all
      * remaining data or it will be discarded.
      */
--- a/src/oss.c
+++ b/src/oss.c
@@ -184,11 +184,7 @@
     file->count = 0;
     file->pos = 0;
     file->eof = 0;
-
-    if ((file->buf = (char *)malloc(file->size)) == NULL) {
-        st_fail_errno(ft,ST_EOF,"Unable to allocate input/output buffer of size %d", file->size);
-        return (ST_EOF);
-    }
+    file->buf = (char *)xmalloc(file->size);
 
     if (ioctl(fileno(ft->fp), SNDCTL_DSP_SYNC, NULL) < 0) {
         st_fail_errno(ft,ST_EOF,"Unable to sync dsp");
--- a/src/pan.c
+++ b/src/pan.c
@@ -82,7 +82,7 @@
     char ich, och;
     double left, right, dir, hdir;
     
-    ibuf_copy = (st_sample_t *)malloc(*isamp * sizeof(st_sample_t));
+    ibuf_copy = (st_sample_t *)xmalloc(*isamp * sizeof(st_sample_t));
     memcpy(ibuf_copy, ibuf, *isamp * sizeof(st_sample_t));
 
     dir   = pan->dir;    /* -1   <=  dir  <= 1   */
--- a/src/phaser.c
+++ b/src/phaser.c
@@ -158,23 +158,10 @@
                 st_warn("phaser: warning >>> gain-out can cause saturation or clipping of output <<<");
 
         phaser->length = effp->ininfo.rate / phaser->speed;
-
-        if (! (phaser->phaserbuf = 
-                (double *) malloc(sizeof (double) * phaser->maxsamples)))
-        {
-                st_fail("phaser: Cannot malloc %d bytes!", 
-                        sizeof(double) * phaser->maxsamples);
-                return (ST_EOF);
-        }
+        phaser->phaserbuf = (double *) xmalloc(sizeof (double) * phaser->maxsamples);
         for ( i = 0; i < phaser->maxsamples; i++ )
                 phaser->phaserbuf[i] = 0.0;
-        if (! (phaser->lookup_tab = 
-                (int *) malloc(sizeof (int) * phaser->length)))
-        {
-                st_fail("phaser: Cannot malloc %d bytes!", 
-                        sizeof(int) * phaser->length);
-                return (ST_EOF);
-        }
+        phaser->lookup_tab = (int *) xmalloc(sizeof (int) * phaser->length);
 
         if (phaser->modulation == MOD_SINE)
           st_generate_wave_table(ST_WAVE_SINE, ST_INT, phaser->lookup_tab,
--- a/src/pitch.c
+++ b/src/pitch.c
@@ -37,8 +37,8 @@
 
 #include "st_i.h"
 
-#include <stdlib.h> /* malloc(), free() */
-#include <string.h> /* memcpy() */
+#include <stdlib.h>
+#include <string.h>
 
 #include <math.h>   /* cos(), pow() */
 
@@ -376,17 +376,10 @@
 
     pitch->size = pitch->step + 2*pitch->overlap;
 
-    pitch->fade = (double *) malloc(pitch->step*sizeof(double));
-    pitch->tmp  = (double *) malloc(pitch->step*sizeof(double));
-    pitch->acc  = (double *) malloc(pitch->step*sizeof(double));
-    pitch->buf  = (st_sample_t *) malloc(pitch->size*sizeof(st_sample_t));
-
-    if (!pitch->fade || !pitch->tmp || !pitch->acc || !pitch->buf)
-    {
-        st_fail("malloc failed in st_pitch_start");
-        return ST_EOF;
-    }
-
+    pitch->fade = (double *) xmalloc(pitch->step*sizeof(double));
+    pitch->tmp  = (double *) xmalloc(pitch->step*sizeof(double));
+    pitch->acc  = (double *) xmalloc(pitch->step*sizeof(double));
+    pitch->buf  = (st_sample_t *) xmalloc(pitch->size*sizeof(st_sample_t));
     pitch->index = pitch->overlap;
 
     /* default initial signal */
--- a/src/polyphas.c
+++ b/src/polyphas.c
@@ -419,7 +419,7 @@
       int j, prod, f_cutoff, f_len;
       polystage *s;
 
-      rate->stage[k] = s = (polystage*) malloc(sizeof(polystage));
+      rate->stage[k] = s = (polystage*) xmalloc(sizeof(polystage));
       s->up = l1[k];
       s->down = l2[k];
       f_cutoff = max(s->up, s->down);
@@ -433,8 +433,8 @@
       st_debug("Poly:  stage %d:  Up by %d, down by %d,  i_samps %d, hsize %d",
               k+1,s->up,s->down,size, s->hsize);
       s->filt_len = f_len;
-      s->filt_array = (Float *) malloc(sizeof(Float) * f_len);
-      s->window = (Float *) malloc(sizeof(Float) * (s->hsize+size));
+      s->filt_array = (Float *) xmalloc(sizeof(Float) * f_len);
+      s->window = (Float *) xmalloc(sizeof(Float) * (s->hsize+size));
       /* zero past_history section of window */
       for(j = 0; j < s->hsize; j++)
         s->window[j] = 0.0;
@@ -455,7 +455,7 @@
     rate->oskip = skip/2;
                 { /* bogus last stage is for output buffering */
       polystage *s;
-      rate->stage[k] = s = (polystage*) malloc(sizeof(polystage));
+      rate->stage[k] = s = (polystage*) xmalloc(sizeof(polystage));
       s->up = s->down = 0;
       s->size = size;
       s->hsize = 0;
@@ -462,7 +462,7 @@
       s->held = 0;
       s->filt_len = 0;
       s->filt_array = NULL;
-      s->window = (Float *) malloc(sizeof(Float) * size);
+      s->window = (Float *) xmalloc(sizeof(Float) * size);
     }
     st_debug("Poly:  output samples %d, oskip %d",size, rate->oskip);
     return (ST_SUCCESS);
--- a/src/rabbit.c
+++ b/src/rabbit.c
@@ -90,10 +90,7 @@
     return (ST_EOF);
   }
 
-  if ((r->data = (SRC_DATA *)calloc(1, sizeof(SRC_DATA))) == NULL) {
-    st_fail("could not allocate SRC_DATA buffer");
-    return (ST_EOF);
-  }
+  r->data = (SRC_DATA *)xcalloc(1, sizeof(SRC_DATA));
   r->data->src_ratio = (double)effp->outinfo.rate / effp->ininfo.rate;
   r->data->input_frames_used = 0;
   r->data->output_frames_gen = 0;
@@ -117,10 +114,7 @@
       return (ST_EOF);
   }
 
-  if ((r->data->data_in = (float *)realloc(r->data->data_in, newsamples * sizeof(float))) == NULL) {
-    st_fail("unable to allocate input buffer of size %d", newsamples);
-    return (ST_EOF);
-  }
+  r->data->data_in = (float *)xrealloc(r->data->data_in, newsamples * sizeof(float));
 
   for (i = 0 ; i < *isamp; i++)
     r->data->data_in[r->samples + i] = ST_SAMPLE_TO_FLOAT_DWORD(ibuf[i], effp->clippedCount);
@@ -154,11 +148,7 @@
       return (ST_EOF);
     }
     r->data->output_frames = outframes;
-    r->data->data_out = (float *)malloc(r->data->output_frames * channels * sizeof(float));
-    if (r->data->data_out == NULL) {
-      st_fail("unable to allocate output frames buffer of size %d", r->data->output_frames);
-      return (ST_EOF);
-    }
+    r->data->data_out = (float *)xmalloc(r->data->output_frames * channels * sizeof(float));
 
     /* Process the data */
     if ((error = src_simple(r->data, r->converter_type, channels))) {
--- a/src/resample.c
+++ b/src/resample.c
@@ -244,7 +244,7 @@
         /* Nwing: # of filter coeffs in right wing */
         r->Nwing = r->Nq * (r->Nmult/2+1) + 1;
 
-        r->Imp = (double *)malloc(sizeof(double) * (r->Nwing+2)) + 1;
+        r->Imp = (double *)xmalloc(sizeof(double) * (r->Nwing+2)) + 1;
         /* need Imp[-1] and Imp[Nwing] for quadratic interpolation */
         /* returns error # <=0, or adjusted wing-len > 0 */
         i = makeFilter(r->Imp, r->Nwing, r->rolloff, r->beta, r->Nq, 1);
@@ -290,7 +290,7 @@
         r->Ysize = BUFFSIZE - r->Xsize;
         /* st_debug("Xsize %d, Ysize %d, Xoff %d",r->Xsize,r->Ysize,r->Xoff); */
 
-        r->X = (double *) malloc(sizeof(double) * (BUFFSIZE));
+        r->X = (double *) xmalloc(sizeof(double) * (BUFFSIZE));
         r->Y = r->X + r->Xsize;
 
         /* Need Xoff zeros at beginning of sample */
@@ -634,7 +634,7 @@
    if (Mwing==0)
       return(-4);
 
-   ImpR = (double *) malloc(sizeof(double) * Mwing);
+   ImpR = (double *) xmalloc(sizeof(double) * Mwing);
 
    /* Design a Nuttall or Kaiser windowed Sinc low-pass filter */
    LpFilter(ImpR, Mwing, Froll, Beta, Num);
--- a/src/reverb.c
+++ b/src/reverb.c
@@ -30,7 +30,7 @@
  * the difference in its defintion.
  * The idea of the echoplexer is modified and enhanceb by an automatic
  * setting of each decay for realistic reverb.
- * Some bugs are fixed concerning malloc and fade-outs.
+ * Some bugs are fixed concerning xmalloc and fade-outs.
  * Added an output volume (gain-out) avoiding saturation or clipping.
  *
  *
@@ -187,12 +187,7 @@
                 if ( reverb->samples[i] > reverb->maxsamples )
                     reverb->maxsamples = reverb->samples[i];
         }
-        if (! (reverb->reverbbuf = (float *) malloc(sizeof (float) * reverb->maxsamples)))
-        {
-                st_fail("reverb: Cannot malloc %d bytes!", 
-                        sizeof(float) * reverb->maxsamples);
-                return(ST_EOF);
-        }
+        reverb->reverbbuf = (float *) xmalloc(sizeof (float) * reverb->maxsamples);
         for ( i = 0; i < reverb->maxsamples; ++i )
                 reverb->reverbbuf[i] = 0.0;
         reverb->pppl = reverb->ppl = reverb->pl = 0x7fffff;             /* fade-outs */
--- a/src/sf.c
+++ b/src/sf.c
@@ -49,13 +49,12 @@
                         finished = 1;
                         break;
                 case SF_COMMENT:
-                        if((commentbuf = (char *) malloc(bsize + 1)) != NULL) {
-                                memcpy(commentbuf, sfcharp, bsize);
-                                st_report("IRCAM comment: %s", sfcharp);
-                                commentbuf[bsize] = '\0';
-                                if((newline = strchr(commentbuf, '\n')) != NULL)
-                                        *newline = '\0';
-                        }
+                        commentbuf = (char *) xmalloc(bsize + 1);
+                        memcpy(commentbuf, sfcharp, bsize);
+                        st_report("IRCAM comment: %s", sfcharp);
+                        commentbuf[bsize] = '\0';
+                        if((newline = strchr(commentbuf, '\n')) != NULL)
+                                *newline = '\0';
                         break;
                 }
                 sfcodep = (SFCODE *) (sfcharp + bsize);
--- a/src/silence.c
+++ b/src/silence.c
@@ -117,12 +117,7 @@
          * parse the duration info yet.  So save argument off
          * for future processing.
          */
-        silence->start_duration_str = (char *)malloc(strlen(argv[0])+1);
-        if (!silence->start_duration_str)
-        {
-            st_fail("Could not allocate memory");
-            return(ST_EOF);
-        }
+        silence->start_duration_str = (char *)xmalloc(strlen(argv[0])+1);
         strcpy(silence->start_duration_str,argv[0]);
         /* Perform a fake parse to do error checking */
         if (st_parsesamples(0,silence->start_duration_str,
@@ -176,12 +171,7 @@
          * parse the duration info yet.  So save argument off
          * for future processing.
          */
-        silence->stop_duration_str = (char *)malloc(strlen(argv[0])+1);
-        if (!silence->stop_duration_str)
-        {
-            st_fail("Could not allocate memory");
-            return(ST_EOF);
-        }
+        silence->stop_duration_str = (char *)xmalloc(strlen(argv[0])+1);
         strcpy(silence->stop_duration_str,argv[0]);
         /* Perform a fake parse to do error checking */
         if (st_parsesamples(0,silence->stop_duration_str,
@@ -260,15 +250,9 @@
          */
         silence->window_size = (effp->ininfo.rate / 50) * 
                                effp->ininfo.channels;
-        silence->window = (double *)malloc(silence->window_size *
+        silence->window = (double *)xmalloc(silence->window_size *
                                            sizeof(double));
 
-        if (!silence->window)
-        {
-            st_fail("Unable to allocate memory");
-            return(ST_EOF);
-        }
-
         clear_rms(effp);
 
         /* Now that we now sample rate, reparse duration. */
@@ -298,22 +282,12 @@
         else
             silence->mode = SILENCE_COPY;
 
-        silence->start_holdoff = (st_sample_t *)malloc(sizeof(st_sample_t)*silence->start_duration);
-        if (!silence->start_holdoff)
-        {
-            st_fail("Could not allocate memory");
-            return(ST_EOF);
-        }
+        silence->start_holdoff = (st_sample_t *)xmalloc(sizeof(st_sample_t)*silence->start_duration);
         silence->start_holdoff_offset = 0;
         silence->start_holdoff_end = 0;
         silence->start_found_periods = 0;
 
-        silence->stop_holdoff = (st_sample_t *)malloc(sizeof(st_sample_t)*silence->stop_duration);
-        if (!silence->stop_holdoff)
-        {
-            st_fail("Could not allocate memory");
-            return(ST_EOF);
-        }
+        silence->stop_holdoff = (st_sample_t *)xmalloc(sizeof(st_sample_t)*silence->stop_duration);
         silence->stop_holdoff_offset = 0;
         silence->stop_holdoff_end = 0;
         silence->stop_found_periods = 0;
--- a/src/skel.c
+++ b/src/skel.c
@@ -47,7 +47,7 @@
     ft->info.size = ST_SIZE_BYTE or WORD ...;
     ft->info.encoding = ST_ENCODING_UNSIGNED or SIGN2 ...;
     ft->info.channels = 1 or 2 or 4;
-    ft->comment = malloc(size_of_comment);
+    ft->comment = xmalloc(size_of_comment);
     strcpy(ft->comment, "any comment in file header.");
 
     /* If your format doesn't have a header then samples_in_file
--- a/src/sox.c
+++ b/src/sox.c
@@ -37,7 +37,7 @@
 #include <math.h>
 #include <stdio.h>
 #include <string.h>
-#include <stdlib.h>             /* for malloc() */
+#include <stdlib.h>
 #include <signal.h>
 #include <errno.h>
 #ifdef HAVE_UNISTD_H
@@ -200,7 +200,7 @@
             exit(1);
         }
 
-        fo = (file_options_t *)calloc(sizeof(file_options_t), 1);
+        fo = (file_options_t *)xcalloc(sizeof(file_options_t), 1);
         fo->info.size = -1;
         fo->info.encoding = ST_ENCODING_UNKNOWN;
         fo->info.channels = 0;
@@ -316,11 +316,7 @@
     file_length = ftello(file);
     file_error |= file_length < 0;
     if (!file_error) {
-      result = malloc(file_length + 1);
-      if (result == NULL) {
-        st_fail("Out of memory reading comment file %s", filename);
-        exit(1);
-      }
+      result = xmalloc(file_length + 1);
       rewind(file);
       file_error |= fread(result, file_length, 1, file) != 1;
     }
@@ -685,13 +681,7 @@
           if (file_desc[f]->length > input_samples)
             input_samples = file_desc[f]->length;
           
-          ibuf[f] = (st_sample_t *)malloc(ST_BUFSIZ * sizeof(st_sample_t));
-          if (!ibuf[f])
-            {
-              st_fail("could not allocate memory");
-              cleanup();
-              exit(1);
-            }
+          ibuf[f] = (st_sample_t *)xmalloc(ST_BUFSIZ * sizeof(st_sample_t));
           
           if (status)
             print_input_status(f);
@@ -1207,7 +1197,7 @@
 
     for(e = 0; e < neffects; e++)
     {
-        efftab[e].obuf = (st_sample_t *)malloc(ST_BUFSIZ * 
+        efftab[e].obuf = (st_sample_t *)xmalloc(ST_BUFSIZ * 
                                                 sizeof(st_sample_t));
         if (efftab[e].obuf == NULL)
         {
@@ -1217,7 +1207,7 @@
         }
         if (efftabR[e].name)
         {
-            efftabR[e].obuf = (st_sample_t *)malloc(ST_BUFSIZ * 
+            efftabR[e].obuf = (st_sample_t *)xmalloc(ST_BUFSIZ * 
                                                      sizeof(st_sample_t));
             if (efftabR[e].obuf == NULL)
             {
--- a/src/speed.c
+++ b/src/speed.c
@@ -121,7 +121,7 @@
         speed->rate = speed->factor;
     }
 
-    speed->ibuf   = (st_sample_t *) malloc(speed->compression*
+    speed->ibuf   = (st_sample_t *) xmalloc(speed->compression*
                                            sizeof(st_sample_t));
     speed->index  = 0;
 
@@ -130,11 +130,6 @@
     speed->icbuf = 1;
     speed->frac = 0.0;
 
-    if (!speed->ibuf) {
-        st_fail("malloc failed");
-        return ST_EOF;
-    }
-
     return ST_SUCCESS;
 }
 
@@ -201,7 +196,7 @@
 
     speed = (speed_t) effp->priv;
 
-    ibuf_copy = (st_sample_t *)malloc(*isamp * sizeof(st_sample_t));
+    ibuf_copy = (st_sample_t *)xmalloc(*isamp * sizeof(st_sample_t));
     memcpy(ibuf_copy, ibuf, *isamp * sizeof(st_sample_t));
 
     len = min(*isamp, *osamp);
--- a/src/sphere.c
+++ b/src/sphere.c
@@ -56,12 +56,7 @@
 
         /* Determine header size, and allocate a buffer large enough to hold it. */
         sscanf(fldsval, "%d", &header_size);
-        buf = (char *)malloc(header_size);
-        if (buf == NULL)
-        {
-            st_fail_errno(ft,ST_ENOMEM,"Unable to allocate memory");
-            return(ST_ENOMEM);
-        }
+        buf = (char *)xmalloc(header_size);
 
         /* Skip what we have read so far */
         header_size -= 16;
--- a/src/st_i.h
+++ b/src/st_i.h
@@ -16,6 +16,8 @@
 #include "stconfig.h"
 #include "st.h"
 
+#include "xmalloc.h"
+
 #ifdef HAVE_BYTESWAP_H
 #include <byteswap.h>
 #endif
--- a/src/stat.c
+++ b/src/stat.c
@@ -120,15 +120,8 @@
         if (stat->fft)
         {
             stat->fft_offset = 0;
-
-            stat->re_in = (float *)malloc(sizeof(float) * stat->fft_size);
-            stat->re_out = (float *)malloc(sizeof(float) * (stat->fft_size / 2));
-
-            if (!stat->re_in || !stat->re_out)
-            {
-                st_fail("Unable to allocate memory for FFT buffers.");
-                return (ST_EOF);
-            }
+            stat->re_in = (float *)xmalloc(sizeof(float) * stat->fft_size);
+            stat->re_out = (float *)xmalloc(sizeof(float) * (stat->fft_size / 2));
         }
 
         return (ST_SUCCESS);
--- a/src/stio.c
+++ b/src/stio.c
@@ -78,13 +78,8 @@
 ft_t st_open_read(const char *path, const st_signalinfo_t *info,
                   const char *filetype)
 {
-    ft_t ft;
+    ft_t ft = (ft_t)xcalloc(sizeof(struct st_soundstream), 1);
 
-    ft = (ft_t)calloc(sizeof(struct st_soundstream), 1);
-
-    if (!ft)
-        return NULL;
-
     ft->filename = strdup(path);
 
     /* Let auto effect do the work if user is not overriding. */
@@ -176,14 +171,9 @@
                          const st_instrinfo_t *instr,
                          const st_loopinfo_t *loops)
 {
-    ft_t ft;
+    ft_t ft = (ft_t)xcalloc(sizeof(struct st_soundstream), 1);
     int i;
     bool no_filetype_given = filetype == NULL;
-
-    ft = (ft_t)calloc(sizeof(struct st_soundstream), 1);
-
-    if (!ft )
-        return NULL;
 
     ft->filename = strdup(path);
 
--- a/src/stretch.c
+++ b/src/stretch.c
@@ -21,8 +21,8 @@
  */
 #include "st_i.h"
 
-#include <stdlib.h> /* malloc and free */
-#include <string.h> /* memcpy() */
+#include <stdlib.h>
+#include <string.h>
 #include <assert.h>
 
 static st_effect_t st_stretch_effect;
@@ -184,8 +184,7 @@
     stretch->size = (int)(effp->outinfo.rate * 0.001 * stretch->window);
     /* start in the middle of an input to avoid initial fading... */
     stretch->index = stretch->size/2;
-    stretch->ibuf  = (st_sample_t *) malloc(stretch->size * 
-                                            sizeof(st_sample_t));
+    stretch->ibuf  = (st_sample_t *) xmalloc(stretch->size * sizeof(st_sample_t));
 
     /* the shift ratio deal with the longest of ishift/oshift
        hence ishift<=size and oshift<=size.
@@ -201,20 +200,10 @@
     assert(stretch->oshift <= stretch->size);
 
     stretch->oindex = stretch->index; /* start as synchronized */
-    stretch->obuf = (double *)
-        malloc(stretch->size * sizeof(double));
-    
+    stretch->obuf = (double *)xmalloc(stretch->size * sizeof(double));
     stretch->fsize = (int) (stretch->fading * stretch->size);
-
-    stretch->fbuf = (double *)
-        malloc(stretch->fsize * sizeof(double));
+    stretch->fbuf = (double *)xmalloc(stretch->fsize * sizeof(double));
         
-    if (!stretch->ibuf || !stretch->obuf || !stretch->fbuf) 
-    {
-        st_fail("some malloc failed");
-        return ST_EOF;
-    }
-
     /* initialize buffers
      */
     for (i=0; i<stretch->size; i++)
--- a/src/sunaudio.c
+++ b/src/sunaudio.c
@@ -59,10 +59,7 @@
     file->pos = 0;
     file->eof = 0;
     file->size = 1024;
-    if ((file->buf = malloc (file->size)) == NULL) {
-        st_fail_errno(ft,ST_ENOMEM,"unable to allocate input buffer of size %d", file->size);
-        return ST_EOF;
-    }
+    file->buf = xmalloc (file->size);
 
     if (ft->info.rate == 0.0) ft->info.rate = 8000;
     if (ft->info.size == -1) ft->info.size = ST_SIZE_BYTE;
@@ -198,10 +195,7 @@
     file->pos = 0;
     file->eof = 0;
     file->size = 1024;
-    if ((file->buf = malloc (file->size)) == NULL) {
-        st_fail_errno(ft,ST_ENOMEM,"unable to allocate output buffer of size %d", file->size);
-        return(ST_EOF);
-    }
+    file->buf = xmalloc (file->size);
 
 #ifdef __SVR4
     /* Read in old values, change to what we need and then send back */
--- a/src/synth.c
+++ b/src/synth.c
@@ -276,12 +276,7 @@
 
     /* read length if given ( if first par starts with digit )*/
     if( isdigit((int)argv[argn][0]) || argv[argn][0] == '.') {
-        synth->length_str = (char *)malloc(strlen(argv[argn])+1);
-        if (!synth->length_str)
-        {
-            st_fail("Could not allocate memeory");
-            return(ST_EOF);
-        }
+        synth->length_str = (char *)xmalloc(strlen(argv[argn])+1);
         strcpy(synth->length_str,argv[argn]);
         /* Do a dummy parse of to see if it will fail */
         if (st_parsesamples(0, synth->length_str, &synth->length, 't') !=
--- a/src/trim.c
+++ b/src/trim.c
@@ -46,12 +46,7 @@
      */
     switch (n) {
         case 2:
-            trim->length_str = (char *)malloc(strlen(argv[1])+1);
-            if (!trim->length_str)
-            {
-                st_fail("Could not allocate memory");
-                return(ST_EOF);
-            }
+            trim->length_str = (char *)xmalloc(strlen(argv[1])+1);
             strcpy(trim->length_str,argv[1]);
             /* Do a dummy parse to see if it will fail */
             if (st_parsesamples(0, trim->length_str,
@@ -61,12 +56,7 @@
                 return(ST_EOF);
             }
         case 1:
-            trim->start_str = (char *)malloc(strlen(argv[0])+1);
-            if (!trim->start_str)
-            {
-                st_fail("Could not allocate memory");
-                return(ST_EOF);
-            }
+            trim->start_str = (char *)xmalloc(strlen(argv[0])+1);
             strcpy(trim->start_str,argv[0]);
             /* Do a dummy parse to see if it will fail */
             if (st_parsesamples(0, trim->start_str,
--- a/src/vibro.c
+++ b/src/vibro.c
@@ -71,12 +71,7 @@
         vibro_t vibro = (vibro_t) effp->priv;
 
         vibro->length = effp->ininfo.rate / vibro->speed;
-        if (! (vibro->sinetab = (short*) malloc(vibro->length * sizeof(short))))
-        {
-                st_fail("Vibro: Cannot malloc %d bytes",
-                        vibro->length * sizeof(short));
-                return (ST_EOF);
-        }
+        vibro->sinetab = (short*) xmalloc(vibro->length * sizeof(short));
 
         st_generate_wave_table(ST_WAVE_SINE, ST_SHORT,
             vibro->sinetab, vibro->length, (1 - vibro->depth) * 256, 256, 0);
--- a/src/vorbis.c
+++ b/src/vorbis.c
@@ -105,12 +105,7 @@
         };
 
         /* Allocate space for decoding structure */
-        vb->vf = (OggVorbis_File *)malloc(sizeof(OggVorbis_File));
-        if (vb->vf == NULL)
-        {
-            st_fail_errno(ft, ST_ENOMEM, "Could not allocate memory");
-            return (ST_EOF);
-        }
+        vb->vf = (OggVorbis_File *)xmalloc(sizeof(OggVorbis_File));
 
         /* Init the decoder */
         if (ov_open_callbacks((void *)ft->fp,vb->vf,NULL,0,callbacks) < 0)
@@ -147,17 +142,8 @@
                 for (i = 0; i < vc->comments; i++)
                         comment_size += vc->comment_lengths[i] + 1;
 
-                if ((ft->comment = (char *)calloc(comment_size, sizeof(char)))
-                     == NULL)
-                {
-                        ov_clear(vb->vf);
-                        free(vb->vf);
+                ft->comment = (char *)xcalloc(comment_size, sizeof(char));
 
-                        st_fail_errno(ft, ST_ENOMEM,
-                                      "Could not allocate memory");
-                        return (ST_EOF);
-                }
-
                 offset = 0;
                 for (i = 0; i < vc->comments; i++)
                 {
@@ -175,13 +161,7 @@
 
         /* Setup buffer */
         vb->buf_len = DEF_BUF_LEN;
-        if ((vb->buf = (char *)calloc(vb->buf_len, sizeof(char))) == NULL )
-        {
-                ov_clear(vb->vf);
-                free(vb->vf);
-                st_fail_errno(ft, ST_ENOMEM, "Could not allocate memory");
-                return (ST_EOF);
-        }
+        vb->buf = (char *)xcalloc(vb->buf_len, sizeof(char));
         vb->start = vb->end = 0;
 
         /* Fill in other info */
@@ -300,8 +280,8 @@
         char *comment;
 
         /* Make the comment structure */
-        vc.user_comments = (char **)calloc(1, sizeof(char *));
-        vc.comment_lengths = (int *)calloc(1, sizeof(int));
+        vc.user_comments = (char **)xcalloc(1, sizeof(char *));
+        vc.comment_lengths = (int *)xcalloc(1, sizeof(int));
         vc.comments = 1;
 
         /* We check if there is a FIELD=value pair already in the comment
@@ -308,11 +288,14 @@
          * if not, add one */
         if (strchr(ft->comment,'=') == NULL)
         {
-            comment = (char *)calloc(1,strlen(ft->comment)+strlen("COMMENT=")+1);
+            comment = (char *)xcalloc(1,strlen(ft->comment)+strlen("COMMENT=")+1);
             strncpy(comment,"COMMENT=",strlen("COMMENT="));
         }
         else
-            comment = (char *)calloc(1,strlen(ft->comment)+1);
+            comment = (char *)xcalloc(1,strlen(ft->comment)+1);
+        
+        if (!comment)
+            return HEADER_ERROR;
 
         strcat(comment,ft->comment);
 
@@ -332,9 +315,10 @@
 
         while((result = ogg_stream_flush(&ve->os, &ve->og)))
         {
-                if(!result) break;
+                if (!result)
+                  break;
                 ret = oe_write_page(&ve->og, ft);
-                if(!ret)
+                if (!ret)
                 {
                     free(comment);
                     return HEADER_ERROR;
@@ -356,12 +340,7 @@
         ft->info.encoding = ST_ENCODING_VORBIS;
 
         /* Allocate memory for all of the structures */
-        ve = vb->vorbis_enc_data = (vorbis_enc_t *)malloc(sizeof(vorbis_enc_t));
-        if (ve == NULL)
-        {
-            st_fail_errno(ft, ST_ENOMEM, "Could not allocate memory");
-            return (ST_EOF);
-        }
+        ve = vb->vorbis_enc_data = (vorbis_enc_t *)xmalloc(sizeof(vorbis_enc_t));
 
         vorbis_info_init(&ve->vi);
 
--- a/src/vox.c
+++ b/src/vox.c
@@ -87,14 +87,7 @@
 
        /* ... setup file info */
 
-       state->file.buf = (char *)malloc(ST_BUFSIZ);
-    
-       if (!state->file.buf)
-          { st_fail_errno (ft,ST_ENOMEM,"Unable to allocate internal buffer memory");
-            
-            return(ST_EOF);
-          }
-
+       state->file.buf = (char *)xmalloc(ST_BUFSIZ);
        state->file.size     = ST_BUFSIZ;
        state->file.count    = 0;
        state->file.pos      = 0;
@@ -205,14 +198,7 @@
 
        /* ... setup file info */
 
-       state->file.buf = (char *)malloc(ST_BUFSIZ);
-    
-       if (!state->file.buf)
-          { st_fail_errno (ft,ST_ENOMEM,"Unable to allocate internal buffer memory");
-            
-            return(ST_EOF);
-          }
-
+       state->file.buf = (char *)xmalloc(ST_BUFSIZ);
        state->file.size     = ST_BUFSIZ;
        state->file.count    = 0;
        state->file.pos      = 0;
--- a/src/wav.c
+++ b/src/wav.c
@@ -11,8 +11,8 @@
  *
  */
 
-#include <string.h>             /* Included for strncmp */
-#include <stdlib.h>             /* Included for malloc and free */
+#include <string.h>
+#include <stdlib.h>
 #include <stdio.h>
 
 #ifdef HAVE_UNISTD_H
@@ -205,11 +205,7 @@
         return (ST_EOF);
     }
 
-    wav->gsmsample=(gsm_signal*)malloc(sizeof(gsm_signal)*160*2);
-    if (wav->gsmsample == NULL){
-        st_fail_errno(ft,ST_ENOMEM,"error allocating memory for gsm buffer");
-        return (ST_EOF);
-    }
+    wav->gsmsample=(gsm_signal*)xmalloc(sizeof(gsm_signal)*160*2);
     wav->gsmindex=0;
     return (ST_SUCCESS);
 }
@@ -665,12 +661,7 @@
             st_fail_errno(ft,ST_EOF,"ADPCM file nCoefs (%.4hx) makes no sense", wav->nCoefs);
             return ST_EOF;
         }
-        wav->packet = (unsigned char *)malloc(wav->blockAlign);
-        if (!wav->packet)
-        {
-            st_fail_errno(ft,ST_EOF,"Unable to alloc resources");
-            return ST_EOF;
-        }
+        wav->packet = (unsigned char *)xmalloc(wav->blockAlign);
 
         len -= 4;
 
@@ -680,21 +671,11 @@
             return ST_EOF;
         }
 
-        wav->samples = (short *)malloc(wChannels*wav->samplesPerBlock*sizeof(short));
-        if (!wav->samples)
-        {
-            st_fail_errno(ft,ST_EOF,"Unable to alloc resources");
-            return ST_EOF;
-        }
+        wav->samples = (short *)xmalloc(wChannels*wav->samplesPerBlock*sizeof(short));
 
         /* nCoefs, iCoefs used by adpcm.c */
-        wav->iCoefs = (short *)malloc(wav->nCoefs * 2 * sizeof(short));
-        if (!wav->iCoefs)
+        wav->iCoefs = (short *)xmalloc(wav->nCoefs * 2 * sizeof(short));
         {
-            st_fail_errno(ft,ST_EOF,"Unable to alloc resources");
-            return ST_EOF;
-        }
-        {
             int i, errct=0;
             for (i=0; len>=2 && i < 2*wav->nCoefs; i++) {
                 st_readw(ft, (unsigned short *)&(wav->iCoefs[i]));
@@ -731,20 +712,10 @@
             return ST_EOF;
         }
 
-        wav->packet = (unsigned char *)malloc(wav->blockAlign);
-        if (!wav->packet)
-        {
-            st_fail_errno(ft,ST_EOF,"Unable to alloc resources");
-            return ST_EOF;
-        }
+        wav->packet = (unsigned char *)xmalloc(wav->blockAlign);
         len -= 2;
 
-        wav->samples = (short *)malloc(wChannels*wav->samplesPerBlock*sizeof(short));
-        if (!wav->samples)
-        {
-            st_fail_errno(ft,ST_EOF,"Unable to alloc resources");
-            return ST_EOF;
-        }
+        wav->samples = (short *)xmalloc(wChannels*wav->samplesPerBlock*sizeof(short));
 
         bytespersample = ST_SIZE_WORD;  /* AFTER de-compression */
         break;
@@ -927,7 +898,7 @@
             findChunk(ft, "LIST", &len) != ST_EOF)
         {
             wav->found_cooledit = 1;
-            ft->comment = (char*)malloc(256);
+            ft->comment = (char*)xmalloc(256);
             /* Initialize comment to a NULL string */
             ft->comment[0] = 0;
             while(!st_eof(ft))
@@ -1211,13 +1182,8 @@
             for (ch=0; ch<ft->info.channels; ch++)
                 wav->state[ch] = 0;
             sbsize = ft->info.channels * wav->samplesPerBlock;
-            wav->packet = (unsigned char *)malloc(wav->blockAlign);
-            wav->samples = (short *)malloc(sbsize*sizeof(short));
-            if (!wav->packet || !wav->samples)
-            {
-                st_fail_errno(ft,ST_EOF,"Unable to alloc resources");
-                return ST_EOF;
-            }
+            wav->packet = (unsigned char *)xmalloc(wav->blockAlign);
+            wav->samples = (short *)xmalloc(sbsize*sizeof(short));
             wav->sampleTop = wav->samples + sbsize;
             wav->samplePtr = wav->samples;
             break;
--- a/src/xa.c
+++ b/src/xa.c
@@ -23,8 +23,8 @@
 /* Thanks to Valery V. Anisimovsky <samael@avn.mccme.ru> for the 
  * "Maxis XA Audio File Format Description", dated 5-01-2002. */
 
-#include <string.h>             /* Included for strncmp */
-#include <stdlib.h>             /* Included for malloc and free */
+#include <string.h>
+#include <stdlib.h>
 #include <stdio.h>
 
 #ifdef HAVE_UNISTD_H
@@ -182,7 +182,7 @@
     xa->bufPos = xa->blockSize;
 
     /* Allocate memory for the block buffer */
-    xa->buf = (unsigned char *) calloc(1, xa->blockSize);
+    xa->buf = (unsigned char *) xcalloc(1, xa->blockSize);
     if (xa->buf == NULL) {
         st_fail_errno(ft, ST_ENOMEM, "Unable to allocate block buffer");
         return ST_EOF;
@@ -189,7 +189,7 @@
     }
     
     /* Allocate memory for the state */
-    xa->state = (xa_state_t *) calloc(sizeof(xa_state_t), ft->info.channels);
+    xa->state = (xa_state_t *) xcalloc(sizeof(xa_state_t), ft->info.channels);
     if (xa->state == NULL) {
         /* Free xa->buf */
         free(xa->buf);
--