NeoMutt  2025-12-11-980-ge38c27
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
memory.h File Reference

Memory management wrappers. More...

#include <stddef.h>
+ Include dependency graph for memory.h:
+ This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define MAX(a, b)
 Return the maximum of two values.
 
#define MIN(a, b)
 Return the minimum of two values.
 
#define CLAMP(val, lo, hi)
 Clamp a value between a lower and upper bound.
 
#define ROUND_UP(NUM, STEP)
 Round up NUM to the nearest multiple of STEP.
 
#define countof(x)
 
#define MUTT_MEM_CALLOC(n, type)
 
#define MUTT_MEM_MALLOC(n, type)
 
#define MUTT_MEM_REALLOC(pptr, n, type)
 
#define FREE(x)
 Free memory and set the pointer to NULL.
 

Functions

void * mutt_mem_calloc (size_t nmemb, size_t size)
 Allocate zeroed memory on the heap.
 
void mutt_mem_free (void *ptr)
 Release memory allocated on the heap.
 
void * mutt_mem_malloc (size_t size)
 Allocate memory on the heap.
 
void * mutt_mem_mallocarray (size_t nmemb, size_t size)
 Allocate memory on the heap (array version)
 
void mutt_mem_realloc (void *pptr, size_t size)
 Resize a block of memory on the heap.
 
void mutt_mem_reallocarray (void *pptr, size_t nmemb, size_t size)
 Resize a block of memory on the heap (array version)
 

Detailed Description

Memory management wrappers.

Authors
  • Richard Russon
  • Alejandro Colomar

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 memory.h.

Macro Definition Documentation

◆ MAX

#define MAX ( a,
b )
Value:
(((a) < (b)) ? (b) : (a))

Return the maximum of two values.

Definition at line 38 of file memory.h.

◆ MIN

#define MIN ( a,
b )
Value:
(((a) < (b)) ? (a) : (b))

Return the minimum of two values.

Definition at line 40 of file memory.h.

◆ CLAMP

#define CLAMP ( val,
lo,
hi )
Value:
MIN(hi, MAX(lo, val))
#define MIN(a, b)
Return the minimum of two values.
Definition memory.h:40
#define MAX(a, b)
Return the maximum of two values.
Definition memory.h:38

Clamp a value between a lower and upper bound.

Definition at line 42 of file memory.h.

◆ ROUND_UP

#define ROUND_UP ( NUM,
STEP )
Value:
((((NUM) + (STEP) -1) / (STEP)) * (STEP))

Round up NUM to the nearest multiple of STEP.

Definition at line 46 of file memory.h.

◆ countof

#define countof ( x)
Value:
(sizeof(x) / sizeof((x)[0]))

Definition at line 49 of file memory.h.

◆ MUTT_MEM_CALLOC

#define MUTT_MEM_CALLOC ( n,
type )
Value:
((type *) mutt_mem_calloc(n, sizeof(type)))
void * mutt_mem_calloc(size_t nmemb, size_t size)
Allocate zeroed memory on the heap.
Definition memory.c:79

Definition at line 52 of file memory.h.

◆ MUTT_MEM_MALLOC

#define MUTT_MEM_MALLOC ( n,
type )
Value:
((type *) mutt_mem_mallocarray(n, sizeof(type)))
void * mutt_mem_mallocarray(size_t nmemb, size_t size)
Allocate memory on the heap (array version)
Definition memory.c:132

Definition at line 53 of file memory.h.

◆ MUTT_MEM_REALLOC

#define MUTT_MEM_REALLOC ( pptr,
n,
type )
Value:
( \
_Generic(*(pptr), type *: mutt_mem_reallocarray(pptr, n, sizeof(type))) \
)
void mutt_mem_reallocarray(void *pptr, size_t nmemb, size_t size)
Resize a block of memory on the heap (array version)
Definition memory.c:165

Definition at line 55 of file memory.h.

55#define MUTT_MEM_REALLOC(pptr, n, type) \
56( \
57 _Generic(*(pptr), type *: mutt_mem_reallocarray(pptr, n, sizeof(type))) \
58)

◆ FREE

#define FREE ( x)
Value:
void mutt_mem_free(void *ptr)
Release memory allocated on the heap.
Definition memory.c:97

Free memory and set the pointer to NULL.

Definition at line 68 of file memory.h.

Function Documentation

◆ mutt_mem_calloc()

void * mutt_mem_calloc ( size_t nmemb,
size_t size )

Allocate zeroed memory on the heap.

Parameters
nmembNumber of blocks
sizeSize of blocks
Return values
ptrMemory on the heap
Note
On error, this function will never return NULL. It will print an error and exit the program.

The caller should call mutt_mem_free() to release the memory

Definition at line 79 of file memory.c.

80{
81 if ((nmemb == 0) || (size == 0))
82 return NULL;
83
84 void *p = calloc(nmemb, size);
85 if (!p)
86 {
87 mutt_error("%s", strerror(errno)); // LCOV_EXCL_LINE
88 mutt_exit(1); // LCOV_EXCL_LINE
89 }
90 return p;
91}
void mutt_exit(int code)
Leave NeoMutt NOW.
Definition exit.c:41
#define mutt_error(...)
Definition logging2.h:94
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_mem_free()

void mutt_mem_free ( void * ptr)

Release memory allocated on the heap.

Parameters
ptrMemory to release

Definition at line 97 of file memory.c.

98{
99 if (!ptr)
100 return;
101 void **p = (void **) ptr;
102 free(*p);
103 *p = NULL;
104}

◆ mutt_mem_malloc()

void * mutt_mem_malloc ( size_t size)

Allocate memory on the heap.

Parameters
sizeSize of block to allocate
Return values
ptrMemory on the heap
Note
On error, this function will never return NULL. It will print an error and exit the program.

The caller should call mutt_mem_free() to release the memory

Definition at line 116 of file memory.c.

117{
118 return mutt_mem_mallocarray(size, 1);
119}
+ Here is the call graph for this function:

◆ mutt_mem_mallocarray()

void * mutt_mem_mallocarray ( size_t nmemb,
size_t size )

Allocate memory on the heap (array version)

Parameters
nmembNumber of blocks
sizeSize of blocks
Return values
ptrMemory on the heap
Note
On error, this function will never return NULL. It will print an error and exit the program.

The caller should call mutt_mem_free() to release the memory

Definition at line 132 of file memory.c.

133{
134 void *p = NULL;
135 mutt_mem_reallocarray(&p, nmemb, size);
136 return p;
137}
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_mem_realloc()

void mutt_mem_realloc ( void * pptr,
size_t size )

Resize a block of memory on the heap.

Parameters
pptrAddress of pointer to memory block to resize
sizeNew size
Note
On error, this function will never return NULL. It will print an error and exit the program.

If the new size is zero, the block will be freed.

Definition at line 149 of file memory.c.

150{
151 mutt_mem_reallocarray(pptr, size, 1);
152}
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_mem_reallocarray()

void mutt_mem_reallocarray ( void * pptr,
size_t nmemb,
size_t size )

Resize a block of memory on the heap (array version)

Parameters
pptrAddress of pointer to memory block to resize
nmembNumber of blocks
sizeSize of blocks
Note
On error, this function will never return NULL. It will print an error and exit the program.

If the new size is zero, the block will be freed.

Definition at line 165 of file memory.c.

166{
167 if (!pptr)
168 return;
169
170 void **pp = (void **) pptr;
171
172 if ((nmemb == 0) || (size == 0))
173 {
174 free(*pp);
175 *pp = NULL;
176 return;
177 }
178
179 void *r = reallocarray(*pp, nmemb, size);
180 if (!r)
181 {
182 mutt_error("%s", strerror(errno)); // LCOV_EXCL_LINE
183 mutt_exit(1); // LCOV_EXCL_LINE
184 }
185
186 *pp = r;
187}
static void * reallocarray(void *p, size_t n, size_t size)
reallocarray(3) implementation
Definition memory.c:56
+ Here is the call graph for this function:
+ Here is the caller graph for this function: