NeoMutt  2023-05-17-33-gce4425
Teaching an old dog new tricks
DOXYGEN
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
37static size_t BufferPoolCount = 0;
39static size_t BufferPoolLen = 0;
41static const size_t BufferPoolIncrement = 20;
43static const size_t BufferPoolInitialBufferSize = 1024;
45static struct Buffer **BufferPool = NULL;
46
52static struct Buffer *pool_new(void)
53{
54 struct Buffer *buf = mutt_mem_malloc(sizeof(struct Buffer));
55 buf_init(buf);
56 return buf;
57}
58
63static void pool_free(struct Buffer **ptr)
64{
65 if (!ptr || !*ptr)
66 return; // LCOV_EXCL_LINE
67
68 buf_dealloc(*ptr);
69 FREE(ptr);
70}
71
75static void pool_increase_size(void)
76{
79
80 mutt_mem_realloc(&BufferPool, BufferPoolLen * sizeof(struct Buffer *));
82 {
83 struct Buffer *newbuf = pool_new();
85 BufferPool[BufferPoolCount++] = newbuf;
86 }
87}
88
92void buf_pool_free(void)
93{
94 mutt_debug(LL_DEBUG1, "%zu of %zu returned to pool\n", BufferPoolCount, BufferPoolLen);
95
96 while (BufferPoolCount)
99 BufferPoolLen = 0;
100}
101
106struct Buffer *buf_pool_get(void)
107{
108 if (BufferPoolCount == 0)
110 return BufferPool[--BufferPoolCount];
111}
112
119void buf_pool_release(struct Buffer **ptr)
120{
121 if (!ptr || !*ptr)
122 return;
123
125 {
126 // LCOV_EXCL_START
127 mutt_debug(LL_DEBUG1, "Internal buffer pool error\n");
128 pool_free(ptr);
129 return;
130 // LCOV_EXCL_STOP
131 }
132
133 // Reset the size if it's too big or too small
134 struct Buffer *buf = *ptr;
135 if ((buf->dsize > (2 * BufferPoolInitialBufferSize)) ||
137 {
139 mutt_mem_realloc(&buf->data, buf->dsize);
140 }
141 buf_reset(buf);
143
144 *ptr = NULL;
145}
void buf_dealloc(struct Buffer *buf)
Release the memory allocated by a buffer.
Definition: buffer.c:352
void buf_reset(struct Buffer *buf)
Reset an existing Buffer.
Definition: buffer.c:86
struct Buffer * buf_init(struct Buffer *buf)
Initialise a new Buffer.
Definition: buffer.c:53
void buf_alloc(struct Buffer *buf, size_t new_size)
Make sure a buffer can store at least new_size bytes.
Definition: buffer.c:316
General purpose object for storing and parsing strings.
#define mutt_debug(LEVEL,...)
Definition: logging2.h:84
Logging Dispatcher.
@ LL_DEBUG1
Log at debug level 1.
Definition: logging2.h:40
void * mutt_mem_malloc(size_t size)
Allocate memory on the heap.
Definition: memory.c:90
void mutt_mem_realloc(void *ptr, size_t size)
Resize a block of memory on the heap.
Definition: memory.c:114
Memory management wrappers.
#define FREE(x)
Definition: memory.h:43
static const size_t BufferPoolInitialBufferSize
Minimum size for a buffer.
Definition: pool.c:43
static void pool_increase_size(void)
Increase the size of the Buffer pool.
Definition: pool.c:75
void buf_pool_free(void)
Release the Buffer pool.
Definition: pool.c:92
struct Buffer * buf_pool_get(void)
Get a Buffer from the pool.
Definition: pool.c:106
void buf_pool_release(struct Buffer **ptr)
Return a Buffer to the pool.
Definition: pool.c:119
static const size_t BufferPoolIncrement
Amount to increase the size of the pool.
Definition: pool.c:41
static size_t BufferPoolLen
Total size of the pool.
Definition: pool.c:39
static struct Buffer * pool_new(void)
Allocate a new Buffer on the heap.
Definition: pool.c:52
static size_t BufferPoolCount
Number of buffers in the pool.
Definition: pool.c:37
static struct Buffer ** BufferPool
A pool of buffers.
Definition: pool.c:45
static void pool_free(struct Buffer **ptr)
Release a Buffer and its contents.
Definition: pool.c:63
A global pool of Buffers.
String manipulation buffer.
Definition: buffer.h:34
size_t dsize
Length of data.
Definition: buffer.h:37
char * data
Pointer to data.
Definition: buffer.h:35