[Tarantool-patches] [PATCH v4 31/53] sql: introduce mem_copy_binary()
imeevma at tarantool.org
imeevma at tarantool.org
Tue Mar 23 12:36:02 MSK 2021
This patch introduces mem_copy_binary() function. Function
mem_copy_binary() clears MEM, allocates enough memory and copies given
binary to allocated memory.
Part of #5818
---
src/box/sql/mem.c | 14 ++++++++++++++
src/box/sql/mem.h | 3 +++
src/box/sql/vdbe.c | 7 ++-----
src/box/sql/vdbeapi.c | 4 ++--
src/box/sql/vdbesort.c | 6 +-----
5 files changed, 22 insertions(+), 12 deletions(-)
diff --git a/src/box/sql/mem.c b/src/box/sql/mem.c
index 5ee49cdca..99beec9ad 100644
--- a/src/box/sql/mem.c
+++ b/src/box/sql/mem.c
@@ -465,6 +465,20 @@ mem_set_allocated_binary(struct Mem *mem, char *value, uint32_t size)
mem_set_dyn_bin(mem, value, size, 0);
}
+int
+mem_copy_binary(struct Mem *mem, const char *value, uint32_t size)
+{
+ bool is_own_value = (mem->flags & MEM_Blob) != 0 && mem->z == value;
+ if (sqlVdbeMemGrow(mem, size, is_own_value) != 0)
+ return -1;
+ if (!is_own_value)
+ memcpy(mem->z, value, size);
+ mem->n = size;
+ mem->flags = MEM_Blob;
+ mem->field_type = FIELD_TYPE_VARBINARY;
+ return 0;
+}
+
int
mem_copy(struct Mem *to, const struct Mem *from)
{
diff --git a/src/box/sql/mem.h b/src/box/sql/mem.h
index 3d33fa98b..f44bb9bcf 100644
--- a/src/box/sql/mem.h
+++ b/src/box/sql/mem.h
@@ -229,6 +229,9 @@ 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_binary(struct Mem *mem, const char *value, uint32_t size);
+
int
mem_copy(struct Mem *to, const struct Mem *from);
diff --git a/src/box/sql/vdbe.c b/src/box/sql/vdbe.c
index 6ae77db63..9fc08e30c 100644
--- a/src/box/sql/vdbe.c
+++ b/src/box/sql/vdbe.c
@@ -2105,11 +2105,8 @@ case OP_MakeRecord: {
* routine.
*/
if (bIsEphemeral) {
- if (sqlVdbeMemClearAndResize(pOut, tuple_size) != 0)
+ if (mem_copy_binary(pOut, tuple, tuple_size) != 0)
goto abort_due_to_error;
- pOut->flags = MEM_Blob;
- pOut->n = tuple_size;
- memcpy(pOut->z, tuple, tuple_size);
region_truncate(region, used);
} else {
/* Allocate memory on the region for the tuple
@@ -2449,7 +2446,7 @@ case OP_SequenceTest: {
* Open a new cursor that points to a fake table that contains a single
* row of data. The content of that one row is the content of memory
* register P2. In other words, cursor P1 becomes an alias for the
- * MEM_Blob content contained in register P2.
+ * MEM with binary content contained in register P2.
*
* A pseudo-table created by this opcode is used to hold a single
* row output from the sorter so that the row can be decomposed into
diff --git a/src/box/sql/vdbeapi.c b/src/box/sql/vdbeapi.c
index 0e51e4809..299172554 100644
--- a/src/box/sql/vdbeapi.c
+++ b/src/box/sql/vdbeapi.c
@@ -188,7 +188,7 @@ sql_result_blob(sql_context * pCtx,
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)
+ else if (mem_copy_binary(pCtx->pOut, z, n) != 0)
pCtx->is_aborted = true;
}
@@ -843,7 +843,7 @@ sql_bind_blob(sql_stmt * pStmt,
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)
+ else if (mem_copy_binary(var, zData, nData) != 0)
return -1;
return sql_bind_type(p, i, "varbinary");
}
diff --git a/src/box/sql/vdbesort.c b/src/box/sql/vdbesort.c
index a9a5f45af..51fe691d2 100644
--- a/src/box/sql/vdbesort.c
+++ b/src/box/sql/vdbesort.c
@@ -2164,12 +2164,8 @@ sqlVdbeSorterRowkey(const VdbeCursor * pCsr, Mem * pOut)
assert(pCsr->eCurType == CURTYPE_SORTER);
pSorter = pCsr->uc.pSorter;
pKey = vdbeSorterRowkey(pSorter, &nKey);
- if (sqlVdbeMemClearAndResize(pOut, nKey)) {
+ if (mem_copy_binary(pOut, pKey, nKey) != 0)
return -1;
- }
- pOut->n = nKey;
- MemSetTypeFlag(pOut, MEM_Blob);
- memcpy(pOut->z, pKey, nKey);
return 0;
}
--
2.25.1
More information about the Tarantool-patches
mailing list