NeoMutt  2024-10-02-37-gfa9146
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 <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

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_realloc (void *ptr, size_t size)
 Resize a block of memory on the heap.
 

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

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

52{
53 if ((nmemb == 0) || (size == 0))
54 return NULL;
55
56 void *p = calloc(nmemb, size);
57 if (!p)
58 {
59 mutt_error("%s", strerror(errno)); // LCOV_EXCL_LINE
60 mutt_exit(1); // LCOV_EXCL_LINE
61 }
62 return p;
63}
#define mutt_error(...)
Definition: logging2.h:92
void mutt_exit(int code)
Leave NeoMutt NOW.
Definition: main.c:269
+ Here is the call 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 69 of file memory.c.

70{
71 if (!ptr)
72 return;
73 void **p = (void **) ptr;
74 if (*p)
75 {
76 free(*p);
77 *p = NULL;
78 }
79}

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

92{
93 if (size == 0)
94 return NULL;
95
96 void *p = malloc(size);
97 if (!p)
98 {
99 mutt_error("%s", strerror(errno)); // LCOV_EXCL_LINE
100 mutt_exit(1); // LCOV_EXCL_LINE
101 }
102 return p;
103}
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_mem_realloc()

void mutt_mem_realloc ( void *  ptr,
size_t  size 
)

Resize a block of memory on the heap.

Parameters
ptrMemory 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 115 of file memory.c.

116{
117 if (!ptr)
118 return;
119
120 void **p = (void **) ptr;
121
122 if (size == 0)
123 {
124 if (*p)
125 {
126 free(*p);
127 *p = NULL;
128 }
129 return;
130 }
131
132 void *r = realloc(*p, size);
133 if (!r)
134 {
135 mutt_error("%s", strerror(errno)); // LCOV_EXCL_LINE
136 mutt_exit(1); // LCOV_EXCL_LINE
137 }
138
139 *p = r;
140}
+ Here is the call graph for this function: