NeoMutt  2022-04-29-215-gc12b98
Teaching an old dog new tricks
DOXYGEN
memory.c File Reference

Memory management wrappers. More...

#include "config.h"
#include <stdlib.h>
#include "memory.h"
#include "exit.h"
#include "logging.h"
#include "message.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. More...
 
void mutt_mem_free (void *ptr)
 Release memory allocated on the heap. More...
 
void * mutt_mem_malloc (size_t size)
 Allocate memory on the heap. More...
 
void mutt_mem_realloc (void *ptr, size_t size)
 Resize a block of memory on the heap. More...
 

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

51{
52 if ((nmemb == 0) || (size == 0))
53 return NULL;
54
55 void *p = calloc(nmemb, size);
56 if (!p)
57 {
58 mutt_error(_("Out of memory"));
59 mutt_exit(1);
60 }
61 return p;
62}
#define mutt_error(...)
Definition: logging.h:87
void mutt_exit(int code)
Leave NeoMutt NOW.
Definition: main.c:248
#define _(a)
Definition: message.h:28
+ 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 68 of file memory.c.

69{
70 if (!ptr)
71 return;
72 void **p = (void **) ptr;
73 if (*p)
74 {
75 free(*p);
76 *p = NULL;
77 }
78}
+ Here is the caller graph for this function:

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

91{
92 if (size == 0)
93 return NULL;
94
95 void *p = malloc(size);
96 if (!p)
97 {
98 mutt_error(_("Out of memory"));
99 mutt_exit(1);
100 }
101 return p;
102}
+ Here is the call 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 114 of file memory.c.

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