Changeset: 96ca2940cd16 for MonetDB
URL: https://dev.monetdb.org/hg/MonetDB/rev/96ca2940cd16
Modified Files:
        gdk/gdk_calc.c
        gdk/gdk_value.c
        monetdb5/mal/mal_builder.c
        monetdb5/mal/mal_interpreter.c
        monetdb5/mal/mal_parser.c
        monetdb5/modules/mal/mal_mapi.c
        monetdb5/modules/mal/remote.c
Branch: default
Log Message:

Avoid (some) duplication, both in code and in work.


diffs (258 lines):

diff --git a/gdk/gdk_calc.c b/gdk/gdk_calc.c
--- a/gdk/gdk_calc.c
+++ b/gdk/gdk_calc.c
@@ -16835,6 +16835,7 @@ VARconvert(ValPtr ret, const ValRecord *
                        nils = BUN_NONE;
                }
                ret->val.mval = tmp.val.btval;
+               ret->len = ATOMsize(TYPE_msk);
        } else if (v->vtype == TYPE_msk) {
                ValRecord tmp;
                tmp.vtype = TYPE_bit;
@@ -16845,9 +16846,11 @@ VARconvert(ValPtr ret, const ValRecord *
                if (v->vtype == TYPE_void ||
                    (*ATOMcompare(v->vtype))(VALptr(v),
                                             ATOMnilptr(v->vtype)) == 0) {
-                       ret->val.sval = GDKstrdup(str_nil);
+                       if (VALinit(ret, TYPE_str, str_nil) == NULL)
+                               return GDK_FAIL;
                } else if (BATatoms[v->vtype].atomToStr == 
BATatoms[TYPE_str].atomToStr) {
-                       ret->val.sval = GDKstrdup(v->val.sval);
+                       if (VALinit(ret, TYPE_str, v->val.sval) == NULL)
+                               return GDK_FAIL;
                } else {
                        ret->len = 0;
                        ret->val.sval = NULL;
@@ -16858,25 +16861,24 @@ VARconvert(ValPtr ret, const ValRecord *
                                GDKfree(ret->val.sval);
                                ret->val.sval = NULL;
                                ret->len = 0;
-                               nils = BUN_NONE;
-                       }
-               }
-               if (ret->val.sval == NULL)
-                       nils = BUN_NONE;
+                               return GDK_FAIL;
+                       }
+               }
        } else if (ret->vtype == TYPE_void) {
                if (abort_on_error &&
                    ATOMcmp(v->vtype, VALptr(v), ATOMnilptr(v->vtype)) != 0) {
                        GDKerror("22003!cannot convert non-nil to void.\n");
-                       nils = BUN_NONE;
+                       return GDK_FAIL;
                }
                ret->val.oval = oid_nil;
+               ret->len = ATOMsize(TYPE_void);
        } else if (v->vtype == TYPE_void) {
                if (VALinit(ret, ret->vtype, ATOMnilptr(ret->vtype)) == NULL)
-                       nils = BUN_NONE;
+                       return GDK_FAIL;
        } else if (v->vtype == TYPE_str) {
                if (strNil(v->val.sval)) {
                        if (VALinit(ret, ret->vtype, ATOMnilptr(ret->vtype)) == 
NULL)
-                               nils = BUN_NONE;
+                               return GDK_FAIL;
                } else if (ATOMstorage(ret->vtype) == TYPE_ptr) {
                        nils = BUN_NONE + 1;
                } else {
@@ -16912,7 +16914,7 @@ VARconvert(ValPtr ret, const ValRecord *
                                                 "to type %s failed.\n",
                                                 ATOMname(ret->vtype));
                                }
-                               nils = BUN_NONE;
+                               return GDK_FAIL;
                        } else {
                                /* now give value obtained to ret */
                                assert(ATOMextern(ret->vtype) ||
@@ -16928,15 +16930,14 @@ VARconvert(ValPtr ret, const ValRecord *
                                              &(struct 
canditer){.tpe=cand_dense, .ncand=1},
                                              0, abort_on_error, &reduce,
                                              scale1, scale2, precision);
-       }
-       if (nils >= BUN_NONE) {
-               if (nils == BUN_NONE + 1) {
-                       GDKerror("conversion from type %s to type %s "
-                               "unsupported.\n",
-                               ATOMname(v->vtype), ATOMname(ret->vtype));
-               }
+               if (nils < BUN_NONE)
+                       ret->len = ATOMlen(ret->vtype, VALptr(ret));
+       }
+       if (nils == BUN_NONE + 1) {
+               GDKerror("conversion from type %s to type %s "
+                        "unsupported.\n",
+                        ATOMname(v->vtype), ATOMname(ret->vtype));
                return GDK_FAIL;
        }
-       ret->len = ATOMlen(ret->vtype, VALptr(ret));
-       return GDK_SUCCEED;
-}
+       return nils == BUN_NONE ? GDK_FAIL : GDK_SUCCEED;
+}
diff --git a/gdk/gdk_value.c b/gdk/gdk_value.c
--- a/gdk/gdk_value.c
+++ b/gdk/gdk_value.c
@@ -150,11 +150,7 @@ VALcopy(ValPtr d, const ValRecord *s)
        if (!ATOMextern(s->vtype)) {
                *d = *s;
        } else if (s->val.pval == NULL) {
-               d->val.pval = ATOMnil(s->vtype);
-               if (d->val.pval == NULL)
-                       return NULL;
-               d->vtype = s->vtype;
-               d->len = ATOMlen(d->vtype, VALptr(d));
+               return VALinit(d, s->vtype, ATOMnilptr(s->vtype));
        } else if (s->vtype == TYPE_str) {
                const char *p = s->val.sval;
                d->vtype = TYPE_str;
diff --git a/monetdb5/mal/mal_builder.c b/monetdb5/mal/mal_builder.c
--- a/monetdb5/mal/mal_builder.c
+++ b/monetdb5/mal/mal_builder.c
@@ -122,13 +122,11 @@ newComment(MalBlkPtr mb, const char *val
                return NULL;
        q->token = REMsymbol;
        q->barrier = 0;
-       cst.vtype= TYPE_str;
-       if ((cst.val.sval= GDKstrdup(val)) == NULL) {
+       if (VALinit(&cst, TYPE_str, val) == NULL) {
                str msg = createException(MAL, "newComment", "Can not allocate 
comment");
                addMalException(mb, msg);
                freeException(msg);
        } else {
-               cst.len = strlen(cst.val.sval);
                k = defConstant(mb, TYPE_str, &cst);
                if( k >= 0){
                        getArg(q,0) = k;
@@ -514,13 +512,12 @@ getStrConstant(MalBlkPtr mb, str val)
        int _t;
        ValRecord cst;
 
-       cst.vtype = TYPE_str;
-       cst.val.sval = val;
-       cst.len = strlen(val);
+       VALset(&cst, TYPE_str, val);
        _t= fndConstant(mb, &cst, MAL_VAR_WINDOW);
        if( _t < 0) {
-               if ((cst.val.sval= GDKstrdup(val)) == NULL)
+               if ((cst.val.sval = GDKmalloc(cst.len)) == NULL)
                        return -1;
+               memcpy(cst.val.sval, val, cst.len); /* includes terminating \0 
*/
                _t = defConstant(mb, TYPE_str, &cst);
        }
        assert(_t >= 0);
@@ -535,13 +532,11 @@ pushStr(MalBlkPtr mb, InstrPtr q, const 
 
        if (q == NULL)
                return NULL;
-       cst.vtype= TYPE_str;
-       if ((cst.val.sval= GDKstrdup(Val)) == NULL) {
+       if (VALinit(&cst, TYPE_str, Val) == NULL) {
                str msg = createException(MAL, "pushStr", "Can not allocate 
string variable");
                addMalException(mb, msg);
                freeException(msg);
        } else{
-               cst.len = strlen(cst.val.sval);
                _t = defConstant(mb,TYPE_str,&cst);
                if( _t >= 0)
                        return pushArgument(mb, q, _t);
@@ -596,14 +591,6 @@ pushNil(MalBlkPtr mb, InstrPtr q, int tp
                if (!tpe) {
                        cst.vtype=TYPE_void;
                        cst.val.oval= oid_nil;
-               } else if (ATOMextern(tpe)) {
-                       ptr p = ATOMnil(tpe);
-                       if( p == NULL){
-                               str msg = createException(MAL, "pushNil", "Can 
not allocate nil variable");
-                               addMalException(mb, msg);
-                               freeException(msg);
-                       } else
-                               VALset(&cst, tpe, p);
                } else {
                        if (VALinit(&cst, tpe, ATOMnilptr(tpe)) == NULL) {
                                str msg =  createException(MAL, "pushNil", "Can 
not allocate nil variable");
diff --git a/monetdb5/mal/mal_interpreter.c b/monetdb5/mal/mal_interpreter.c
--- a/monetdb5/mal/mal_interpreter.c
+++ b/monetdb5/mal/mal_interpreter.c
@@ -959,9 +959,7 @@ str runMALsequence(Client cntxt, MalBlkP
                                v = &stk->stk[exceptionVar];
                                if (v->val.sval)
                                        freeException(v->val.sval);    /* old 
exception*/
-                               v->vtype = TYPE_str;
-                               v->val.sval = ret;
-                               v->len = strlen(v->val.sval);
+                               VALset(v, TYPE_str, ret);
                                ret = MAL_SUCCEED;
                                MT_lock_unset(&mal_contextLock);
                        } else {
diff --git a/monetdb5/mal/mal_parser.c b/monetdb5/mal/mal_parser.c
--- a/monetdb5/mal/mal_parser.c
+++ b/monetdb5/mal/mal_parser.c
@@ -422,13 +422,8 @@ cstToken(Client cntxt, ValPtr cst)
                /* JSON Literal */
                break;
        case '"':
-               cst->vtype = TYPE_str;
                i = stringLength(cntxt);
-               cst->val.sval = strCopy(cntxt, i);
-               if (cst->val.sval)
-                       cst->len = strlen(cst->val.sval);
-               else
-                       cst->len = 0;
+               VALset(cst, TYPE_str, strCopy(cntxt, i));
                return i;
        case '-':
                i++;
@@ -1823,9 +1818,7 @@ parseMAL(Client cntxt, Symbol curPrg, in
                                }
                                curInstr->token= REMsymbol;
                                curInstr->barrier= 0;
-                               cst.vtype = TYPE_str;
-                               cst.len = strlen(start);
-                               if((cst.val.sval = GDKstrdup(start)) == NULL) {
+                               if (VALinit(&cst, TYPE_str, start) == NULL) {
                                        parseError(cntxt, SQLSTATE(HY013) 
MAL_MALLOC_FAIL);
                                        freeInstruction(curInstr);
                                        continue;
diff --git a/monetdb5/modules/mal/mal_mapi.c b/monetdb5/modules/mal/mal_mapi.c
--- a/monetdb5/modules/mal/mal_mapi.c
+++ b/monetdb5/modules/mal/mal_mapi.c
@@ -1695,13 +1695,11 @@ static int SERVERfieldAnalysis(str fld, 
                break;
        case TYPE_str:
                if(fld==0 || strcmp(fld,"nil")==0){
-                       if((v->val.sval= GDKstrdup(str_nil)) == NULL)
+                       if (VALinit(v, TYPE_str, str_nil) == NULL)
                                return -1;
-                       v->len = strlen(v->val.sval);
                } else {
-                       if((v->val.sval= GDKstrdup(fld)) == NULL)
+                       if (VALinit(v, TYPE_str, fld) == NULL)
                                return -1;
-                       v->len = strlen(fld);
                }
                break;
        }
diff --git a/monetdb5/modules/mal/remote.c b/monetdb5/modules/mal/remote.c
--- a/monetdb5/modules/mal/remote.c
+++ b/monetdb5/modules/mal/remote.c
@@ -359,8 +359,7 @@ RMTconnectTable(Client cntxt, MalBlkPtr 
 
        if (msg == MAL_SUCCEED) {
                v = &stk->stk[pci->argv[0]];
-               v->vtype = TYPE_str;
-               if((v->val.sval = GDKstrdup(ret)) == NULL) {
+               if (VALinit(v, TYPE_str, ret) == NULL) {
                        GDKfree(ret);
                        throw(MAL, "remote.connect", SQLSTATE(HY013) 
MAL_MALLOC_FAIL);
                }
@@ -1141,8 +1140,7 @@ static str RMTput(Client cntxt, MalBlkPt
 
        /* return the identifier */
        v = &stk->stk[pci->argv[0]];
-       v->vtype = TYPE_str;
-       if((v->val.sval = GDKstrdup(ident)) == NULL)
+       if (VALinit(v, TYPE_str, ident) == NULL)
                throw(MAL, "remote.put", SQLSTATE(HY013) MAL_MALLOC_FAIL);
        return(MAL_SUCCEED);
 }
_______________________________________________
checkin-list mailing list
checkin-list@monetdb.org
https://www.monetdb.org/mailman/listinfo/checkin-list

Reply via email to