Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chores: put _new and _free functions into a macro #138

Merged
merged 1 commit into from
Sep 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 5 additions & 24 deletions lib/read/archive/compression_options.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#include "../../../include/sqsh_archive.h"
#include "../../../include/sqsh_data_private.h"
#include "../../../include/sqsh_error.h"
#include "../utils/utils.h"

#include <stdlib.h>
#include <string.h>
Expand All @@ -48,24 +49,9 @@ compression_options(const struct SqshCompressionOptions *context) {

struct SqshCompressionOptions *
sqsh_compression_options_new(struct SqshArchive *sqsh, int *err) {
int rv = 0;
struct SqshCompressionOptions *compression_options =
calloc(1, sizeof(struct SqshCompressionOptions));
if (compression_options == NULL) {
rv = -SQSH_ERROR_MALLOC_FAILED;
goto out;
}
rv = sqsh__compression_options_init(compression_options, sqsh);
if (rv < 0) {
free(compression_options);
compression_options = NULL;
}
rv = 0;
out:
if (err != NULL) {
*err = rv;
}
return compression_options;
SQSH_NEW_IMPL(
sqsh__compression_options_init, struct SqshCompressionOptions,
sqsh);
}

int
Expand Down Expand Up @@ -207,10 +193,5 @@ sqsh__compression_options_cleanup(struct SqshCompressionOptions *context) {

int
sqsh_compression_options_free(struct SqshCompressionOptions *context) {
if (context == NULL) {
return 0;
}
int rv = sqsh__compression_options_cleanup(context);
free(context);
return rv;
SQSH_FREE_IMPL(sqsh__compression_options_cleanup, context);
}
26 changes: 3 additions & 23 deletions lib/read/directory/directory_iterator.c
Original file line number Diff line number Diff line change
Expand Up @@ -206,23 +206,8 @@ sqsh__directory_iterator_init(

struct SqshDirectoryIterator *
sqsh_directory_iterator_new(struct SqshFile *file, int *err) {
int rv = 0;
struct SqshDirectoryIterator *iterator =
calloc(1, sizeof(struct SqshDirectoryIterator));
if (file == NULL) {
rv = -SQSH_ERROR_MALLOC_FAILED;
goto out;
}
rv = sqsh__directory_iterator_init(iterator, file);
if (rv < 0) {
free(iterator);
iterator = NULL;
}
out:
if (err != NULL) {
*err = rv;
}
return iterator;
SQSH_NEW_IMPL(
sqsh__directory_iterator_init, struct SqshDirectoryIterator, file);
}

uint16_t
Expand Down Expand Up @@ -455,10 +440,5 @@ sqsh__directory_iterator_cleanup(struct SqshDirectoryIterator *iterator) {

int
sqsh_directory_iterator_free(struct SqshDirectoryIterator *iterator) {
if (iterator == NULL) {
return 0;
}
int rv = sqsh__directory_iterator_cleanup(iterator);
free(iterator);
return rv;
SQSH_FREE_IMPL(sqsh__directory_iterator_cleanup, iterator);
}
25 changes: 2 additions & 23 deletions lib/read/file/file_iterator.c
Original file line number Diff line number Diff line change
Expand Up @@ -93,23 +93,7 @@ sqsh__file_iterator_init(

struct SqshFileIterator *
sqsh_file_iterator_new(const struct SqshFile *file, int *err) {
int rv = 0;
struct SqshFileIterator *iterator =
calloc(1, sizeof(struct SqshFileIterator));
if (iterator == NULL) {
rv = -SQSH_ERROR_MALLOC_FAILED;
goto out;
}
rv = sqsh__file_iterator_init(iterator, file);
if (rv < 0) {
free(iterator);
iterator = NULL;
}
out:
if (err != NULL) {
*err = rv;
}
return iterator;
SQSH_NEW_IMPL(sqsh__file_iterator_init, struct SqshFileIterator, file);
}

static int
Expand Down Expand Up @@ -388,10 +372,5 @@ sqsh__file_iterator_cleanup(struct SqshFileIterator *iterator) {

int
sqsh_file_iterator_free(struct SqshFileIterator *iterator) {
if (iterator == NULL) {
return 0;
}
int rv = sqsh__file_iterator_cleanup(iterator);
free(iterator);
return rv;
SQSH_FREE_IMPL(sqsh__file_iterator_cleanup, iterator);
}
24 changes: 2 additions & 22 deletions lib/read/file/file_reader.c
Original file line number Diff line number Diff line change
Expand Up @@ -79,22 +79,7 @@ sqsh__file_reader_init(

struct SqshFileReader *
sqsh_file_reader_new(const struct SqshFile *file, int *err) {
int rv = 0;
struct SqshFileReader *reader = calloc(1, sizeof(struct SqshFileReader));
if (reader == NULL) {
rv = -SQSH_ERROR_MALLOC_FAILED;
goto out;
}
rv = sqsh__file_reader_init(reader, file);
if (rv < 0) {
free(reader);
reader = NULL;
}
out:
if (err != NULL) {
*err = rv;
}
return reader;
SQSH_NEW_IMPL(sqsh__file_reader_init, struct SqshFileReader, file);
}

int
Expand Down Expand Up @@ -123,10 +108,5 @@ sqsh__file_reader_cleanup(struct SqshFileReader *reader) {

int
sqsh_file_reader_free(struct SqshFileReader *context) {
if (context == NULL) {
return 0;
}
int rv = sqsh__file_reader_cleanup(context);
free(context);
return rv;
SQSH_FREE_IMPL(sqsh__file_reader_cleanup, context);
}
24 changes: 2 additions & 22 deletions lib/read/tree/walker.c
Original file line number Diff line number Diff line change
Expand Up @@ -123,22 +123,7 @@ sqsh__tree_walker_init(

struct SqshTreeWalker *
sqsh_tree_walker_new(struct SqshArchive *archive, int *err) {
int rv = 0;
struct SqshTreeWalker *walker = calloc(1, sizeof(struct SqshTreeWalker));
if (walker == NULL) {
rv = -SQSH_ERROR_MALLOC_FAILED;
goto out;
}
rv = sqsh__tree_walker_init(walker, archive);
if (rv < 0) {
free(walker);
walker = NULL;
}
out:
if (err != NULL) {
*err = rv;
}
return walker;
SQSH_NEW_IMPL(sqsh__tree_walker_init, struct SqshTreeWalker, archive);
}

int
Expand Down Expand Up @@ -424,10 +409,5 @@ sqsh__tree_walker_cleanup(struct SqshTreeWalker *walker) {

int
sqsh_tree_walker_free(struct SqshTreeWalker *walker) {
if (walker == NULL) {
return 0;
}
int rv = sqsh__tree_walker_cleanup(walker);
free(walker);
return rv;
SQSH_FREE_IMPL(sqsh__tree_walker_cleanup, walker);
}
26 changes: 26 additions & 0 deletions lib/read/utils/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,32 @@ extern "C" {

#define SQSH_CONFIG_DEFAULT(x, d) (size_t)(x == 0 ? (d) : SQSH_MAX(x, 0))

#define SQSH_NEW_IMPL(init, type, ...) \
int rv = 0; \
type *obj = calloc(1, sizeof(type)); \
if (obj == NULL) { \
rv = -SQSH_ERROR_MALLOC_FAILED; \
goto out; \
} \
rv = init(obj, __VA_ARGS__); \
if (rv < 0) { \
free(obj); \
obj = NULL; \
} \
out: \
if (err != NULL) { \
*err = rv; \
} \
return obj

#define SQSH_FREE_IMPL(cleanup, obj) \
if (obj == NULL) { \
return 0; \
} \
int rv = cleanup(obj); \
free(obj); \
return rv

SQSH_NO_UNUSED static inline uint64_t
sqsh_address_ref_outer_offset(uint64_t ref) {
return ref >> 16;
Expand Down
25 changes: 2 additions & 23 deletions lib/read/xattr/xattr_iterator.c
Original file line number Diff line number Diff line change
Expand Up @@ -111,23 +111,7 @@ sqsh__xattr_iterator_init(

struct SqshXattrIterator *
sqsh_xattr_iterator_new(const struct SqshFile *file, int *err) {
int rv = 0;
struct SqshXattrIterator *iterator =
calloc(1, sizeof(struct SqshXattrIterator));
if (iterator == NULL) {
rv = -SQSH_ERROR_MALLOC_FAILED;
goto out;
}
rv = sqsh__xattr_iterator_init(iterator, file);
if (rv < 0) {
free(iterator);
iterator = NULL;
}
out:
if (err != NULL) {
*err = rv;
}
return iterator;
SQSH_NEW_IMPL(sqsh__xattr_iterator_init, struct SqshXattrIterator, file);
}

static const struct SqshDataXattrValue *
Expand Down Expand Up @@ -393,10 +377,5 @@ sqsh__xattr_iterator_cleanup(struct SqshXattrIterator *iterator) {

int
sqsh_xattr_iterator_free(struct SqshXattrIterator *iterator) {
if (iterator == NULL) {
return 0;
}
int rv = sqsh__xattr_iterator_cleanup(iterator);
free(iterator);
return rv;
SQSH_FREE_IMPL(sqsh__xattr_iterator_cleanup, iterator);
}