Tarantool development patches archive
 help / color / mirror / Atom feed
From: Nikita Pettik <korablev@tarantool.org>
To: Roman Khabibov <roman.habibov@tarantool.org>
Cc: tarantool-patches@dev.tarantool.org
Subject: Re: [Tarantool-patches] [PATCH v4 4/5] sql: use parser's region of "index" array
Date: Thu, 5 Nov 2020 22:26:21 +0000	[thread overview]
Message-ID: <20201105222621.GD8188@tarantool.org> (raw)
In-Reply-To: <20201009134529.13212-5-roman.habibov@tarantool.org>

On 09 Oct 16:45, 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.

Would be nice pointing out where leak occured. Also realloc usage looks
better to me (27 lines vs 8 with straight execution flaw). 
 
> Needed for #2349
> ---
>  src/box/sql/build.c | 48 +++++++++++++++++++++++++++++++++------------
>  1 file changed, 35 insertions(+), 13 deletions(-)
> 
> diff --git a/src/box/sql/build.c b/src/box/sql/build.c
> index 3b3c8099a..fd8e7ed1e 100644
> --- a/src/box/sql/build.c
> +++ b/src/box/sql/build.c
> @@ -2228,29 +2228,52 @@ 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 parse's region if needed.
> + *
>   * We follow convention that PK comes first in list.
>   *
> + * @param parse Parsing structure.
>   * @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)
> +static int
> +sql_space_add_index(struct Parse *parse, 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;
> +	struct region *region = &parse->region;
> +	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;
>  	/* 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 0;
> +
> +alloc_error:
> +	diag_set(OutOfMemory, size, "region_alloc_array", "idx");
> +	parse->is_aborted = true;
> +	return -1;
>  }
>  
>  int
> @@ -2717,9 +2740,8 @@ sql_create_index(struct Parse *parse) {
>  		sqlVdbeAddOp0(vdbe, OP_Expire);
>  	}
>  
> -	if (tbl_name != NULL)
> +	if (tbl_name != NULL || sql_space_add_index(parse, space, index) != 0)
>  		goto exit_create_index;
> -	table_add_index(space, index);
>  	index = NULL;
>  
>  	/* Clean up before exiting. */
> -- 
> 2.24.3 (Apple Git-128)
> 

  reply	other threads:[~2020-11-05 22:26 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-10-09 13:45 [Tarantool-patches] [PATCH v4 0/5] Support column addition Roman Khabibov
2020-10-09 13:45 ` [Tarantool-patches] [PATCH v4 1/5] sql: rename TK_COLUMN to TK_COLUMN_REF Roman Khabibov
2020-11-05 22:17   ` Nikita Pettik
2020-10-09 13:45 ` [Tarantool-patches] [PATCH v4 2/5] sql: refactor create_table_def and parse Roman Khabibov
2020-11-05 22:17   ` Nikita Pettik
2020-11-10 12:17     ` Roman Khabibov
2020-11-17 20:23       ` Nikita Pettik
2020-11-15 11:51         ` roman
2020-10-09 13:45 ` [Tarantool-patches] [PATCH v4 3/5] schema: add box_space_field_MAX Roman Khabibov
2020-11-05 22:18   ` Nikita Pettik
2020-10-09 13:45 ` [Tarantool-patches] [PATCH v4 4/5] sql: use parser's region of "index" array Roman Khabibov
2020-11-05 22:26   ` Nikita Pettik [this message]
2020-11-10 12:18     ` Roman Khabibov
2020-11-17 20:35       ` Nikita Pettik
     [not found] ` <20201009134529.13212-6-roman.habibov@tarantool.org>
2020-11-06 15:57   ` [Tarantool-patches] [PATCH v4 5/5] sql: support column addition Nikita Pettik
2020-11-10 12:18     ` Roman Khabibov
2020-11-18 16:15       ` Nikita Pettik
2020-11-15 11:51         ` roman

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20201105222621.GD8188@tarantool.org \
    --to=korablev@tarantool.org \
    --cc=roman.habibov@tarantool.org \
    --cc=tarantool-patches@dev.tarantool.org \
    --subject='Re: [Tarantool-patches] [PATCH v4 4/5] sql: use parser'\''s region of "index" array' \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox