Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

PATCH: colour buffer allocation



I somehow hit a case where the debug message freeing the buffer for
colour code composition was triggering (it was a trashzle just as a
prompt was being redrawn, or something like that).  It looks like
allocation and free can happen in a nested fashion.  I think it's always
used immediately, so simply counting the nested allocations will work.
I think.

Index: Src/prompt.c
===================================================================
RCS file: /cvsroot/zsh/zsh/Src/prompt.c,v
retrieving revision 1.58
diff -u -r1.58 prompt.c
--- Src/prompt.c	12 May 2009 19:58:08 -0000	1.58
+++ Src/prompt.c	10 Jul 2009 21:27:27 -0000
@@ -1764,13 +1764,18 @@
 struct colour_sequences fg_bg_sequences[2];
 
 /*
- * We need a buffer for colour sequence compostion.  It may
+ * We need a buffer for colour sequence composition.  It may
  * vary depending on the sequences set.  However, it's inefficient
  * allocating it separately every time we send a colour sequence,
  * so do it once per refresh.
  */
 static char *colseq_buf;
 
+/*
+ * Count how often this has been allocated, for recursive usage.
+ */
+static int colseq_buf_allocs;
+
 /**/
 void
 set_default_colour_sequences(void)
@@ -1801,9 +1806,13 @@
 mod_export void
 allocate_colour_buffer(void)
 {
-    char **atrs = getaparam("zle_highlight");
+    char **atrs;
     int lenfg, lenbg, len;
 
+    if (colseq_buf_allocs++)
+	return;
+
+    atrs = getaparam("zle_highlight");
     if (atrs) {
 	for (; *atrs; atrs++) {
 	    if (strpfx("fg_start_code:", *atrs)) {
@@ -1846,6 +1855,9 @@
 mod_export void
 free_colour_buffer(void)
 {
+    if (--colseq_buf_allocs)
+	return;
+
     DPUTS(!colseq_buf, "Freeing colour sequence buffer without alloc");
     /* Free buffer for colour code composition */
     free(colseq_buf);

-- 
Peter Stephenson <p.w.stephenson@xxxxxxxxxxxx>
Web page now at http://homepage.ntlworld.com/p.w.stephenson/



Messages sorted by: Reverse Date, Date, Thread, Author