NeoMutt  2020-06-26-89-g172cd3
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 "logging.h"
34 #include "memory.h"
35 
36 static size_t BufferPoolCount = 0;
37 static size_t BufferPoolLen = 0;
38 static size_t BufferPoolIncrement = 20;
39 static size_t BufferPoolInitialBufferSize = 1024;
40 static struct Buffer **BufferPool = NULL;
41 
47 static struct Buffer *buffer_new(void)
48 {
49  struct Buffer *buf = mutt_mem_malloc(sizeof(struct Buffer));
50  mutt_buffer_init(buf);
51  return buf;
52 }
53 
58 static void buffer_free(struct Buffer **p)
59 {
60  if (!p || !*p)
61  return;
62 
64  FREE(p);
65 }
66 
70 static void increase_buffer_pool(void)
71 {
74 
75  mutt_mem_realloc(&BufferPool, BufferPoolLen * sizeof(struct Buffer *));
77  {
78  struct Buffer *newbuf = buffer_new();
80  BufferPool[BufferPoolCount++] = newbuf;
81  }
82 }
83 
88 {
89  mutt_debug(LL_DEBUG1, "%zu of %zu returned to pool\n", BufferPoolCount, BufferPoolLen);
90 
91  while (BufferPoolCount)
92  buffer_free(&BufferPool[--BufferPoolCount]);
93  FREE(&BufferPool);
94  BufferPoolLen = 0;
95 }
96 
102 {
103  if (BufferPoolCount == 0)
105  return BufferPool[--BufferPoolCount];
106 }
107 
113 {
114  if (!pbuf || !*pbuf)
115  return;
116 
118  {
119  mutt_debug(LL_DEBUG1, "Internal buffer pool error\n");
120  buffer_free(pbuf);
121  return;
122  }
123 
124  struct Buffer *buf = *pbuf;
125  if (buf->dsize > 2 * BufferPoolInitialBufferSize)
126  {
128  mutt_mem_realloc(&buf->data, buf->dsize);
129  }
130  mutt_buffer_reset(buf);
131  BufferPool[BufferPoolCount++] = buf;
132 
133  *pbuf = NULL;
134 }
static size_t BufferPoolIncrement
Definition: pool.c:38
void mutt_buffer_reset(struct Buffer *buf)
Reset an existing Buffer.
Definition: buffer.c:79
struct Buffer * mutt_buffer_pool_get(void)
Get a Buffer from the pool.
Definition: pool.c:101
Memory management wrappers.
static void increase_buffer_pool(void)
Increase the size of the Buffer pool.
Definition: pool.c:70
A global pool of Buffers.
void mutt_buffer_pool_release(struct Buffer **pbuf)
Free a Buffer from the pool.
Definition: pool.c:112
String manipulation buffer.
Definition: buffer.h:33
Logging Dispatcher.
size_t dsize
Length of data.
Definition: buffer.h:37
void mutt_buffer_dealloc(struct Buffer *buf)
Release the memory allocated by a buffer.
Definition: buffer.c:294
void mutt_mem_realloc(void *ptr, size_t size)
Resize a block of memory on the heap.
Definition: memory.c:114
void * mutt_mem_malloc(size_t size)
Allocate memory on the heap.
Definition: memory.c:90
char * data
Pointer to data.
Definition: buffer.h:35
static struct Buffer * buffer_new(void)
Allocate a new Buffer on the heap.
Definition: pool.c:47
static size_t BufferPoolLen
Definition: pool.c:37
General purpose object for storing and parsing strings.
Log at debug level 1.
Definition: logging.h:40
static size_t BufferPoolInitialBufferSize
Definition: pool.c:39
#define FREE(x)
Definition: memory.h:40
#define mutt_debug(LEVEL,...)
Definition: logging.h:81
struct Buffer * mutt_buffer_init(struct Buffer *buf)
Initialise a new Buffer.
Definition: buffer.c:46
void mutt_buffer_pool_free(void)
Release the Buffer pool.
Definition: pool.c:87
static unsigned char * pbuf
Cache PGP data packet.
Definition: pgppacket.c:38
static size_t BufferPoolCount
Definition: pool.c:36
static void buffer_free(struct Buffer **p)
Release a Buffer and its contents.
Definition: pool.c:58
void mutt_buffer_alloc(struct Buffer *buf, size_t new_size)
Make sure a buffer can store at least new_size bytes.
Definition: buffer.c:265