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 62ECA6AA80; Tue, 23 Mar 2021 12:49:48 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 62ECA6AA80 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1616492988; bh=66UX4OHzlHIY6l/d5ibo1KNwtpdP5fYp6ZmG8y1PaBY=; 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=nlxqMMBghWSYO39ULqXDkMNUE6KCJgPYas2JMQEi59qNE7A6bEtntFdUoJ/ezFqh8 6k0b3b0KWBFSDe6gei8oXzBYTDNDYOkyyjBtIt+KmOAn5hpsncV+giEJwW6YraNnBr vlrdbMWqTswf8xC9jNI3WJEVWtEdRcqVu2vQx9wo= Received: from smtp59.i.mail.ru (smtp59.i.mail.ru [217.69.128.39]) (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 8738D68F53 for ; Tue, 23 Mar 2021 12:36:01 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 8738D68F53 Received: by smtp59.i.mail.ru with esmtpa (envelope-from ) id 1lOdSC-00070w-Jv; Tue, 23 Mar 2021 12:36:01 +0300 To: v.shpilevoy@tarantool.org, tsafin@tarantool.org Cc: tarantool-patches@dev.tarantool.org Date: Tue, 23 Mar 2021 12:36:00 +0300 Message-Id: X-Mailer: git-send-email 2.25.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-7564579A: 646B95376F6C166E X-77F55803: 4F1203BC0FB41BD95D6E7CC48CB1F5F145AC670795810553045AFCF1215FE694182A05F538085040A94904DD2231BC6F97B8A0341EC83A1458003B5DB097CCBA1D09E43A61EBAA91 X-7FA49CB5: FF5795518A3D127A4AD6D5ED66289B5278DA827A17800CE757AEC41D7AA04458EA1F7E6F0F101C67BD4B6F7A4D31EC0BCC500DACC3FED6E28638F802B75D45FF8AA50765F7900637EA9DEEAA3ECF8E948638F802B75D45FF914D58D5BE9E6BC131B5C99E7648C95C686AF563A045C75EDC7DDCB8E1494F59FED063B697A107BEA471835C12D1D9774AD6D5ED66289B5278DA827A17800CE7BEE62E5629C982429FA2833FD35BB23D2EF20D2F80756B5F868A13BD56FB6657A471835C12D1D977725E5C173C3A84C34ECBDE8281F904F9117882F4460429728AD0CFFFB425014E868A13BD56FB6657D81D268191BDAD3DC09775C1D3CA48CFD2E92EEE58F0EA19BA3038C0950A5D36C8A9BA7A39EFB766EC990983EF5C0329BA3038C0950A5D36D5E8D9A59859A8B699D9E8F3AA2CD04476E601842F6C81A1F004C906525384307823802FF610243DF43C7A68FF6260569E8FC8737B5C2249EC8D19AE6D49635B3BBE47FD9DD3FB59A8DF7F3B2552694A57739F23D657EF2B13377AFFFEAFD26923F8577A6DFFEA7C837C4FEFBD1860714AD6D5ED66289B524E70A05D1297E1BB35872C767BF85DA227C277FBC8AE2E8B374CE26A9E38610E75ECD9A6C639B01B4E70A05D1297E1BBC6867C52282FAC85D9B7C4F32B44FF57C2F2A386D11C4599BD9CCCA9EDD067B1EDA766A37F9254B7 X-B7AD71C0: AC4F5C86D027EB782CDD5689AFBDA7A2BBE337FB72E92315FF39D8DB89857825EFA8BF88FCBFD63CE0852D54D1EC5181386D740E071D760D974A023B2C53FAE4 X-C1DE0DAB: C20DE7B7AB408E4181F030C43753B8186998911F362727C414F749A5E30D975C686AF563A045C75EDC7DDCB8E1494F59FED063B697A107BE9C2B6934AE262D3EE7EAB7254005DCED7532B743992DF240BDC6A1CF3F042BAD6DF99611D93F60EF0417BEADF48D1460699F904B3F4130E343918A1A30D5E7FCCB5012B2E24CD356 X-C8649E89: 4E36BF7865823D7055A7F0CF078B5EC49A30900B95165D3447DF5779098ECEE91253E1D7D317A7AC09826A62E9F50E105CBA35B3D71AB7A62D09E8F3F79BB3CC1D7E09C32AA3244CE46B7D45EE8E2216B9B33587A09E3EC460759606DA2E136AFACE5A9C96DEB163 X-D57D3AED: 3ZO7eAau8CL7WIMRKs4sN3D3tLDjz0dLbV79QFUyzQ2Ujvy7cMT6pYYqY16iZVKkSc3dCLJ7zSJH7+u4VD18S7Vl4ZUrpaVfd2+vE6kuoey4m4VkSEu530nj6fImhcD4MUrOEAnl0W826KZ9Q+tr5ycPtXkTV4k65bRjmOUUP8cvGozZ33TWg5HZplvhhXbhDGzqmQDTd6OAevLeAnq3Ra9uf7zvY2zzsIhlcp/Y7m53TZgf2aB4JOg4gkr2biojWJZv20R+6UgNe/EM9R7lGw== X-Mailru-Sender: 5C3750E245F362008BC1685FEC6306ED3A1085E50F1013E897B8A0341EC83A14CA71892AC981DD5F5105BD0848736F9966FEC6BF5C9C28D97E07721503EA2E00ED97202A5A4E92BF7402F9BA4338D657ED14614B50AE0675 X-Mras: Ok Subject: [Tarantool-patches] [PATCH v4 30/53] sql: introduce mem_set_*_binary() 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_set_*_binary() functions. Functions mem_set_*_binary() clears MEM and sets it to given binary value. Part of #5818 --- src/box/sql/mem.c | 68 ++++++++++++++++++++++++++++++++++--------- src/box/sql/mem.h | 15 ++++++++-- src/box/sql/vdbe.c | 24 ++++++++++----- src/box/sql/vdbeapi.c | 21 +++++++++---- 4 files changed, 100 insertions(+), 28 deletions(-) diff --git a/src/box/sql/mem.c b/src/box/sql/mem.c index 59a378e1b..5ee49cdca 100644 --- a/src/box/sql/mem.c +++ b/src/box/sql/mem.c @@ -410,6 +410,61 @@ mem_copy_string0(struct Mem *mem, const char *value) return 0; } +static inline void +mem_set_const_bin(struct Mem *mem, char *value, uint32_t size, int alloc_type) +{ + assert((alloc_type & (MEM_Static | MEM_Ephem)) != 0); + mem_clear(mem); + mem->z = value; + mem->n = size; + mem->flags = MEM_Blob | alloc_type; + mem->field_type = FIELD_TYPE_VARBINARY; +} + +static inline void +mem_set_dyn_bin(struct Mem *mem, char *value, uint32_t size, int alloc_type) +{ + assert((mem->flags & MEM_Dyn) == 0 || value != mem->z); + assert(mem->szMalloc == 0 || value != mem->zMalloc); + assert(alloc_type == MEM_Dyn || alloc_type == 0); + mem_destroy(mem); + mem->z = value; + mem->n = size; + mem->flags = MEM_Blob | alloc_type; + mem->field_type = FIELD_TYPE_VARBINARY; + if (alloc_type == MEM_Dyn) { + mem->xDel = sql_free; + } else { + mem->xDel = NULL; + mem->zMalloc = mem->z; + mem->szMalloc = sqlDbMallocSize(mem->db, mem->zMalloc); + } +} + +void +mem_set_ephemeral_binary(struct Mem *mem, char *value, uint32_t size) +{ + mem_set_const_bin(mem, value, size, MEM_Ephem); +} + +void +mem_set_static_binary(struct Mem *mem, char *value, uint32_t size) +{ + mem_set_const_bin(mem, value, size, MEM_Static); +} + +void +mem_set_dynamic_binary(struct Mem *mem, char *value, uint32_t size) +{ + mem_set_dyn_bin(mem, value, size, MEM_Dyn); +} + +void +mem_set_allocated_binary(struct Mem *mem, char *value, uint32_t size) +{ + mem_set_dyn_bin(mem, value, size, 0); +} + int mem_copy(struct Mem *to, const struct Mem *from) { @@ -2596,19 +2651,6 @@ mem_is_type_compatible(struct Mem *mem, enum field_type type) return field_mp_plain_type_is_compatible(type, mp_type, true); } -/* Allocate memory for internal VDBE structure on region. */ -int -vdbe_mem_alloc_blob_region(struct Mem *vdbe_mem, uint32_t size) -{ - vdbe_mem->n = size; - vdbe_mem->z = region_alloc(&fiber()->gc, size); - if (vdbe_mem->z == NULL) - return -1; - vdbe_mem->flags = MEM_Ephem | MEM_Blob; - assert(sqlVdbeCheckMemInvariants(vdbe_mem)); - return 0; -} - int sql_vdbemem_finalize(struct Mem *mem, struct func *func) { diff --git a/src/box/sql/mem.h b/src/box/sql/mem.h index 12853606d..3d33fa98b 100644 --- a/src/box/sql/mem.h +++ b/src/box/sql/mem.h @@ -217,6 +217,18 @@ mem_copy_string(struct Mem *mem, const char *value, uint32_t len); int mem_copy_string0(struct Mem *mem, const char *value); +void +mem_set_ephemeral_binary(struct Mem *mem, char *value, uint32_t size); + +void +mem_set_static_binary(struct Mem *mem, char *value, uint32_t size); + +void +mem_set_dynamic_binary(struct Mem *mem, char *value, uint32_t size); + +void +mem_set_allocated_binary(struct Mem *mem, char *value, uint32_t size); + int mem_copy(struct Mem *to, const struct Mem *from); @@ -534,9 +546,6 @@ mem_is_type_compatible(struct Mem *mem, enum field_type type); /** MEM manipulate functions. */ -int -vdbe_mem_alloc_blob_region(struct Mem *vdbe_mem, uint32_t size); - /** * Memory cell mem contains the context of an aggregate function. * This routine calls the finalize method for that function. The diff --git a/src/box/sql/vdbe.c b/src/box/sql/vdbe.c index 341205cc4..6ae77db63 100644 --- a/src/box/sql/vdbe.c +++ b/src/box/sql/vdbe.c @@ -854,8 +854,15 @@ case OP_Null: { /* out2 */ case OP_Blob: { /* out2 */ assert(pOp->p1 <= SQL_MAX_LENGTH); pOut = vdbe_prepare_null_out(p, pOp->p2); - sqlVdbeMemSetStr(pOut, pOp->p4.z, pOp->p1, 0, 0); - if (pOp->p3!=0) { + if (pOp->p3 == 0) { + /* + * TODO: It is possible that vabinary should be stored as + * ephemeral or static depending on value. There is no way to + * determine right now, so it is stored as static. + */ + mem_set_static_binary(pOut, pOp->p4.z, pOp->p1); + } else { + sqlVdbeMemSetStr(pOut, pOp->p4.z, pOp->p1, 0, 0); pOut->flags |= MEM_Subtype; pOut->subtype = pOp->p3; } @@ -2110,9 +2117,7 @@ case OP_MakeRecord: { * sure previously allocated memory has gone. */ mem_destroy(pOut); - pOut->flags = MEM_Blob | MEM_Ephem; - pOut->n = tuple_size; - pOut->z = tuple; + mem_set_ephemeral_binary(pOut, tuple, tuple_size); } assert(sqlVdbeCheckMemInvariants(pOut)); assert(pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor)); @@ -3196,9 +3201,14 @@ case OP_RowData: { } testcase( n==0); - if (vdbe_mem_alloc_blob_region(pOut, n) != 0) + char *buf = region_alloc(&fiber()->gc, n); + if (buf == NULL) { + diag_set(OutOfMemory, n, "region_alloc", "buf"); goto abort_due_to_error; - sqlCursorPayload(pCrsr, 0, n, pOut->z); + } + sqlCursorPayload(pCrsr, 0, n, buf); + mem_set_ephemeral_binary(pOut, buf, n); + assert(sqlVdbeCheckMemInvariants(pOut)); UPDATE_MAX_BLOBSIZE(pOut); REGISTER_TRACE(p, pOp->p2, pOut); break; diff --git a/src/box/sql/vdbeapi.c b/src/box/sql/vdbeapi.c index 5b5e5b0c8..0e51e4809 100644 --- a/src/box/sql/vdbeapi.c +++ b/src/box/sql/vdbeapi.c @@ -111,9 +111,8 @@ sql_metadata_is_full() * The following routines are used by user-defined functions to specify * the function result. * - * The setStrOrError() function calls sqlVdbeMemSetStr() to store the - * result as a string or blob but if the string or blob is too large, it - * then sets the error code. + * The setStrOrError() function sets the result as a string or blob but + * if the string or blob is too large, it then sets the error code. * * The invokeValueDestructor(P,X) routine invokes destructor function X() * on value P is not going to be used and need to be destroyed. @@ -183,7 +182,13 @@ sql_result_blob(sql_context * pCtx, ) { assert(n >= 0); - if (sqlVdbeMemSetStr(pCtx->pOut, z, n, 0, xDel) != 0) + if (xDel == SQL_STATIC) + mem_set_static_binary(pCtx->pOut, (char *)z, n); + else if (xDel == SQL_DYNAMIC) + mem_set_allocated_binary(pCtx->pOut, (char *)z, n); + else if (xDel != SQL_TRANSIENT) + mem_set_dynamic_binary(pCtx->pOut, (char *)z, n); + else if (sqlVdbeMemSetStr(pCtx->pOut, z, n, 0, xDel) != 0) pCtx->is_aborted = true; } @@ -832,7 +837,13 @@ sql_bind_blob(sql_stmt * pStmt, if (zData == NULL) return 0; struct Mem *var = &p->aVar[i - 1]; - if (sqlVdbeMemSetStr(var, zData, nData, 0, xDel) != 0) + if (xDel == SQL_STATIC) + mem_set_static_binary(var, (char *)zData, nData); + else if (xDel == SQL_DYNAMIC) + mem_set_allocated_binary(var, (char *)zData, nData); + else if (xDel != SQL_TRANSIENT) + mem_set_dynamic_binary(var, (char *)zData, nData); + else if (sqlVdbeMemSetStr(var, zData, nData, 0, xDel) != 0) return -1; return sql_bind_type(p, i, "varbinary"); } -- 2.25.1