# HG changeset patch # User rosfran # Date 1164917185 0 # Node ID 30f32f34287c9f0b7026590c4c76b63131e412ee # Parent a0e5b7d75f251bce05c53c2c2552e3b3e75ec480 [svn r155] Do better memory usage, and alloc/de-alloc heap space. diff -r a0e5b7d75f25 -r 30f32f34287c gmyth/src/gmyth_socket.c --- a/gmyth/src/gmyth_socket.c Thu Nov 30 19:44:22 2006 +0000 +++ b/gmyth/src/gmyth_socket.c Thu Nov 30 20:06:25 2006 +0000 @@ -109,14 +109,16 @@ gchar *portStr = NULL; gint errorn = EADDRNOTAVAIL; + hints = g_new0 (struct addrinfo, 1); hints->ai_family = AF_INET; hints->ai_socktype = SOCK_STREAM; - /* hints.ai_flags = AI_NUMERICHOST; */ + hints->ai_flags = AI_NUMERICHOST; + if ( port != -1 ) portStr = g_strdup_printf ("%d", port); else - portStr = NULL; + portStr = NULL; gmyth_debug ("Address: %s, port: %s\n", addr, portStr); if ( ( errorn = getaddrinfo(addr, portStr, hints, addrInfo) ) != 0 ) { @@ -191,7 +193,7 @@ #else -static const char *PATH_PROC_NET_DEV = "/proc/net/dev"; +static const gchar *PATH_PROC_NET_DEV = "/proc/net/dev"; /** Gets the list of all local network interfaces (using the /proc/net/dev directory). * @@ -626,18 +628,8 @@ g_static_mutex_lock( &mutex ); gmyth_debug ("[%s] Sending command to backend: %s\n", __FUNCTION__, command->str); - /* - io_cond = g_io_channel_get_buffer_condition( gmyth_socket->sd_io_ch ); - - if ( ( io_cond & G_IO_IN ) == 0 ) { - g_warning ("[%s] IO channel is not able to send data!\n", __FUNCTION__); - ret = FALSE; - goto done; - } - */ - buffer = g_strnfill( BUFLEN, ' ' ); - snprintf( buffer, MYTH_PROTOCOL_FIELD_SIZE+1, "%-8d", command->len); + g_snprintf( buffer, MYTH_PROTOCOL_FIELD_SIZE+1, "%-8d", command->len); gmyth_debug ( "[%s] buffer = [%s]\n", __FUNCTION__, buffer ); command = g_string_prepend(command, buffer); @@ -846,7 +838,7 @@ { GIOStatus io_status = G_IO_STATUS_NORMAL; GError* error = NULL; - gchar *buffer = NULL; + gchar *buffer; GString *str = NULL; @@ -860,7 +852,8 @@ g_static_mutex_lock( &mutex ); - buffer = g_new0 (gchar, MYTH_PROTOCOL_FIELD_SIZE); + //buffer = g_new0 (gchar, MYTH_PROTOCOL_FIELD_SIZE); + buffer = g_strnfill (MYTH_PROTOCOL_FIELD_SIZE, ' '); io_status = g_io_channel_read_chars (gmyth_socket->sd_io_ch, buffer, MYTH_PROTOCOL_FIELD_SIZE, &bytes_read, &error); /* verify if the input (read) buffer is ready to receive data */ @@ -878,24 +871,31 @@ /* verify if the input (read) buffer is ready to receive data */ io_cond = g_io_channel_get_buffer_condition( gmyth_socket->sd_io_ch ); - if ( ( io_cond & G_IO_IN ) != 0 ) { - gchar *buffer_aux = NULL; + //if ( ( io_cond & G_IO_IN ) != 0 ) { + //gchar *buffer_aux = NULL; /* removes trailing whitespace */ - buffer_aux = g_strstrip (buffer); - len = (gint)g_ascii_strtoull (buffer_aux, NULL, 10 ); + //buffer_aux = g_strstrip (buffer); + len = (gint)g_ascii_strtoull ( g_strstrip (buffer), NULL, 10 ); if (buffer != NULL) { g_free (buffer); buffer = NULL; } + + /* + if (buffer_aux != NULL) { + g_free (buffer_aux); + buffer_aux = NULL; + } + */ buffer = g_new0 (gchar, len+1); bytes_read = 0; io_status = g_io_channel_read_chars( gmyth_socket->sd_io_ch, buffer, len, &bytes_read, &error); buffer[bytes_read] = '\0'; - } + //} } g_static_mutex_unlock( &mutex ); @@ -984,7 +984,7 @@ str_array = g_strsplit (response->str, MYTH_SEPARATOR, -1); for (i=0; i< g_strv_length (str_array); i++) { - gmyth_string_list_append_string (str_list, g_string_new (str_array[i])); + gmyth_string_list_append_char_array (str_list, str_array[i] ); } g_static_mutex_unlock( &mutex );