NeoMutt  2022-04-29-215-gc12b98
Teaching an old dog new tricks
DOXYGEN
memory.c
Go to the documentation of this file.
1
32#include "config.h"
33#include <stdlib.h>
34#include "memory.h"
35#include "exit.h"
36#include "logging.h"
37#include "message.h"
38
50void *mutt_mem_calloc(size_t nmemb, size_t size)
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}
63
68void mutt_mem_free(void *ptr)
69{
70 if (!ptr)
71 return;
72 void **p = (void **) ptr;
73 if (*p)
74 {
75 free(*p);
76 *p = NULL;
77 }
78}
79
90void *mutt_mem_malloc(size_t size)
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}
103
114void mutt_mem_realloc(void *ptr, size_t size)
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}
Leave the program NOW.
#define mutt_error(...)
Definition: logging.h:87
Logging Dispatcher.
void mutt_exit(int code)
Leave NeoMutt NOW.
Definition: main.c:248
void * mutt_mem_calloc(size_t nmemb, size_t size)
Allocate zeroed memory on the heap.
Definition: memory.c:50
void mutt_mem_free(void *ptr)
Release memory allocated on the heap.
Definition: memory.c:68
void * mutt_mem_malloc(size_t size)
Allocate memory on the heap.
Definition: memory.c:90
void mutt_mem_realloc(void *ptr, size_t size)
Resize a block of memory on the heap.
Definition: memory.c:114
Memory management wrappers.
Message logging.
#define _(a)
Definition: message.h:28