Commit 619fe069 authored by Kevin J. McCarthy's avatar Kevin J. McCarthy

Add buffer pool functions.

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 *.
parent 563afcc1
......@@ -25,6 +25,12 @@
#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;
}
......@@ -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
......@@ -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?
......
......@@ -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);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment