add function sf_binlog_index_expand_array

storage_engine
YuQing 2021-09-27 17:43:56 +08:00
parent ae832465a1
commit fd9d59dbd2
2 changed files with 19 additions and 13 deletions

View File

@ -243,29 +243,28 @@ int sf_binlog_index_save(SFBinlogIndexContext *ctx)
return 0; return 0;
} }
int sf_binlog_index_expand(SFBinlogIndexContext *ctx) int sf_binlog_index_expand_array(SFBinlogIndexArray *array,
const int elt_size)
{ {
int alloc; int alloc;
void *indexes; void *indexes;
if (ctx->index_array.alloc == 0) { if (array->alloc == 0) {
alloc = 64; alloc = 1024;
} else { } 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) { if (indexes == NULL) {
return ENOMEM; return ENOMEM;
} }
if (ctx->index_array.count > 0) { if (array->count > 0) {
memcpy(indexes, ctx->index_array.indexes, memcpy(indexes, array->indexes, elt_size * array->count);
ctx->array_elt_size * free(array->indexes);
ctx->index_array.count);
free(ctx->index_array.indexes);
} }
ctx->index_array.indexes = indexes; array->indexes = indexes;
ctx->index_array.alloc = alloc; array->alloc = alloc;
return 0; return 0;
} }

View File

@ -75,7 +75,14 @@ int sf_binlog_index_load(SFBinlogIndexContext *ctx);
int sf_binlog_index_save(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) static inline void sf_binlog_index_free(SFBinlogIndexContext *ctx)
{ {