NeoMutt
2025-12-11-1039-g550ac6
Teaching an old dog new tricks
DOXYGEN
Toggle main menu visibility
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)
56
static
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
79
void
*
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
97
void
mutt_mem_free
(
void
*ptr)
98
{
99
if
(!ptr)
100
return
;
101
void
**p = (
void
**) ptr;
102
free(*p);
103
*p = NULL;
104
}
105
116
void
*
mutt_mem_malloc
(
size_t
size)
117
{
118
return
mutt_mem_mallocarray
(size, 1);
119
}
120
132
void
*
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
149
void
mutt_mem_realloc
(
void
*pptr,
size_t
size)
150
{
151
mutt_mem_reallocarray
(pptr, size, 1);
152
}
153
165
void
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
}
mutt_exit
void mutt_exit(int code)
Leave NeoMutt NOW.
Definition
exit.c:41
exit.h
Leave the program NOW.
mutt_error
#define mutt_error(...)
Definition
logging2.h:94
logging2.h
Logging Dispatcher.
mutt_mem_calloc
void * mutt_mem_calloc(size_t nmemb, size_t size)
Allocate zeroed memory on the heap.
Definition
memory.c:79
reallocarray
static void * reallocarray(void *p, size_t n, size_t size)
reallocarray(3) implementation
Definition
memory.c:56
mutt_mem_mallocarray
void * mutt_mem_mallocarray(size_t nmemb, size_t size)
Allocate memory on the heap (array version).
Definition
memory.c:132
mutt_mem_realloc
void mutt_mem_realloc(void *pptr, size_t size)
Resize a block of memory on the heap.
Definition
memory.c:149
mutt_mem_free
void mutt_mem_free(void *ptr)
Release memory allocated on the heap.
Definition
memory.c:97
mutt_mem_reallocarray
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
mutt_mem_malloc
void * mutt_mem_malloc(size_t size)
Allocate memory on the heap.
Definition
memory.c:116
memory.h
Memory management wrappers.