]> granicus.if.org Git - mutt/commitdiff
Add buffer pool functions.
authorKevin McCarthy <kevin@8t8.us>
Sat, 6 Oct 2018 20:53:33 +0000 (13:53 -0700)
committerKevin McCarthy <kevin@8t8.us>
Sun, 7 Oct 2018 02:59:52 +0000 (19:59 -0700)
Also add a few helper functions: mutt_buffer_clear(),
mutt_buffer_strcpy(), and a macro mutt_b2s to grab the buffer data as
a const char *.

buffer.c
buffer.h
init.c
main.c

index ab02c15a6b6fd6080f82db5d9120e391f4fa8ad5..b860f0e0bf4ae8269387a44b8cd128e2d44e3545 100644 (file)
--- a/buffer.c
+++ b/buffer.c
 #include "mutt.h"
 #include "buffer.h"
 
+
+static size_t BufferPoolCount = 0;
+static size_t BufferPoolLen  = 0;
+static BUFFER **BufferPool = NULL;
+
+
 /* Creates and initializes a BUFFER */
 BUFFER *mutt_buffer_new (void)
 {
@@ -54,6 +60,13 @@ void mutt_buffer_free (BUFFER **p)
    FREE (p);           /* __FREE_CHECKED__ */
 }
 
+void mutt_buffer_clear (BUFFER *b)
+{
+  b->dptr = b->data;
+  if (b->dptr)
+    *(b->dptr) = '\0';
+}
+
 /* Creates and initializes a BUFFER by copying the seed string. */
 BUFFER *mutt_buffer_from (char *seed)
 {
@@ -140,3 +153,77 @@ void mutt_buffer_addch (BUFFER* buf, char c)
 {
   mutt_buffer_add (buf, &c, 1);
 }
+
+void mutt_buffer_strcpy (BUFFER *buf, const char *s)
+{
+  mutt_buffer_clear (buf);
+  mutt_buffer_addstr (buf, s);
+}
+
+
+static void increase_buffer_pool (void)
+{
+  BUFFER *newbuf;
+
+  BufferPoolLen += 5;
+  safe_realloc (&BufferPool, BufferPoolLen * sizeof (BUFFER *));
+  while (BufferPoolCount < 5)
+  {
+    newbuf = mutt_buffer_new ();
+    mutt_buffer_increase_size (newbuf, LONG_STRING);
+    mutt_buffer_clear (newbuf);
+    BufferPool[BufferPoolCount++] = newbuf;
+  }
+}
+
+void mutt_buffer_pool_init (void)
+{
+  increase_buffer_pool ();
+}
+
+void mutt_buffer_pool_free (void)
+{
+  if (BufferPoolCount != BufferPoolLen)
+  {
+    dprint (1, (debugfile, "Buffer pool leak: %zu/%zu\n",
+                BufferPoolCount, BufferPoolLen));
+  }
+  while (BufferPoolCount)
+    mutt_buffer_free (&BufferPool[--BufferPoolCount]);
+  FREE (&BufferPool);
+  BufferPoolLen = 0;
+}
+
+BUFFER *mutt_buffer_pool_get (void)
+{
+  if (!BufferPoolCount)
+    increase_buffer_pool ();
+  return BufferPool[--BufferPoolCount];
+}
+
+void mutt_buffer_pool_release (BUFFER **pbuf)
+{
+  BUFFER *buf;
+
+  if (!pbuf || !*pbuf)
+    return;
+
+  if (BufferPoolCount >= BufferPoolLen)
+  {
+    dprint (1, (debugfile, "Internal buffer pool error\n"));
+    mutt_buffer_free (pbuf);
+    return;
+  }
+
+  buf = *pbuf;
+  if (buf->dsize > LONG_STRING*2)
+  {
+    buf->dsize = LONG_STRING;
+    safe_realloc (&buf->data, buf->dsize);
+  }
+  mutt_buffer_clear (buf);
+  buf->destroy = 0;
+  BufferPool[BufferPoolCount++] = buf;
+
+  *pbuf = NULL;
+}
index 328884031023f1cc8e987b51ea5a5e6c123fd603..bde4d8dd1c338a397c4dc8adea0659e712c72a13 100644 (file)
--- a/buffer.h
+++ b/buffer.h
@@ -29,15 +29,27 @@ typedef struct
   int destroy; /* destroy `data' when done? */
 } BUFFER;
 
+/* Convert a buffer to a const char * "string" */
+#define mutt_b2s(b) (b->data ? (const char *)b->data : "")
+
 BUFFER *mutt_buffer_new (void);
 BUFFER *mutt_buffer_init (BUFFER *);
 void mutt_buffer_free (BUFFER **);
 BUFFER *mutt_buffer_from (char *);
+void mutt_buffer_clear (BUFFER *);
 
 void mutt_buffer_increase_size (BUFFER *, size_t);
 
 int mutt_buffer_printf (BUFFER*, const char*, ...);
 void mutt_buffer_addstr (BUFFER*, const char*);
 void mutt_buffer_addch (BUFFER*, char);
+void mutt_buffer_strcpy (BUFFER *, const char *);
+
+
+void mutt_buffer_pool_init (void);
+void mutt_buffer_pool_free (void);
+
+BUFFER *mutt_buffer_pool_get (void);
+void mutt_buffer_pool_release (BUFFER **);
 
 #endif
diff --git a/init.c b/init.c
index 60808003d4cdfc79bfefb596f08f08fefdb1eee6..a73e31f65fef60bffbaad39d81de449f809eac54 100644 (file)
--- a/init.c
+++ b/init.c
@@ -3387,6 +3387,7 @@ void mutt_init (int skip_sys_rc, LIST *commands)
   
   mutt_menu_init ();
   mutt_srandom ();
+  mutt_buffer_pool_init ();
 
   /* 
    * XXX - use something even more difficult to predict?
diff --git a/main.c b/main.c
index a84b8bcf22f558d39f34a151247be87afdf40146..10fc5256aad54e7de9202b4835f9e59e00115e57 100644 (file)
--- a/main.c
+++ b/main.c
@@ -1337,6 +1337,7 @@ cleanup_and_exit:
 #endif
   mutt_free_opts ();
   mutt_free_windows ();
+  mutt_buffer_pool_free ();
   if (!option (OPTNOCURSES))
     mutt_endwin (exit_endwin_msg);
   exit (exit_code);