From ddc16d8f7ae2cd0103ca311d2026654c5f44cc8d Mon Sep 17 00:00:00 2001 From: YangHau Date: Mon, 23 Sep 2019 11:52:18 +0800 Subject: [PATCH] feat(utils): Add datatype bundle object array --- utils/BUILD | 9 +++++++ utils/bundle_array.h | 61 ++++++++++++++++++++++++++++++++++++++++++++ utils/pow.h | 2 +- 3 files changed, 71 insertions(+), 1 deletion(-) create mode 100644 utils/bundle_array.h diff --git a/utils/BUILD b/utils/BUILD index ea9a411d..16fcac46 100644 --- a/utils/BUILD +++ b/utils/BUILD @@ -37,3 +37,12 @@ cc_library( "@entangled//common/model:transaction", ], ) + +cc_library( + name = "bundle_array", + srcs = ["bundle_array.h"], + deps = [ + "@com_github_uthash//:uthash", + "@entangled//common/model:bundle", + ], +) diff --git a/utils/bundle_array.h b/utils/bundle_array.h new file mode 100644 index 00000000..73c8a91c --- /dev/null +++ b/utils/bundle_array.h @@ -0,0 +1,61 @@ +/* + * Copyright (C) 2019 BiiLabs Co., Ltd. and Contributors + * All Rights Reserved. + * This is free software; you can redistribute it and/or modify it under the + * terms of the MIT license. A copy of the license can be found in the file + * "LICENSE" at the root of this distribution. + */ + +#ifndef UTILS_BUNDLE_ARRAY_H_ +#define UTILS_BUNDLE_ARRAY_H_ + +#ifdef __cplusplus +extern "C" { +#endif + +#include "common/model/bundle.h" +#include "utarray.h" + +/** + * @file bundle_array.h + * @brief Implementation of bundle array object. This object would be used when we fetch multiple + * `bundle_transactions_t` objects. It provides an easier way to save and traverse all the bundles. + */ + +typedef UT_array bundle_array_t; + +static UT_icd bundle_array_icd = {sizeof(bundle_transactions_t), 0, 0, 0}; + +/** + * Allocate memory of bundle_array_t + */ +static inline void bundle_array_new(bundle_array_t **const bundle_array) { + utarray_new(*bundle_array, &bundle_array_icd); +} + +/** + * @brief The bundle array iterator. + */ +#define BUNDLE_ARRAY_FOREACH(bundles, bdl) \ + for (bdl = (bundle_transactions_t *)utarray_front(bundles); bdl != NULL; \ + bdl = (bundle_transactions_t *)utarray_next(bundles, bdl)) + +/** + * Free memory of bundle_array_t + */ +static inline void bundle_array_free(bundle_array_t **const bundle_array) { + // TODO set dtor and use utarray_free() instead. + if (bundle_array && *bundle_array) { + bundle_transactions_t *bundle = NULL; + iota_transaction_t *tx = NULL; + BUNDLE_ARRAY_FOREACH(*bundle_array, bundle) { bundle_transactions_free(&bundle); } + free(*bundle_array); + } + *bundle_array = NULL; +} + +#ifdef __cplusplus +} +#endif + +#endif // UTILS_BUNDLE_ARRAY_H_ diff --git a/utils/pow.h b/utils/pow.h index 4e001dec..555f08a2 100644 --- a/utils/pow.h +++ b/utils/pow.h @@ -10,10 +10,10 @@ #define UTILS_POW_H_ #include -#include "./utarray.h" #include "accelerator/errors.h" #include "common/model/bundle.h" #include "common/trinary/flex_trit.h" +#include "utarray.h" #ifdef __cplusplus extern "C" {