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



Hi, folks.

As discussed previously, here is a patch to use GLib functions always as
possible.

May I commit?
-- 
Jonh Wendell
www.bani.com.br

diff -r b7bbfb2b5a47 src/gvnc.c
--- a/src/gvnc.c	Sun Apr 06 17:44:38 2008 -0500
+++ b/src/gvnc.c	Thu Apr 10 19:33:45 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,7 +1150,7 @@ static void gvnc_raw_update(struct gvnc 
 		return;
 	}
 
-	dst = malloc(width * (gvnc->fmt.bits_per_pixel / 8));
+	dst = g_try_malloc(width * (gvnc->fmt.bits_per_pixel / 8));
 	if (dst == NULL) {
 		GVNC_DEBUG("Closing the connection: gvnc_raw_update\n");
 		gvnc->has_error = TRUE;
@@ -1163,7 +1162,7 @@ static void gvnc_raw_update(struct gvnc 
 		gvnc_blt(gvnc, dst, 0, x, y + i, width, 1);
 	}
 	
-	free(dst);
+	g_free(dst);
 }
 
 static void gvnc_copyrect_update(struct gvnc *gvnc,
@@ -1449,7 +1448,7 @@ static void gvnc_zrle_update(struct gvnc
 	uint8_t *zlib_data;
 
 	length = gvnc_read_u32(gvnc);
-	zlib_data = malloc(length);
+	zlib_data = g_try_malloc(length);
 	if (zlib_data == NULL) {
 		GVNC_DEBUG("Closing the connection: gvnc_zrle_update\n");
 		gvnc->has_error = TRUE;
@@ -1480,7 +1479,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,23 +1875,23 @@ static void gvnc_rich_cursor(struct gvnc
 		imagelen = width * height * (gvnc->fmt.bits_per_pixel / 8);
 		masklen = ((width + 7)/8) * height;
 
-		image = malloc(imagelen);
+		image = g_try_malloc(imagelen);
 		if (!image) {
 			GVNC_DEBUG("Closing the connection: gvnc_rich_cursor() - !image \n");
 			gvnc->has_error = TRUE;
 			return;
 		}
-		mask = malloc(masklen);
+		mask = g_try_malloc(masklen);
 		if (!mask) {
-			free(image);
+			g_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 */
+		pixbuf = g_try_malloc(width * height * 4); /* RGB-A 8bit */
 		if (!pixbuf) {
-			free(mask);
-			free(image);
+			g_free(mask);
+			g_free(image);
 			GVNC_DEBUG("Closing the connection: gvnc_rich_cursor() - !pixbuf \n");
 			gvnc->has_error = TRUE;
 			return;
@@ -1904,8 +1903,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,7 +1914,7 @@ static void gvnc_rich_cursor(struct gvnc
 		gvnc->has_error = TRUE;
 	}
 
-	free(pixbuf);
+	g_free(pixbuf);
 }
 
 
@@ -1936,18 +1935,18 @@ 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))) {
+		if (!(data = g_try_malloc(rowlen*height))) {
 			GVNC_DEBUG("Closing the connection: gvnc_xcursor() - !data \n");
 			gvnc->has_error = TRUE;
 			return;
 		}
-		if (!(mask = malloc(rowlen*height))) {
-			free(data);
+		if (!(mask = g_try_malloc(rowlen*height))) {
+			g_free(data);
 			GVNC_DEBUG("Closing the connection: gvnc_xcursor() - !mask \n");
 			gvnc->has_error = TRUE;
 			return;
 		}
-		pixbuf = malloc(width * height * 4); /* RGB-A 8bit */
+		pixbuf = g_try_malloc(width * height * 4); /* RGB-A 8bit */
 		gvnc_read(gvnc, data, rowlen*height);
 		gvnc_read(gvnc, mask, rowlen*height);
 		datap = data;
@@ -1961,8 +1960,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 +1971,7 @@ static void gvnc_xcursor(struct gvnc *gv
 		gvnc->has_error = TRUE;
 	}
 
-	free(pixbuf);
+	g_free(pixbuf);
 }
 
 
@@ -2110,7 +2109,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 +2120,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 +2650,10 @@ static gboolean gvnc_perform_auth(struct
 
 struct gvnc *gvnc_new(const struct gvnc_ops *ops, gpointer ops_data)
 {
-	struct gvnc *gvnc = malloc(sizeof(*gvnc));
+	struct gvnc *gvnc = g_try_malloc0(sizeof(*gvnc));
 	if (gvnc == NULL)
 		return NULL;
 
-	memset(gvnc, 0, sizeof(*gvnc));
 	gvnc->fd = -1;
 
 	memcpy(&gvnc->ops, ops, sizeof(*ops));
@@ -2674,7 +2672,7 @@ void gvnc_free(struct gvnc *gvnc)
 	if (gvnc_is_open(gvnc))
 		gvnc_close(gvnc);
 
-	free(gvnc);
+	g_free(gvnc);
 	gvnc = NULL;
 }
 
@@ -2706,7 +2704,7 @@ void gvnc_close(struct gvnc *gvnc)
 	}
 
 	if (gvnc->name) {
-		free(gvnc->name);
+		g_free(gvnc->name);
 		gvnc->name = NULL;
 	}
 
@@ -2833,7 +2831,7 @@ gboolean gvnc_initialize(struct gvnc *gv
 	if (n_name > 4096)
 		goto fail;
 
-	gvnc->name = malloc(n_name + 1);
+	gvnc->name = g_try_new(char, n_name + 1);
 	if (gvnc->name == NULL)
 		goto fail;
 


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