common: Add znew/znew_n() macros

This commit is contained in:
John Lindgren 2022-09-18 15:22:26 -04:00
parent da57483961
commit a54d378e6c
26 changed files with 47 additions and 47 deletions

View file

@ -11,6 +11,17 @@
*/
void *xzalloc(size_t size);
/*
* Type-safe macros in the style of C++ new/new[].
* <expr> may be either a type name or value expression.
*
* Examples:
* struct wlr_box *box = znew(*box);
* char *buf = znew_n(char, 80);
*/
#define znew(expr) ((__typeof__(expr) *)xzalloc(sizeof(expr)))
#define znew_n(expr, n) ((__typeof__(expr) *)xzalloc((n) * sizeof(expr)))
/*
* As defined in FreeBSD.
* Like realloc(), but calls exit() on error.