#ifndef RALLOC_H
#define RALLOC_H
-#ifdef __cplusplus
-extern "C" {
-#endif
-
#include <stddef.h>
#include <stdarg.h>
#include <stdbool.h>
#include "macros.h"
+#ifdef __cplusplus
+extern "C" {
+#endif
+
/**
* \def ralloc(ctx, type)
* Allocate a new object chained off of the given context.
* simply allocates storage for \p size bytes and returns the pointer,
* similar to \c malloc.
*/
-void *ralloc_size(const void *ctx, size_t size);
+void *ralloc_size(const void *ctx, size_t size) MALLOCLIKE;
/**
* Allocate zero-initialized memory chained off of the given context.
*
* This is similar to \c calloc with a size of 1.
*/
-void *rzalloc_size(const void *ctx, size_t size);
+void *rzalloc_size(const void *ctx, size_t size) MALLOCLIKE;
/**
* Resize a piece of ralloc-managed memory, preserving data.
* More than a convenience function, this also checks for integer overflow when
* multiplying \p size and \p count. This is necessary for security.
*/
-void *ralloc_array_size(const void *ctx, size_t size, unsigned count);
+void *ralloc_array_size(const void *ctx, size_t size, unsigned count) MALLOCLIKE;
/**
* Allocate a zero-initialized array chained off the given context.
* More than a convenience function, this also checks for integer overflow when
* multiplying \p size and \p count. This is necessary for security.
*/
-void *rzalloc_array_size(const void *ctx, size_t size, unsigned count);
+void *rzalloc_array_size(const void *ctx, size_t size, unsigned count) MALLOCLIKE;
/**
* Resize a ralloc-managed array, preserving data.
*/
void ralloc_steal(const void *new_ctx, void *ptr);
+/**
+ * Reparent all children from one context to another.
+ *
+ * This effectively calls ralloc_steal(new_ctx, child) for all children of \p old_ctx.
+ */
+void ralloc_adopt(const void *new_ctx, void *old_ctx);
+
/**
* Return the given pointer's ralloc context.
*/
/**
* Duplicate a string, allocating the memory from the given context.
*/
-char *ralloc_strdup(const void *ctx, const char *str);
+char *ralloc_strdup(const void *ctx, const char *str) MALLOCLIKE;
/**
* Duplicate a string, allocating the memory from the given context.
* Like \c strndup, at most \p n characters are copied. If \p str is longer
* than \p n characters, \p n are copied, and a termining \c '\0' byte is added.
*/
-char *ralloc_strndup(const void *ctx, const char *str, size_t n);
+char *ralloc_strndup(const void *ctx, const char *str, size_t n) MALLOCLIKE;
/**
* Concatenate two strings, allocating the necessary space.
*
* \return The newly allocated string.
*/
-char *ralloc_asprintf (const void *ctx, const char *fmt, ...) PRINTFLIKE(2, 3);
+char *ralloc_asprintf (const void *ctx, const char *fmt, ...) PRINTFLIKE(2, 3) MALLOCLIKE;
/**
* Print to a string, given a va_list.
*
* \return The newly allocated string.
*/
-char *ralloc_vasprintf(const void *ctx, const char *fmt, va_list args);
+char *ralloc_vasprintf(const void *ctx, const char *fmt, va_list args) MALLOCLIKE;
/**
* Rewrite the tail of an existing string, starting at a given index.