#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)
{
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)
{
{
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;
+}
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