[ekg2-commit] r3894 - in trunk: ekg plugins/gtk plugins/logs: trunk/ekg/configfile.c trunk/ekg/dynstuff.h trunk/ekg/ekg.c trunk/ekg/plugins.h trunk/ekg/stuff.c trunk/ekg/stuff.h trunk/plugins/gtk/completion.c trunk/plugins/logs/main.c

SVN commit svn w toxygen.net
Śro, 12 Mar 2008, 20:11:16 CET


Author: peres
Date: 2008-03-12 20:11:16 +0100 (Wed, 12 Mar 2008)
New Revision: 3894

Modified:
   trunk/ekg/configfile.c
   trunk/ekg/dynstuff.h
   trunk/ekg/ekg.c
   trunk/ekg/plugins.h
   trunk/ekg/stuff.c
   trunk/ekg/stuff.h
   trunk/plugins/gtk/completion.c
   trunk/plugins/logs/main.c
Log:

Buffers optimization + (gtk+conf) fix.



Modified: trunk/ekg/configfile.c
===================================================================
--- trunk/ekg/configfile.c	2008-03-11 16:10:29 UTC (rev 3893)
+++ trunk/ekg/configfile.c	2008-03-12 19:11:16 UTC (rev 3894)
@@ -728,7 +728,7 @@
 {
 	char name[32];
 	FILE *f;
-	list_t l;
+	struct buffer *b;
 
 	chdir(config_dir);
 
@@ -738,11 +738,8 @@
 
 	chmod(name, 0400);
 	
-	for (l = buffer_debug; l; l = l->next) {
-		struct buffer *b = l->data;
-
+	for (b = buffer_debug.data; b; b = b->next)
 		fprintf(f, "%s\n", b->line);
-	}
 	
 	fclose(f);
 }

Modified: trunk/ekg/dynstuff.h
===================================================================
--- trunk/ekg/dynstuff.h	2008-03-11 16:10:29 UTC (rev 3893)
+++ trunk/ekg/dynstuff.h	2008-03-12 19:11:16 UTC (rev 3894)
@@ -79,6 +79,7 @@
 #define LIST_ADD2(list, data)				list_add3((list_t *) list, (list_t) data)
 
 #define LIST_COUNT2(list)				list_count((list_t) list)
+#define LIST_GET_NTH2(list, id)				list_get_nth3((list_t) list, id)
 #define LIST_RESORT(list, comp)				list_resort(list, (void *) comp)
 #define LIST_RESORT2(list, comp)			list_resort3((list_t *) list, (void *) comp)
 

Modified: trunk/ekg/ekg.c
===================================================================
--- trunk/ekg/ekg.c	2008-03-11 16:10:29 UTC (rev 3893)
+++ trunk/ekg/ekg.c	2008-03-12 19:11:16 UTC (rev 3894)
@@ -250,7 +250,7 @@
                                         if (!config_speech_app)
                                                 buffer_free(&buffer_speech);
 
-                                        if (buffer_speech && !WEXITSTATUS(status)) {
+                                        if (buffer_speech.count && !WEXITSTATUS(status)) {
                                                 char *str = buffer_tail(&buffer_speech);
                                                 say_it(str);
                                                 xfree(str);
@@ -625,7 +625,7 @@
 		default:			theme_format = "debug";		break;
 	}
 
-	buffer_add(&buffer_debug, theme_format, tmp, DEBUG_MAX_LINES);
+	buffer_add(&buffer_debug, theme_format, tmp);
 
 	query_emit_id(NULL, UI_IS_INITIALIZED, &is_UI);
 
@@ -925,9 +925,10 @@
 	if (!have_plugin_of_class(PLUGIN_UI)) plugin_load(("readline"), -254, 1);
 #endif
 	if (!have_plugin_of_class(PLUGIN_UI)) fprintf(stderr, "No UI-PLUGIN!\n");
-	else for (l = buffer_debug; l; l = l->next) {
-		struct buffer *b = l->data;
-		print_window_w(window_debug, 0, b->target, b->line);
+	else {
+		struct buffer *b;
+		for (b = buffer_debug.data; b; b = b->next)
+			print_window_w(window_debug, 0, b->target, b->line);
 	}
 
         if (!have_plugin_of_class(PLUGIN_PROTOCOL)) {

Modified: trunk/ekg/plugins.h
===================================================================
--- trunk/ekg/plugins.h	2008-03-11 16:10:29 UTC (rev 3893)
+++ trunk/ekg/plugins.h	2008-03-12 19:11:16 UTC (rev 3894)
@@ -26,7 +26,7 @@
 #include "dynstuff.h"
 #include "sessions.h"
 
-#define EKG_ABI_VER 3888
+#define EKG_ABI_VER 3894
 
 #define EXPORT __attribute__ ((visibility("default")))
 

Modified: trunk/ekg/stuff.c
===================================================================
--- trunk/ekg/stuff.c	2008-03-11 16:10:29 UTC (rev 3893)
+++ trunk/ekg/stuff.c	2008-03-12 19:11:16 UTC (rev 3894)
@@ -93,8 +93,8 @@
 static list_t ekg_converters = NULL;	/**< list for internal use of ekg_convert_string_*() */
 #endif
 
-list_t buffer_debug;		/**< debug list_t struct buffer */
-list_t buffer_speech;		/**< speech list_t struct buffer */
+struct buffer_info buffer_debug = { NULL, 0, DEBUG_MAX_LINES };		/**< debug buffer */
+struct buffer_info buffer_speech = { NULL, 0, 50 };		/**< speech buffer */
 
 binding_added_t *bindings_added;
 int old_stderr;
@@ -455,46 +455,53 @@
 static LIST_FREE_ITEM(list_buffer_free, struct buffer *) {
 	xfree(data->line);
 	xfree(data->target);
-	xfree(data);
 }
 
+inline static void buffer_add_common(struct buffer_info *type, const char *target, const char *line, time_t ts) {
+	struct buffer *b;
+
+	if (type->max_lines) { /* XXX: move to idles? */
+		b = type->data;
+		int n		= type->count - type->max_lines + 1;
+		
+		if (n > 0) { /* list slice removal */
+			b = LIST_GET_NTH2(b, n);		/* last element to remove */
+			type->data	= b->next;
+			b->next		= NULL;			/* unlink elements to be removed */
+			LIST_DESTROY2(b, list_buffer_free);	/* and remove them */
+			type->count -= n;
+		}
+	}
+	
+	b		= xmalloc(sizeof(struct buffer));
+	b->ts		= time(NULL);
+	b->target	= xstrdup(target);
+	b->line 	= xstrdup(line);
+
+	LIST_ADD2(type->last ? &(type->last) : &(type->data), b);
+
+	type->last	= b;
+	type->count++;
+}
+
 /**
  * buffer_add()
  *
  * Add new line to given buffer_t, if max_lines > 0 than it maintain list that we can have max: @a max_lines items on it.
  *
- * @param type 		- pointer to buffer list_t 
+ * @param type 		- pointer to buffer beginning ptr
  * @param target	- name of target.. or just name of smth we want to keep in b->target
  * @param line		- line which we want to save.
- * @param max_lines	- max number of items in buffer
  *
  * @return 	0 - when line was successfully added to buffer, else -1	(when @a type was NULL)
  */
 
-int buffer_add(list_t *type, const char *target, const char *line, int max_lines) {
-	struct buffer *b;
-
+int buffer_add(struct buffer_info *type, const char *target, const char *line) {
 	if (!type)
 		return -1;
 
-	if (max_lines) {
-		list_t l = *type;
-		int bcount = list_count(*type);
-		
-		while (bcount >= max_lines && l) {
-			b = l->data;
-			l = l->next;
+	buffer_add_common(type, target, line, time(NULL));
 
-			LIST_REMOVE(type, b, list_buffer_free);
-			bcount--;
-		}
-	}
-	b = xmalloc(sizeof(struct buffer));
-	b->ts	= time(NULL);
-	b->target = xstrdup(target);
-	b->line = xstrdup(line);
-
-	list_add(type, b);		/* return x ? 0 : -1 -> list_add() can't fail if type is ok */
 	return 0;			/* so always return success here */
 }
 
@@ -503,16 +510,14 @@
  *
  * Add new line to given buffer_t, if max_lines > 0 than it maintain list that we can have max: @a max_lines items on it.
  *
- * @param type		- pointer to buffer list_t
+ * @param type		- pointer to buffer beginning ptr
  * @param target	- name of target, or just name of smth we want to keep in b->target
  * @param str		- string in format: [time_when_it_happen proper line... blah, blah] <i>time_when_it_happen</i> should be in digits.
- * @param max_lines	- max number of items in buffer
  *
  * @return	0 - when line was successfully added to buffer, else -1 (when @a type was NULL, or @a line was in wrong format)
  */
 
-int buffer_add_str(list_t *type, const char *target, const char *str, int max_lines) {
-	struct buffer *b;
+int buffer_add_str(struct buffer_info *type, const char *target, const char *str) {
 	char *sep;
 	time_t ts = 0;
 
@@ -530,25 +535,7 @@
 		return -1;
 	}
 
-	if (max_lines) {
-		list_t l = *type;
-		int bcount = list_count(*type);
-		
-		while (bcount >= max_lines && l) {
-			b = l->data;
-			l = l->next;
-
-			LIST_REMOVE(type, b, list_buffer_free);
-			bcount--;
-		}
-	}
-
-	b	= xmalloc(sizeof(struct buffer));
-	b->ts		= ts;
-	b->target	= xstrdup(target);
-	b->line		= xstrdup(sep+1);
-
-	list_add(type, b);		/* return x ? 0 : -1 -> list_add() can't fail if type is ok */
+	buffer_add_common(type, target, sep+1, ts);
 	return 0;			/* so always return success here */
 }
 
@@ -557,24 +544,24 @@
  *
  * Return oldest b->line, free b->target and remove whole buffer_t from list
  * 
- * @param type	- pointer to buffer list_t 
+ * @param type	- pointer to buffer beginning ptr
  *
  * @return First b->line on the list, or NULL, if no items on list.
  */
 
-char *buffer_tail(list_t *type) {
+char *buffer_tail(struct buffer_info *type) {
 	struct buffer *b;
 	char *str;
 
-	if (!type || !(*type))
+	if (!type || !type->data)
 		return NULL;
 
-	b = (*type)->data;
-
+	b = type->data;
 	str = b->line;			/* save b->line */
 
 	xfree(b->target);		/* free b->target */
-	list_remove(type, b, 1);	/* remove struct */
+	LIST_REMOVE2(type, b, NULL);	/* remove struct */
+	type->count--;
 
 	return str;			/* return saved b->line */
 }
@@ -585,16 +572,17 @@
  * Free memory after given buffer.<br>
  * After it set *type to NULL
  *
- * @param type - pointer to list_t
+ * @param type - pointer to buffer beginning ptr
  * 
  */
 
-void buffer_free(list_t *type) {
-	if (!type || !(*type))
+void buffer_free(struct buffer_info *type) {
+	if (!type || !type->data)
 		return;
 
-	LIST_DESTROY(*type, list_buffer_free);
-	*type = NULL;
+	LIST_DESTROY2(type->data, list_buffer_free);
+	type->data	= NULL;
+	type->count	= 0;
 }
 
 void changed_make_window(const char *var)
@@ -2219,7 +2207,7 @@
 		return -1;
 
 	if (speech_pid) {
-		buffer_add(&buffer_speech, NULL, str, 50);
+		buffer_add(&buffer_speech, NULL, str);
 		return -2;
 	}
 

Modified: trunk/ekg/stuff.h
===================================================================
--- trunk/ekg/stuff.h	2008-03-11 16:10:29 UTC (rev 3893)
+++ trunk/ekg/stuff.h	2008-03-12 19:11:16 UTC (rev 3894)
@@ -149,11 +149,20 @@
 } newconference_t;
 
 struct buffer {
+	struct buffer	*next;
+
 	time_t		ts;
 	char		*target;
 	char		*line;
 };
 
+struct buffer_info {
+	struct buffer	*data;
+	int		count;
+	int		max_lines;
+	struct buffer	*last;		/* fast access to last element, esp. for log_raw */
+};
+
 struct color_map {
 	int		color;
 	unsigned char	r, g, b;
@@ -167,8 +176,8 @@
 extern struct timer *timers;
 extern struct conference *conferences;
 extern newconference_t *newconferences;
-extern list_t buffer_debug;
-extern list_t buffer_speech;
+extern struct buffer_info buffer_debug;
+extern struct buffer_info buffer_speech;
 extern binding_added_t *bindings_added;
 
 extern time_t last_save;
@@ -276,10 +285,10 @@
 void binding_list(int quiet, const char *name, int all);
 void binding_free();
 
-int buffer_add(list_t *type, const char *target, const char *line, int max_lines);
-int buffer_add_str(list_t *type, const char *target, const char *str, int max_lines);
-char *buffer_tail(list_t *type);
-void buffer_free(list_t *type);
+int buffer_add(struct buffer_info *type, const char *target, const char *line);
+int buffer_add_str(struct buffer_info *type, const char *target, const char *str);
+char *buffer_tail(struct buffer_info *type);
+void buffer_free(struct buffer_info *type);
 
 void changed_auto_save(const char *var);
 void changed_display_blinking(const char *var);

Modified: trunk/plugins/gtk/completion.c
===================================================================
--- trunk/plugins/gtk/completion.c	2008-03-11 16:10:29 UTC (rev 3893)
+++ trunk/plugins/gtk/completion.c	2008-03-12 19:11:16 UTC (rev 3894)
@@ -208,11 +208,9 @@
 
 static void conference_generator(const char *text, int len)
 {
-        list_t l;
+        struct conference *c;
 
-        for (l = conferences; l; l = l->next) {
-                struct conference *c = l->data;
-
+        for (c = conferences; c; c = c->next) {
                 if (!xstrncasecmp(text, c->name, len))
                         array_add_check(&completions, xstrdup(c->name), 1);
         }

Modified: trunk/plugins/logs/main.c
===================================================================
--- trunk/plugins/logs/main.c	2008-03-11 16:10:29 UTC (rev 3893)
+++ trunk/plugins/logs/main.c	2008-03-12 19:11:16 UTC (rev 3894)
@@ -74,8 +74,7 @@
 	EKG2_WIN32_SHARED_LIB_HELPER
 #endif
 
-static list_t buffer_lograw;
-static list_t buffer_lograw_tail;	/* last item of buffer_lograw */
+static struct buffer_info buffer_lograw = { NULL, 0, 0 };
 
 static logs_log_t *log_curlog = NULL;
 
@@ -384,10 +383,8 @@
 
 static void logs_changed_raw(const char *var) {
 	/* if logs:log_raw == 0, clean LOGRAW buffer */
-	if (!config_logs_log_raw) {
+	if (!config_logs_log_raw)
 		buffer_free(&buffer_lograw);
-		buffer_lograw_tail = NULL;
-	}
 }
 
 static QUERY(logs_postinit) {
@@ -433,7 +430,7 @@
 	/* items == -1 display all */
 static int logs_buffer_raw_display(const char *file, int items) {
 	struct buffer **bs = NULL;
-	list_t l;
+	struct buffer *b;
 	char *beg = NULL, *profile = NULL, *sesja = NULL, *target = NULL;
 
 	int item = 0;
@@ -473,8 +470,7 @@
 
 	if (w) w->lock++;
 
-	for (l = buffer_lograw; l; l = l->next) {
-		struct buffer *b = l->data;
+	for (b = buffer_lograw.data; b; b = b->next) {
 		if (!xstrcmp(b->target, file)) {
 			/* we asume that (b->ts < (b->next)->ts, it's quite correct unless other plugin do this trick... */
 			if (items == -1) { 
@@ -505,23 +501,11 @@
 
 static int logs_buffer_raw_add(const char *file, const char *str) {
 	/* XXX, get global maxsize variable and if > than current ..... */
-	if (buffer_add(buffer_lograw_tail ? &buffer_lograw_tail : &buffer_lograw, file, str, 0) == 0) {
-		if (!buffer_lograw_tail)
-			buffer_lograw_tail = buffer_lograw;
-		else	buffer_lograw_tail = buffer_lograw_tail->next;
-		return 0;
-	}
-	return -1;
+	return buffer_add(&buffer_lograw, file, str);
 }
 
 static int logs_buffer_raw_add_line(const char *file, const char *line) {
-	if (buffer_add_str(buffer_lograw_tail ? &buffer_lograw_tail : &buffer_lograw, file, line, 0) == 0) {
-		if (!buffer_lograw_tail)
-			buffer_lograw_tail = buffer_lograw;
-		else	buffer_lograw_tail = buffer_lograw_tail->next;
-		return 0;
-	}
-	return -1;
+	return buffer_add_str(&buffer_lograw, file, line);
 }
 
 static QUERY(logs_handler_newwin) {
@@ -569,8 +553,6 @@
 
 	plugin_register(&logs_plugin, prio);
 	
-	buffer_lograw_tail = NULL;
-
 	query_connect_id(&logs_plugin, SET_VARS_DEFAULT,logs_setvar_default, NULL);
 	query_connect_id(&logs_plugin, PROTOCOL_MESSAGE_POST, logs_handler, NULL);
 	query_connect_id(&logs_plugin, IRC_PROTOCOL_MESSAGE, logs_handler_irc, NULL);
@@ -601,7 +583,7 @@
 
 static int logs_plugin_destroy() {
 	list_t old_logs = log_logs;
-	list_t l;
+	struct buffer *b;
 
 	for (; log_logs; log_logs = log_logs->next) {
 		logs_log_t *ll = log_logs->data;
@@ -634,9 +616,7 @@
 	}
 	list_destroy(old_logs, 1);	log_logs = NULL;
 
-	if (config_logs_log_raw) for (l = buffer_lograw; l;) {
-		struct buffer *b = l->data;
-
+	if (config_logs_log_raw) for (b = buffer_lograw.data; b;) {
 		static FILE *f = NULL;
 		static char *oldtarget = NULL;
 		/*
@@ -644,8 +624,6 @@
 		 *   ts: b->ts
 		 *  str: b->line
 		 */
-		l = l->next;
-
 		if (f && !xstrcmp(b->target, oldtarget)); 		/* if file is already opened and current target match old one, use it */
 		else {
 			if (f) fclose(f);				/* close file */
@@ -660,16 +638,16 @@
 		xfree(oldtarget);
 		oldtarget = b->target;
 
-		list_remove(&buffer_lograw, b, 1);
+		b = (struct buffer *) LIST_REMOVE2(&(buffer_lograw.data), b, NULL);
 
-		if (!l) {
+		if (!b) {
 			if (f) fclose(f);
 			xfree(oldtarget);
 		}
 	}
 	debug_error("[logs] 0x%x\n", buffer_lograw);
 	/* just in case */
-	buffer_free(&buffer_lograw);	buffer_lograw_tail = NULL;
+	buffer_free(&buffer_lograw);
 
 	plugin_unregister(&logs_plugin);
 	return 0;



Więcej informacji o liście dyskusyjnej ekg2-commit