#pragma once

#include "compiler.h"
#include <stdlib.h>

extern_c no_return __noinline alloc_panic(void);
#define null_check(x) ({			\
    __typeof__(x) __x = (x);			\
    if (unlikely(!__x))				\
	alloc_panic();				\
    __x;					\
})
extern_c void * __safe_alloc(1)   __malloc_func xmalloc(size_t);
extern_c void * __safe_alloc(1)   __malloc_func xmalloc_dma(size_t);
extern_c void * __safe_alloc(1)   __malloc_func xzalloc(size_t);
extern_c void * __safe_alloc(1,2) __malloc_func xncalloc(size_t, size_t);
extern_c void * __safe_alloc(1,2) __malloc_func xcalloc(size_t, size_t);
extern_c void * __safe_alloc(2)   xrealloc(void *, size_t);
extern_c void * __safe_alloc(2,3) xnrealloc(void *, size_t, size_t);

#define xnew(x)		((x) = xzalloc(sizeof(*(x))))
#define xnnew(x,n)	((x) = xcalloc((n),sizeof(*(x))))

static inline void xfree(void *p)
{
    if (p)
	free(p);
}
