NeoMutt  2019-11-11
Teaching an old dog new tricks
DOXYGEN
pool.c File Reference

A global pool of Buffers. More...

#include "config.h"
#include <stdio.h>
#include "buffer.h"
#include "logging.h"
#include "memory.h"
+ Include dependency graph for pool.c:

Go to the source code of this file.

Functions

static struct Bufferbuffer_new (void)
 Allocate a new Buffer on the heap. More...
 
static void buffer_free (struct Buffer **p)
 Release a Buffer and its contents. More...
 
static void increase_buffer_pool (void)
 Increase the size of the Buffer pool. More...
 
void mutt_buffer_pool_free (void)
 Release the Buffer pool. More...
 
struct Buffermutt_buffer_pool_get (void)
 Get a Buffer from the pool. More...
 
void mutt_buffer_pool_release (struct Buffer **pbuf)
 Free a Buffer from the pool. More...
 

Variables

static size_t BufferPoolCount = 0
 
static size_t BufferPoolLen = 0
 
static size_t BufferPoolIncrement = 20
 
static size_t BufferPoolInitialBufferSize = 1024
 
static struct Buffer ** BufferPool = NULL
 

Detailed Description

A global pool of Buffers.

Authors
  • Richard Russon

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/.

Definition in file pool.c.

Function Documentation

◆ buffer_new()

static struct Buffer* buffer_new ( void  )
static

Allocate a new Buffer on the heap.

Return values
bufA newly allocated Buffer
Note
call buffer_free to release the memory

Definition at line 46 of file pool.c.

47 {
48  struct Buffer *buf = mutt_mem_malloc(sizeof(struct Buffer));
49  mutt_buffer_init(buf);
50  return buf;
51 }
String manipulation buffer.
Definition: buffer.h:33
void * mutt_mem_malloc(size_t size)
Allocate memory on the heap.
Definition: memory.c:90
struct Buffer * mutt_buffer_init(struct Buffer *buf)
Initialise a new Buffer.
Definition: buffer.c:46
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ buffer_free()

static void buffer_free ( struct Buffer **  p)
static

Release a Buffer and its contents.

Parameters
[out]pBuffer pointer to free and NULL

Definition at line 57 of file pool.c.

58 {
59  if (!p || !*p)
60  return;
61 
63  FREE(p);
64 }
void mutt_buffer_dealloc(struct Buffer *buf)
Release the memory allocated by a buffer.
Definition: buffer.c:294
#define FREE(x)
Definition: memory.h:40
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ increase_buffer_pool()

static void increase_buffer_pool ( void  )
static

Increase the size of the Buffer pool.

Definition at line 69 of file pool.c.

70 {
73 
74  mutt_mem_realloc(&BufferPool, BufferPoolLen * sizeof(struct Buffer *));
76  {
77  struct Buffer *newbuf = buffer_new();
79  BufferPool[BufferPoolCount++] = newbuf;
80  }
81 }
static size_t BufferPoolIncrement
Definition: pool.c:37
String manipulation buffer.
Definition: buffer.h:33
void mutt_mem_realloc(void *ptr, size_t size)
Resize a block of memory on the heap.
Definition: memory.c:114
static struct Buffer * buffer_new(void)
Allocate a new Buffer on the heap.
Definition: pool.c:46
static size_t BufferPoolLen
Definition: pool.c:36
Log at debug level 1.
Definition: logging.h:56
static size_t BufferPoolInitialBufferSize
Definition: pool.c:38
#define mutt_debug(LEVEL,...)
Definition: logging.h:81
static struct Buffer ** BufferPool
Definition: pool.c:39
static size_t BufferPoolCount
Definition: pool.c:35
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
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_buffer_pool_free()

void mutt_buffer_pool_free ( void  )

Release the Buffer pool.

Definition at line 86 of file pool.c.

87 {
88  mutt_debug(LL_DEBUG1, "%zu of %zu returned to pool\n", BufferPoolCount, BufferPoolLen);
89 
90  while (BufferPoolCount)
92  FREE(&BufferPool);
93  BufferPoolLen = 0;
94 }
static size_t BufferPoolLen
Definition: pool.c:36
Log at debug level 1.
Definition: logging.h:56
#define FREE(x)
Definition: memory.h:40
#define mutt_debug(LEVEL,...)
Definition: logging.h:81
static struct Buffer ** BufferPool
Definition: pool.c:39
static size_t BufferPoolCount
Definition: pool.c:35
static void buffer_free(struct Buffer **p)
Release a Buffer and its contents.
Definition: pool.c:57
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_buffer_pool_get()

struct Buffer* mutt_buffer_pool_get ( void  )

Get a Buffer from the pool.

Return values
ptrBuffer

Definition at line 100 of file pool.c.

101 {
102  if (BufferPoolCount == 0)
104  return BufferPool[--BufferPoolCount];
105 }
static void increase_buffer_pool(void)
Increase the size of the Buffer pool.
Definition: pool.c:69
static struct Buffer ** BufferPool
Definition: pool.c:39
static size_t BufferPoolCount
Definition: pool.c:35
+ Here is the call graph for this function:

◆ mutt_buffer_pool_release()

void mutt_buffer_pool_release ( struct Buffer **  pbuf)

Free a Buffer from the pool.

Parameters
[out]pbufBuffer to free

Definition at line 111 of file pool.c.

112 {
113  if (!pbuf || !*pbuf)
114  return;
115 
117  {
118  mutt_debug(LL_DEBUG1, "Internal buffer pool error\n");
119  buffer_free(pbuf);
120  return;
121  }
122 
123  struct Buffer *buf = *pbuf;
124  if (buf->dsize > 2 * BufferPoolInitialBufferSize)
125  {
127  mutt_mem_realloc(&buf->data, buf->dsize);
128  }
129  mutt_buffer_reset(buf);
130  BufferPool[BufferPoolCount++] = buf;
131 
132  *pbuf = NULL;
133 }
void mutt_buffer_reset(struct Buffer *buf)
Reset an existing Buffer.
Definition: buffer.c:79
String manipulation buffer.
Definition: buffer.h:33
size_t dsize
Length of data.
Definition: buffer.h:37
void mutt_mem_realloc(void *ptr, size_t size)
Resize a block of memory on the heap.
Definition: memory.c:114
char * data
Pointer to data.
Definition: buffer.h:35
static size_t BufferPoolLen
Definition: pool.c:36
Log at debug level 1.
Definition: logging.h:56
static size_t BufferPoolInitialBufferSize
Definition: pool.c:38
#define mutt_debug(LEVEL,...)
Definition: logging.h:81
static struct Buffer ** BufferPool
Definition: pool.c:39
static unsigned char * pbuf
Cache PGP data packet.
Definition: pgppacket.c:37
static size_t BufferPoolCount
Definition: pool.c:35
static void buffer_free(struct Buffer **p)
Release a Buffer and its contents.
Definition: pool.c:57
+ Here is the call graph for this function:

Variable Documentation

◆ BufferPoolCount

size_t BufferPoolCount = 0
static

Definition at line 35 of file pool.c.

◆ BufferPoolLen

size_t BufferPoolLen = 0
static

Definition at line 36 of file pool.c.

◆ BufferPoolIncrement

size_t BufferPoolIncrement = 20
static

Definition at line 37 of file pool.c.

◆ BufferPoolInitialBufferSize

size_t BufferPoolInitialBufferSize = 1024
static

Definition at line 38 of file pool.c.

◆ BufferPool

struct Buffer** BufferPool = NULL
static

Definition at line 39 of file pool.c.