NeoMutt  2024-11-14-138-ge5ca67
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
memory.c File Reference

Memory management wrappers. More...

#include "config.h"
#include <errno.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include "memory.h"
#include "exit.h"
#include "logging2.h"
+ Include dependency graph for memory.c:

Go to the source code of this file.

Functions

static void * reallocarray (void *p, size_t n, size_t size)
 reallocarray(3) implementation
 
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

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.c.

Function Documentation

◆ reallocarray()

static void * reallocarray ( void *  p,
size_t  n,
size_t  size 
)
static

reallocarray(3) implementation

Parameters
pAddress of memory block to resize
nNumber of elements
sizeSize of element

See reallocarray(3) for the documentation of this function. We need to implement it because MacOS is cruft from another century and doesn't have this fundamental API. We'll remove it once all the systems we support have it.

Definition at line 53 of file memory.c.

54{
55 if ((n != 0) && (size > (SIZE_MAX / n)))
56 {
57 errno = ENOMEM;
58 return NULL;
59 }
60
61 return realloc(p, n * size);
62}
+ Here is the caller graph for this function:

◆ 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 76 of file memory.c.

77{
78 if ((nmemb == 0) || (size == 0))
79 return NULL;
80
81 void *p = calloc(nmemb, size);
82 if (!p)
83 {
84 mutt_error("%s", strerror(errno)); // LCOV_EXCL_LINE
85 mutt_exit(1); // LCOV_EXCL_LINE
86 }
87 return p;
88}
#define mutt_error(...)
Definition: logging2.h:92
void mutt_exit(int code)
Leave NeoMutt NOW.
Definition: main.c:266
+ 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 94 of file memory.c.

95{
96 if (!ptr)
97 return;
98 void **p = (void **) ptr;
99 free(*p);
100 *p = NULL;
101}

◆ 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 113 of file memory.c.

114{
115 return mutt_mem_mallocarray(size, 1);
116}
void * mutt_mem_mallocarray(size_t nmemb, size_t size)
Allocate memory on the heap (array version)
Definition: memory.c:129
+ 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 129 of file memory.c.

130{
131 void *p = NULL;
132 mutt_mem_reallocarray(&p, nmemb, size);
133 return p;
134}
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:162
+ 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 146 of file memory.c.

147{
148 mutt_mem_reallocarray(pptr, size, 1);
149}
+ 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 162 of file memory.c.

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