Re: [gtk-vnc-devel] [patch] - Use GLib memory functions



Em Qui, 2008-04-10 às 23:17 -0500, Anthony Liguori escreveu:
> Jonh Wendell wrote:
> > Hi, folks.
> >
> > As discussed previously, here is a patch to use GLib functions always as
> > possible.
> >
> > May I commit?
> >   
> 
> I'd kind of prefer replacing g_try_malloc with g_malloc.  We can't 
> gracefully handle out of memory conditions so we might as well let glib 
> kill the application.
> 
> Regards,
> 
> Anthony Liguori

OK, it makes sense to me.
I just pushed this patch.

Cheers,
-- 
Jonh Wendell
www.bani.com.br

# HG changeset patch
# User Jonh Wendell <wendell bani com br>
# Date 1207916148 10800
# Node ID 53a42c599c777196fd2bfbd58999e80ccb734805
# Parent  b7bbfb2b5a47fb9aa68165fccb31414a45c9891a
Use GLib memory functions

diff -r b7bbfb2b5a47 -r 53a42c599c77 src/gvnc.c
--- a/src/gvnc.c	Sun Apr 06 17:44:38 2008 -0500
+++ b/src/gvnc.c	Fri Apr 11 09:15:48 2008 -0300
@@ -16,7 +16,6 @@
 
 #include <netdb.h>
 #include <string.h>
-#include <malloc.h>
 #include <unistd.h>
 #include <stdio.h>
 #include <errno.h>
@@ -1151,19 +1150,12 @@ static void gvnc_raw_update(struct gvnc 
 		return;
 	}
 
-	dst = malloc(width * (gvnc->fmt.bits_per_pixel / 8));
-	if (dst == NULL) {
-		GVNC_DEBUG("Closing the connection: gvnc_raw_update\n");
-		gvnc->has_error = TRUE;
-		return;
-	}
-	
+	dst = g_malloc(width * (gvnc->fmt.bits_per_pixel / 8));
 	for (i = 0; i < height; i++) {
 		gvnc_read(gvnc, dst, width * (gvnc->fmt.bits_per_pixel / 8));
 		gvnc_blt(gvnc, dst, 0, x, y + i, width, 1);
 	}
-	
-	free(dst);
+	g_free(dst);
 }
 
 static void gvnc_copyrect_update(struct gvnc *gvnc,
@@ -1449,13 +1441,7 @@ static void gvnc_zrle_update(struct gvnc
 	uint8_t *zlib_data;
 
 	length = gvnc_read_u32(gvnc);
-	zlib_data = malloc(length);
-	if (zlib_data == NULL) {
-		GVNC_DEBUG("Closing the connection: gvnc_zrle_update\n");
-		gvnc->has_error = TRUE;
-		return;
-	}
-
+	zlib_data = g_malloc(length);
 	gvnc_read(gvnc, zlib_data, length);
 
 	/* setup subsequent calls to gvnc_read*() to use the compressed data */
@@ -1480,7 +1466,7 @@ static void gvnc_zrle_update(struct gvnc
 	gvnc->compressed_length = 0;
 	gvnc->compressed_buffer = NULL;
 
-	free(zlib_data);
+	g_free(zlib_data);
 }
 
 static void gvnc_rgb24_blt(struct gvnc *gvnc, int x, int y,
@@ -1876,27 +1862,10 @@ static void gvnc_rich_cursor(struct gvnc
 		imagelen = width * height * (gvnc->fmt.bits_per_pixel / 8);
 		masklen = ((width + 7)/8) * height;
 
-		image = malloc(imagelen);
-		if (!image) {
-			GVNC_DEBUG("Closing the connection: gvnc_rich_cursor() - !image \n");
-			gvnc->has_error = TRUE;
-			return;
-		}
-		mask = malloc(masklen);
-		if (!mask) {
-			free(image);
-			GVNC_DEBUG("Closing the connection: gvnc_rich_cursor() - !mask \n");
-			gvnc->has_error = TRUE;
-			return;
-		}
-		pixbuf = malloc(width * height * 4); /* RGB-A 8bit */
-		if (!pixbuf) {
-			free(mask);
-			free(image);
-			GVNC_DEBUG("Closing the connection: gvnc_rich_cursor() - !pixbuf \n");
-			gvnc->has_error = TRUE;
-			return;
-		}
+		image = g_malloc(imagelen);
+		mask = g_malloc(masklen);
+		pixbuf = g_malloc(width * height * 4); /* RGB-A 8bit */
+
 		gvnc_read(gvnc, image, imagelen);
 		gvnc_read(gvnc, mask, masklen);
 
@@ -1904,8 +1873,8 @@ static void gvnc_rich_cursor(struct gvnc
 				     width * (gvnc->fmt.bits_per_pixel/8),
 				     width, height);
 
-		free(image);
-		free(mask);
+		g_free(image);
+		g_free(mask);
 	}
 
 	if (gvnc->has_error || !gvnc->ops.local_cursor)
@@ -1915,9 +1884,8 @@ static void gvnc_rich_cursor(struct gvnc
 		gvnc->has_error = TRUE;
 	}
 
-	free(pixbuf);
+	g_free(pixbuf);
 }
-
 
 static void gvnc_xcursor(struct gvnc *gvnc, int x, int y, int width, int height)
 {
@@ -1936,18 +1904,10 @@ static void gvnc_xcursor(struct gvnc *gv
 		bg = (255 << 24) | (bgrgb[0] << 16) | (bgrgb[1] << 8) | bgrgb[2];
 
 		rowlen = ((width + 7)/8);
-		if (!(data = malloc(rowlen*height))) {
-			GVNC_DEBUG("Closing the connection: gvnc_xcursor() - !data \n");
-			gvnc->has_error = TRUE;
-			return;
-		}
-		if (!(mask = malloc(rowlen*height))) {
-			free(data);
-			GVNC_DEBUG("Closing the connection: gvnc_xcursor() - !mask \n");
-			gvnc->has_error = TRUE;
-			return;
-		}
-		pixbuf = malloc(width * height * 4); /* RGB-A 8bit */
+		data = g_malloc(rowlen*height);
+		mask = g_malloc(rowlen*height);
+		pixbuf = g_malloc(width * height * 4); /* RGB-A 8bit */
+
 		gvnc_read(gvnc, data, rowlen*height);
 		gvnc_read(gvnc, mask, rowlen*height);
 		datap = data;
@@ -1961,8 +1921,8 @@ static void gvnc_xcursor(struct gvnc *gv
 			datap += rowlen;
 			maskp += rowlen;
 		}
-		free(data);
-		free(mask);
+		g_free(data);
+		g_free(mask);
 	}
 
 	if (gvnc->has_error || !gvnc->ops.local_cursor)
@@ -1972,7 +1932,7 @@ static void gvnc_xcursor(struct gvnc *gv
 		gvnc->has_error = TRUE;
 	}
 
-	free(pixbuf);
+	g_free(pixbuf);
 }
 
 
@@ -2110,7 +2070,7 @@ gboolean gvnc_server_message(struct gvnc
 			break;
 		}
 
-		data = malloc(n_text + 1);
+		data = g_new(char, n_text + 1);
 		if (data == NULL) {
 			GVNC_DEBUG("Closing the connection: gvnc_server_message() - cutText - !data \n");
 			gvnc->has_error = TRUE;
@@ -2121,7 +2081,7 @@ gboolean gvnc_server_message(struct gvnc
 		data[n_text] = 0;
 
 		gvnc_server_cut_text(gvnc, data, n_text);
-		free(data);
+		g_free(data);
 	}	break;
 	default:
 		GVNC_DEBUG("Received an unknown message: %u\n", msg);
@@ -2651,11 +2611,8 @@ static gboolean gvnc_perform_auth(struct
 
 struct gvnc *gvnc_new(const struct gvnc_ops *ops, gpointer ops_data)
 {
-	struct gvnc *gvnc = malloc(sizeof(*gvnc));
-	if (gvnc == NULL)
-		return NULL;
+	struct gvnc *gvnc = g_malloc0(sizeof(*gvnc));
 
-	memset(gvnc, 0, sizeof(*gvnc));
 	gvnc->fd = -1;
 
 	memcpy(&gvnc->ops, ops, sizeof(*ops));
@@ -2674,7 +2631,7 @@ void gvnc_free(struct gvnc *gvnc)
 	if (gvnc_is_open(gvnc))
 		gvnc_close(gvnc);
 
-	free(gvnc);
+	g_free(gvnc);
 	gvnc = NULL;
 }
 
@@ -2706,7 +2663,7 @@ void gvnc_close(struct gvnc *gvnc)
 	}
 
 	if (gvnc->name) {
-		free(gvnc->name);
+		g_free(gvnc->name);
 		gvnc->name = NULL;
 	}
 
@@ -2833,9 +2790,7 @@ gboolean gvnc_initialize(struct gvnc *gv
 	if (n_name > 4096)
 		goto fail;
 
-	gvnc->name = malloc(n_name + 1);
-	if (gvnc->name == NULL)
-		goto fail;
+	gvnc->name = g_new(char, n_name + 1);
 
 	gvnc_read(gvnc, gvnc->name, n_name);
 	gvnc->name[n_name] = 0;


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