NeoMutt  2019-12-07
Teaching an old dog new tricks
DOXYGEN
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)   (((a) < (b)) ? (b) : (a))
 
#define MIN(a, b)   (((a) < (b)) ? (a) : (b))
 
#define mutt_array_size(x)   (sizeof(x) / sizeof((x)[0]))
 
#define FREE(x)   mutt_mem_free(x)
 

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

Macro Definition Documentation

◆ MAX

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

Definition at line 30 of file memory.h.

◆ MIN

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

Definition at line 31 of file memory.h.

◆ mutt_array_size

#define mutt_array_size (   x)    (sizeof(x) / sizeof((x)[0]))

Definition at line 33 of file memory.h.

◆ FREE

#define FREE (   x)    mutt_mem_free(x)

Definition at line 40 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
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 _(a)
Definition: message.h:28
void mutt_exit(int code)
Leave NeoMutt NOW.
Definition: main.c:210
#define mutt_error(...)
Definition: logging.h:84
+ 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
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 }
#define _(a)
Definition: message.h:28
void mutt_exit(int code)
Leave NeoMutt NOW.
Definition: main.c:210
#define mutt_error(...)
Definition: logging.h:84
+ 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
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 }
#define _(a)
Definition: message.h:28
void mutt_exit(int code)
Leave NeoMutt NOW.
Definition: main.c:210
#define mutt_error(...)
Definition: logging.h:84
+ Here is the call graph for this function: