From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from [87.239.111.99] (localhost [127.0.0.1]) by dev.tarantool.org (Postfix) with ESMTP id 77E3568F40; Tue, 23 Mar 2021 12:40:46 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 77E3568F40 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1616492446; bh=fKzkMpgz5i6+oMBIakjs0AmCnsDWEf2gPeesnzCnc+w=; h=To:Cc:Date:In-Reply-To:References:Subject:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From:Reply-To:From; b=vj9zzK+Sy1eebFvFzcxsehA7BZRb+CjbT/9mAIUn4HUrbOgrLtgeVWZPBqHKjSXRt U8FZ0q8uavYaN3ZqLwsnuxS095hldp0lISp1SY7yF/IQ3m27uNX3iIGLeOutYM+Z3v Kehm88vdycq7r6nEb/FNwXsAZIsdmL50eHziHLzs= Received: from smtp40.i.mail.ru (smtp40.i.mail.ru [94.100.177.100]) (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 DA53868F40 for ; Tue, 23 Mar 2021 12:35:21 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org DA53868F40 Received: by smtp40.i.mail.ru with esmtpa (envelope-from ) id 1lOdRY-0005Dg-RW; Tue, 23 Mar 2021 12:35:21 +0300 To: v.shpilevoy@tarantool.org, tsafin@tarantool.org Cc: tarantool-patches@dev.tarantool.org Date: Tue, 23 Mar 2021 12:35:20 +0300 Message-Id: <36c5f57f22e685be0fbd657c6aa13ac5a807a7d0.1616491731.git.imeevma@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-7564579A: B8F34718100C35BD X-77F55803: 4F1203BC0FB41BD95D6E7CC48CB1F5F1C82687294EF6886B62CC997B0B8C95C4182A05F538085040237C7EB16944463AA6793288027541C5EB818720427008489B68AE07178E917A X-7FA49CB5: FF5795518A3D127A4AD6D5ED66289B5278DA827A17800CE7F6EE1C40B2E8BE15EA1F7E6F0F101C67BD4B6F7A4D31EC0BCC500DACC3FED6E28638F802B75D45FF8AA50765F79006378D70459436292EC88638F802B75D45FF914D58D5BE9E6BC131B5C99E7648C95C686AF563A045C75EB9FC2EF95868301FFA01350B017A5A9BA471835C12D1D9774AD6D5ED66289B5278DA827A17800CE7ABB305BD10C6E5099FA2833FD35BB23D2EF20D2F80756B5F868A13BD56FB6657A471835C12D1D977725E5C173C3A84C31E02C13459908652117882F4460429728AD0CFFFB425014E868A13BD56FB6657D81D268191BDAD3DC09775C1D3CA48CFCF1F65291FBE50C4BA3038C0950A5D36C8A9BA7A39EFB766EC990983EF5C0329BA3038C0950A5D36D5E8D9A59859A8B6F5F00D179C2AB61C76E601842F6C81A1F004C906525384307823802FF610243DF43C7A68FF6260569E8FC8737B5C2249EC8D19AE6D49635B3BBE47FD9DD3FB59A8DF7F3B2552694A57739F23D657EF2B13377AFFFEAFD26923F8577A6DFFEA7C054662F8F2CA352F93EC92FD9297F6715571747095F342E857739F23D657EF2BD5E8D9A59859A8B698B1B85E56A716AC089D37D7C0E48F6C5571747095F342E857739F23D657EF2B6825BDBE14D8E7024847893F9AA87235E5BFE6E7EFDEDCD789D4C264860C145E X-C1DE0DAB: C20DE7B7AB408E4181F030C43753B8186998911F362727C414F749A5E30D975C686AF563A045C75EB9FC2EF95868301FFA01350B017A5A9B9C2B6934AE262D3EE7EAB7254005DCED7532B743992DF240BDC6A1CF3F042BAD6DF99611D93F60EF0417BEADF48D1460699F904B3F4130E343918A1A30D5E7FCCB5012B2E24CD356 X-C8649E89: 4E36BF7865823D7055A7F0CF078B5EC49A30900B95165D34EA882B598A209811A6049BBD4A67D9864049ECD0B95C7E480737BEB8FF57D2B7CE0084FB42FB08651D7E09C32AA3244C82D9E6AF47C757B2A56D2477B03206C9E646F07CC2D4F3D8FACE5A9C96DEB163 X-D57D3AED: 3ZO7eAau8CL7WIMRKs4sN3D3tLDjz0dLbV79QFUyzQ2Ujvy7cMT6pYYqY16iZVKkSc3dCLJ7zSJH7+u4VD18S7Vl4ZUrpaVfd2+vE6kuoey4m4VkSEu530nj6fImhcD4MUrOEAnl0W826KZ9Q+tr5ycPtXkTV4k65bRjmOUUP8cvGozZ33TWg5HZplvhhXbhDGzqmQDTd6OAevLeAnq3Ra9uf7zvY2zzsIhlcp/Y7m53TZgf2aB4JOg4gkr2biojWJZv20R+6UgSj6PFJYXXTg== X-Mailru-Sender: 5C3750E245F362008BC1685FEC6306ED033BE48C37A29927A6793288027541C5EFC4EB6826DC22485105BD0848736F9966FEC6BF5C9C28D97E07721503EA2E00ED97202A5A4E92BF7402F9BA4338D657ED14614B50AE0675 X-Mras: Ok Subject: [Tarantool-patches] [PATCH v4 12/53] sql: introduce mem_copy() X-BeenThere: tarantool-patches@dev.tarantool.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: Tarantool development patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , From: Mergen Imeev via Tarantool-patches Reply-To: imeevma@tarantool.org Errors-To: tarantool-patches-bounces@dev.tarantool.org Sender: "Tarantool-patches" This patch introduces mem_copy(). This function copies value from source MEM to destination MEM. In case value is string or binary and have not static allocation type, it is copied to newly allocated memory. Part of #5818 --- src/box/sql/func.c | 4 ++-- src/box/sql/mem.c | 54 +++++++++++++++++++++++++------------------ src/box/sql/mem.h | 9 +++++++- src/box/sql/vdbeapi.c | 2 +- src/box/sql/vdbeaux.c | 2 +- 5 files changed, 44 insertions(+), 27 deletions(-) diff --git a/src/box/sql/func.c b/src/box/sql/func.c index 81b537d9b..6b6081150 100644 --- a/src/box/sql/func.c +++ b/src/box/sql/func.c @@ -2079,13 +2079,13 @@ minmaxStep(sql_context * context, int NotUsed, sql_value ** argv) bool is_max = (func->flags & SQL_FUNC_MAX) != 0; cmp = sqlMemCompare(pBest, pArg, pColl); if ((is_max && cmp < 0) || (!is_max && cmp > 0)) { - sqlVdbeMemCopy(pBest, pArg); + mem_copy(pBest, pArg); } else { sqlSkipAccumulatorLoad(context); } } else { pBest->db = sql_context_db_handle(context); - sqlVdbeMemCopy(pBest, pArg); + mem_copy(pBest, pArg); } } diff --git a/src/box/sql/mem.c b/src/box/sql/mem.c index abc9291ef..f12441d7c 100644 --- a/src/box/sql/mem.c +++ b/src/box/sql/mem.c @@ -257,6 +257,38 @@ 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)) { + if (sqlVdbeMemExpandBlob(to) != 0) + return -1; + return 0; + } + if (to->szMalloc == 0) + to->zMalloc = sqlDbMallocRaw(to->db, to->n); + else + to->zMalloc = sqlDbReallocOrFree(to->db, to->zMalloc, to->n); + 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; +} + static inline bool mem_has_msgpack_subtype(struct Mem *mem) { @@ -1993,28 +2025,6 @@ vdbe_mem_alloc_blob_region(struct Mem *vdbe_mem, uint32_t size) return 0; } -/* - * Make a full copy of pFrom into pTo. Prior contents of pTo are - * freed before the copy is made. - */ -int -sqlVdbeMemCopy(Mem * pTo, const Mem * pFrom) -{ - int rc = 0; - - mem_clear(pTo); - memcpy(pTo, pFrom, MEMCELLSIZE); - pTo->flags &= ~MEM_Dyn; - if (pTo->flags & (MEM_Str | MEM_Blob)) { - if (0 == (pFrom->flags & MEM_Static)) { - pTo->flags |= MEM_Ephem; - rc = sqlVdbeMemMakeWriteable(pTo); - } - } - - return rc; -} - void sqlVdbeMemShallowCopy(Mem * pTo, const Mem * pFrom, int srcType) { diff --git a/src/box/sql/mem.h b/src/box/sql/mem.h index 19920af3a..fb649891a 100644 --- a/src/box/sql/mem.h +++ b/src/box/sql/mem.h @@ -169,6 +169,14 @@ mem_create(struct Mem *mem); void mem_destroy(struct Mem *mem); +/** + * Copy content of MEM from one MEM to another. In case source MEM contains + * string or binary and allocation type is not STATIC, this value is copied to + * newly allocated by destination MEM memory. + */ +int +mem_copy(struct Mem *to, const struct Mem *from); + /* One or more of the following flags are set to indicate the validOK * representations of the value stored in the Mem struct. * @@ -485,7 +493,6 @@ mem_is_type_compatible(struct Mem *mem, enum field_type type); int vdbe_mem_alloc_blob_region(struct Mem *vdbe_mem, uint32_t size); -int sqlVdbeMemCopy(Mem *, const Mem *); void sqlVdbeMemShallowCopy(Mem *, const Mem *, int); void sqlVdbeMemMove(Mem *, Mem *); int sqlVdbeMemMakeWriteable(Mem *); diff --git a/src/box/sql/vdbeapi.c b/src/box/sql/vdbeapi.c index 8a9573c57..b13385647 100644 --- a/src/box/sql/vdbeapi.c +++ b/src/box/sql/vdbeapi.c @@ -229,7 +229,7 @@ sql_result_text64(sql_context * pCtx, void sql_result_value(sql_context * pCtx, sql_value * pValue) { - sqlVdbeMemCopy(pCtx->pOut, pValue); + mem_copy(pCtx->pOut, pValue); } void diff --git a/src/box/sql/vdbeaux.c b/src/box/sql/vdbeaux.c index 11304b5bc..21ac84099 100644 --- a/src/box/sql/vdbeaux.c +++ b/src/box/sql/vdbeaux.c @@ -2309,7 +2309,7 @@ sqlVdbeGetBoundValue(Vdbe * v, int iVar, u8 aff) if (!mem_is_null(pMem)) { sql_value *pRet = sqlValueNew(v->db); if (pRet) { - sqlVdbeMemCopy((Mem *) pRet, pMem); + mem_copy(pRet, pMem); sql_value_apply_type(pRet, aff); } return pRet; -- 2.25.1