NeoMutt  2024-10-02-37-gfa9146
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 <stdlib.h>
35#include <string.h>
36#include "memory.h"
37#include "exit.h"
38#include "logging2.h"
39
51void *mutt_mem_calloc(size_t nmemb, size_t size)
52{
53 if ((nmemb == 0) || (size == 0))
54 return NULL;
55
56 void *p = calloc(nmemb, size);
57 if (!p)
58 {
59 mutt_error("%s", strerror(errno)); // LCOV_EXCL_LINE
60 mutt_exit(1); // LCOV_EXCL_LINE
61 }
62 return p;
63}
64
69void mutt_mem_free(void *ptr)
70{
71 if (!ptr)
72 return;
73 void **p = (void **) ptr;
74 if (*p)
75 {
76 free(*p);
77 *p = NULL;
78 }
79}
80
91void *mutt_mem_malloc(size_t size)
92{
93 if (size == 0)
94 return NULL;
95
96 void *p = malloc(size);
97 if (!p)
98 {
99 mutt_error("%s", strerror(errno)); // LCOV_EXCL_LINE
100 mutt_exit(1); // LCOV_EXCL_LINE
101 }
102 return p;
103}
104
115void mutt_mem_realloc(void *ptr, size_t size)
116{
117 if (!ptr)
118 return;
119
120 void **p = (void **) ptr;
121
122 if (size == 0)
123 {
124 if (*p)
125 {
126 free(*p);
127 *p = NULL;
128 }
129 return;
130 }
131
132 void *r = realloc(*p, size);
133 if (!r)
134 {
135 mutt_error("%s", strerror(errno)); // LCOV_EXCL_LINE
136 mutt_exit(1); // LCOV_EXCL_LINE
137 }
138
139 *p = r;
140}
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:51
void mutt_mem_free(void *ptr)
Release memory allocated on the heap.
Definition: memory.c:69
void * mutt_mem_malloc(size_t size)
Allocate memory on the heap.
Definition: memory.c:91
void mutt_mem_realloc(void *ptr, size_t size)
Resize a block of memory on the heap.
Definition: memory.c:115
Memory management wrappers.