[svn r155] Do better memory usage, and alloc/de-alloc heap space. trunk
authorrosfran
Thu Nov 30 20:06:25 2006 +0000 (2006-11-30)
branchtrunk
changeset 15430f32f34287c
parent 153 a0e5b7d75f25
child 155 1842993f4473
[svn r155] Do better memory usage, and alloc/de-alloc heap space.
gmyth/src/gmyth_socket.c
     1.1 --- a/gmyth/src/gmyth_socket.c	Thu Nov 30 19:44:22 2006 +0000
     1.2 +++ b/gmyth/src/gmyth_socket.c	Thu Nov 30 20:06:25 2006 +0000
     1.3 @@ -109,14 +109,16 @@
     1.4      gchar *portStr = NULL;
     1.5      gint errorn = EADDRNOTAVAIL;
     1.6  
     1.7 +
     1.8      hints = g_new0 (struct addrinfo, 1);
     1.9      hints->ai_family = AF_INET;
    1.10      hints->ai_socktype = SOCK_STREAM;
    1.11 -    /* hints.ai_flags = AI_NUMERICHOST; */
    1.12 +    hints->ai_flags = AI_NUMERICHOST;
    1.13 +    
    1.14      if ( port != -1 )	
    1.15          portStr = g_strdup_printf ("%d", port);
    1.16      else
    1.17 -		portStr = NULL;
    1.18 +				portStr = NULL;
    1.19  
    1.20      gmyth_debug ("Address: %s, port: %s\n", addr, portStr);
    1.21      if ( ( errorn = getaddrinfo(addr, portStr, hints, addrInfo) ) != 0 ) {
    1.22 @@ -191,7 +193,7 @@
    1.23  
    1.24  #else
    1.25  
    1.26 -static const char *PATH_PROC_NET_DEV = "/proc/net/dev";
    1.27 +static const gchar *PATH_PROC_NET_DEV = "/proc/net/dev";
    1.28  
    1.29  /** Gets the list of all local network interfaces (using the /proc/net/dev directory).
    1.30   * 
    1.31 @@ -626,18 +628,8 @@
    1.32      g_static_mutex_lock( &mutex );
    1.33      gmyth_debug ("[%s] Sending command to backend: %s\n", __FUNCTION__, command->str);
    1.34  
    1.35 -    /*
    1.36 -       io_cond = g_io_channel_get_buffer_condition( gmyth_socket->sd_io_ch );
    1.37 -
    1.38 -       if ( ( io_cond & G_IO_IN ) == 0 ) {
    1.39 -       g_warning ("[%s] IO channel is not able to send data!\n", __FUNCTION__);
    1.40 -       ret = FALSE;
    1.41 -       goto done;
    1.42 -       }
    1.43 -       */
    1.44 -
    1.45      buffer = g_strnfill( BUFLEN, ' ' );
    1.46 -    snprintf( buffer, MYTH_PROTOCOL_FIELD_SIZE+1, "%-8d", command->len);
    1.47 +    g_snprintf( buffer, MYTH_PROTOCOL_FIELD_SIZE+1, "%-8d", command->len);
    1.48      gmyth_debug ( "[%s] buffer = [%s]\n", __FUNCTION__, buffer  );
    1.49  
    1.50      command = g_string_prepend(command, buffer);
    1.51 @@ -846,7 +838,7 @@
    1.52  {
    1.53      GIOStatus io_status = G_IO_STATUS_NORMAL;
    1.54      GError* error = NULL;
    1.55 -    gchar *buffer = NULL;
    1.56 +    gchar *buffer;
    1.57  
    1.58      GString *str = NULL;
    1.59  
    1.60 @@ -860,7 +852,8 @@
    1.61  
    1.62      g_static_mutex_lock( &mutex );
    1.63  
    1.64 -    buffer = g_new0 (gchar, MYTH_PROTOCOL_FIELD_SIZE);
    1.65 +    //buffer = g_new0 (gchar, MYTH_PROTOCOL_FIELD_SIZE);
    1.66 +    buffer = g_strnfill (MYTH_PROTOCOL_FIELD_SIZE, ' ');
    1.67      io_status = g_io_channel_read_chars (gmyth_socket->sd_io_ch, buffer, MYTH_PROTOCOL_FIELD_SIZE, &bytes_read, &error);
    1.68  
    1.69      /* verify if the input (read) buffer is ready to receive data */
    1.70 @@ -878,24 +871,31 @@
    1.71  		/* verify if the input (read) buffer is ready to receive data */
    1.72  		io_cond = g_io_channel_get_buffer_condition( gmyth_socket->sd_io_ch );
    1.73  	
    1.74 -		if ( ( io_cond & G_IO_IN ) != 0 ) {
    1.75 -            gchar *buffer_aux = NULL;
    1.76 +		//if ( ( io_cond & G_IO_IN ) != 0 ) {
    1.77 +            //gchar *buffer_aux = NULL;
    1.78  
    1.79  		    /* removes trailing whitespace */
    1.80 -		    buffer_aux = g_strstrip (buffer);
    1.81 -		    len = (gint)g_ascii_strtoull (buffer_aux, NULL, 10 );
    1.82 +		    //buffer_aux = g_strstrip (buffer);
    1.83 +		    len = (gint)g_ascii_strtoull ( g_strstrip (buffer), NULL, 10 );
    1.84  
    1.85              if (buffer != NULL) {
    1.86                  g_free (buffer);
    1.87                  buffer = NULL;
    1.88              }
    1.89 +            
    1.90 +            /*            
    1.91 +            if (buffer_aux != NULL) {
    1.92 +                g_free (buffer_aux);
    1.93 +                buffer_aux = NULL;
    1.94 +            }
    1.95 +            */
    1.96  
    1.97              buffer = g_new0 (gchar, len+1);
    1.98  	
    1.99  		    bytes_read = 0;
   1.100  		    io_status = g_io_channel_read_chars( gmyth_socket->sd_io_ch, buffer, len, &bytes_read, &error);
   1.101  		    buffer[bytes_read] = '\0';
   1.102 -		}
   1.103 +		//}
   1.104      }  
   1.105  
   1.106      g_static_mutex_unlock( &mutex );
   1.107 @@ -984,7 +984,7 @@
   1.108      str_array = g_strsplit (response->str, MYTH_SEPARATOR, -1);
   1.109  
   1.110      for (i=0; i< g_strv_length (str_array); i++) {
   1.111 -	gmyth_string_list_append_string (str_list, g_string_new (str_array[i]));
   1.112 +	gmyth_string_list_append_char_array (str_list, str_array[i] );
   1.113      }
   1.114      g_static_mutex_unlock( &mutex );
   1.115