From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: From: Vladislav Shpilevoy Subject: [PATCH v2 1/2] Do not mix box_key_def_delete and free to delete key_def Date: Thu, 15 Mar 2018 15:19:15 +0300 Message-Id: <87d5c968fc7e9b823f93a827e5188678fb960f5a.1521116161.git.v.shpilevoy@tarantool.org> In-Reply-To: References: In-Reply-To: References: To: tarantool-patches@freelists.org Cc: vdavydov.dev@gmail.com, Vladislav Shpilevoy List-ID: Use key_def_delete instead. It is more safe to use one destructor everywhere for a case, if in a future key_def will not be deleted by simple free(). Signed-off-by: Vladislav Shpilevoy --- src/box/alter.cc | 3 ++- src/box/key_def.cc | 10 ++++++++-- src/box/key_def.h | 7 +++++++ src/box/schema.cc | 4 ++-- src/box/vinyl.c | 4 ++-- src/box/vy_index.c | 8 ++++---- test/unit/vy_iterators_helper.c | 2 +- test/unit/vy_mem.c | 4 ++-- test/unit/vy_point_lookup.c | 2 +- test/unit/vy_write_iterator.c | 2 +- 10 files changed, 30 insertions(+), 16 deletions(-) diff --git a/src/box/alter.cc b/src/box/alter.cc index 8455373b2..c89fe8814 100644 --- a/src/box/alter.cc +++ b/src/box/alter.cc @@ -328,7 +328,8 @@ index_def_new_from_tuple(struct tuple *tuple, struct space *space) } auto key_def_guard = make_scoped_guard([&] { free(part_def); - free(key_def); + if (key_def != NULL) + key_def_delete(key_def); }); if (is_166plus) { /* 1.6.6+ */ diff --git a/src/box/key_def.cc b/src/box/key_def.cc index 955349cf3..2f1574195 100644 --- a/src/box/key_def.cc +++ b/src/box/key_def.cc @@ -106,6 +106,12 @@ key_def_dup(const struct key_def *src) return res; } +void +key_def_delete(struct key_def *def) +{ + free(def); +} + static void key_def_set_cmp(struct key_def *def) { @@ -145,7 +151,7 @@ key_def_new_with_parts(struct key_part_def *parts, uint32_t part_count) if (coll == NULL) { diag_set(ClientError, ER_WRONG_INDEX_OPTIONS, i + 1, "collation was not found by ID"); - free(def); + key_def_delete(def); return NULL; } } @@ -187,7 +193,7 @@ box_key_def_new(uint32_t *fields, uint32_t *types, uint32_t part_count) void box_key_def_delete(box_key_def_t *key_def) { - free(key_def); + key_def_delete(key_def); } int diff --git a/src/box/key_def.h b/src/box/key_def.h index 4726c38a2..fe9ee56f7 100644 --- a/src/box/key_def.h +++ b/src/box/key_def.h @@ -149,6 +149,13 @@ struct key_def { struct key_def * key_def_dup(const struct key_def *src); +/** + * Delete @a key_def. + * @param def Key_def to delete. + */ +void +key_def_delete(struct key_def *def); + /** \cond public */ typedef struct key_def box_key_def_t; diff --git a/src/box/schema.cc b/src/box/schema.cc index 2e8533b94..1b96f978c 100644 --- a/src/box/schema.cc +++ b/src/box/schema.cc @@ -278,7 +278,7 @@ schema_init() struct key_def *key_def = key_def_new(1); /* part count */ if (key_def == NULL) diag_raise(); - auto key_def_guard = make_scoped_guard([&] { box_key_def_delete(key_def); }); + auto key_def_guard = make_scoped_guard([&] { key_def_delete(key_def); }); key_def_set_part(key_def, 0 /* part no */, 0 /* field no */, FIELD_TYPE_STRING, false, NULL); @@ -329,7 +329,7 @@ schema_init() sc_space_new(BOX_CLUSTER_ID, "_cluster", key_def, &on_replace_cluster, NULL); - free(key_def); + key_def_delete(key_def); key_def = key_def_new(2); /* part count */ if (key_def == NULL) diag_raise(); diff --git a/src/box/vinyl.c b/src/box/vinyl.c index 3467281a7..225698eb0 100644 --- a/src/box/vinyl.c +++ b/src/box/vinyl.c @@ -3097,7 +3097,7 @@ vy_join_cb(const struct vy_log_record *record, void *arg) ctx->space_id = record->space_id; ctx->index_id = record->index_id; if (ctx->key_def != NULL) - free(ctx->key_def); + key_def_delete(ctx->key_def); ctx->key_def = key_def_new_with_parts(record->key_parts, record->key_part_count); if (ctx->key_def == NULL) @@ -3234,7 +3234,7 @@ vinyl_engine_join(struct engine *engine, struct vclock *vclock, /* Cleanup. */ if (ctx->key_def != NULL) - free(ctx->key_def); + key_def_delete(ctx->key_def); if (ctx->format != NULL) tuple_format_unref(ctx->format); if (ctx->upsert_format != NULL) diff --git a/src/box/vy_index.c b/src/box/vy_index.c index 68fccab52..d9160041b 100644 --- a/src/box/vy_index.c +++ b/src/box/vy_index.c @@ -257,9 +257,9 @@ fail_mem_format_with_colmask: fail_upsert_format: tuple_format_unref(index->disk_format); fail_format: - free(cmp_def); + key_def_delete(cmp_def); fail_cmp_def: - free(key_def); + key_def_delete(key_def); fail_key_def: free(index->tree); fail_tree: @@ -308,8 +308,8 @@ vy_index_delete(struct vy_index *index) tuple_format_unref(index->disk_format); tuple_format_unref(index->mem_format_with_colmask); tuple_format_unref(index->upsert_format); - free(index->cmp_def); - free(index->key_def); + key_def_delete(index->cmp_def); + key_def_delete(index->key_def); histogram_delete(index->run_hist); vy_index_stat_destroy(&index->stat); vy_cache_destroy(&index->cache); diff --git a/test/unit/vy_iterators_helper.c b/test/unit/vy_iterators_helper.c index 0e41de4b1..9eab13d34 100644 --- a/test/unit/vy_iterators_helper.c +++ b/test/unit/vy_iterators_helper.c @@ -252,7 +252,7 @@ destroy_test_cache(struct vy_cache *cache, struct key_def *def, { tuple_format_unref(format); vy_cache_destroy(cache); - box_key_def_delete(def); + key_def_delete(def); } bool diff --git a/test/unit/vy_mem.c b/test/unit/vy_mem.c index 5d1608771..60fcf0849 100644 --- a/test/unit/vy_mem.c +++ b/test/unit/vy_mem.c @@ -56,7 +56,7 @@ test_basic(void) /* Clean up */ vy_mem_delete(mem); - box_key_def_delete(key_def); + key_def_delete(key_def); fiber_gc(); footer(); @@ -302,7 +302,7 @@ test_iterator_restore_after_insertion() tuple_format_unref(format); lsregion_destroy(&lsregion); - box_key_def_delete(key_def); + key_def_delete(key_def); fiber_gc(); diff --git a/test/unit/vy_point_lookup.c b/test/unit/vy_point_lookup.c index 52f4427e9..963329c9f 100644 --- a/test/unit/vy_point_lookup.c +++ b/test/unit/vy_point_lookup.c @@ -312,7 +312,7 @@ test_basic() index_def_delete(index_def); tuple_format_unref(format); vy_cache_destroy(&cache); - box_key_def_delete(key_def); + key_def_delete(key_def); vy_cache_env_destroy(&cache_env); vy_run_env_destroy(&run_env); vy_index_env_destroy(&index_env); diff --git a/test/unit/vy_write_iterator.c b/test/unit/vy_write_iterator.c index 9058298cb..bf8ef39e8 100644 --- a/test/unit/vy_write_iterator.c +++ b/test/unit/vy_write_iterator.c @@ -501,7 +501,7 @@ test_basic(void) expected, expected_count, vlsns, vlsns_count, true, false); } - box_key_def_delete(key_def); + key_def_delete(key_def); fiber_gc(); footer(); check_plan(); -- 2.14.3 (Apple Git-98)