NeoMutt  2024-12-12-14-g7b49f7
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 <stdlib.h>
36#include <string.h>
37#include "memory.h"
38#include "exit.h"
39#include "logging2.h"
40
52#if !defined(HAVE_REALLOCARRAY)
53static void *reallocarray(void *p, size_t n, size_t size)
54{
55 if ((n != 0) && (size > (SIZE_MAX / n)))
56 {
57 errno = ENOMEM;
58 return NULL;
59 }
60
61 return realloc(p, n * size);
62}
63#endif
64
76void *mutt_mem_calloc(size_t nmemb, size_t size)
77{
78 if ((nmemb == 0) || (size == 0))
79 return NULL;
80
81 void *p = calloc(nmemb, size);
82 if (!p)
83 {
84 mutt_error("%s", strerror(errno)); // LCOV_EXCL_LINE
85 mutt_exit(1); // LCOV_EXCL_LINE
86 }
87 return p;
88}
89
94void mutt_mem_free(void *ptr)
95{
96 if (!ptr)
97 return;
98 void **p = (void **) ptr;
99 free(*p);
100 *p = NULL;
101}
102
113void *mutt_mem_malloc(size_t size)
114{
115 return mutt_mem_mallocarray(size, 1);
116}
117
129void *mutt_mem_mallocarray(size_t nmemb, size_t size)
130{
131 void *p = NULL;
132 mutt_mem_reallocarray(&p, nmemb, size);
133 return p;
134}
135
146void mutt_mem_realloc(void *pptr, size_t size)
147{
148 mutt_mem_reallocarray(pptr, size, 1);
149}
150
162void mutt_mem_reallocarray(void *pptr, size_t nmemb, size_t size)
163{
164 if (!pptr)
165 return;
166
167 void **pp = (void **) pptr;
168
169 if ((nmemb == 0) || (size == 0))
170 {
171 free(*pp);
172 *pp = NULL;
173 return;
174 }
175
176 void *r = reallocarray(*pp, nmemb, size);
177 if (!r)
178 {
179 mutt_error("%s", strerror(errno)); // LCOV_EXCL_LINE
180 mutt_exit(1); // LCOV_EXCL_LINE
181 }
182
183 *pp = r;
184}
Leave the program NOW.
#define mutt_error(...)
Definition: logging2.h:92
Logging Dispatcher.
void mutt_exit(int code)
Leave NeoMutt NOW.
Definition: main.c:266
void * mutt_mem_calloc(size_t nmemb, size_t size)
Allocate zeroed memory on the heap.
Definition: memory.c:76
static void * reallocarray(void *p, size_t n, size_t size)
reallocarray(3) implementation
Definition: memory.c:53
void * mutt_mem_mallocarray(size_t nmemb, size_t size)
Allocate memory on the heap (array version)
Definition: memory.c:129
void mutt_mem_realloc(void *pptr, size_t size)
Resize a block of memory on the heap.
Definition: memory.c:146
void mutt_mem_free(void *ptr)
Release memory allocated on the heap.
Definition: memory.c:94
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:162
void * mutt_mem_malloc(size_t size)
Allocate memory on the heap.
Definition: memory.c:113
Memory management wrappers.