NeoMutt  2025-09-05-2-g4bf191
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
memory.h
Go to the documentation of this file.
1
23#ifndef MUTT_MUTT_MEMORY_H
24#define MUTT_MUTT_MEMORY_H
25
26#if defined __has_include
27# if __has_include(<stdcountof.h>)
28# include <stdcountof.h>
29# endif
30#endif
31#include <stddef.h>
32
33#undef MAX
34#undef MIN
35#undef CLAMP
36#define MAX(a, b) (((a) < (b)) ? (b) : (a))
37#define MIN(a, b) (((a) < (b)) ? (a) : (b))
38#define CLAMP(val, lo, hi) MIN(hi, MAX(lo, val))
39
40#undef ROUND_UP
41#define ROUND_UP(NUM, STEP) ((((NUM) + (STEP) -1) / (STEP)) * (STEP))
42
43#if !defined(countof)
44# define countof(x) (sizeof(x) / sizeof((x)[0]))
45#endif
46
47#define MUTT_MEM_CALLOC(n, type) ((type *) mutt_mem_calloc(n, sizeof(type)))
48#define MUTT_MEM_MALLOC(n, type) ((type *) mutt_mem_mallocarray(n, sizeof(type)))
49
50#define MUTT_MEM_REALLOC(pptr, n, type) \
51( \
52 _Generic(*(pptr), type *: mutt_mem_reallocarray(pptr, n, sizeof(type))) \
53)
54
55void *mutt_mem_calloc(size_t nmemb, size_t size);
56void mutt_mem_free(void *ptr);
57void *mutt_mem_malloc(size_t size);
58void *mutt_mem_mallocarray(size_t nmemb, size_t size);
59void mutt_mem_realloc(void *pptr, size_t size);
60void mutt_mem_reallocarray(void *pptr, size_t nmemb, size_t size);
61
62#define FREE(x) mutt_mem_free(x)
63
64#endif /* MUTT_MUTT_MEMORY_H */
void * mutt_mem_calloc(size_t nmemb, size_t size)
Allocate zeroed memory on the heap.
Definition: memory.c:76
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