From 44c81eeffe9c8820b1009a7a5c728782aa5ebf40 Mon Sep 17 00:00:00 2001 From: David Oberhollenzer Date: Tue, 3 Mar 2020 19:43:54 +0100 Subject: Add a generic copying mechanism to sqfs_object_t This patch adds a deep-copy callback to sqfs_object_t and removes the copying mechanism from sqfs_compressor_t. This is also interesting for other types. Signed-off-by: David Oberhollenzer --- include/sqfs/predef.h | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) (limited to 'include/sqfs/predef.h') diff --git a/include/sqfs/predef.h b/include/sqfs/predef.h index 6ca3503..0e81933 100644 --- a/include/sqfs/predef.h +++ b/include/sqfs/predef.h @@ -121,6 +121,8 @@ typedef struct sqfs_xattr_id_table_t sqfs_xattr_id_table_t; */ typedef struct sqfs_object_t { void (*destroy)(struct sqfs_object_t *instance); + + struct sqfs_object_t *(*copy)(const struct sqfs_object_t *orig); } sqfs_object_t; /** @@ -135,4 +137,22 @@ static SQFS_INLINE void sqfs_destroy(void *obj) ((sqfs_object_t *)obj)->destroy(obj); } +/** + * @brief Create a deep copy of an object if possible. + * + * @memberof sqfs_object_t + * + * @param obj A pointer to an object + * + * @return A pointer to a new object, instantiated from the old on success, + * NULL on failure. + */ +static SQFS_INLINE void *sqfs_copy(const void *obj) +{ + if (((sqfs_object_t *)obj)->copy != NULL) + return ((sqfs_object_t *)obj)->copy(obj); + + return NULL; +} + #endif /* SQFS_PREDEF_H */ -- cgit v1.2.3