NeoMutt  2024-11-14-34-g5aaf0d
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
memory.h File Reference

Memory management wrappers. More...

#include <stddef.h>
+ Include dependency graph for memory.h:
+ This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define MAX(a, b)   (((a) < (b)) ? (b) : (a))
 
#define MIN(a, b)   (((a) < (b)) ? (a) : (b))
 
#define CLAMP(val, lo, hi)   MIN(hi, MAX(lo, val))
 
#define ROUND_UP(NUM, STEP)   ((((NUM) + (STEP) -1) / (STEP)) * (STEP))
 
#define mutt_array_size(x)   (sizeof(x) / sizeof((x)[0]))
 
#define MUTT_MEM_CALLOC(n, type)   ((type *) mutt_mem_calloc(n, sizeof(type)))
 
#define MUTT_MEM_MALLOC(n, type)   ((type *) mutt_mem_mallocarray(n, sizeof(type)))
 
#define MUTT_MEM_REALLOC(pptr, n, type)
 
#define FREE(x)   mutt_mem_free(x)
 

Functions

void * mutt_mem_calloc (size_t nmemb, size_t size)
 Allocate zeroed memory on the heap.
 
void mutt_mem_free (void *ptr)
 Release memory allocated on the heap.
 
void * mutt_mem_malloc (size_t size)
 Allocate memory on the heap.
 
void * mutt_mem_mallocarray (size_t nmemb, size_t size)
 Allocate memory on the heap (array version)
 
void mutt_mem_realloc (void *pptr, size_t size)
 Resize a block of memory on the heap.
 
void mutt_mem_reallocarray (void *pptr, size_t nmemb, size_t size)
 Resize a block of memory on the heap (array version)
 

Detailed Description

Memory management wrappers.

Authors
  • Richard Russon

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/.

Definition in file memory.h.

Macro Definition Documentation

◆ MAX

#define MAX (   a,
 
)    (((a) < (b)) ? (b) : (a))

Definition at line 31 of file memory.h.

◆ MIN

#define MIN (   a,
 
)    (((a) < (b)) ? (a) : (b))

Definition at line 32 of file memory.h.

◆ CLAMP

#define CLAMP (   val,
  lo,
  hi 
)    MIN(hi, MAX(lo, val))

Definition at line 33 of file memory.h.

◆ ROUND_UP

#define ROUND_UP (   NUM,
  STEP 
)    ((((NUM) + (STEP) -1) / (STEP)) * (STEP))

Definition at line 36 of file memory.h.

◆ mutt_array_size

#define mutt_array_size (   x)    (sizeof(x) / sizeof((x)[0]))

Definition at line 38 of file memory.h.

◆ MUTT_MEM_CALLOC

#define MUTT_MEM_CALLOC (   n,
  type 
)    ((type *) mutt_mem_calloc(n, sizeof(type)))

Definition at line 40 of file memory.h.

◆ MUTT_MEM_MALLOC

#define MUTT_MEM_MALLOC (   n,
  type 
)    ((type *) mutt_mem_mallocarray(n, sizeof(type)))

Definition at line 41 of file memory.h.

◆ MUTT_MEM_REALLOC

#define MUTT_MEM_REALLOC (   pptr,
  n,
  type 
)
Value:
( \
_Generic(*(pptr), type *: mutt_mem_reallocarray(pptr, n, sizeof(type))) \
)
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

Definition at line 43 of file memory.h.

◆ FREE

#define FREE (   x)    mutt_mem_free(x)

Definition at line 55 of file memory.h.

Function Documentation

◆ mutt_mem_calloc()

void * mutt_mem_calloc ( size_t  nmemb,
size_t  size 
)

Allocate zeroed memory on the heap.

Parameters
nmembNumber of blocks
sizeSize of blocks
Return values
ptrMemory on the heap
Note
On error, this function will never return NULL. It will print an error and exit the program.

The caller should call mutt_mem_free() to release the memory

Definition at line 77 of file memory.c.

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}
#define mutt_error(...)
Definition: logging2.h:92
void mutt_exit(int code)
Leave NeoMutt NOW.
Definition: main.c:269
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_mem_free()

void mutt_mem_free ( void *  ptr)

Release memory allocated on the heap.

Parameters
ptrMemory to release

Definition at line 95 of file memory.c.

96{
97 if (!ptr)
98 return;
99 void **p = (void **) ptr;
100 free(*p);
101 *p = NULL;
102}

◆ mutt_mem_malloc()

void * mutt_mem_malloc ( size_t  size)

Allocate memory on the heap.

Parameters
sizeSize of block to allocate
Return values
ptrMemory on the heap
Note
On error, this function will never return NULL. It will print an error and exit the program.

The caller should call mutt_mem_free() to release the memory

Definition at line 114 of file memory.c.

115{
116 return mutt_mem_mallocarray(size, 1);
117}
void * mutt_mem_mallocarray(size_t nmemb, size_t size)
Allocate memory on the heap (array version)
Definition: memory.c:130
+ Here is the call graph for this function:

◆ mutt_mem_mallocarray()

void * mutt_mem_mallocarray ( size_t  nmemb,
size_t  size 
)

Allocate memory on the heap (array version)

Parameters
nmembNumber of blocks
sizeSize of blocks
Return values
ptrMemory on the heap
Note
On error, this function will never return NULL. It will print an error and exit the program.

The caller should call mutt_mem_free() to release the memory

Definition at line 130 of file memory.c.

131{
132 void *p = NULL;
133 mutt_mem_reallocarray(&p, nmemb, size);
134 return p;
135}
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
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_mem_realloc()

void mutt_mem_realloc ( void *  pptr,
size_t  size 
)

Resize a block of memory on the heap.

Parameters
pptrAddress of pointer to memory block to resize
sizeNew size
Note
On error, this function will never return NULL. It will print an error and exit the program.

If the new size is zero, the block will be freed.

Definition at line 147 of file memory.c.

148{
149 mutt_mem_reallocarray(pptr, size, 1);
150}
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ 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)

Parameters
pptrAddress of pointer to memory block to resize
nmembNumber of blocks
sizeSize of blocks
Note
On error, this function will never return NULL. It will print an error and exit the program.

If the new size is zero, the block will be freed.

Definition at line 163 of file memory.c.

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}
static void * reallocarray(void *p, size_t n, size_t size)
reallocarray(3) implementation
Definition: memory.c:54
+ Here is the call graph for this function:
+ Here is the caller graph for this function: