Cleaned up memory allocations.
authorTilman Sauerbeck <tilman@code-monkey.de>
Wed, 16 Aug 2006 19:12:06 +0000 (21:12 +0200)
committerTilman Sauerbeck <tilman@code-monkey.de>
Wed, 23 Aug 2006 17:39:14 +0000 (19:39 +0200)
Now we don't allocate the same chunks of memory in every call to
vcedit_open(), but we allocate the memory with the vcedit_state object
and keep it throughout its life time.

ext/vcedit.c

index 24622e855974eafc7a6c7765c7edfc21a017d302..073196b3fdb1b1b9ebfcda7166b871f7d1354de3 100644 (file)
@@ -17,6 +17,7 @@
  */
 
 #include <stdio.h>
  */
 
 #include <stdio.h>
+#include <stdbool.h>
 #include <stdlib.h>
 #include <string.h>
 #include <errno.h>
 #include <stdlib.h>
 #include <string.h>
 #include <errno.h>
@@ -56,6 +57,44 @@ struct vcedit_state_St {
        int eosin;
 };
 
        int eosin;
 };
 
+static void
+vcedit_state_free (vcedit_state *state)
+{
+       free (state->oy);
+       free (state->os);
+       free (state->vc);
+       free (state->vi);
+       free (state->mainbuf);
+       free (state->bookbuf);
+       free (state->vendor);
+
+       free (state);
+}
+
+static bool
+vcedit_state_init (vcedit_state *state)
+{
+       state->refcount = 1;
+
+       state->oy = malloc (sizeof (ogg_sync_state));
+       if (!state->oy)
+               return false;
+
+       state->os = malloc (sizeof (ogg_stream_state));
+       if (!state->os)
+               return false;
+
+       state->vc = malloc (sizeof (vorbis_comment));
+       if (!state->vc)
+               return false;
+
+       state->vi = malloc (sizeof (vorbis_info));
+       if (!state->vi)
+               return false;
+
+       return true;
+}
+
 vcedit_state *
 vcedit_state_new (const char *filename)
 {
 vcedit_state *
 vcedit_state_new (const char *filename)
 {
@@ -67,7 +106,10 @@ vcedit_state_new (const char *filename)
 
        memset (state, 0, sizeof (vcedit_state));
 
 
        memset (state, 0, sizeof (vcedit_state));
 
-       state->refcount = 1;
+       if (!vcedit_state_init (state)) {
+               vcedit_state_free (state);
+               return NULL;
+       }
 
        snprintf (state->filename, sizeof (state->filename),
                  "%s", filename);
 
        snprintf (state->filename, sizeof (state->filename),
                  "%s", filename);
@@ -75,7 +117,7 @@ vcedit_state_new (const char *filename)
        state->in = fopen (state->filename, "rb");
 
        if (vcedit_open (state) < 0) {
        state->in = fopen (state->filename, "rb");
 
        if (vcedit_open (state) < 0) {
-               free (state);
+               vcedit_state_free (state);
                return NULL;
        }
 
                return NULL;
        }
 
@@ -97,23 +139,11 @@ vcedit_comments (vcedit_state *state)
 static void
 vcedit_clear_internals (vcedit_state *state)
 {
 static void
 vcedit_clear_internals (vcedit_state *state)
 {
-       if (state->vc) {
-               vorbis_comment_clear (state->vc);
-               free (state->vc);
-               state->vc = NULL;
-       }
+       ogg_stream_clear (state->os);
+       ogg_sync_clear (state->oy);
 
 
-       if (state->os) {
-               ogg_stream_clear (state->os);
-               free (state->os);
-               state->os = NULL;
-       }
-
-       if (state->oy) {
-               ogg_sync_clear (state->oy);
-               free (state->oy);
-               state->oy = NULL;
-       }
+       vorbis_info_clear (state->vi);
+       vorbis_comment_clear (state->vc);
 
        free (state->vendor);
        state->vendor = NULL;
 
        free (state->vendor);
        state->vendor = NULL;
@@ -126,12 +156,6 @@ vcedit_clear_internals (vcedit_state *state)
        state->bookbuf = NULL;
        state->booklen = 0;
 
        state->bookbuf = NULL;
        state->booklen = 0;
 
-       if (state->vi) {
-               vorbis_info_clear (state->vi);
-               free (state->vi);
-               state->vi = NULL;
-       }
-
        state->serial = 0;
 }
 
        state->serial = 0;
 }
 
@@ -144,13 +168,11 @@ vcedit_state_ref (vcedit_state *state)
 void
 vcedit_state_unref (vcedit_state *state)
 {
 void
 vcedit_state_unref (vcedit_state *state)
 {
-       state->refcount--;
+       if (--state->refcount)
+               return;
 
 
-       if (!state->refcount) {
-               fclose (state->in);
-               vcedit_clear_internals (state);
-               free (state);
-       }
+       vcedit_clear_internals (state);
+       vcedit_state_free (state);
 }
 
 /* Next two functions pulled straight from libvorbis, apart from one change
 }
 
 /* Next two functions pulled straight from libvorbis, apart from one change
@@ -270,7 +292,6 @@ vcedit_open (vcedit_state *state)
        ogg_packet header_main, header_comments, header_codebooks;
        ogg_page og;
 
        ogg_packet header_main, header_comments, header_codebooks;
        ogg_page og;
 
-       state->oy = malloc (sizeof (ogg_sync_state));
        ogg_sync_init (state->oy);
 
        while (1) {
        ogg_sync_init (state->oy);
 
        while (1) {
@@ -295,13 +316,8 @@ vcedit_open (vcedit_state *state)
 
        state->serial = ogg_page_serialno (&og);
 
 
        state->serial = ogg_page_serialno (&og);
 
-       state->os = malloc (sizeof (ogg_stream_state));
        ogg_stream_init (state->os, state->serial);
        ogg_stream_init (state->os, state->serial);
-
-       state->vi = malloc (sizeof (vorbis_info));
        vorbis_info_init (state->vi);
        vorbis_info_init (state->vi);
-
-       state->vc = malloc (sizeof (vorbis_comment));
        vorbis_comment_init (state->vc);
 
        if (ogg_stream_pagein (state->os, &og) < 0) {
        vorbis_comment_init (state->vc);
 
        if (ogg_stream_pagein (state->os, &og) < 0) {