NeoMutt  2024-10-02-37-gfa9146
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
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
50static void pool_increase_size(void)
51{
54
55 mutt_mem_realloc(&BufferPool, BufferPoolLen * sizeof(struct Buffer *));
57 {
58 struct Buffer *newbuf = buf_new(NULL);
60 BufferPool[BufferPoolCount++] = newbuf;
61 }
62}
63
68{
69 mutt_debug(LL_DEBUG1, "%zu of %zu returned to pool\n", BufferPoolCount, BufferPoolLen);
70
71 while (BufferPoolCount)
74 BufferPoolLen = 0;
75}
76
81struct Buffer *buf_pool_get(void)
82{
83 if (BufferPoolCount == 0)
86}
87
94void buf_pool_release(struct Buffer **ptr)
95{
96 if (!ptr || !*ptr)
97 return;
98
100 {
101 // LCOV_EXCL_START
102 mutt_debug(LL_DEBUG1, "Internal buffer pool error\n");
103 buf_free(ptr);
104 return;
105 // LCOV_EXCL_STOP
106 }
107
108 // Reset the size if it's too big or too small
109 struct Buffer *buf = *ptr;
110 if ((buf->dsize > (2 * BufferPoolInitialBufferSize)) ||
112 {
114 mutt_mem_realloc(&buf->data, buf->dsize);
115 }
116 buf_reset(buf);
118
119 *ptr = NULL;
120}
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
void mutt_mem_realloc(void *ptr, size_t size)
Resize a block of memory on the heap.
Definition: memory.c:115
Memory management wrappers.
#define FREE(x)
Definition: memory.h:45
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:50
struct Buffer * buf_pool_get(void)
Get a Buffer from the pool.
Definition: pool.c:81
void buf_pool_release(struct Buffer **ptr)
Return a Buffer to the pool.
Definition: pool.c:94
static const size_t BufferPoolIncrement
Amount to increase the size of the pool.
Definition: pool.c:41
void buf_pool_cleanup(void)
Release the Buffer pool.
Definition: pool.c:67
static size_t BufferPoolLen
Total size of the pool.
Definition: pool.c:39
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
A global pool of Buffers.
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