NeoMutt  2025-12-11-1042-g26977c0
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
memory.h
Go to the documentation of this file.
1
23
24#ifndef MUTT_MUTT_MEMORY_H
25#define MUTT_MUTT_MEMORY_H
26
27#if defined __has_include
28# if __has_include(<stdcountof.h>)
29# include <stdcountof.h>
30# endif
31#endif
32#include <stddef.h>
33
34#undef MAX
35#undef MIN
36#undef CLAMP
38#define MAX(a, b) (((a) < (b)) ? (b) : (a))
40#define MIN(a, b) (((a) < (b)) ? (a) : (b))
42#define CLAMP(val, lo, hi) MIN(hi, MAX(lo, val))
43
44#undef ROUND_UP
46#define ROUND_UP(NUM, STEP) ((((NUM) + (STEP) -1) / (STEP)) * (STEP))
47
48#if !defined(countof)
49# define countof(x) (sizeof(x) / sizeof((x)[0]))
50#endif
51
52#if !defined(typeas)
63# define typeas(T) __typeof__(*(__typeof__(T) *){_Generic(0, T: NULL, default: NULL)})
64#endif
65
66#define RVALUE(lv) ((void)0, (lv))
67
68#define MUTT_MEM_CALLOC(n, T) \
69( \
70 RVALUE((typeas(T) *){mutt_mem_calloc(n, sizeof(T))}) \
71)
72
73#define MUTT_MEM_MALLOC(n, T) \
74( \
75 RVALUE((typeas(T) *){mutt_mem_mallocarray(n, sizeof(T))}) \
76)
77
78#define MUTT_MEM_REALLOC(pptr, n, T) \
79( \
80 _Generic(*(pptr), typeas(T) *: (void)0), \
81 mutt_mem_reallocarray(pptr, n, sizeof(T)) \
82)
83
84void *mutt_mem_calloc(size_t nmemb, size_t size);
85void mutt_mem_free(void *ptr);
86void *mutt_mem_malloc(size_t size);
87void *mutt_mem_mallocarray(size_t nmemb, size_t size);
88void mutt_mem_realloc(void *pptr, size_t size);
89void mutt_mem_reallocarray(void *pptr, size_t nmemb, size_t size);
90
92#define FREE(x) mutt_mem_free(x)
93
94#endif /* MUTT_MUTT_MEMORY_H */
void * mutt_mem_calloc(size_t nmemb, size_t size)
Allocate zeroed memory on the heap.
Definition memory.c:79
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