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 AABDB63340; Mon, 1 Feb 2021 11:15:21 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org AABDB63340 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1612167321; bh=XNGKx3AQyR+g+qp3XpFQgBXUMK2oYr7dnyaI27TSMMU=; 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=k8VqeGLyoQHgLxUpgSsscWxd9FZZFW9n+vCV8naH4AEU+/MWZXVESLnTPG0t17sdw IQJfdSRMS1XMuUG1HJcuJFczPD3kGaa0gYTB5tvUq5rum+jBgFFp4u/4sh7tUwaTjC EKkZQBWTN2d5ce7ocK0ITpCshUV2j6vacKGX25TA= Received: from smtp36.i.mail.ru (smtp36.i.mail.ru [94.100.177.96]) (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 7132563340 for ; Mon, 1 Feb 2021 11:14:51 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 7132563340 Received: by smtp36.i.mail.ru with esmtpa (envelope-from ) id 1l6UME-0001yY-Ks; Mon, 01 Feb 2021 11:14:51 +0300 To: s.ostanevich@corp.mail.ru, tsafin@tarantool.org Cc: tarantool-patches@dev.tarantool.org Date: Mon, 1 Feb 2021 11:14:50 +0300 Message-Id: <53e58dd59bc6eed361dbb11a3fcdc7260777499c.1612166870.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: 4F1203BC0FB41BD953AC099BC0052A9CD238BCF93DF23716D1711D0DDC4F5AC2182A05F53808504008ED732A1503C0DDE0C1C6927E50AC39D72F066A02E18C8F5860B7A7330E87D5 X-7FA49CB5: FF5795518A3D127A4AD6D5ED66289B5278DA827A17800CE7FBB2043146276655EA1F7E6F0F101C67BD4B6F7A4D31EC0BCC500DACC3FED6E28638F802B75D45FF8AA50765F79006370CE92FB8C11ED3D88638F802B75D45FF5571747095F342E8C7A0BC55FA0FE5FC632E30DA10E3D6BD8A6C3B580C57A7E4A998CBEAF612AEC0389733CBF5DBD5E913377AFFFEAFD269176DF2183F8FC7C045A75973B56231AD8941B15DA834481FCF19DD082D7633A0EF3E4896CB9E6436389733CBF5DBD5E9D5E8D9A59859A8B6A50BD5087FBFCDAACC7F00164DA146DA6F5DAA56C3B73B237318B6A418E8EAB8D32BA5DBAC0009BE9E8FC8737B5C2249F4FEE3E5751A897976E601842F6C81A12EF20D2F80756B5F7E9C4E3C761E06A776E601842F6C81A127C277FBC8AE2E8B4AFA663C125B16563AA81AA40904B5D9DBF02ECDB25306B2B25CBF701D1BE8734AD6D5ED66289B5278DA827A17800CE7C7AF01494741750667F23339F89546C5A8DF7F3B2552694A6FED454B719173D6725E5C173C3A84C3D703628AB378667775ECD9A6C639B01B4E70A05D1297E1BBC6867C52282FAC85D9B7C4F32B44FF57D4B828FA1BC0F1ACBD9CCCA9EDD067B1EDA766A37F9254B7 X-C1DE0DAB: C20DE7B7AB408E4181F030C43753B8186998911F362727C414F749A5E30D975C6FA9FB90A9CF7018BD3EB5896FFABD47E67958134E39594E9C2B6934AE262D3EE7EAB7254005DCED7532B743992DF240BDC6A1CF3F042BAD6DF99611D93F60EF31C0090ACECF247D699F904B3F4130E343918A1A30D5E7FCCB5012B2E24CD356 X-C8649E89: 4E36BF7865823D7055A7F0CF078B5EC49A30900B95165D3451BBE684D17D72211E03D9369093EE094455F92DBA65F3C833ACC4B5773D7F351D63ECD4D7C3261F1D7E09C32AA3244C88E342BE8DBBEF6B593822F37B1ACDCEB018FE5BB746DCD1FACE5A9C96DEB163 X-D57D3AED: 3ZO7eAau8CL7WIMRKs4sN3D3tLDjz0dLbV79QFUyzQ2Ujvy7cMT6pYYqY16iZVKkSc3dCLJ7zSJH7+u4VD18S7Vl4ZUrpaVfd2+vE6kuoey4m4VkSEu530nj6fImhcD4MUrOEAnl0W826KZ9Q+tr5ycPtXkTV4k65bRjmOUUP8cvGozZ33TWg5HZplvhhXbhDGzqmQDTd6OAevLeAnq3Ra9uf7zvY2zzsIhlcp/Y7m53TZgf2aB4JOg4gkr2biojyKiJYJ15DtKRJj9JYW1yLw== X-Mailru-Sender: 5C3750E245F362008BC1685FEC6306EDA0C571F8F7CF3430E0C1C6927E50AC392F2E01304D987C7F5105BD0848736F9966FEC6BF5C9C28D97E07721503EA2E00ED97202A5A4E92BF7402F9BA4338D657ED14614B50AE0675 X-Mras: Ok Subject: [Tarantool-patches] [PATCH v1 01/10] sql: introduce mem_set_*() functions 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 add some new mem_set_*() functions. These functions will be used to change value of a MEM. Also, function mem_init() is introduced. This function is used to initialize a newly created MEM. Part of #4470 --- src/box/sql/vdbeInt.h | 101 ++++++++++++++++++++++++++++++++++++++++++ src/box/sql/vdbemem.c | 101 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 202 insertions(+) diff --git a/src/box/sql/vdbeInt.h b/src/box/sql/vdbeInt.h index 7205f1af3..a24fbfc0e 100644 --- a/src/box/sql/vdbeInt.h +++ b/src/box/sql/vdbeInt.h @@ -484,6 +484,30 @@ void sqlVdbeMemMove(Mem *, Mem *); int sqlVdbeMemNulTerminate(Mem *); int sqlVdbeMemSetStr(Mem *, const char *, int, u8, void (*)(void *)); +/** + * Initialize a new mem. After initializing the mem will hold a NULL value. + * + * @param mem VDBE memory register to initialize. + */ +void +mem_init(struct Mem *mem); + +/** + * Set VDBE memory register as NULL. + * + * @param mem VDBE memory register to update. + */ +void +mem_set_null(struct Mem *mem); + +/** + * Set VDBE memory register as Undefined. MEM is not cleared prior to that. + * + * @param mem VDBE memory register to update. + */ +void +mem_set_undefined(struct Mem *mem); + void mem_set_bool(struct Mem *mem, bool value); @@ -495,6 +519,15 @@ mem_set_bool(struct Mem *mem, bool value); void mem_set_ptr(struct Mem *mem, void *ptr); +/** + * Set VDBE memory register as frame. + * + * @param mem VDBE memory register to update. + * @param frame Frame to set. + */ +void +mem_set_frame(struct Mem *mem, struct VdbeFrame *frame); + /** * Set integer value. Depending on its sign MEM_Int (in case * of negative value) or MEM_UInt flag is set. @@ -517,6 +550,74 @@ mem_set_int(struct Mem *mem, int64_t value, bool is_neg); void mem_set_double(struct Mem *mem, double value); +/** + * Set VDBE memory register as string. Length and way of allocation is also + * given. If is_null_terminated is true then sizeof(value) should be len + 1, + * otherwise sizeof(value) == len. If alloc_type is either MEM_Static or + * MEM_Ephem, then no more allocation, deallocation, or copying is required. + * In case it is MEM_Dyn, no need to allocate and copy, but MEM should be freed + * each time MEM changes or destroyed. If it is 0, the entire string should be + * copied into newly allocated memory, which should be freed when the memory is + * destroyed. However, in this case, there is no need to free memory every time + * the MEM changes. We only need to free memory if this MEM is given a new + * string or binary string with alloc_type 0. + * + * @param mem VDBE memory register to update. + * @param value String to set. + * @param len Length of the string. + * @param alloc_type Type of allocation of binary string. + * @param is_null_terminated True if string is NULL-terminated. + */ +int +mem_set_str(struct Mem *mem, char *value, uint32_t len, int alloc_type, + bool is_null_terminated); + +/** + * Set VDBE memory register as binary. Length and way of allocation is also + * given. If is_zero is true then binary received from this MEM may have length + * more than given size, however this is done outside of this MEM. If alloc_type + * is either MEM_Static or MEM_Ephem, then no more allocation, deallocation, or + * copying is required. In case it is MEM_Dyn, no need to allocate and copy, but + * MEM should be freed each time MEM changes or destroyed. If it is 0, the + * entire binary string should be copied into newly allocated memory, which + * should be freed when the memory is destroyed. However, in this case, there is + * no need to free memory every time the MEM changes. We only need to free + * memory if this MEM is given a new string or binary string with alloc_type 0. + * + * @param mem VDBE memory register to update. + * @param value Binary string to set. + * @param len Length of the string. + * @param alloc_type Type of allocation of binary string. + * @param is_zero True if binary string may be expanded with zeroes at the end. + */ +int +mem_set_bin(struct Mem *mem, char *value, uint32_t size, int alloc_type, + bool is_zero); + +/** + * Set VDBE memory register as MAP. See @a mem_set_bin with is_zero = 0 for + * more. + * + * @param mem VDBE memory register to update. + * @param value Binary string that contains msgpack with type MP_MAP to set. + * @param len Length of the binary string. + * @param alloc_type Type of allocation of binary string. + */ +int +mem_set_map(struct Mem *mem, char *value, uint32_t size, int alloc_type); + +/** + * Set VDBE memory register as ARRAY. See @a mem_set_bin with is_zero = 0 for + * more. + * + * @param mem VDBE memory register to update. + * @param value Binary string that contains msgpack with type MP_ARRAY to set. + * @param len Length of the binary string. + * @param alloc_type Type of allocation of binary string. + */ +int +mem_set_array(struct Mem *mem, char *value, uint32_t size, int alloc_type); + void sqlVdbeMemInit(Mem *, sql *, u32); void sqlVdbeMemSetNull(Mem *); void sqlVdbeMemSetZeroBlob(Mem *, int); diff --git a/src/box/sql/vdbemem.c b/src/box/sql/vdbemem.c index 1101f7205..186aebe03 100644 --- a/src/box/sql/vdbemem.c +++ b/src/box/sql/vdbemem.c @@ -799,6 +799,29 @@ sqlValueSetNull(sql_value * p) sqlVdbeMemSetNull((Mem *) p); } +void +mem_init(struct Mem *mem) +{ + memset(mem, 0, sizeof(*mem)); + mem->db = sql_get(); + mem->flags = MEM_Null; + mem->field_type = field_type_MAX; +} + +void +mem_set_null(struct Mem *mem) +{ + sqlVdbeMemSetNull(mem); + mem->field_type = field_type_MAX; +} + +void +mem_set_undefined(struct Mem *mem) +{ + mem->flags = MEM_Undefined; + mem->field_type = field_type_MAX; +} + void mem_set_ptr(struct Mem *mem, void *ptr) { @@ -807,6 +830,15 @@ mem_set_ptr(struct Mem *mem, void *ptr) mem->u.p = ptr; } +void +mem_set_frame(struct Mem *mem, struct VdbeFrame *frame) +{ + sqlVdbeMemSetNull(mem); + mem->u.pFrame = frame; + mem->flags = MEM_Frame; + mem->field_type = field_type_MAX; +} + /* * Delete any previous value and set the value to be a BLOB of length * n containing all zeros. @@ -880,6 +912,75 @@ mem_set_double(struct Mem *mem, double value) mem->field_type = FIELD_TYPE_DOUBLE; } +int +mem_set_str(struct Mem *mem, char *value, uint32_t len, int alloc_type, + bool is_null_terminated) +{ + sqlVdbeMemSetNull(mem); + if (alloc_type != 0) { + mem->z = value; + if (alloc_type == MEM_Dyn) + mem->xDel = SQL_DYNAMIC; + } else { + uint32_t size = is_null_terminated ? len + 1 : len; + if (sqlVdbeMemClearAndResize(mem, size) != 0) + return -1; + memcpy(mem->z, value, size); + } + mem->n = len; + mem->flags = MEM_Str | alloc_type; + if (is_null_terminated) + mem->flags |= MEM_Term; + mem->field_type = FIELD_TYPE_STRING; + return 0; +} + +int +mem_set_bin(struct Mem *mem, char *value, uint32_t size, int alloc_type, + bool is_zero) +{ + sqlVdbeMemSetNull(mem); + if (alloc_type != 0) { + mem->z = value; + if (alloc_type == MEM_Dyn) + mem->xDel = SQL_DYNAMIC; + } else { + if (sqlVdbeMemClearAndResize(mem, size) != 0) + return -1; + memcpy(mem->z, value, size); + } + mem->n = size; + mem->flags = MEM_Blob | alloc_type; + if (is_zero) + mem->flags |= MEM_Zero; + mem->field_type = FIELD_TYPE_VARBINARY; + return 0; +} + +int +mem_set_map(struct Mem *mem, char *value, uint32_t size, int alloc_type) +{ + assert(mp_typeof(*value) == MP_MAP); + if (mem_set_bin(mem, value, size, alloc_type, false) != 0) + return -1; + mem->subtype = SQL_SUBTYPE_MSGPACK; + mem->flags |= MEM_Subtype; + mem->field_type = FIELD_TYPE_MAP; + return 0; +} + +int +mem_set_array(struct Mem *mem, char *value, uint32_t size, int alloc_type) +{ + assert(mp_typeof(*value) == MP_ARRAY); + if (mem_set_bin(mem, value, size, alloc_type, false) != 0) + return -1; + mem->subtype = SQL_SUBTYPE_MSGPACK; + mem->flags |= MEM_Subtype; + mem->field_type = FIELD_TYPE_ARRAY; + return 0; +} + /* * Return true if the Mem object contains a TEXT or BLOB that is * too large - whose size exceeds SQL_MAX_LENGTH. -- 2.25.1