changeset 13623:51d436a267ac

[gaim-migrate @ 16009] Use the GSlice allocator (for logs and log sets) when compiling with glib 2.10 or greater and eliminate some unnecessary memory zeroing. This cuts the log loading time in half for one of Luke's contacts. committer: Tailor Script <tailor@pidgin.im>
author Richard Laager <rlaager@wiktel.com>
date Tue, 11 Apr 2006 16:06:24 +0000
parents 64e664654177
children ff70d3009409
files src/log.c src/log.h
diffstat 2 files changed, 68 insertions(+), 23 deletions(-) [+]
line wrap: on
line diff
--- a/src/log.c	Tue Apr 11 16:02:57 2006 +0000
+++ b/src/log.c	Tue Apr 11 16:06:24 2006 +0000
@@ -76,16 +76,26 @@
 GaimLog *gaim_log_new(GaimLogType type, const char *name, GaimAccount *account,
                       GaimConversation *conv, time_t time, const struct tm *tm)
 {
-	GaimLog *log = g_new0(GaimLog, 1);
+	GaimLog *log;
+
+	/* IMPORTANT: Make sure to initialize all the members of GaimLog */
+	log = g_slice_new(GaimLog);
+
+	log->type = type;
 	log->name = g_strdup(gaim_normalize(account, name));
 	log->account = account;
 	log->conv = conv;
 	log->time = time;
-	log->type = type;
+	log->logger = gaim_log_logger_get();
 	log->logger_data = NULL;
-	if (tm != NULL)
+
+	if (tm == NULL)
+		log->tm = NULL;
+	else
 	{
-		log->tm = g_new0(struct tm, 1);
+		/* There's no need to zero this as we immediately do a direct copy. */
+		log->tm = g_slice_new(struct tm);
+
 		*(log->tm) = *tm;
 
 #ifdef HAVE_STRUCT_TM_TM_ZONE
@@ -101,7 +111,7 @@
 		}
 #endif
 	}
-	log->logger = gaim_log_logger_get();
+
 	if (log->logger && log->logger->create)
 		log->logger->create(log);
 	return log;
@@ -120,10 +130,10 @@
 		/* XXX: This is so wrong... */
 		g_free((char *)log->tm->tm_zone);
 #endif
-		g_free(log->tm);
+		g_slice_free(struct tm, log->tm);
 	}
 
-	g_free(log);
+	g_slice_free(GaimLog, log);
 }
 
 void gaim_log_write(GaimLog *log, GaimMessageFlags type,
@@ -504,7 +514,8 @@
 	g_free(set->name);
 	if (set->normalized_name != set->name)
 		g_free(set->normalized_name);
-	g_free(set);
+
+	g_slice_free(GaimLogSet, set);
 }
 
 GList *gaim_log_get_system_logs(GaimAccount *account)
@@ -652,7 +663,7 @@
 		g_free(dir);
 		g_free(filename);
 
-		log->logger_data = data = g_new0(GaimLogCommonLoggerData, 1);
+		log->logger_data = data = g_slice_new0(GaimLogCommonLoggerData);
 
 		data->file = g_fopen(path, "a");
 		if (data->file == NULL)
@@ -728,7 +739,8 @@
 #endif
 
 			log->logger = logger;
-			log->logger_data = data = g_new0(GaimLogCommonLoggerData, 1);
+			log->logger_data = data = g_slice_new0(GaimLogCommonLoggerData);
+
 			data->path = g_build_filename(path, filename, NULL);
 			list = g_list_prepend(list, log);
 		}
@@ -860,7 +872,10 @@
 			/* Don't worry about the cast, name will point to dynamically allocated memory shortly. */
 			while ((name = (gchar *)g_dir_read_name(username_dir)) != NULL) {
 				size_t len;
-				GaimLogSet *set = g_new0(GaimLogSet, 1);
+				GaimLogSet *set;
+
+				/* IMPORTANT: Always initialize all members of GaimLogSet */
+				set = g_slice_new(GaimLogSet);
 
 				/* Unescape the filename. */
 				name = g_strdup(gaim_unescape_filename(name));
@@ -868,12 +883,13 @@
 				/* Get the (possibly new) length of name. */
 				len = strlen(name);
 
+				set->type = GAIM_LOG_IM;
+				set->name = name;
 				set->account = account;
-				set->name = name;
+				/* set->buddy is always set below */
 				set->normalized_name = g_strdup(gaim_normalize(account, name));
 
 				/* Chat for .chat or .system at the end of the name to determine the type. */
-				set->type = GAIM_LOG_IM;
 				if (len > 7) {
 					gchar *tmp = &name[len - 7];
 					if (!strcmp(tmp, ".system")) {
@@ -892,6 +908,8 @@
 				/* Determine if this (account, name) combination exists as a buddy. */
 				if (account != NULL)
 					set->buddy = (gaim_find_buddy(account, name) != NULL);
+				else
+					set->buddy = FALSE;
 
 				log_add_log_set_to_hash(sets, set);
 			}
@@ -1117,7 +1135,8 @@
 			fclose(data->file);
 		}
 		g_free(data->path);
-		g_free(data);
+
+		g_slice_free(GaimLogCommonLoggerData, data);
 	}
 }
 
@@ -1248,7 +1267,8 @@
 			fclose(data->file);
 		if(data->path)
 			g_free(data->path);
-		g_free(data);
+
+		g_slice_free(GaimLogCommonLoggerData, data);
 	}
 }
 
@@ -1371,10 +1391,14 @@
 						log = gaim_log_new(GAIM_LOG_IM, sn, account, NULL, -1, NULL);
 						log->logger = old_logger;
 						log->time = (time_t)idx_time;
-						data = g_new0(struct old_logger_data, 1);
+
+						/* IMPORTANT: Always set all members of struct old_logger_data */
+						data = g_slice_new(struct old_logger_data);
+
+						data->pathref = gaim_stringref_ref(pathref);
 						data->offset = lastoff;
 						data->length = newlen;
-						data->pathref = gaim_stringref_ref(pathref);
+
 						log->logger_data = data;
 						list = g_list_prepend(list, log);
 					}
@@ -1450,10 +1474,14 @@
 					log = gaim_log_new(GAIM_LOG_IM, sn, account, NULL, -1, NULL);
 					log->logger = old_logger;
 					log->time = lasttime;
-					data = g_new0(struct old_logger_data, 1);
+
+					/* IMPORTANT: Always set all members of struct old_logger_data */
+					data = g_slice_new(struct old_logger_data);
+
+					data->pathref = gaim_stringref_ref(pathref);
 					data->offset = lastoff;
 					data->length = newlen;
-					data->pathref = gaim_stringref_ref(pathref);
+
 					log->logger_data = data;
 					list = g_list_prepend(list, log);
 
@@ -1505,10 +1533,14 @@
 			log = gaim_log_new(GAIM_LOG_IM, sn, account, NULL, -1, NULL);
 			log->logger = old_logger;
 			log->time = lasttime;
-			data = g_new0(struct old_logger_data, 1);
+
+			/* IMPORTANT: Always set all members of struct old_logger_data */
+			data = g_slice_new(struct old_logger_data);
+
+			data->pathref = gaim_stringref_ref(pathref);
 			data->offset = lastoff;
 			data->length = newlen;
-			data->pathref = gaim_stringref_ref(pathref);
+
 			log->logger_data = data;
 			list = g_list_prepend(list, log);
 
@@ -1626,7 +1658,8 @@
 			continue;
 		}
 
-		set = g_new0(GaimLogSet, 1);
+		/* IMPORTANT: Always set all members of GaimLogSet */
+		set = g_slice_new(GaimLogSet);
 
 		/* Chat for .chat at the end of the name to determine the type. */
 		*ext = '\0';
@@ -1665,6 +1698,12 @@
 			}
 		}
 
+		if (!found)
+		{
+			set->account = NULL;
+			set->buddy = FALSE;
+		}
+
 		cb(sets, set);
 	}
 	g_dir_close(log_dir);
@@ -1674,5 +1713,5 @@
 {
 	struct old_logger_data *data = log->logger_data;
 	gaim_stringref_unref(data->pathref);
-	g_free(data);
+	g_slice_free(struct old_logger_data, data);
 }
--- a/src/log.h	Tue Apr 11 16:02:57 2006 +0000
+++ b/src/log.h	Tue Apr 11 16:06:24 2006 +0000
@@ -126,6 +126,9 @@
 	                                           if struct tm has the BSD
 	                                           timezone fields, else @c NULL.
 	                                           Do NOT modify anything in this struct.*/
+
+	/* IMPORTANT: Some code in log.c allocates these without zeroing them.
+	 * IMPORTANT: Update that code if you add members here. */
 };
 
 /**
@@ -160,6 +163,9 @@
 	                                           @a name. It must be set, and
 	                                           may be set to the same pointer
 	                                           value as @a name. */
+
+	/* IMPORTANT: Some code in log.c allocates these without zeroing them.
+	 * IMPORTANT: Update that code if you add members here. */
 };
 
 #ifdef __cplusplus