[Tarantool-patches] [PATCH v5 13/52] sql: introduce mem_copy()

Vladislav Shpilevoy v.shpilevoy at tarantool.org
Sun Apr 11 21:06:24 MSK 2021


Thanks for the patch!

>> Also what was wrong with sqlVdbeMemCopy's way of using sqlVdbeMemMakeWriteable?
>>
> I see that this as a hack. It changes dynamic or allocated type (only type!) to
> ephemeral and then calls sqlVdbeMemMakeWriteable(), which converts ephemeral
> value to allocated value. Isn't it better to just directly copy?

Yes, your way sounds better.

> diff --git a/src/box/sql/mem.c b/src/box/sql/mem.c
> index 25b2e75ee..ea3917fe3 100644
> --- a/src/box/sql/mem.c
> +++ b/src/box/sql/mem.c
> @@ -267,6 +267,35 @@ mem_destroy(struct Mem *mem)
>  	mem->zMalloc = NULL;
>  }
>  
> +int
> +mem_copy(struct Mem *to, const struct Mem *from)
> +{
> +	mem_clear(to);
> +	to->u = from->u;
> +	to->flags = from->flags;
> +	to->subtype = from->subtype;
> +	to->field_type = from->field_type;
> +	to->n = from->n;
> +	to->z = from->z;
> +	if ((to->flags & (MEM_Str | MEM_Blob)) == 0)
> +		return 0;
> +	if ((to->flags & MEM_Static) != 0)
> +		return 0;
> +	if ((to->flags & (MEM_Zero | MEM_Blob)) == (MEM_Zero | MEM_Blob))
> +		return sqlVdbeMemExpandBlob(to);
> +	if (to->szMalloc == 0)
> +		to->zMalloc = sqlDbMallocRaw(to->db, to->n);
> +	else
> +		to->zMalloc = sqlDbReallocOrFree(to->db, to->zMalloc, to->n);

You can call realloc always. It turns into malloc when
the pointer is NULL, which is the case for szMalloc == 0
I think.

> +	if (to->zMalloc == NULL)
> +		return -1;
> +	to->szMalloc = sqlDbMallocSize(to->db, to->zMalloc);
> +	memcpy(to->zMalloc, to->z, to->n);
> +	to->z = to->zMalloc;
> +	to->flags &= (MEM_Str | MEM_Blob | MEM_Term | MEM_Subtype);
> +	return 0;
> +}


More information about the Tarantool-patches mailing list