NeoMutt  2024-12-12-14-g7b49f7
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
pool.c
Go to the documentation of this file.
1
29#include "config.h"
30#include <stdio.h>
31#include "pool.h"
32#include "buffer.h"
33#include "logging2.h"
34#include "memory.h"
35#include "signal2.h"
36
38static size_t BufferPoolCount = 0;
40static size_t BufferPoolLen = 0;
42static const size_t BufferPoolIncrement = 20;
44static const size_t BufferPoolInitialBufferSize = 1024;
46static struct Buffer **BufferPool = NULL;
47
51static void pool_increase_size(void)
52{
55
58 {
59 struct Buffer *newbuf = buf_new(NULL);
61 BufferPool[BufferPoolCount++] = newbuf;
62 }
63}
64
69{
70 mutt_debug(LL_DEBUG1, "%zu of %zu returned to pool\n", BufferPoolCount, BufferPoolLen);
71
72 while (BufferPoolCount)
75 BufferPoolLen = 0;
76}
77
82struct Buffer *buf_pool_get(void)
83{
84 if (BufferPoolCount == 0)
88}
89
96void buf_pool_release(struct Buffer **ptr)
97{
98 if (!ptr || !*ptr)
99 return;
100
102 {
103 // LCOV_EXCL_START
104 mutt_debug(LL_DEBUG1, "Internal buffer pool error\n");
105 buf_free(ptr);
106 return;
107 // LCOV_EXCL_STOP
108 }
109
110 // Reset the size if it's too big or too small
111 struct Buffer *buf = *ptr;
112 if ((buf->dsize > (2 * BufferPoolInitialBufferSize)) ||
114 {
116 MUTT_MEM_REALLOC(&buf->data, buf->dsize, char);
117 }
118 buf_reset(buf);
120
121 *ptr = NULL;
122}
void buf_reset(struct Buffer *buf)
Reset an existing Buffer.
Definition: buffer.c:76
void buf_free(struct Buffer **ptr)
Deallocates a buffer.
Definition: buffer.c:319
struct Buffer * buf_new(const char *str)
Allocate a new Buffer.
Definition: buffer.c:304
void buf_alloc(struct Buffer *buf, size_t new_size)
Make sure a buffer can store at least new_size bytes.
Definition: buffer.c:337
General purpose object for storing and parsing strings.
#define mutt_debug(LEVEL,...)
Definition: logging2.h:89
Logging Dispatcher.
@ LL_DEBUG1
Log at debug level 1.
Definition: logging2.h:43
Memory management wrappers.
#define FREE(x)
Definition: memory.h:55
#define MUTT_MEM_REALLOC(pptr, n, type)
Definition: memory.h:43
static const size_t BufferPoolInitialBufferSize
Minimum size for a buffer.
Definition: pool.c:44
static void pool_increase_size(void)
Increase the size of the Buffer pool.
Definition: pool.c:51
struct Buffer * buf_pool_get(void)
Get a Buffer from the pool.
Definition: pool.c:82
void buf_pool_release(struct Buffer **ptr)
Return a Buffer to the pool.
Definition: pool.c:96
static const size_t BufferPoolIncrement
Amount to increase the size of the pool.
Definition: pool.c:42
void buf_pool_cleanup(void)
Release the Buffer pool.
Definition: pool.c:68
static size_t BufferPoolLen
Total size of the pool.
Definition: pool.c:40
static size_t BufferPoolCount
Number of buffers in the pool.
Definition: pool.c:38
static struct Buffer ** BufferPool
A pool of buffers.
Definition: pool.c:46
A global pool of Buffers.
Signal handling.
#define ASSERT(COND)
Definition: signal2.h:58
String manipulation buffer.
Definition: buffer.h:36
size_t dsize
Length of data.
Definition: buffer.h:39
char * data
Pointer to data.
Definition: buffer.h:37