#ifndef RALLOC_H
#define RALLOC_H
#ifdef __cplusplus
extern "C" {
#endif
#include <stddef.h>
#include <stdarg.h>
#include <stdbool.h>
#include "macros.h"
#define ralloc(ctx, type) ((type *) ralloc_size(ctx, sizeof(type)))
#define rzalloc(ctx, type) ((type *) rzalloc_size(ctx, sizeof(type)))
void *ralloc_context(const void *ctx);
void *ralloc_size(const void *ctx, size_t size) MALLOCLIKE;
void *rzalloc_size(const void *ctx, size_t size) MALLOCLIKE;
void *reralloc_size(const void *ctx, void *ptr, size_t size);
#define ralloc_array(ctx, type, count) \
((type *) ralloc_array_size(ctx, sizeof(type), count))
#define rzalloc_array(ctx, type, count) \
((type *) rzalloc_array_size(ctx, sizeof(type), count))
#define reralloc(ctx, ptr, type, count) \
((type *) reralloc_array_size(ctx, ptr, sizeof(type), count))
void *ralloc_array_size(const void *ctx, size_t size, size_t count) MALLOCLIKE;
void *rzalloc_array_size(const void *ctx, size_t size, size_t count) MALLOCLIKE;
void *reralloc_array_size(const void *ctx, void *ptr, size_t size,
size_t count);
void ralloc_free(void *ptr);
void ralloc_steal(const void *new_ctx, void *ptr);
void *ralloc_parent(const void *ptr);
void *ralloc_autofree_context(void);
void ralloc_set_destructor(const void *ptr, void(*destructor)(void *));
char *ralloc_strdup(const void *ctx, const char *str) MALLOCLIKE;
char *ralloc_strndup(const void *ctx, const char *str, size_t n) MALLOCLIKE;
bool ralloc_strcat(char **dest, const char *str);
bool ralloc_strncat(char **dest, const char *str, size_t n);
char *ralloc_asprintf (const void *ctx, const char *fmt, ...) PRINTFLIKE(2, 3) MALLOCLIKE;
char *ralloc_vasprintf(const void *ctx, const char *fmt, va_list args) MALLOCLIKE;
bool ralloc_asprintf_rewrite_tail(char **str, size_t *start,
const char *fmt, ...)
PRINTFLIKE(3, 4);
bool ralloc_vasprintf_rewrite_tail(char **str, size_t *start, const char *fmt,
va_list args);
bool ralloc_asprintf_append (char **str, const char *fmt, ...)
PRINTFLIKE(2, 3);
bool ralloc_vasprintf_append(char **str, const char *fmt, va_list args);
size_t printf_length(const char *fmt, va_list untouched_args);
#ifdef __cplusplus
}
#endif
#define DECLARE_RALLOC_CXX_OPERATORS(TYPE) \
private: \
static void _ralloc_destructor(void *p) \
{ \
reinterpret_cast<TYPE *>(p)->~TYPE(); \
} \
public: \
static void* operator new(size_t size, void *mem_ctx) \
{ \
void *p = ralloc_size(mem_ctx, size); \
assert(p != NULL); \
if (!HAS_TRIVIAL_DESTRUCTOR(TYPE)) \
ralloc_set_destructor(p, _ralloc_destructor); \
return p; \
} \
\
static void operator delete(void *p) \
{ \
\
if (!HAS_TRIVIAL_DESTRUCTOR(TYPE)) \
ralloc_set_destructor(p, NULL); \
ralloc_free(p); \
}
#endif