|
| 1 | +/* |
| 2 | + * Copyright (C) 2019 BiiLabs Co., Ltd. and Contributors |
| 3 | + * All Rights Reserved. |
| 4 | + * This is free software; you can redistribute it and/or modify it under the |
| 5 | + * terms of the MIT license. A copy of the license can be found in the file |
| 6 | + * "LICENSE" at the root of this distribution. |
| 7 | + */ |
| 8 | + |
| 9 | +#ifndef UTILS_BUNDLE_ARRAY_H_ |
| 10 | +#define UTILS_BUNDLE_ARRAY_H_ |
| 11 | + |
| 12 | +#ifdef __cplusplus |
| 13 | +extern "C" { |
| 14 | +#endif |
| 15 | + |
| 16 | +#include "common/model/bundle.h" |
| 17 | +#include "utarray.h" |
| 18 | + |
| 19 | +/** |
| 20 | + * @file bundle_array.h |
| 21 | + * @brief Implementation of bundle array object. This object would be used when we fetch multiple |
| 22 | + * `bundle_transactions_t` objects. It provides an easier way to save and traverse all the bundles. |
| 23 | + */ |
| 24 | + |
| 25 | +typedef UT_array bundle_array_t; |
| 26 | + |
| 27 | +static UT_icd bundle_array_icd = {sizeof(bundle_transactions_t), 0, 0, 0}; |
| 28 | + |
| 29 | +/** |
| 30 | + * Allocate memory of bundle_array_t |
| 31 | + */ |
| 32 | +static inline void bundle_array_new(bundle_array_t **const bundle_array) { |
| 33 | + utarray_new(*bundle_array, &bundle_array_icd); |
| 34 | +} |
| 35 | + |
| 36 | +/** |
| 37 | + * @brief The bundle array iterator. |
| 38 | + */ |
| 39 | +#define BUNDLE_ARRAY_FOREACH(bundles, bdl) \ |
| 40 | + for (bdl = (bundle_transactions_t *)utarray_front(bundles); bdl != NULL; \ |
| 41 | + bdl = (bundle_transactions_t *)utarray_next(bundles, bdl)) |
| 42 | + |
| 43 | +/** |
| 44 | + * Free memory of bundle_array_t |
| 45 | + */ |
| 46 | +static inline void bundle_array_free(bundle_array_t **const bundle_array) { |
| 47 | + // TODO set dtor and use utarray_free() instead. |
| 48 | + if (bundle_array && *bundle_array) { |
| 49 | + bundle_transactions_t *bundle = NULL; |
| 50 | + iota_transaction_t *tx = NULL; |
| 51 | + BUNDLE_ARRAY_FOREACH(*bundle_array, bundle) { bundle_transactions_free(&bundle); } |
| 52 | + free(*bundle_array); |
| 53 | + } |
| 54 | + *bundle_array = NULL; |
| 55 | +} |
| 56 | + |
| 57 | +#ifdef __cplusplus |
| 58 | +} |
| 59 | +#endif |
| 60 | + |
| 61 | +#endif // UTILS_BUNDLE_ARRAY_H_ |
0 commit comments