[Tarantool-patches] [PATCH v4 30/53] sql: introduce mem_set_*_binary()
imeevma at tarantool.org
imeevma at tarantool.org
Tue Mar 23 12:36:00 MSK 2021
This patch introduces mem_set_*_binary() functions. Functions
mem_set_*_binary() clears MEM and sets it to given binary value.
Part of #5818
---
src/box/sql/mem.c | 68 ++++++++++++++++++++++++++++++++++---------
src/box/sql/mem.h | 15 ++++++++--
src/box/sql/vdbe.c | 24 ++++++++++-----
src/box/sql/vdbeapi.c | 21 +++++++++----
4 files changed, 100 insertions(+), 28 deletions(-)
diff --git a/src/box/sql/mem.c b/src/box/sql/mem.c
index 59a378e1b..5ee49cdca 100644
--- a/src/box/sql/mem.c
+++ b/src/box/sql/mem.c
@@ -410,6 +410,61 @@ mem_copy_string0(struct Mem *mem, const char *value)
return 0;
}
+static inline void
+mem_set_const_bin(struct Mem *mem, char *value, uint32_t size, int alloc_type)
+{
+ assert((alloc_type & (MEM_Static | MEM_Ephem)) != 0);
+ mem_clear(mem);
+ mem->z = value;
+ mem->n = size;
+ mem->flags = MEM_Blob | alloc_type;
+ mem->field_type = FIELD_TYPE_VARBINARY;
+}
+
+static inline void
+mem_set_dyn_bin(struct Mem *mem, char *value, uint32_t size, 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 = size;
+ mem->flags = MEM_Blob | alloc_type;
+ mem->field_type = FIELD_TYPE_VARBINARY;
+ 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_binary(struct Mem *mem, char *value, uint32_t size)
+{
+ mem_set_const_bin(mem, value, size, MEM_Ephem);
+}
+
+void
+mem_set_static_binary(struct Mem *mem, char *value, uint32_t size)
+{
+ mem_set_const_bin(mem, value, size, MEM_Static);
+}
+
+void
+mem_set_dynamic_binary(struct Mem *mem, char *value, uint32_t size)
+{
+ mem_set_dyn_bin(mem, value, size, MEM_Dyn);
+}
+
+void
+mem_set_allocated_binary(struct Mem *mem, char *value, uint32_t size)
+{
+ mem_set_dyn_bin(mem, value, size, 0);
+}
+
int
mem_copy(struct Mem *to, const struct Mem *from)
{
@@ -2596,19 +2651,6 @@ mem_is_type_compatible(struct Mem *mem, enum field_type type)
return field_mp_plain_type_is_compatible(type, mp_type, true);
}
-/* Allocate memory for internal VDBE structure on region. */
-int
-vdbe_mem_alloc_blob_region(struct Mem *vdbe_mem, uint32_t size)
-{
- vdbe_mem->n = size;
- vdbe_mem->z = region_alloc(&fiber()->gc, size);
- if (vdbe_mem->z == NULL)
- return -1;
- vdbe_mem->flags = MEM_Ephem | MEM_Blob;
- assert(sqlVdbeCheckMemInvariants(vdbe_mem));
- return 0;
-}
-
int
sql_vdbemem_finalize(struct Mem *mem, struct func *func)
{
diff --git a/src/box/sql/mem.h b/src/box/sql/mem.h
index 12853606d..3d33fa98b 100644
--- a/src/box/sql/mem.h
+++ b/src/box/sql/mem.h
@@ -217,6 +217,18 @@ mem_copy_string(struct Mem *mem, const char *value, uint32_t len);
int
mem_copy_string0(struct Mem *mem, const char *value);
+void
+mem_set_ephemeral_binary(struct Mem *mem, char *value, uint32_t size);
+
+void
+mem_set_static_binary(struct Mem *mem, char *value, uint32_t size);
+
+void
+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(struct Mem *to, const struct Mem *from);
@@ -534,9 +546,6 @@ mem_is_type_compatible(struct Mem *mem, enum field_type type);
/** MEM manipulate functions. */
-int
-vdbe_mem_alloc_blob_region(struct Mem *vdbe_mem, uint32_t size);
-
/**
* Memory cell mem contains the context of an aggregate function.
* This routine calls the finalize method for that function. The
diff --git a/src/box/sql/vdbe.c b/src/box/sql/vdbe.c
index 341205cc4..6ae77db63 100644
--- a/src/box/sql/vdbe.c
+++ b/src/box/sql/vdbe.c
@@ -854,8 +854,15 @@ case OP_Null: { /* out2 */
case OP_Blob: { /* out2 */
assert(pOp->p1 <= SQL_MAX_LENGTH);
pOut = vdbe_prepare_null_out(p, pOp->p2);
- sqlVdbeMemSetStr(pOut, pOp->p4.z, pOp->p1, 0, 0);
- if (pOp->p3!=0) {
+ if (pOp->p3 == 0) {
+ /*
+ * TODO: It is possible that vabinary should be stored as
+ * ephemeral or static depending on value. There is no way to
+ * determine right now, so it is stored as static.
+ */
+ mem_set_static_binary(pOut, pOp->p4.z, pOp->p1);
+ } else {
+ sqlVdbeMemSetStr(pOut, pOp->p4.z, pOp->p1, 0, 0);
pOut->flags |= MEM_Subtype;
pOut->subtype = pOp->p3;
}
@@ -2110,9 +2117,7 @@ case OP_MakeRecord: {
* sure previously allocated memory has gone.
*/
mem_destroy(pOut);
- pOut->flags = MEM_Blob | MEM_Ephem;
- pOut->n = tuple_size;
- pOut->z = tuple;
+ mem_set_ephemeral_binary(pOut, tuple, tuple_size);
}
assert(sqlVdbeCheckMemInvariants(pOut));
assert(pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor));
@@ -3196,9 +3201,14 @@ case OP_RowData: {
}
testcase( n==0);
- if (vdbe_mem_alloc_blob_region(pOut, n) != 0)
+ char *buf = region_alloc(&fiber()->gc, n);
+ if (buf == NULL) {
+ diag_set(OutOfMemory, n, "region_alloc", "buf");
goto abort_due_to_error;
- sqlCursorPayload(pCrsr, 0, n, pOut->z);
+ }
+ sqlCursorPayload(pCrsr, 0, n, buf);
+ mem_set_ephemeral_binary(pOut, buf, n);
+ assert(sqlVdbeCheckMemInvariants(pOut));
UPDATE_MAX_BLOBSIZE(pOut);
REGISTER_TRACE(p, pOp->p2, pOut);
break;
diff --git a/src/box/sql/vdbeapi.c b/src/box/sql/vdbeapi.c
index 5b5e5b0c8..0e51e4809 100644
--- a/src/box/sql/vdbeapi.c
+++ b/src/box/sql/vdbeapi.c
@@ -111,9 +111,8 @@ sql_metadata_is_full()
* The following routines are used by user-defined functions to specify
* the function result.
*
- * The setStrOrError() function calls sqlVdbeMemSetStr() to store the
- * result as a string or blob but if the string or blob is too large, it
- * then sets the error code.
+ * The setStrOrError() function sets the result as a string or blob but
+ * if the string or blob is too large, it then sets the error code.
*
* The invokeValueDestructor(P,X) routine invokes destructor function X()
* on value P is not going to be used and need to be destroyed.
@@ -183,7 +182,13 @@ sql_result_blob(sql_context * pCtx,
)
{
assert(n >= 0);
- if (sqlVdbeMemSetStr(pCtx->pOut, z, n, 0, xDel) != 0)
+ if (xDel == SQL_STATIC)
+ mem_set_static_binary(pCtx->pOut, (char *)z, n);
+ else if (xDel == SQL_DYNAMIC)
+ 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)
pCtx->is_aborted = true;
}
@@ -832,7 +837,13 @@ sql_bind_blob(sql_stmt * pStmt,
if (zData == NULL)
return 0;
struct Mem *var = &p->aVar[i - 1];
- if (sqlVdbeMemSetStr(var, zData, nData, 0, xDel) != 0)
+ if (xDel == SQL_STATIC)
+ mem_set_static_binary(var, (char *)zData, nData);
+ else if (xDel == SQL_DYNAMIC)
+ 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)
return -1;
return sql_bind_type(p, i, "varbinary");
}
--
2.25.1
More information about the Tarantool-patches
mailing list