speechd-discuss
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH 2/2] Use GLib allocation in the audio subsystem.


From: Christopher Brannon
Subject: [PATCH 2/2] Use GLib allocation in the audio subsystem.
Date: Sat, 2 Oct 2010 10:03:43 -0500

Now, all memory management in the audio subsystem is done with
g_malloc, g_free, g_strdup, etc.
---
 src/modules/audio/alsa.c  |   31 ++++++++++++++++---------------
 src/modules/audio/libao.c |   13 +++++++------
 src/modules/audio/nas.c   |    5 +++--
 src/modules/audio/oss.c   |   34 +++++++++++++++-------------------
 src/modules/audio/pulse.c |    7 ++++---
 5 files changed, 45 insertions(+), 45 deletions(-)

diff --git a/src/modules/audio/alsa.c b/src/modules/audio/alsa.c
index 8dcf98e..9fbfc7f 100644
--- a/src/modules/audio/alsa.c
+++ b/src/modules/audio/alsa.c
@@ -33,6 +33,7 @@
 #include <sys/time.h>
 #include <time.h>
 #include <pthread.h>
+#include <glib.h>
 
 #include <alsa/asoundlib.h>
 
@@ -82,7 +83,7 @@ do { \
      struct timeval tv; \
      char *tstr; \
      t = time(NULL); \
-     tstr = strdup(ctime(&t)); \
+     tstr = g_strdup(ctime(&t)); \
      tstr[strlen(tstr)-1] = 0; \
      gettimeofday(&tv,NULL); \
      fprintf(stderr," %s [%d]",tstr, (int) tv.tv_usec); \
@@ -90,7 +91,7 @@ do { \
      fprintf(stderr,arg); \
      fprintf(stderr,"\n"); \
      fflush(stderr); \
-     xfree(tstr); \
+     g_free(tstr); \
   }
 
 #define ERR(arg...) \
@@ -99,7 +100,7 @@ do { \
      struct timeval tv; \
      char *tstr; \
      t = time(NULL); \
-     tstr = strdup(ctime(&t)); \
+     tstr = g_strdup(ctime(&t)); \
      tstr[strlen(tstr)-1] = 0; \
      gettimeofday(&tv,NULL); \
      fprintf(stderr," %s [%d]",tstr, (int) tv.tv_usec); \
@@ -107,7 +108,7 @@ do { \
      fprintf(stderr,arg); \
      fprintf(stderr,"\n"); \
      fflush(stderr); \
-     xfree(tstr); \
+     g_free(tstr); \
   }
 
 static int alsa_log_level;
@@ -230,7 +231,7 @@ _alsa_close(spd_alsa_id_t *id)
 
     snd_pcm_sw_params_free (id->alsa_sw_params);
 
-    free(id->alsa_poll_fds);
+    g_free(id->alsa_poll_fds);
     pthread_mutex_unlock(&id->alsa_pipe_mutex);
 
     MSG(1, "Closing ALSA device ... success");
@@ -258,7 +259,7 @@ alsa_open(void **pars)
        return NULL;
     }
 
-    alsa_id = (spd_alsa_id_t *) malloc(sizeof(spd_alsa_id_t));
+    alsa_id = (spd_alsa_id_t *) g_malloc(sizeof(spd_alsa_id_t));
 
     pthread_mutex_init(&alsa_id->alsa_pipe_mutex, NULL);
 
@@ -266,12 +267,12 @@ alsa_open(void **pars)
     
     MSG(1, "Opening ALSA sound output");
 
-    alsa_id->alsa_device_name = strdup(pars[1]);
+    alsa_id->alsa_device_name = g_strdup(pars[1]);
     
     ret = _alsa_open(alsa_id);
     if (ret){
        ERR("Cannot initialize Alsa device '%s': Can't open.", 
alsa_id->alsa_device_name);
-    free (alsa_id);
+    g_free (alsa_id);
        return NULL;
     }
 
@@ -294,8 +295,8 @@ alsa_close(AudioID *id)
     }
     MSG(1, "ALSA closed.");
 
-    free (alsa_id->alsa_device_name);
-    free (alsa_id);
+    g_free (alsa_id->alsa_device_name);
+    g_free (alsa_id);
     id = NULL;
 
     return 0;
@@ -370,7 +371,7 @@ int wait_for_poll(spd_alsa_id_t *id, struct pollfd 
*alsa_poll_fds,
 
 
 #define ERROR_EXIT()\
-    free(track_volume.samples); \
+    g_free(track_volume.samples); \
     ERR("alsa_play() abnormal exit"); \
     _alsa_close(alsa_id); \
     return -1;
@@ -464,7 +465,7 @@ alsa_play(AudioID *id, AudioTrack track)
     }
 
     /* Create and fill in struct pollfd *alsa_poll_fds with ALSA descriptors */
-    alsa_id->alsa_poll_fds = malloc ((alsa_id->alsa_fd_count + 1) * 
sizeof(struct pollfd));
+    alsa_id->alsa_poll_fds = g_malloc ((alsa_id->alsa_fd_count + 1) * 
sizeof(struct pollfd));
     assert(alsa_id->alsa_poll_fds);
     if ((err = snd_pcm_poll_descriptors(alsa_id->alsa_pcm, 
alsa_id->alsa_poll_fds, alsa_id->alsa_fd_count)) < 0) {
        ERR("Unable to obtain poll descriptors for playback: %s\n", 
snd_strerror(err));
@@ -602,7 +603,7 @@ alsa_play(AudioID *id, AudioTrack track)
     /* Create a copy of track with adjusted volume. */
     MSG(4, "Making copy of track and adjusting volume");
     track_volume = track;
-    track_volume.samples = (short*) malloc(volume_size);
+    track_volume.samples = (short*) g_malloc(volume_size);
     real_volume = ((float) alsa_id->id.volume + 100)/(float)200;
     for (i=0; i<=track.num_samples-1; i++)
         track_volume.samples[i] = track.samples[i] * real_volume;
@@ -739,7 +740,7 @@ alsa_play(AudioID *id, AudioTrack track)
  terminate:
     /* Terminating (successfully or after a stop) */
     if (track_volume.samples != NULL)
-       free(track_volume.samples);
+       g_free(track_volume.samples);
 
     err = snd_pcm_drop(alsa_id->alsa_pcm);
     if (err < 0) {
@@ -756,7 +757,7 @@ alsa_play(AudioID *id, AudioTrack track)
     close(alsa_id->alsa_stop_pipe[0]);
     close(alsa_id->alsa_stop_pipe[1]);
 
-    xfree(alsa_id->alsa_poll_fds);
+    g_free(alsa_id->alsa_poll_fds);
     pthread_mutex_unlock(&alsa_id->alsa_pipe_mutex);
     
     MSG(1, "End of playback on ALSA");
diff --git a/src/modules/audio/libao.c b/src/modules/audio/libao.c
index 6179c94..79b3e16 100644
--- a/src/modules/audio/libao.c
+++ b/src/modules/audio/libao.c
@@ -28,6 +28,7 @@
 #include <sys/time.h>
 #include <time.h>
 #include <string.h>
+#include <glib.h>
 #include <ao/ao.h>
 
 #include "spd_audio_plugin.h"
@@ -41,7 +42,7 @@
      struct timeval tv; \
      char *tstr; \
      t = time(NULL); \
-     tstr = strdup(ctime(&t)); \
+     tstr = g_strdup(ctime(&t)); \
      tstr[strlen(tstr)-1] = 0; \
      gettimeofday(&tv,NULL); \
      fprintf(stderr," %s [%d]",tstr, (int) tv.tv_usec); \
@@ -49,7 +50,7 @@
      fprintf(stderr,arg); \
      fprintf(stderr,"\n"); \
      fflush(stderr); \
-     xfree(tstr); \
+     g_free(tstr); \
   }
 
 #define ERR(arg...) \
@@ -58,7 +59,7 @@
      struct timeval tv; \
      char *tstr; \
      t = time(NULL); \
-     tstr = strdup(ctime(&t)); \
+     tstr = g_strdup(ctime(&t)); \
      tstr[strlen(tstr)-1] = 0; \
      gettimeofday(&tv,NULL); \
      fprintf(stderr," %s [%d]",tstr, (int) tv.tv_usec); \
@@ -66,7 +67,7 @@
      fprintf(stderr,arg); \
      fprintf(stderr,"\n"); \
      fflush(stderr); \
-     xfree(tstr); \
+     g_free(tstr); \
   }
 
 /* AO_FORMAT_INITIALIZER is an ao_sample_format structure with zero values
@@ -112,7 +113,7 @@ static AudioID * libao_open (void **pars)
 {
   AudioID * id;
 
-  id = (AudioID *) malloc(sizeof(AudioID));
+  id = (AudioID *) g_malloc(sizeof(AudioID));
 
   ao_initialize ();
   default_driver = ao_default_driver_id ();
@@ -204,7 +205,7 @@ static int libao_close (AudioID * id)
   libao_close_handle();
   ao_shutdown ();
 
-  free (id);
+  g_free (id);
   id = NULL;
   return 0;
 }
diff --git a/src/modules/audio/nas.c b/src/modules/audio/nas.c
index 056e14b..3d8d6e3 100644
--- a/src/modules/audio/nas.c
+++ b/src/modules/audio/nas.c
@@ -27,6 +27,7 @@
 
 #include <stdio.h>
 #include <stdlib.h>
+#include <glib.h>
 #include <audio/audiolib.h>
 #include <audio/soundlib.h>
 
@@ -90,7 +91,7 @@ nas_open(void **pars)
     int ret;
     AuBool r;
 
-    nas_id = (spd_nas_id_t *) malloc(sizeof(spd_nas_id_t));
+    nas_id = (spd_nas_id_t *) g_malloc(sizeof(spd_nas_id_t));
 
     nas_id->aud = AuOpenServer(pars[2], 0, NULL, 0, NULL, NULL);
     if (!nas_id->aud){
@@ -215,7 +216,7 @@ nas_close(AudioID *id)
 
     AuCloseServer(nas_id->aud);
 
-    free (nas_id);
+    g_free (nas_id);
     id = NULL;
 
     return 0;
diff --git a/src/modules/audio/oss.c b/src/modules/audio/oss.c
index 5da6173..e6466f9 100644
--- a/src/modules/audio/oss.c
+++ b/src/modules/audio/oss.c
@@ -37,6 +37,7 @@
 #include <unistd.h> /* for open, close */
 #include <sys/ioctl.h>
 #include <pthread.h>
+#include <glib.h>
 
 #include <sys/soundcard.h>
 
@@ -62,7 +63,7 @@ static int _oss_sync(spd_oss_id_t *id);
      struct timeval tv; \
      char *tstr; \
      t = time(NULL); \
-     tstr = strdup(ctime(&t)); \
+     tstr = g_strdup(ctime(&t)); \
      tstr[strlen(tstr)-1] = 0; \
      gettimeofday(&tv,NULL); \
      fprintf(stderr," %s [%d]",tstr, (int) tv.tv_usec); \
@@ -70,7 +71,7 @@ static int _oss_sync(spd_oss_id_t *id);
      fprintf(stderr,arg); \
      fprintf(stderr,"\n"); \
      fflush(stderr); \
-     xfree(tstr); \
+     g_free(tstr); \
   }
 
 #define ERR(arg...) \
@@ -79,7 +80,7 @@ static int _oss_sync(spd_oss_id_t *id);
      struct timeval tv; \
      char *tstr; \
      t = time(NULL); \
-     tstr = strdup(ctime(&t)); \
+     tstr = g_strdup(ctime(&t)); \
      tstr[strlen(tstr)-1] = 0; \
      gettimeofday(&tv,NULL); \
      fprintf(stderr," %s [%d]",tstr, (int) tv.tv_usec); \
@@ -87,17 +88,12 @@ static int _oss_sync(spd_oss_id_t *id);
      fprintf(stderr,arg); \
      fprintf(stderr,"\n"); \
      fflush(stderr); \
-     xfree(tstr); \
+     g_free(tstr); \
   }
 
 static int oss_log_level;
 static char const * oss_play_cmd="play";
 
-void
-xfree(void* p)
-{
-    if (p != NULL) free(p);
-}
 
 static int
 _oss_open(spd_oss_id_t *id)
@@ -146,9 +142,9 @@ oss_open(void **pars)
 
     if (pars[0] == NULL) return NULL;
 
-    oss_id = (spd_oss_id_t *) malloc(sizeof(spd_oss_id_t));
+    oss_id = (spd_oss_id_t *) g_malloc(sizeof(spd_oss_id_t));
 
-    oss_id->device_name = (char*) strdup((char*) pars[0]);
+    oss_id->device_name = g_strdup((char*) pars[0]);
 
     pthread_mutex_init(&oss_id->fd_mutex, NULL);
 
@@ -158,14 +154,14 @@ oss_open(void **pars)
     /* Test if it's possible to access the device */
     ret = _oss_open(oss_id);
     if (ret) {
-        free (oss_id->device_name);
-        free (oss_id);
+        g_free (oss_id->device_name);
+        g_free (oss_id);
         return NULL;
     }
     ret = _oss_close(oss_id);
     if (ret) {
-        free (oss_id->device_name);
-        free (oss_id);
+        g_free (oss_id->device_name);
+        g_free (oss_id);
         return NULL;
     }
 
@@ -219,7 +215,7 @@ oss_play(AudioID *id, AudioTrack track)
 
     /* Create a copy of track with the adjusted volume */
     track_volume = track;
-    track_volume.samples = (short*) malloc(sizeof(short)*track.num_samples);
+    track_volume.samples = (short*) g_malloc(sizeof(short)*track.num_samples);
     real_volume = ((float) id->volume + 100)/(float)200;
     for (i=0; i<=track.num_samples-1; i++)
        track_volume.samples[i] = track.samples[i] * real_volume;
@@ -409,7 +405,7 @@ oss_play(AudioID *id, AudioTrack track)
     }
     MSG(4, "End of wait");
 
-    if (track_volume.samples!=NULL) free(track_volume.samples);
+    if (track_volume.samples!=NULL) g_free(track_volume.samples);
 
     /* Flush all the buffers */
     _oss_sync(oss_id);
@@ -460,8 +456,8 @@ oss_close(AudioID *id)
     /* Does nothing because the device is being automatically openned and
        closed in oss_play before and after playing each sample. */
 
-    free(oss_id->device_name);
-    free (oss_id);
+    g_free(oss_id->device_name);
+    g_free (oss_id);
     id = NULL;
 
     return 0;
diff --git a/src/modules/audio/pulse.c b/src/modules/audio/pulse.c
index 86110fc..4145965 100644
--- a/src/modules/audio/pulse.c
+++ b/src/modules/audio/pulse.c
@@ -38,6 +38,7 @@
 #include <time.h>
 #include <string.h>
 #include <stdarg.h>
+#include <glib.h>
 
 #include <pulse/simple.h>
 #include <pulse/error.h>
@@ -143,7 +144,7 @@ static AudioID * pulse_open (void **pars)
     spd_pulse_id_t * pulse_id;
     int ret;
 
-    pulse_id = (spd_pulse_id_t *) malloc(sizeof(spd_pulse_id_t));
+    pulse_id = (spd_pulse_id_t *) g_malloc(sizeof(spd_pulse_id_t));
 
     /* Select an Endianness for the initial connection. */
 #if defined(BYTE_ORDER) && (BYTE_ORDER == BIG_ENDIAN)
@@ -170,7 +171,7 @@ static AudioID * pulse_open (void **pars)
 
     ret =  _pulse_open(pulse_id, 44100, 1, 2);
     if (ret) {
-        free(pulse_id);
+        g_free(pulse_id);
         pulse_id = NULL;
     }
 
@@ -256,7 +257,7 @@ static int pulse_close (AudioID * id)
 {
     spd_pulse_id_t * pulse_id = (spd_pulse_id_t *)id;
     pulse_connection_close(pulse_id);
-    free (pulse_id);
+    g_free (pulse_id);
     id = NULL;
 
     return 0;
-- 
1.7.3




reply via email to

[Prev in Thread] Current Thread [Next in Thread]