From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from smtp53.i.mail.ru (smtp53.i.mail.ru [94.100.177.113]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dev.tarantool.org (Postfix) with ESMTPS id 88D02469719 for ; Wed, 16 Sep 2020 16:30:25 +0300 (MSK) Date: Wed, 16 Sep 2020 13:30:24 +0000 From: Nikita Pettik Message-ID: <20200916133024.GC10599@tarantool.org> References: <20200911215115.6622-1-roman.habibov@tarantool.org> <20200911215115.6622-5-roman.habibov@tarantool.org> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: <20200911215115.6622-5-roman.habibov@tarantool.org> Subject: Re: [Tarantool-patches] [PATCH v4 4/6] sql: use parser's region of "index" array List-Id: Tarantool development patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Roman Khabibov Cc: tarantool-patches@dev.tarantool.org, v.shpilevoy@tarantool.org On 12 Sep 00:51, Roman Khabibov wrote: > Allocate memory for the "index" array of ephemeral space on the > parser's region instead of a heap as it was before. Fixed a memory > leak that realloc() generated. > > Needed for #3075 Why it is related to 3075?? > --- > src/box/sql/build.c | 42 +++++++++++++++++++++++++++++++----------- > 1 file changed, 31 insertions(+), 11 deletions(-) > > diff --git a/src/box/sql/build.c b/src/box/sql/build.c > index 6cc76bb77..d1d240315 100644 > --- a/src/box/sql/build.c > +++ b/src/box/sql/build.c > @@ -2226,29 +2226,49 @@ vdbe_emit_new_sec_index_id(struct Parse *parse, uint32_t space_id, > } > > /** > - * Add new index to table's indexes list. > + * Add new index to ephemeral @a space's "index" array. Reallocate > + * memory on @a region if needed. > + * > * We follow convention that PK comes first in list. > * > + * @param region Region (on parser). > * @param space Space to which belongs given index. > * @param index Index to be added to list. > */ > static void > -table_add_index(struct space *space, struct index *index) > +sql_space_add_index(struct region *region, struct space *space, > + struct index *index) > { > uint32_t idx_count = space->index_count; > - size_t indexes_sz = sizeof(struct index *) * (idx_count + 1); > - struct index **idx = (struct index **) realloc(space->index, > - indexes_sz); > - if (idx == NULL) { > - diag_set(OutOfMemory, indexes_sz, "realloc", "idx"); > - return; > - } > - space->index = idx; > + size_t size = 0; > + struct index **idx = NULL; > + if (idx_count == 0) { > + idx = region_alloc_array(region, typeof(struct index *), 1, > + &size); > + if (idx == NULL) > + goto alloc_error; > + /* > + * Reallocate each time the idx_count becomes equal to the > + * power of two. > + */ > + } else if ((idx_count & (idx_count - 1)) == 0) { > + idx = region_alloc_array(region, typeof(struct index *), > + idx_count * 2, &size); > + if (idx == NULL) > + goto alloc_error; > + memcpy(idx, space->index, idx_count * sizeof(struct index *)); > + } > + if (idx != NULL) > + space->index = idx; DOes it make any sense? Why not simply call ::free (in case there really was memleak)? > /* Make sure that PK always comes as first member. */ > if (index->def->iid == 0 && idx_count != 0) > SWAP(space->index[0], index); > space->index[space->index_count++] = index; > space->index_id_max = MAX(space->index_id_max, index->def->iid);; > + return; > + > +alloc_error: > + diag_set(OutOfMemory, size, "region_alloc_array", "idx"); > } > > int > @@ -2717,7 +2737,7 @@ sql_create_index(struct Parse *parse) { > > if (tbl_name != NULL) > goto exit_create_index; > - table_add_index(space, index); > + sql_space_add_index(&parse->region, space, index); > index = NULL; > > /* Clean up before exiting. */ > -- > 2.24.3 (Apple Git-128) >