From fd9d59dbd20c6b47792ba811d8ef1c75cfdf80cb Mon Sep 17 00:00:00 2001 From: YuQing <384681@qq.com> Date: Mon, 27 Sep 2021 17:43:56 +0800 Subject: [PATCH] add function sf_binlog_index_expand_array --- src/sf_binlog_index.c | 23 +++++++++++------------ src/sf_binlog_index.h | 9 ++++++++- 2 files changed, 19 insertions(+), 13 deletions(-) diff --git a/src/sf_binlog_index.c b/src/sf_binlog_index.c index 4cf76e1..bd43bd3 100644 --- a/src/sf_binlog_index.c +++ b/src/sf_binlog_index.c @@ -243,29 +243,28 @@ int sf_binlog_index_save(SFBinlogIndexContext *ctx) return 0; } -int sf_binlog_index_expand(SFBinlogIndexContext *ctx) +int sf_binlog_index_expand_array(SFBinlogIndexArray *array, + const int elt_size) { int alloc; void *indexes; - if (ctx->index_array.alloc == 0) { - alloc = 64; + if (array->alloc == 0) { + alloc = 1024; } else { - alloc = ctx->index_array.alloc * 2; + alloc = array->alloc * 2; } - indexes = fc_malloc(ctx->array_elt_size * alloc); + indexes = fc_malloc(elt_size * alloc); if (indexes == NULL) { return ENOMEM; } - if (ctx->index_array.count > 0) { - memcpy(indexes, ctx->index_array.indexes, - ctx->array_elt_size * - ctx->index_array.count); - free(ctx->index_array.indexes); + if (array->count > 0) { + memcpy(indexes, array->indexes, elt_size * array->count); + free(array->indexes); } - ctx->index_array.indexes = indexes; - ctx->index_array.alloc = alloc; + array->indexes = indexes; + array->alloc = alloc; return 0; } diff --git a/src/sf_binlog_index.h b/src/sf_binlog_index.h index 0fc8832..1727fe4 100644 --- a/src/sf_binlog_index.h +++ b/src/sf_binlog_index.h @@ -75,7 +75,14 @@ int sf_binlog_index_load(SFBinlogIndexContext *ctx); int sf_binlog_index_save(SFBinlogIndexContext *ctx); -int sf_binlog_index_expand(SFBinlogIndexContext *ctx); +int sf_binlog_index_expand_array(SFBinlogIndexArray *array, + const int elt_size); + +static inline int sf_binlog_index_expand(SFBinlogIndexContext *ctx) +{ + return sf_binlog_index_expand_array(&ctx-> + index_array, ctx->array_elt_size); +} static inline void sf_binlog_index_free(SFBinlogIndexContext *ctx) {