Code

We now allocate and free memory with glib
authorKalle Wallin <kaw@linux.se>
Sun, 28 Mar 2004 21:27:11 +0000 (21:27 +0000)
committerKalle Wallin <kaw@linux.se>
Sun, 28 Mar 2004 21:27:11 +0000 (21:27 +0000)
git-svn-id: https://svn.musicpd.org/ncmpc/trunk@526 09075e82-0dd4-0310-85a5-a0d7c8717e4f

conf.c
list_window.c
main.c
mpc.c
options.c
screen.c
screen_file.c
screen_play.c
screen_utils.c

diff --git a/conf.c b/conf.c
index 12fc15a61609e619a3419820c09e4feae69cac76..170da5e5e19c80fb5871edcaca354ade9fa19fcf 100644 (file)
--- a/conf.c
+++ b/conf.c
@@ -6,6 +6,8 @@
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <fcntl.h>
+
+#include <glib.h>
 #include <ncurses.h>
 
 #include "config.h"
@@ -104,7 +106,7 @@ read_rc_file(char *filename, options_t *options)
     {
       D(perror(filename));
       if( free_filename )
-       free(filename);
+       g_free(filename);
       return -1;
     }
 
@@ -248,7 +250,7 @@ read_rc_file(char *filename, options_t *options)
   D(printf( "--\n\n" ));
 
   if( free_filename )
-    free(filename);
+    g_free(filename);
  
   return 0;
 }
index d976ce05c351372b1f87efea8427f4f31dc3d89b..b04b817daab212dc7cc5a0e8d9750441273b89fc 100644 (file)
@@ -14,7 +14,7 @@ list_window_init(WINDOW *w, int width, int height)
 {
   list_window_t *lw;
 
-  lw = malloc(sizeof(list_window_t));
+  lw = g_malloc(sizeof(list_window_t));
   memset(lw, 0, sizeof(list_window_t));
   lw->w = w;
   lw->cols = width;
@@ -29,7 +29,7 @@ list_window_free(list_window_t *lw)
   if( lw )
     {
       memset(lw, 0, sizeof(list_window_t));
-      free(lw);
+      g_free(lw);
     }
   return NULL;
 }
diff --git a/main.c b/main.c
index 0bfcb74349ae9a1e6283bcb72373e036b0341d8f..56c18a31652bc4cebb16bb0d37075a7c60eca3be 100644 (file)
--- a/main.c
+++ b/main.c
@@ -28,6 +28,7 @@ exit_and_cleanup(void)
        fprintf(stderr,"Error: %s\n", mpc_error_str(mpc));
       mpc_close(mpc);
     }
+  g_free(options.host);
 }
 
 void
diff --git a/mpc.c b/mpc.c
index f48d83cd7d904051281fffde5e85e351312b8fb8..785117fc9ad58c8b0ddd420d0a6696e2bc174a27 100644 (file)
--- a/mpc.c
+++ b/mpc.c
@@ -1,8 +1,3 @@
-/* 
- * $Id: mpc.c,v 1.5 2004/03/17 23:19:21 kalle Exp $ 
- *
- */
-
 #include <stdio.h>
 #include <stdlib.h>
 #include <unistd.h>
@@ -62,7 +57,7 @@ mpc_close(mpd_client_t *c)
   if( c->connection )
     mpd_closeConnection(c->connection);
   if( c->cwd )
-    free( c->cwd );
+    g_free( c->cwd );
   
   return 0;
 }
@@ -80,10 +75,10 @@ mpc_connect(char *host, int port)
       exit(EXIT_FAILURE);
     }
   
-  c = malloc(sizeof(mpd_client_t));
+  c = g_malloc(sizeof(mpd_client_t));
   memset(c, 0, sizeof(mpd_client_t));
   c->connection = connection;
-  c->cwd = strdup("");
+  c->cwd = g_strdup("");
 
   return c;
 }
@@ -228,20 +223,20 @@ mpc_get_song_name(mpd_Song *song)
          snprintf(buf, MAX_SONG_LENGTH, "%s - %s", song->artist, song->title);
          name = utf8_to_locale(buf);
          strncpy(buf, name, MAX_SONG_LENGTH);
-         free(name);
+         g_free(name);
          return buf;
        }
       else
        {
          name = utf8_to_locale(song->title);
          strncpy(buf, name, MAX_SONG_LENGTH);
-         free(name);
+         g_free(name);
          return buf;
        }
     }
   name = utf8_to_locale(basename(song->file));
   strncpy(buf, name, MAX_SONG_LENGTH);
-  free(name);
+  g_free(name);
   return buf;
 }
 
@@ -294,7 +289,7 @@ mpc_free_filelist(mpd_client_t *c)
 
       if( entry->entity )
        mpd_freeInfoEntity(entry->entity);
-      free(entry);
+      g_free(entry);
       list=list->next;
     }
   g_list_free(c->filelist);
@@ -325,7 +320,7 @@ mpc_update_filelist(mpd_client_t *c)
   if( c->cwd && c->cwd[0] )
     {
       /* add a dummy entry for ./.. */
-      filelist_entry_t *entry = malloc(sizeof(filelist_entry_t));
+      filelist_entry_t *entry = g_malloc(sizeof(filelist_entry_t));
       memset(entry, 0, sizeof(filelist_entry_t));
       entry->entity = NULL;
       c->filelist = g_list_append(c->filelist, (gpointer) entry);
@@ -334,7 +329,7 @@ mpc_update_filelist(mpd_client_t *c)
 
   while( (entity=mpd_getNextInfoEntity(c->connection)) ) 
     {
-      filelist_entry_t *entry = malloc(sizeof(filelist_entry_t));
+      filelist_entry_t *entry = g_malloc(sizeof(filelist_entry_t));
       
       memset(entry, 0, sizeof(filelist_entry_t));
       entry->entity = entity;
index f1840325b56440baddcf08c6d70804a68dd52db6..cf3bbca27142e1c76ff4dd2d1c1bd42ddbe97f2a 100644 (file)
--- a/options.c
+++ b/options.c
@@ -3,6 +3,7 @@
 #include <unistd.h>
 #include <string.h>
 #include <ncurses.h>
+#include <glib.h>
 #include <popt.h>
 
 #include "config.h"
@@ -98,9 +99,9 @@ options_init( void )
 
   memset(&options, 0, sizeof(options_t));
   if( (value=getenv(MPD_HOST_ENV)) )
-    options.host = strdup(value);
+    options.host = g_strdup(value);
   else
-    options.host = strdup(DEFAULT_HOST);
+    options.host = g_strdup(DEFAULT_HOST);
   if( (value=getenv(MPD_PORT_ENV)) )
     options.port = atoi(value);
   else
index 3025d9a01f1718ba01b1e60b089772cd8ee320b4..67ecc18bf74b8c64d89aa62c605062df5ba0724f 100644 (file)
--- a/screen.c
+++ b/screen.c
@@ -220,9 +220,9 @@ screen_exit(void)
       screen->playlist = list_window_free(screen->playlist);
       screen->filelist = list_window_free(screen->filelist);
       screen->helplist = list_window_free(screen->helplist);
-      free(screen->buf);
-      free(screen->findbuf);
-      free(screen);
+      g_free(screen->buf);
+      g_free(screen->findbuf);
+      g_free(screen);
       screen = NULL;
     }
   return 0;
@@ -311,12 +311,12 @@ screen_init(void)
       exit(EXIT_FAILURE);
     }
 
-  screen = malloc(sizeof(screen_t));
+  screen = g_malloc(sizeof(screen_t));
   memset(screen, 0, sizeof(screen_t));
   screen->mode = SCREEN_PLAY_WINDOW;
   screen->cols = COLS;
   screen->rows = LINES;
-  screen->buf  = malloc(screen->cols);
+  screen->buf  = g_malloc(screen->cols);
   screen->buf_size = screen->cols;
   screen->findbuf = NULL;
   screen->painted = 0;
index c866566de66ba5e866add2223aa6c188e10cfdee..42d9456173ab211a02870b454b77d2e397d890ba 100644 (file)
@@ -15,7 +15,7 @@
 
 #define BUFSIZE 1024
 
-#undef USE_OLD_LAYOUT
+#define USE_OLD_LAYOUT
 
 static char *
 list_callback(int index, int *highlight, void *data)
@@ -37,7 +37,7 @@ list_callback(int index, int *highlight, void *data)
 #ifdef USE_OLD_LAYOUT
       return "[..]";
 #else
-      return "<d> ..";
+      return "d ..";
 #endif
     }
   if( entity->type==MPD_INFO_ENTITY_TYPE_DIRECTORY ) 
@@ -48,9 +48,9 @@ list_callback(int index, int *highlight, void *data)
 #ifdef USE_OLD_LAYOUT
       snprintf(buf, BUFSIZE, "[%s]", dirname);
 #else
-      snprintf(buf, BUFSIZE, "<d> %s", dirname);
+      snprintf(buf, BUFSIZE, "d %s", dirname);
 #endif
-      free(dirname);
+      g_free(dirname);
       return buf;
     }
   else if( entity->type==MPD_INFO_ENTITY_TYPE_SONG )
@@ -58,9 +58,9 @@ list_callback(int index, int *highlight, void *data)
       mpd_Song *song = entity->info.song;
 
 #ifdef USE_OLD_LAYOUT      
-      return mpc_get_song_name(song));
+      return mpc_get_song_name(song);
 #else
-      snprintf(buf, BUFSIZE, "<m> %s", mpc_get_song_name(song));
+      snprintf(buf, BUFSIZE, "m %s", mpc_get_song_name(song));
       return buf;
 #endif
 
@@ -73,9 +73,9 @@ list_callback(int index, int *highlight, void *data)
 #ifdef USE_OLD_LAYOUT      
       snprintf(buf, BUFSIZE, "*%s*", filename);
 #else      
-      snprintf(buf, BUFSIZE, "<p> %s", filename);
+      snprintf(buf, BUFSIZE, "p %s", filename);
 #endif
-      free(filename);
+      g_free(filename);
       return buf;
     }
   return "Error: Unknow entry!";
@@ -96,7 +96,7 @@ change_directory(screen_t *screen, mpd_client_t *c, filelist_entry_t *entry)
          parent[0] = '\0';
        }
       if( c->cwd )
-       free(c->cwd);
+       g_free(c->cwd);
       c->cwd = parent;
     }
   else
@@ -104,8 +104,8 @@ change_directory(screen_t *screen, mpd_client_t *c, filelist_entry_t *entry)
       {
        mpd_Directory *dir = entity->info.directory;
        if( c->cwd )
-         free(c->cwd);
-       c->cwd = strdup(dir->path);      
+         g_free(c->cwd);
+       c->cwd = g_strdup(dir->path);      
       }
     else
       return -1;
@@ -126,7 +126,7 @@ load_playlist(screen_t *screen, mpd_client_t *c, filelist_entry_t *entry)
   mpd_finishCommand(c->connection);
 
   screen_status_printf("Loading playlist %s...", filename);
-  free(filename);
+  g_free(filename);
   return 0;
 }
 
@@ -156,7 +156,7 @@ handle_delete(screen_t *screen, mpd_client_t *c)
   plf = entity->info.playlistFile;
   str = utf8_to_locale(basename(plf->path));
   snprintf(buf, BUFSIZE, "Delete playlist %s [y/n] ? ", str);
-  free(str);  
+  g_free(str);  
   key = tolower(screen_getch(screen->status_window.w, buf));
   if( key!='y' )
     {
@@ -170,7 +170,7 @@ handle_delete(screen_t *screen, mpd_client_t *c)
     {
       str = utf8_to_locale(mpc_error_str(c));
       screen_status_printf("Error: %s", str);
-      free(str);
+      g_free(str);
       beep();
       return -1;
     }
@@ -210,7 +210,7 @@ add_directory(mpd_client_t *c, char *dir)
 
   dirname = utf8_to_locale(dir);
   screen_status_printf("Adding directory %s...\n", dirname);
-  free(dirname);
+  g_free(dirname);
   dirname = NULL;
 
   mpd_sendLsInfoCommand(c->connection, dir);
@@ -356,7 +356,7 @@ file_get_header(mpd_client_t *c)
           TOP_HEADER_FILE ": %s                          ",
           tmp
           );
-  free(tmp);
+  g_free(tmp);
 
   return buf;
 }
index 5f93ad09331e0108cb57f05af4b7488d292b39c9..72c223ed33fbddb49c78a642400dace3758db3f3 100644 (file)
@@ -50,7 +50,7 @@ handle_save_playlist(screen_t *screen, mpd_client_t *c)
   /* send save command to mpd */
   mpd_sendSaveCommand(c->connection, filename_utf8);
   mpd_finishCommand(c->connection);
-  free(filename_utf8);
+  g_free(filename_utf8);
   /* handle errors */
   if( mpc_error(c))
     {
@@ -58,7 +58,7 @@ handle_save_playlist(screen_t *screen, mpd_client_t *c)
        {
          char *str = utf8_to_locale(mpc_error_str(c));
          screen_status_message(str);
-         free(str);
+         g_free(str);
        }
       else
        screen_status_printf("Error: Unable to save playlist as %s", filename);
@@ -68,7 +68,7 @@ handle_save_playlist(screen_t *screen, mpd_client_t *c)
     }
   /* success */
   screen_status_printf("Saved %s", filename);
-  free(filename);
+  g_free(filename);
   /* update the file list if it has been initalized */
   if( c->filelist )
     {
index 61c212edc9431d2e63fa29c7db9a07ad4ede2ee3..b93069c22959259324ef8adc04aa95ab4906fb20 100644 (file)
@@ -56,7 +56,7 @@ screen_getstr(WINDOW *w, char *prompt)
   curs_set(1);
 
   if( wgetnstr(w, buf, 256) == OK )
-    line = strdup(buf);
+    line = g_strdup(buf);
 
   noecho();
   curs_set(0);
@@ -90,7 +90,7 @@ screen_find(screen_t *screen,
     case CMD_LIST_RFIND:
       if( screen->findbuf )
        {
-         free(screen->findbuf);
+         g_free(screen->findbuf);
          screen->findbuf=NULL;
        }
       /* continue... */