[Tarantool-patches] [PATCH v4 09/53] sql: introduce mem_create()

imeevma at tarantool.org imeevma at tarantool.org
Tue Mar 23 12:35:13 MSK 2021


This patch introduces mem_create(). This function should be used to
initialize a MEM. MEM should be initialized before usage.

Part of #5818
---
 src/box/sql/func.c    |  2 +-
 src/box/sql/mem.c     | 49 +++++++++++++++++--------------------------
 src/box/sql/mem.h     | 14 ++++++-------
 src/box/sql/vdbe.c    |  2 +-
 src/box/sql/vdbeaux.c |  8 +++++--
 5 files changed, 33 insertions(+), 42 deletions(-)

diff --git a/src/box/sql/func.c b/src/box/sql/func.c
index 5ece28d6d..5e4eff931 100644
--- a/src/box/sql/func.c
+++ b/src/box/sql/func.c
@@ -95,7 +95,7 @@ vdbemem_alloc_on_region(uint32_t count)
 	}
 	memset(ret, 0, count * sizeof(*ret));
 	for (uint32_t i = 0; i < count; i++) {
-		sqlVdbeMemInit(&ret[i], sql_get(), MEM_Null);
+		mem_create(&ret[i]);
 		assert(memIsValid(&ret[i]));
 	}
 	return ret;
diff --git a/src/box/sql/mem.c b/src/box/sql/mem.c
index 0ed5e38d4..242360a66 100644
--- a/src/box/sql/mem.c
+++ b/src/box/sql/mem.c
@@ -70,6 +70,25 @@ mem_str(const struct Mem *mem)
 	return "unknown";
 }
 
+void
+mem_create(struct Mem *mem)
+{
+	mem->flags = MEM_Null;
+	mem->subtype = SQL_SUBTYPE_NO;
+	mem->field_type = field_type_MAX;
+	mem->n = 0;
+	mem->z = NULL;
+	mem->zMalloc = NULL;
+	mem->szMalloc = 0;
+	mem->uTemp = 0;
+	mem->db = sql_get();
+	mem->xDel = NULL;
+#ifdef SQL_DEBUG
+	mem->pScopyFrom = NULL;
+	mem->pFiller = NULL;
+#endif
+}
+
 static inline bool
 mem_has_msgpack_subtype(struct Mem *mem)
 {
@@ -1339,21 +1358,6 @@ sqlVdbeMemSetStr(Mem * pMem,	/* Memory cell to set to string value */
 	return 0;
 }
 
-/*
- * Initialize bulk memory to be a consistent Mem object.
- *
- * The minimum amount of initialization feasible is performed.
- */
-void
-sqlVdbeMemInit(Mem * pMem, sql * db, u32 flags)
-{
-	assert((flags & ~MEM_TypeMask) == 0);
-	pMem->flags = flags;
-	pMem->db = db;
-	pMem->szMalloc = 0;
-	pMem->field_type = field_type_MAX;
-}
-
 /*
  * Delete any previous value and set the value stored in *pMem to NULL.
  *
@@ -1438,21 +1442,6 @@ sqlValueNew(sql * db)
 	return p;
 }
 
-void
-initMemArray(Mem * p, int N, sql * db, u32 flags)
-{
-	while ((N--) > 0) {
-		p->db = db;
-		p->flags = flags;
-		p->szMalloc = 0;
-		p->field_type = field_type_MAX;
-#ifdef SQL_DEBUG
-		p->pScopyFrom = 0;
-#endif
-		p++;
-	}
-}
-
 void
 releaseMemArray(Mem * p, int N)
 {
diff --git a/src/box/sql/mem.h b/src/box/sql/mem.h
index 740c7dfba..9a3b275fc 100644
--- a/src/box/sql/mem.h
+++ b/src/box/sql/mem.h
@@ -94,6 +94,12 @@ struct Mem {
 const char *
 mem_str(const struct Mem *mem);
 
+/**
+ * Initialize MEM and set NULL.
+ */
+void
+mem_create(struct Mem *mem);
+
 /* One or more of the following flags are set to indicate the validOK
  * representations of the value stored in the Mem struct.
  *
@@ -321,8 +327,6 @@ mem_set_double(struct Mem *mem, double value);
 int
 sqlVdbeMemSetStr(struct Mem *, const char *, int, u8, void (*)(void *));
 void
-sqlVdbeMemInit(struct Mem *, sql *, u32);
-void
 sqlVdbeMemSetNull(struct Mem *);
 void
 sqlVdbeMemSetZeroBlob(struct Mem *, int);
@@ -332,12 +336,6 @@ void sqlValueSetNull(struct Mem *);
 void sqlValueFree(struct Mem *);
 struct Mem *sqlValueNew(struct sql *);
 
-/*
- * Initialize an array of N Mem element.
- */
-void
-initMemArray(Mem * p, int N, sql * db, u32 flags);
-
 /*
  * Release an array of N Mem elements
  */
diff --git a/src/box/sql/vdbe.c b/src/box/sql/vdbe.c
index 9b392f9cb..5378bc92b 100644
--- a/src/box/sql/vdbe.c
+++ b/src/box/sql/vdbe.c
@@ -4710,7 +4710,7 @@ case OP_AggStep: {
 #endif
 
 	pMem->n++;
-	sqlVdbeMemInit(&t, db, MEM_Null);
+	mem_create(&t);
 	pCtx->pOut = &t;
 	pCtx->is_aborted = false;
 	pCtx->skipFlag = 0;
diff --git a/src/box/sql/vdbeaux.c b/src/box/sql/vdbeaux.c
index f270b0ed1..55d4fc14f 100644
--- a/src/box/sql/vdbeaux.c
+++ b/src/box/sql/vdbeaux.c
@@ -1565,9 +1565,13 @@ sqlVdbeMakeReady(Vdbe * p,	/* The VDBE */
 	} else {
 		p->nCursor = nCursor;
 		p->nVar = (ynVar) nVar;
-		initMemArray(p->aVar, nVar, db, MEM_Null);
+		for (int i = 0; i < nVar; ++i)
+			mem_create(&p->aVar[i]);
 		p->nMem = nMem;
-		initMemArray(p->aMem, nMem, db, MEM_Undefined);
+		for (int i = 0; i < nMem; ++i) {
+			mem_create(&p->aMem[i]);
+			p->aMem[i].flags = MEM_Undefined;
+		}
 		memset(p->apCsr, 0, nCursor * sizeof(VdbeCursor *));
 	}
 	sqlVdbeRewind(p);
-- 
2.25.1



More information about the Tarantool-patches mailing list