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 9B4586BD38; Tue, 23 Mar 2021 12:48:48 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 9B4586BD38 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1616492928; bh=WbaY/3mdyQEVWkcyE0b5+PIBxuvcN3aJmS+9O37bkBc=; 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=DLr4CEvK4re9tqoBkccLF1SrQl8nGLrxQLqAAI1o6nJu6fCj7IcAw20my6bdkB8az E0XUaGrHURoN/CdupH0tItwFTDRs8vt0w4fUAS1jyJCwhYYd2uK/zJTtSgdXr6fTWc dBHbmhX0LeU0I1dbwYGlFcBzA0k/QpUdiNK3vcNE= Received: from smtp49.i.mail.ru (smtp49.i.mail.ru [94.100.177.109]) (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 A251568F51 for ; Tue, 23 Mar 2021 12:35:56 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org A251568F51 Received: by smtp49.i.mail.ru with esmtpa (envelope-from ) id 1lOdS7-0003PY-QU; Tue, 23 Mar 2021 12:35:56 +0300 To: v.shpilevoy@tarantool.org, tsafin@tarantool.org Cc: tarantool-patches@dev.tarantool.org Date: Tue, 23 Mar 2021 12:35:55 +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: B8F34718100C35BD X-77F55803: 4F1203BC0FB41BD95D6E7CC48CB1F5F10D3016C09B407F8B88411E9FEB481E8E182A05F5380850401CC20EA2EBCC7CED1CE955043EC9517217625C698CAEA67A9346A8636164D4A5 X-7FA49CB5: FF5795518A3D127A4AD6D5ED66289B5278DA827A17800CE72424932217190DC2EA1F7E6F0F101C67BD4B6F7A4D31EC0BCC500DACC3FED6E28638F802B75D45FF8AA50765F7900637663000D94094A5D68638F802B75D45FF914D58D5BE9E6BC131B5C99E7648C95C686AF563A045C75EF105D63ED118D24B8346E93B55254A46A471835C12D1D9774AD6D5ED66289B5278DA827A17800CE7820CF4CC0E318EFB9FA2833FD35BB23D2EF20D2F80756B5F868A13BD56FB6657A471835C12D1D977725E5C173C3A84C3BCA4DA3BE1BC1572CC7F00164DA146DA6F5DAA56C3B73B237318B6A418E8EAB8D32BA5DBAC0009BE9E8FC8737B5C2249ADE67BC57278FB1076E601842F6C81A12EF20D2F80756B5F7E9C4E3C761E06A776E601842F6C81A127C277FBC8AE2E8BE78E946E591156C53AA81AA40904B5D9DBF02ECDB25306B2201CA6A4E26CD07C3BBE47FD9DD3FB595F5C1EE8F4F765FC72CEEB2601E22B091A620F70A64A45A99449624AB7ADAF3735872C767BF85DA29E625A9149C048EE3F735096452955E34DCCFF368A6260CF4AD6D5ED66289B524E70A05D1297E1BB35872C767BF85DA227C277FBC8AE2E8BCFC11AAA4A30FD1F75ECD9A6C639B01B4E70A05D1297E1BBC6867C52282FAC85D9B7C4F32B44FF57D4B828FA1BC0F1ACBD9CCCA9EDD067B1EDA766A37F9254B7 X-B7AD71C0: AC4F5C86D027EB782CDD5689AFBDA7A24A6D60772A99906F8E1CD14B953EB46DA634A1AFB0E698A4355D89D7DBCDD132 X-C1DE0DAB: C20DE7B7AB408E4181F030C43753B8186998911F362727C414F749A5E30D975C686AF563A045C75EF105D63ED118D24B8346E93B55254A469C2B6934AE262D3EE7EAB7254005DCED7532B743992DF240BDC6A1CF3F042BAD6DF99611D93F60EF0417BEADF48D1460699F904B3F4130E343918A1A30D5E7FCCB5012B2E24CD356 X-C8649E89: 4E36BF7865823D7055A7F0CF078B5EC49A30900B95165D34AC6E62257D6CD1C941FB9CD0F47B7ACD1722C03BFDDA53916DD4D952849FEA112C98BBFB0CD977F21D7E09C32AA3244C018F2FA87E37933939B1A355D51692B3408A6A02710B7304FACE5A9C96DEB163 X-D57D3AED: 3ZO7eAau8CL7WIMRKs4sN3D3tLDjz0dLbV79QFUyzQ2Ujvy7cMT6pYYqY16iZVKkSc3dCLJ7zSJH7+u4VD18S7Vl4ZUrpaVfd2+vE6kuoey4m4VkSEu530nj6fImhcD4MUrOEAnl0W826KZ9Q+tr5ycPtXkTV4k65bRjmOUUP8cvGozZ33TWg5HZplvhhXbhDGzqmQDTd6OAevLeAnq3Ra9uf7zvY2zzsIhlcp/Y7m53TZgf2aB4JOg4gkr2biojWJZv20R+6UjXaQKo4DEowA== X-Mailru-Sender: 5C3750E245F362008BC1685FEC6306EDC9A266263AAE4C781CE955043EC95172B67A942A63B4089A5105BD0848736F9966FEC6BF5C9C28D97E07721503EA2E00ED97202A5A4E92BF7402F9BA4338D657ED14614B50AE0675 X-Mras: Ok Subject: [Tarantool-patches] [PATCH v4 28/53] sql: refactor mem_set_*_string() 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_*_string() functions. Functions mem_set_*_string() clears MEM and sets it to given string value. Part of #5818 --- src/box/sql/mem.c | 111 +++++++++++++++++++++++++++++++++--------- src/box/sql/mem.h | 26 +++++++++- src/box/sql/sqlInt.h | 4 -- src/box/sql/vdbe.c | 5 +- src/box/sql/vdbeapi.c | 47 ++++++++++++++---- src/box/sql/vdbeaux.c | 45 +++++++---------- 6 files changed, 171 insertions(+), 67 deletions(-) diff --git a/src/box/sql/mem.c b/src/box/sql/mem.c index 47a71fb30..91ef7f3c8 100644 --- a/src/box/sql/mem.c +++ b/src/box/sql/mem.c @@ -301,6 +301,89 @@ mem_set_double(struct Mem *mem, double value) mem->flags = MEM_Real; } +static inline void +mem_set_const_string(struct Mem *mem, char *value, uint32_t len, int alloc_type) +{ + assert((alloc_type & (MEM_Static | MEM_Ephem)) != 0); + mem_clear(mem); + mem->z = value; + mem->n = len; + mem->flags = MEM_Str | alloc_type; + mem->field_type = FIELD_TYPE_STRING; +} + +static inline void +mem_set_dyn_string(struct Mem *mem, char *value, uint32_t len, 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 = len; + mem->flags = MEM_Str | alloc_type; + mem->field_type = FIELD_TYPE_STRING; + 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_string(struct Mem *mem, char *value, uint32_t len) +{ + mem_set_const_string(mem, value, len, MEM_Ephem); +} + +void +mem_set_static_string(struct Mem *mem, char *value, uint32_t len) +{ + mem_set_const_string(mem, value, len, MEM_Static); +} + +void +mem_set_dynamic_string(struct Mem *mem, char *value, uint32_t len) +{ + mem_set_dyn_string(mem, value, len, MEM_Dyn); +} + +void +mem_set_allocated_string(struct Mem *mem, char *value, uint32_t len) +{ + mem_set_dyn_string(mem, value, len, 0); +} + +void +mem_set_ephemeral_string0(struct Mem *mem, char *value) +{ + mem_set_const_string(mem, value, strlen(value), MEM_Ephem); + mem->flags |= MEM_Term; +} + +void +mem_set_static_string0(struct Mem *mem, char *value) +{ + mem_set_const_string(mem, value, strlen(value), MEM_Static); + mem->flags |= MEM_Term; +} + +void +mem_set_dynamic_string0(struct Mem *mem, char *value) +{ + mem_set_dyn_string(mem, value, strlen(value), MEM_Dyn); + mem->flags |= MEM_Term; +} + +void +mem_set_allocated_string0(struct Mem *mem, char *value) +{ + mem_set_dyn_string(mem, value, strlen(value), 0); + mem->flags |= MEM_Term; +} + int mem_copy(struct Mem *to, const struct Mem *from) { @@ -2039,20 +2122,6 @@ sqlVdbeMemSetZeroBlob(Mem * pMem, int n) pMem->z = 0; } -/* - * Change the string value of an sql_value object - */ -void -sqlValueSetStr(sql_value * v, /* Value to be set */ - int n, /* Length of string z */ - const void *z, /* Text of the new string */ - void (*xDel) (void *) /* Destructor for the string */ - ) -{ - if (v) - sqlVdbeMemSetStr((Mem *) v, z, n, 1, xDel); -} - /* * Free an sql_value object */ @@ -2780,13 +2849,9 @@ vdbe_decode_msgpack_into_ephemeral_mem(const char *buf, struct Mem *mem, break; } case MP_STR: { - /* XXX u32->int */ - mem->n = (int) mp_decode_strl(&buf); - mem->flags = MEM_Str | MEM_Ephem; - mem->field_type = FIELD_TYPE_STRING; -install_blob: - mem->z = (char *)buf; - buf += mem->n; + uint32_t len = mp_decode_strl(&buf); + mem_set_ephemeral_string(mem, (char *)buf, len); + buf += len; break; } case MP_BIN: { @@ -2794,7 +2859,9 @@ install_blob: mem->n = (int) mp_decode_binl(&buf); mem->flags = MEM_Blob | MEM_Ephem; mem->field_type = FIELD_TYPE_VARBINARY; - goto install_blob; + mem->z = (char *)buf; + buf += mem->n; + break; } case MP_FLOAT: { mem->u.r = mp_decode_float(&buf); diff --git a/src/box/sql/mem.h b/src/box/sql/mem.h index 49b4e4b1a..70b5350f6 100644 --- a/src/box/sql/mem.h +++ b/src/box/sql/mem.h @@ -182,6 +182,30 @@ mem_set_boolean(struct Mem *mem, bool value); void mem_set_double(struct Mem *mem, double value); +void +mem_set_ephemeral_string(struct Mem *mem, char *value, uint32_t len); + +void +mem_set_static_string(struct Mem *mem, char *value, uint32_t len); + +void +mem_set_dynamic_string(struct Mem *mem, char *value, uint32_t len); + +void +mem_set_allocated_string(struct Mem *mem, char *value, uint32_t len); + +void +mem_set_ephemeral_string0(struct Mem *mem, char *value); + +void +mem_set_static_string0(struct Mem *mem, char *value); + +void +mem_set_dynamic_string0(struct Mem *mem, char *value); + +void +mem_set_allocated_string0(struct Mem *mem, char *value); + /** * 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 @@ -426,8 +450,6 @@ int sqlVdbeMemSetStr(struct Mem *, const char *, int, u8, void (*)(void *)); void sqlVdbeMemSetZeroBlob(struct Mem *, int); -void sqlValueSetStr(struct Mem *, int, const void *, - void (*)(void *)); void sqlValueFree(struct Mem *); struct Mem *sqlValueNew(struct sql *); diff --git a/src/box/sql/sqlInt.h b/src/box/sql/sqlInt.h index 01c0cc5a1..f7ead1343 100644 --- a/src/box/sql/sqlInt.h +++ b/src/box/sql/sqlInt.h @@ -641,10 +641,6 @@ sql_bind_uint64(struct sql_stmt *stmt, int i, uint64_t value); int sql_bind_null(sql_stmt *, int); -int -sql_bind_text(sql_stmt *, int, const char *, int, - void (*)(void *)); - int sql_bind_text64(sql_stmt *, int, const char *, sql_uint64, void (*)(void *)); diff --git a/src/box/sql/vdbe.c b/src/box/sql/vdbe.c index a7b7c5011..341205cc4 100644 --- a/src/box/sql/vdbe.c +++ b/src/box/sql/vdbe.c @@ -809,9 +809,8 @@ case OP_String8: { /* same as TK_STRING, out2 */ case OP_String: { /* out2 */ assert(pOp->p4.z!=0); pOut = vdbe_prepare_null_out(p, pOp->p2); - pOut->flags = MEM_Str|MEM_Static|MEM_Term; - pOut->z = pOp->p4.z; - pOut->n = pOp->p1; + assert(strlen(pOp->p4.z) == (size_t)pOp->p1); + mem_set_static_string0(pOut, pOp->p4.z); UPDATE_MAX_BLOBSIZE(pOut); break; } diff --git a/src/box/sql/vdbeapi.c b/src/box/sql/vdbeapi.c index d76efcfcf..5fc7b4e44 100644 --- a/src/box/sql/vdbeapi.c +++ b/src/box/sql/vdbeapi.c @@ -125,6 +125,27 @@ setResultStrOrError(sql_context * pCtx, /* Function context */ void (*xDel) (void *) /* Destructor function */ ) { + if (xDel == SQL_STATIC) { + if (n < 0) + mem_set_static_string0(pCtx->pOut, (char *)z); + else + mem_set_static_string(pCtx->pOut, (char *)z, n); + return; + } + if (xDel == SQL_DYNAMIC) { + if (n < 0) + mem_set_allocated_string0(pCtx->pOut, (char *)z); + else + mem_set_allocated_string(pCtx->pOut, (char *)z, n); + return; + } + if (xDel != SQL_TRANSIENT) { + if (n < 0) + mem_set_dynamic_string0(pCtx->pOut, (char *)z); + else + mem_set_dynamic_string(pCtx->pOut, (char *)z, n); + return; + } if (sqlVdbeMemSetStr(pCtx->pOut, z, n, 1, xDel) != 0) pCtx->is_aborted = true; } @@ -762,8 +783,24 @@ bindText(sql_stmt * pStmt, /* The statement to bind against */ if (zData == NULL) return 0; pVar = &p->aVar[i - 1]; - if (sqlVdbeMemSetStr(pVar, zData, nData, 1, xDel) != 0) + if (xDel == SQL_STATIC) { + if (nData < 0) + mem_set_static_string0(pVar, (char *)zData); + else + mem_set_static_string(pVar, (char *)zData, nData); + } else if (xDel == SQL_DYNAMIC) { + if (nData < 0) + mem_set_allocated_string0(pVar, (char *)zData); + else + mem_set_allocated_string(pVar, (char *)zData, nData); + } else if (xDel != SQL_TRANSIENT) { + if (nData < 0) + mem_set_dynamic_string0(pVar, (char *)zData); + else + mem_set_dynamic_string(pVar, (char *)zData, nData); + } else if (sqlVdbeMemSetStr(pVar, zData, nData, 1, xDel) != 0) { return -1; + } return sql_bind_type(p, i, "text"); } @@ -876,14 +913,6 @@ sql_bind_ptr(struct sql_stmt *stmt, int i, void *ptr) return rc; } -int -sql_bind_text(sql_stmt * pStmt, - int i, const char *zData, int nData, void (*xDel) (void *) - ) -{ - return bindText(pStmt, i, zData, nData, xDel); -} - int sql_bind_text64(sql_stmt * pStmt, int i, diff --git a/src/box/sql/vdbeaux.c b/src/box/sql/vdbeaux.c index c4cf1109f..86da1449c 100644 --- a/src/box/sql/vdbeaux.c +++ b/src/box/sql/vdbeaux.c @@ -1295,10 +1295,8 @@ sqlVdbeList(Vdbe * p) pMem++; - pMem->flags = MEM_Static | MEM_Str | MEM_Term; - pMem->z = (char *)sqlOpcodeName(pOp->opcode); /* Opcode */ - assert(pMem->z != 0); - pMem->n = sqlStrlen30(pMem->z); + char *value = (char *)sqlOpcodeName(pOp->opcode); + mem_set_static_string0(pMem, value); pMem++; /* When an OP_Program opcode is encounter (the only opcode that has @@ -1333,41 +1331,34 @@ sqlVdbeList(Vdbe * p) mem_set_integer(pMem, pOp->p3, pOp->p3 < 0); pMem++; - if (sqlVdbeMemClearAndResize(pMem, 256)) { - assert(p->db->mallocFailed); + char *buf = sqlDbMallocRaw(sql_get(), 256); + if (buf == NULL) return -1; - } - pMem->flags = MEM_Str | MEM_Term; - zP4 = displayP4(pOp, pMem->z, pMem->szMalloc); - - if (zP4 != pMem->z) { - pMem->n = 0; - sqlVdbeMemSetStr(pMem, zP4, -1, 1, 0); + zP4 = displayP4(pOp, buf, sqlDbMallocSize(sql_get(), buf)); + if (zP4 != buf) { + sqlDbFree(sql_get(), buf); + mem_set_ephemeral_string0(pMem, zP4); } else { - assert(pMem->z != 0); - pMem->n = sqlStrlen30(pMem->z); + mem_set_allocated_string0(pMem, zP4); } pMem++; if (p->explain == 1) { - if (sqlVdbeMemClearAndResize(pMem, 4)) { - assert(p->db->mallocFailed); + buf = sqlDbMallocRaw(sql_get(), 4); + if (buf == NULL) return -1; - } - pMem->flags = MEM_Str | MEM_Term; - pMem->n = 2; - sql_snprintf(3, pMem->z, "%.2x", pOp->p5); /* P5 */ + sql_snprintf(3, buf, "%.2x", pOp->p5); + mem_set_allocated_string0(pMem, buf); pMem++; #ifdef SQL_ENABLE_EXPLAIN_COMMENTS - if (sqlVdbeMemClearAndResize(pMem, 500)) { - assert(p->db->mallocFailed); + buf = sqlDbMallocRaw(sql_get(), 500); + if (buf == NULL) return -1; - } - pMem->flags = MEM_Str | MEM_Term; - pMem->n = displayComment(pOp, zP4, pMem->z, 500); + displayComment(pOp, zP4, buf, 500); + mem_set_allocated_string0(pMem, buf); #else - pMem->flags = MEM_Null; /* Comment */ + mem_set_null(pMem); #endif } -- 2.25.1