Sun, 19 Oct 2025 21:18:17 +0200
define the function pointer for clone functions
relates to #743 and issue #744
src/cx/allocator.h | file | annotate | diff | comparison | revisions |
--- a/src/cx/allocator.h Fri Oct 17 21:03:11 2025 +0200 +++ b/src/cx/allocator.h Sun Oct 19 21:18:17 2025 +0200 @@ -119,6 +119,32 @@ */ typedef void (*cx_destructor_func2)(void *data, void *memory); + +/** + * Function pointer type for clone functions. + * + * A clone function is supposed to create a deep copy of the memory pointed to + * by the @p source pointer. + * If the @p target pointer is non-null, the clone function is supposed to store + * the copy into that memory region. + * Otherwise, the clone function shall use the specified @p allocator to create + * a new object. + * + * The return value of a clone function is always a pointer to the target + * memory region, or @c NULL if any allocation failed. + * A clone function SHOULD NOT fail for any other reason than an allocation + * failure. + * + * @param target the target memory or @c NULL, if memory shall be allocated + * @param source the source memory + * @param allocator the allocator that shall be used + * @param data optional additional data + * @return either the specified @p target, a pointer to the allocated memory, + * or @c NULL, if any error occurred + */ +typedef void*(cx_clone_func)(void *target, const void *source, + const CxAllocator *allocator, void *data); + /** * Reallocate a previously allocated block and changes the pointer in-place, * if necessary. @@ -135,10 +161,7 @@ * @see cx_reallocatearray() */ cx_attr_nonnull cx_attr_nodiscard -CX_EXPORT int cx_reallocate_( - void **mem, - size_t n -); +CX_EXPORT int cx_reallocate_(void **mem, size_t n); /** * Reallocate a previously allocated block and changes the pointer in-place,