NeoMutt  2024-11-14-34-g5aaf0d
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
memory.c
Go to the documentation of this file.
1
32#include "config.h"
33#include <errno.h>
34#include <stddef.h>
35#include <stdint.h>
36#include <stdlib.h>
37#include <string.h>
38#include "memory.h"
39#include "exit.h"
40#include "logging2.h"
41
53#if !defined(HAVE_REALLOCARRAY)
54static void *reallocarray(void *p, size_t n, size_t size)
55{
56 if ((n != 0) && (size > (SIZE_MAX / n)))
57 {
58 errno = ENOMEM;
59 return NULL;
60 }
61
62 return realloc(p, n * size);
63}
64#endif
65
77void *mutt_mem_calloc(size_t nmemb, size_t size)
78{
79 if ((nmemb == 0) || (size == 0))
80 return NULL;
81
82 void *p = calloc(nmemb, size);
83 if (!p)
84 {
85 mutt_error("%s", strerror(errno)); // LCOV_EXCL_LINE
86 mutt_exit(1); // LCOV_EXCL_LINE
87 }
88 return p;
89}
90
95void mutt_mem_free(void *ptr)
96{
97 if (!ptr)
98 return;
99 void **p = (void **) ptr;
100 free(*p);
101 *p = NULL;
102}
103
114void *mutt_mem_malloc(size_t size)
115{
116 return mutt_mem_mallocarray(size, 1);
117}
118
130void *mutt_mem_mallocarray(size_t nmemb, size_t size)
131{
132 void *p = NULL;
133 mutt_mem_reallocarray(&p, nmemb, size);
134 return p;
135}
136
147void mutt_mem_realloc(void *pptr, size_t size)
148{
149 mutt_mem_reallocarray(pptr, size, 1);
150}
151
163void mutt_mem_reallocarray(void *pptr, size_t nmemb, size_t size)
164{
165 if (!pptr)
166 return;
167
168 void **pp = (void **) pptr;
169
170 if ((nmemb == 0) || (size == 0))
171 {
172 free(*pp);
173 *pp = NULL;
174 return;
175 }
176
177 void *r = reallocarray(*pp, nmemb, size);
178 if (!r)
179 {
180 mutt_error("%s", strerror(errno)); // LCOV_EXCL_LINE
181 mutt_exit(1); // LCOV_EXCL_LINE
182 }
183
184 *pp = r;
185}
Leave the program NOW.
#define mutt_error(...)
Definition: logging2.h:92
Logging Dispatcher.
void mutt_exit(int code)
Leave NeoMutt NOW.
Definition: main.c:269
void * mutt_mem_calloc(size_t nmemb, size_t size)
Allocate zeroed memory on the heap.
Definition: memory.c:77
static void * reallocarray(void *p, size_t n, size_t size)
reallocarray(3) implementation
Definition: memory.c:54
void * mutt_mem_mallocarray(size_t nmemb, size_t size)
Allocate memory on the heap (array version)
Definition: memory.c:130
void mutt_mem_realloc(void *pptr, size_t size)
Resize a block of memory on the heap.
Definition: memory.c:147
void mutt_mem_free(void *ptr)
Release memory allocated on the heap.
Definition: memory.c:95
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:163
void * mutt_mem_malloc(size_t size)
Allocate memory on the heap.
Definition: memory.c:114
Memory management wrappers.