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 24B3671219; Tue, 26 Oct 2021 13:37:15 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 24B3671219 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1635244635; bh=sVAQO/2iKETOpIOpsHx4C5eDawGvCVyFkAHQr90/HDQ=; 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=b3gZvaXwu339Tdxlg5l0FgdusSUe02o/YktQNY/49T7dUp9UBDByPUFVqqpXuYUrU rGPeSKL4DDQZIPBqtDhKf+CJBvFJDAmzhZzyYDrHV2PF2JJwBdFJGBRTgCezurDwOy kSB49nbcMUV7GogBJctNMM6iAmoehRZb7wqTOWC0= 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 53009711B8 for ; Tue, 26 Oct 2021 13:34:15 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 53009711B8 Received: by smtpng1.m.smailru.net with esmtpa (envelope-from ) id 1mfJmY-0000dA-6K; Tue, 26 Oct 2021 13:34:14 +0300 To: kyukhin@tarantool.org Cc: tarantool-patches@dev.tarantool.org Date: Tue, 26 Oct 2021 13:34:13 +0300 Message-Id: <2b7d5cf1c1df9853970ae0623f698024dd7e9625.1635244212.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: 4F1203BC0FB41BD9D1D35DBD2D15487E95B7EC5918E2245194B7D4D1C7EDE589182A05F5380850404C72E74CA2D32065A012BC2E66F7DD9DF8ADB1EDBC773D31E18AA4EEC930F6D4 X-7FA49CB5: FF5795518A3D127A4AD6D5ED66289B5278DA827A17800CE7227E4400968B082FEA1F7E6F0F101C67BD4B6F7A4D31EC0BCC500DACC3FED6E28638F802B75D45FF8AA50765F7900637BC9F889FE9D346E88638F802B75D45FF36EB9D2243A4F8B5A6FCA7DBDB1FC311F39EFFDF887939037866D6147AF826D89523B9FEE5FE6F1ADBAC8DF9AE0CA489117882F4460429724CE54428C33FAD305F5C1EE8F4F765FC2EE5AD8F952D28FBA471835C12D1D9774AD6D5ED66289B52BA9C0B312567BB23117882F446042972877693876707352026055571C92BF10FE5D25F19253116ADD2E47CDBA5A96583BA9C0B312567BB231DD303D21008E29813377AFFFEAFD269A417C69337E82CC2E827F84554CEF50127C277FBC8AE2E8BA83251EDC214901ED5E8D9A59859A8B62CFFCC7B69C47339089D37D7C0E48F6C5571747095F342E88FB05168BE4CE3AF X-C1DE0DAB: C20DE7B7AB408E4181F030C43753B8186998911F362727C414F749A5E30D975C2749EEF4D85F8CCAFBF912A4B553CE961C74AE402CFA78F69C2B6934AE262D3EE7EAB7254005DCED7532B743992DF240BDC6A1CF3F042BAD6DF99611D93F60EF166FC1BB2721D293DC48ACC2A39D04F89CDFB48F4795C241BDAD6C7F3747799A X-C8649E89: 4E36BF7865823D7055A7F0CF078B5EC49A30900B95165D34C1E32F4AD4B2486B3521888197A50D4166E23B1DAEE870397639B905D80B49973D7414B0CD56E2861D7E09C32AA3244C9943F99BCBB5CC08BDEF4E17F0C28ACD55E75C8D0ED9F6EE729B2BEF169E0186 X-D57D3AED: 3ZO7eAau8CL7WIMRKs4sN3D3tLDjz0dLbV79QFUyzQ2Ujvy7cMT6pYYqY16iZVKkSc3dCLJ7zSJH7+u4VD18S7Vl4ZUrpaVfd2+vE6kuoey4m4VkSEu530nj6fImhcD4MUrOEAnl0W826KZ9Q+tr5ycPtXkTV4k65bRjmOUUP8cvGozZ33TWg5HZplvhhXbhDGzqmQDTd6OAevLeAnq3Ra9uf7zvY2zzsIhlcp/Y7m53TZgf2aB4JOg4gkr2biojNvH0qP3qm73D+RA8DLzr/Q== X-Mailru-Sender: 689FA8AB762F7393C37E3C1AEC41BA5D813D1E1E2AE811F8554F73061560F59483D72C36FC87018B9F80AB2734326CD2FB559BB5D741EB96352A0ABBE4FDA4210A04DAD6CC59E33667EA787935ED9F1B X-Mras: Ok Subject: [Tarantool-patches] [PATCH v4 06/16] 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 the mem_append() function. This function appends the specified string to the end of the STRING or VARBINARY contained in MEM. In case MEM needs to increase the size of allocated memory, extra memory is allocated in an attempt to reduce the total number of allocations. Needed for #4145 --- src/box/sql/mem.c | 21 +++++++++++++++++++++ src/box/sql/mem.h | 8 ++++++++ src/box/sql/vdbeaux.c | 9 ++------- 3 files changed, 31 insertions(+), 7 deletions(-) diff --git a/src/box/sql/mem.c b/src/box/sql/mem.c index 99ac4d8e7..44ac5ad1e 100644 --- a/src/box/sql/mem.c +++ b/src/box/sql/mem.c @@ -1929,6 +1929,27 @@ 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); + if (len == 0) + return 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(const struct Mem *a, const struct Mem *b, struct Mem *result) { diff --git a/src/box/sql/mem.h b/src/box/sql/mem.h index 1574da22d..b567aea50 100644 --- a/src/box/sql/mem.h +++ b/src/box/sql/mem.h @@ -582,6 +582,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 4c2bd11ba..3015760e1 100644 --- a/src/box/sql/vdbeaux.c +++ b/src/box/sql/vdbeaux.c @@ -1294,14 +1294,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