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 5A6D26EC40; Tue, 21 Sep 2021 14:01:33 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 5A6D26EC40 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1632222093; bh=v1h0e0cM+1QoB/4mVEpBlRA5LH2crKsmNWMckjPItNE=; 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=JCCXABrAVRnxKv5MTzGarfLLpg9xCLRoaXQ66otzyxE1llh8HeeUwnjHcb5g6or7h N9gDq2qQZ7k4HhP9jr+atK9lnnnfRC8yO+4qiaiZ5A76LCS86LTNKDvcMdZS9b/nhn RSk1zAutKZzmxQnnTrDB6mDQTCH+W9PdQLawV4fs= Received: from smtpng1.i.mail.ru (smtpng1.i.mail.ru [94.100.181.251]) (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 57CE06EC45 for ; Tue, 21 Sep 2021 13:59:11 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 57CE06EC45 Received: by smtpng1.m.smailru.net with esmtpa (envelope-from ) id 1mSdUU-00013W-Kp; Tue, 21 Sep 2021 13:59:11 +0300 To: v.shpilevoy@tarantool.org Cc: tarantool-patches@dev.tarantool.org Date: Tue, 21 Sep 2021 13:59:10 +0300 Message-Id: <660d25eecfe9f32dc610f89da493622bba84f13e.1632220375.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-4EC0790: 10 X-7564579A: 646B95376F6C166E X-77F55803: 4F1203BC0FB41BD91AE02D33A9C88A2F5A5BD9D2085A59586CBBC9313A5A610100894C459B0CD1B9110A3C4E67CDEDDE0F4A414083446284994D8960CDEEF0A806A95C38A1BD7B27 X-7FA49CB5: FF5795518A3D127A4AD6D5ED66289B5278DA827A17800CE75AD53DF1D86BACA3EA1F7E6F0F101C67BD4B6F7A4D31EC0BCC500DACC3FED6E28638F802B75D45FF8AA50765F7900637BE899A9B5C1209058638F802B75D45FF36EB9D2243A4F8B5A6FCA7DBDB1FC311F39EFFDF887939037866D6147AF826D82B8E8412360E09C00C3E12C728A03AD9117882F4460429724CE54428C33FAD305F5C1EE8F4F765FCF1175FABE1C0F9B6A471835C12D1D9774AD6D5ED66289B52BA9C0B312567BB23117882F44604297287769387670735201E561CDFBCA1751F618001F51B5FD3F9D2E47CDBA5A96583BA9C0B312567BB231DD303D21008E29813377AFFFEAFD269A417C69337E82CC2E827F84554CEF50127C277FBC8AE2E8BA83251EDC214901ED5E8D9A59859A8B62CFFCC7B69C47339089D37D7C0E48F6C5571747095F342E88FB05168BE4CE3AF X-B7AD71C0: AC4F5C86D027EB782CDD5689AFBDA7A213B5FB47DCBC3458834459D11680B5053604CE418F1C85F7F322153EF839EB7E X-C1DE0DAB: C20DE7B7AB408E4181F030C43753B8186998911F362727C414F749A5E30D975CB382FA991BF7A44C4B365B64EFBBC19264A62E23FE08DC479C2B6934AE262D3EE7EAB7254005DCED7532B743992DF240BDC6A1CF3F042BAD6DF99611D93F60EF520746E264B47742F36E2E0160E5C5538A5251AA20633D2658C12E6D310A6D5391A5E952AFEBA5A4DC48ACC2A39D04F89CDFB48F4795C241BDAD6C7F3747799A X-C8649E89: 4E36BF7865823D7055A7F0CF078B5EC49A30900B95165D34C1E32F4AD4B2486BDDD5A2FC344AADACB0E40E6DB85868DBEF9EDD8192DCD9DBA9419465D9CA38981D7E09C32AA3244C2A8D106EAB4BDDB23B7805C7D75594538894E9C85370243E729B2BEF169E0186 X-D57D3AED: 3ZO7eAau8CL7WIMRKs4sN3D3tLDjz0dLbV79QFUyzQ2Ujvy7cMT6pYYqY16iZVKkSc3dCLJ7zSJH7+u4VD18S7Vl4ZUrpaVfd2+vE6kuoey4m4VkSEu530nj6fImhcD4MUrOEAnl0W826KZ9Q+tr5ycPtXkTV4k65bRjmOUUP8cvGozZ33TWg5HZplvhhXbhDGzqmQDTd6OAevLeAnq3Ra9uf7zvY2zzsIhlcp/Y7m53TZgf2aB4JOg4gkr2biojnvI84oHUDXCg3p7cNilBIg== X-Mailru-Sender: 689FA8AB762F7393C37E3C1AEC41BA5D0A98E5CB86C93D3B92FEEA3F67DB20FA83D72C36FC87018B9F80AB2734326CD2FB559BB5D741EB96352A0ABBE4FDA4210A04DAD6CC59E33667EA787935ED9F1B X-Mras: Ok Subject: [Tarantool-patches] [PATCH v2 05/15] sql: introduce mem_append() 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 function mem_append(). This function appends the given string to the end of the STRING or VARBINARY contained in MEM. In case MEM needs to increase the size of allocated memory, additional memory is allocated in an attempt to reduce the total number of allocations. The main use of this function is to add multiple strings to a single MEM. Needed for #4145 --- src/box/sql/mem.c | 19 +++++++++++++++++++ src/box/sql/mem.h | 8 ++++++++ src/box/sql/vdbeaux.c | 9 ++------- 3 files changed, 29 insertions(+), 7 deletions(-) diff --git a/src/box/sql/mem.c b/src/box/sql/mem.c index cc0fd836b..a9011fc63 100644 --- a/src/box/sql/mem.c +++ b/src/box/sql/mem.c @@ -1953,6 +1953,25 @@ mem_move(struct Mem *to, struct Mem *from) from->zMalloc = NULL; } +int +mem_append(struct Mem *mem, const char *value, uint32_t len) +{ + assert((mem->type & (MEM_TYPE_BIN | MEM_TYPE_STR)) != 0); + int new_size = mem->n + len; + if (((mem->flags & (MEM_Static | MEM_Dyn | MEM_Ephem)) != 0) || + mem->szMalloc < new_size) { + /* + * Force exponential buffer size growth to avoid having to call + * this routine too often. + */ + if (sqlVdbeMemGrow(mem, new_size + mem->n, 1) != 0) + return -1; + } + memcpy(&mem->z[mem->n], value, len); + mem->n = new_size; + return 0; +} + int mem_concat(struct Mem *a, struct Mem *b, struct Mem *result) { diff --git a/src/box/sql/mem.h b/src/box/sql/mem.h index 0da45b8af..54a1e931b 100644 --- a/src/box/sql/mem.h +++ b/src/box/sql/mem.h @@ -597,6 +597,14 @@ mem_copy_as_ephemeral(struct Mem *to, const struct Mem *from); void mem_move(struct Mem *to, struct Mem *from); +/** + * Append the given string to the end of the STRING or VARBINARY contained in + * MEM. In case MEM needs to increase the size of allocated memory, additional + * memory is allocated in an attempt to reduce the total number of allocations. + */ +int +mem_append(struct Mem *mem, const char *value, uint32_t len); + /** * Concatenate strings or binaries from the first and the second MEMs and write * to the result MEM. In case the first MEM or the second MEM is NULL, the diff --git a/src/box/sql/vdbeaux.c b/src/box/sql/vdbeaux.c index b7e44a386..6f13d2b03 100644 --- a/src/box/sql/vdbeaux.c +++ b/src/box/sql/vdbeaux.c @@ -1300,14 +1300,9 @@ sqlVdbeList(Vdbe * p) if (mem_copy_bin(pSub, bin, size) != 0) return -1; } else if (j == nSub) { - struct Mem tmp; - mem_create(&tmp); - uint32_t size = sizeof(SubProgram *); char *bin = (char *)&pOp->p4.pProgram; - mem_set_bin_ephemeral(&tmp, bin, size); - int rc = mem_concat(pSub, &tmp, pSub); - mem_destroy(&tmp); - if (rc != 0) + uint32_t size = sizeof(SubProgram *); + if (mem_append(pSub, bin, size) != 0) return -1; } } -- 2.25.1