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