NeoMutt
2025-12-11-1039-g550ac6
Teaching an old dog new tricks
DOXYGEN
Toggle main menu visibility
Loading...
Searching...
No Matches
pool.c
Go to the documentation of this file.
1
22
28
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
38
static
size_t
BufferPoolCount
= 0;
40
static
size_t
BufferPoolLen
= 0;
42
static
const
size_t
BufferPoolIncrement
= 20;
44
static
const
size_t
BufferPoolInitialBufferSize
= 1024;
46
static
struct
Buffer
**
BufferPool
= NULL;
47
49
static
size_t
BufferPoolGetCount
= 0;
51
static
size_t
BufferPoolGivenOut
= 0;
53
static
size_t
BufferPoolMaxGivenOut
= 0;
54
58
static
void
pool_increase_size
(
void
)
59
{
60
BufferPoolLen
+=
BufferPoolIncrement
;
61
mutt_debug
(
LL_DEBUG1
,
"%zu\n"
,
BufferPoolLen
);
62
63
MUTT_MEM_REALLOC
(&
BufferPool
,
BufferPoolLen
,
struct
Buffer
*);
64
while
(
BufferPoolCount
<
BufferPoolIncrement
)
65
{
66
struct
Buffer
*newbuf =
MUTT_MEM_CALLOC
(1,
struct
Buffer
);
67
buf_alloc
(newbuf,
BufferPoolInitialBufferSize
);
68
BufferPool
[
BufferPoolCount
++] = newbuf;
69
}
70
}
71
75
void
buf_pool_cleanup
(
void
)
76
{
77
mutt_debug
(
LL_DEBUG3
,
"%zu of %zu returned to pool\n"
,
BufferPoolCount
,
BufferPoolLen
);
78
mutt_debug
(
LL_DEBUG3
,
"pool stats: %zu gets, %zu max buffers in use\n"
,
79
BufferPoolGetCount
,
BufferPoolMaxGivenOut
);
80
81
while
(
BufferPoolCount
)
82
buf_free
(&
BufferPool
[--
BufferPoolCount
]);
83
FREE
(&
BufferPool
);
84
BufferPoolLen
= 0;
85
}
86
91
struct
Buffer
*
buf_pool_get
(
void
)
92
{
93
if
(
BufferPoolCount
== 0)
94
pool_increase_size
();
95
ASSERT
(
BufferPoolCount
> 0);
96
97
BufferPoolGetCount
++;
98
BufferPoolGivenOut
++;
99
if
(
BufferPoolGivenOut
>
BufferPoolMaxGivenOut
)
100
BufferPoolMaxGivenOut
=
BufferPoolGivenOut
;
101
102
return
BufferPool
[--
BufferPoolCount
];
103
}
104
111
void
buf_pool_release
(
struct
Buffer
**ptr)
112
{
113
if
(!ptr || !*ptr)
114
return
;
115
116
if
(
BufferPoolGivenOut
> 0)
117
BufferPoolGivenOut
--;
118
119
if
(
BufferPoolCount
>=
BufferPoolLen
)
120
{
121
// LCOV_EXCL_START
122
mutt_debug
(
LL_DEBUG1
,
"Internal buffer pool error\n"
);
123
buf_free
(ptr);
124
return
;
125
// LCOV_EXCL_STOP
126
}
127
128
// Reset the size if it's too big or too small
129
struct
Buffer
*buf = *ptr;
130
if
((buf->
dsize
> (2 *
BufferPoolInitialBufferSize
)) ||
131
(buf->
dsize
<
BufferPoolInitialBufferSize
))
132
{
133
buf->
dsize
=
BufferPoolInitialBufferSize
;
134
MUTT_MEM_REALLOC
(&buf->
data
, buf->
dsize
,
char
);
135
}
136
buf_reset
(buf);
137
BufferPool
[
BufferPoolCount
++] = buf;
138
139
*ptr = NULL;
140
}
buf_reset
void buf_reset(struct Buffer *buf)
Reset an existing Buffer.
Definition
buffer.c:89
buf_free
void buf_free(struct Buffer **ptr)
Deallocates a buffer.
Definition
buffer.c:326
buf_alloc
void buf_alloc(struct Buffer *buf, size_t new_size)
Make sure a buffer can store at least new_size bytes.
Definition
buffer.c:342
buffer.h
General purpose object for storing and parsing strings.
mutt_debug
#define mutt_debug(LEVEL,...)
Definition
logging2.h:91
logging2.h
Logging Dispatcher.
LL_DEBUG3
@ LL_DEBUG3
Log at debug level 3.
Definition
logging2.h:47
LL_DEBUG1
@ LL_DEBUG1
Log at debug level 1.
Definition
logging2.h:45
memory.h
Memory management wrappers.
FREE
#define FREE(x)
Free memory and set the pointer to NULL.
Definition
memory.h:68
MUTT_MEM_CALLOC
#define MUTT_MEM_CALLOC(n, type)
Definition
memory.h:52
MUTT_MEM_REALLOC
#define MUTT_MEM_REALLOC(pptr, n, type)
Definition
memory.h:55
BufferPoolInitialBufferSize
static const size_t BufferPoolInitialBufferSize
Minimum size for a buffer.
Definition
pool.c:44
pool_increase_size
static void pool_increase_size(void)
Increase the size of the Buffer pool.
Definition
pool.c:58
BufferPoolMaxGivenOut
static size_t BufferPoolMaxGivenOut
Statistics: maximum number of buffers given out concurrently.
Definition
pool.c:53
buf_pool_get
struct Buffer * buf_pool_get(void)
Get a Buffer from the pool.
Definition
pool.c:91
buf_pool_release
void buf_pool_release(struct Buffer **ptr)
Return a Buffer to the pool.
Definition
pool.c:111
BufferPoolIncrement
static const size_t BufferPoolIncrement
Amount to increase the size of the pool.
Definition
pool.c:42
buf_pool_cleanup
void buf_pool_cleanup(void)
Release the Buffer pool.
Definition
pool.c:75
BufferPoolLen
static size_t BufferPoolLen
Total size of the pool.
Definition
pool.c:40
BufferPoolCount
static size_t BufferPoolCount
Number of buffers in the pool.
Definition
pool.c:38
BufferPool
static struct Buffer ** BufferPool
A pool of buffers.
Definition
pool.c:46
BufferPoolGetCount
static size_t BufferPoolGetCount
Statistics: total number of buf_pool_get() calls.
Definition
pool.c:49
BufferPoolGivenOut
static size_t BufferPoolGivenOut
Statistics: number of buffers currently given out.
Definition
pool.c:51
pool.h
A global pool of Buffers.
signal2.h
Signal handling.
ASSERT
#define ASSERT(COND)
Definition
signal2.h:59
Buffer
String manipulation buffer.
Definition
buffer.h:36
Buffer::dsize
size_t dsize
Length of data.
Definition
buffer.h:39
Buffer::data
char * data
Pointer to data.
Definition
buffer.h:37