From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: From: Vladimir Davydov Subject: [PATCH 09/10] key_def: pass alloc callback to key_def_dump_parts Date: Fri, 17 May 2019 17:52:43 +0300 Message-Id: In-Reply-To: References: In-Reply-To: References: To: tarantool-patches@freelists.org List-ID: So that we can use an allocator different from the region in vylog. --- src/box/key_def.c | 6 +++--- src/box/key_def.h | 4 ++-- src/box/sql/select.c | 2 +- src/box/vy_log.c | 22 ++++++++++------------ 4 files changed, 16 insertions(+), 18 deletions(-) diff --git a/src/box/key_def.c b/src/box/key_def.c index fc547570..46f9c4ef 100644 --- a/src/box/key_def.c +++ b/src/box/key_def.c @@ -286,7 +286,7 @@ key_def_new(const struct key_part_def *parts, uint32_t part_count) int key_def_dump_parts(const struct key_def *def, struct key_part_def *parts, - struct region *region) + void *(*alloc_cb)(void *, size_t), void *alloc_ctx) { for (uint32_t i = 0; i < def->part_count; i++) { const struct key_part *part = &def->parts[i]; @@ -297,7 +297,7 @@ key_def_dump_parts(const struct key_def *def, struct key_part_def *parts, part_def->nullable_action = part->nullable_action; part_def->coll_id = part->coll_id; if (part->path != NULL) { - char *path = region_alloc(region, part->path_len + 1); + char *path = alloc_cb(alloc_ctx, part->path_len + 1); if (path == NULL) { diag_set(OutOfMemory, part->path_len + 1, "region", "part_def->path"); @@ -808,7 +808,7 @@ key_def_find_pk_in_cmp_def(const struct key_def *cmp_def, "region", "key def parts"); goto out; } - if (key_def_dump_parts(pk_def, parts, region) != 0) + if (key_def_dump_parts(pk_def, parts, region_alloc_cb, region) != 0) goto out; /* * Second, update field numbers to match the primary key diff --git a/src/box/key_def.h b/src/box/key_def.h index f4a1a8fd..147064da 100644 --- a/src/box/key_def.h +++ b/src/box/key_def.h @@ -332,12 +332,12 @@ key_def_new(const struct key_part_def *parts, uint32_t part_count); /** * Dump part definitions of the given key def. - * The region is used for allocating JSON paths, if any. + * The callback is used for allocating JSON paths, if any. * Return -1 on memory allocation error, 0 on success. */ int key_def_dump_parts(const struct key_def *def, struct key_part_def *parts, - struct region *region); + void *(*alloc_cb)(void *, size_t), void *alloc_ctx); /** * Update 'has_optional_parts' of @a key_def with correspondence diff --git a/src/box/sql/select.c b/src/box/sql/select.c index d3472a92..5745fb4c 100644 --- a/src/box/sql/select.c +++ b/src/box/sql/select.c @@ -1415,7 +1415,7 @@ sql_key_info_new_from_key_def(sql *db, const struct key_def *key_def) key_info->key_def = NULL; key_info->refs = 1; key_info->part_count = key_def->part_count; - key_def_dump_parts(key_def, key_info->parts, NULL); + key_def_dump_parts(key_def, key_info->parts, NULL, NULL); return key_info; } diff --git a/src/box/vy_log.c b/src/box/vy_log.c index 67b8a763..7157bbf3 100644 --- a/src/box/vy_log.c +++ b/src/box/vy_log.c @@ -675,11 +675,10 @@ fail: * are duplicated as well. */ static struct vy_log_record * -vy_log_record_dup(struct region *pool, const struct vy_log_record *src) +vy_log_record_dup(const struct vy_log_record *src, + void *(*alloc_cb)(void *, size_t), void *alloc_ctx) { - size_t used = region_used(pool); - - struct vy_log_record *dst = region_alloc(pool, sizeof(*dst)); + struct vy_log_record *dst = alloc_cb(alloc_ctx, sizeof(*dst)); if (dst == NULL) { diag_set(OutOfMemory, sizeof(*dst), "region", "struct vy_log_record"); @@ -690,7 +689,7 @@ vy_log_record_dup(struct region *pool, const struct vy_log_record *src) const char *data = src->begin; mp_next(&data); size_t size = data - src->begin; - dst->begin = region_alloc(pool, size); + dst->begin = alloc_cb(alloc_ctx, size); if (dst->begin == NULL) { diag_set(OutOfMemory, size, "region", "vy_log_record::begin"); @@ -702,7 +701,7 @@ vy_log_record_dup(struct region *pool, const struct vy_log_record *src) const char *data = src->end; mp_next(&data); size_t size = data - src->end; - dst->end = region_alloc(pool, size); + dst->end = alloc_cb(alloc_ctx, size); if (dst->end == NULL) { diag_set(OutOfMemory, size, "region", "struct vy_log_record"); @@ -713,21 +712,20 @@ vy_log_record_dup(struct region *pool, const struct vy_log_record *src) if (src->key_def != NULL) { size_t size = src->key_def->part_count * sizeof(struct key_part_def); - dst->key_parts = region_alloc(pool, size); + dst->key_parts = alloc_cb(alloc_ctx, size); if (dst->key_parts == NULL) { diag_set(OutOfMemory, size, "region", "struct key_part_def"); goto err; } - if (key_def_dump_parts(src->key_def, dst->key_parts, pool) != 0) + if (key_def_dump_parts(src->key_def, dst->key_parts, + alloc_cb, alloc_ctx) != 0) goto err; dst->key_part_count = src->key_def->part_count; dst->key_def = NULL; } return dst; - err: - region_truncate(pool, used); return NULL; } @@ -1272,8 +1270,8 @@ vy_log_write(const struct vy_log_record *record) { assert(latch_owner(&vy_log.latch) == fiber()); - struct vy_log_record *tx_record = vy_log_record_dup(&vy_log.pool, - record); + struct vy_log_record *tx_record = vy_log_record_dup(record, + region_alloc_cb, &vy_log.pool); if (tx_record == NULL) { diag_move(diag_get(), &vy_log.tx_diag); vy_log.tx_failed = true; -- 2.11.0