Changeset: cb72f71d7327 for MonetDB
URL: https://dev.monetdb.org/hg/MonetDB?cmd=changeset;node=cb72f71d7327
Modified Files:
        monetdb5/mal/mal_builder.c
        monetdb5/mal/mal_instruction.c
        monetdb5/mal/mal_instruction.h
Branch: default
Log Message:

Add more defensive code for the rare case that mallocs starting to fail
near the end-of-life situation of running out of memory.
The basic approach is to mark MAL blocks with exceptions and stumble on
with invalide blocks, which will likely be catched by optimizers/interpreter.


diffs (truncated from 514 to 300 lines):

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
@@ -24,13 +24,9 @@ InstrPtr
 newAssignment(MalBlkPtr mb)
 {
        InstrPtr q = newInstruction(mb,NULL,NULL);
-
-       if ( q == NULL)
-               return NULL;
-       if ((getArg(q,0)= newTmpVariable(mb,TYPE_any)) < 0 || mb->errors != 
MAL_SUCCEED) {
-               freeInstruction(q);
-               return NULL;
-       }
+       assert(q);
+       if ((getArg(q,0)= newTmpVariable(mb,TYPE_any)) < 0) 
+               addMalException(mb, createException(MAL, "newAssignment", "Can 
not allocate variable"));
        pushInstruction(mb, q);
        return q;
 }
@@ -41,17 +37,11 @@ newStmt(MalBlkPtr mb, const char *module
        InstrPtr q;
        str mName = putName(module), nName = putName(name);
 
-       if(mName == NULL || nName == NULL) {
-               return NULL;
-       }
        q = newInstruction(mb, mName, nName);
-       if ( q == NULL)
-               return NULL;
+       assert(q);
        setDestVar(q, newTmpVariable(mb, TYPE_any));
-       if (getDestVar(q) < 0 || mb->errors != MAL_SUCCEED) {
-               freeInstruction(q);
-               return NULL;
-       }
+       if (getDestVar(q) < 0 )
+               addMalException(mb, createException(MAL, "newStmt", "Can not 
allocate variable"));
        pushInstruction(mb, q);
        return q;
 }
@@ -62,17 +52,12 @@ newStmtArgs(MalBlkPtr mb, const char *mo
        InstrPtr q;
        str mName = putName(module), nName = putName(name);
 
-       if(mName == NULL || nName == NULL) {
-               return NULL;
-       }
        q = newInstructionArgs(mb, mName, nName, args);
-       if ( q == NULL)
-               return NULL;
+       assert(q);
+
        setDestVar(q, newTmpVariable(mb, TYPE_any));
-       if (getDestVar(q) < 0 || mb->errors != MAL_SUCCEED) {
-               freeInstruction(q);
-               return NULL;
-       }
+       if (getDestVar(q) < 0 || mb->errors != MAL_SUCCEED) 
+               addMalException(mb, createException(MAL, "newStmtArgs", "Can 
not allocate variable"));
        pushInstruction(mb, q);
        return q;
 }
@@ -81,13 +66,9 @@ InstrPtr
 newReturnStmt(MalBlkPtr mb)
 {
        InstrPtr q = newInstruction(mb, NULL, NULL);
-
-       if ( q == NULL)
-               return NULL;
-       if ((getArg(q,0)= newTmpVariable(mb,TYPE_any)) < 0 || mb->errors != 
MAL_SUCCEED) {
-               freeInstruction(q);
-               return NULL;
-       }
+       assert(q);
+       if ((getArg(q,0)= newTmpVariable(mb,TYPE_any)) < 0 )
+               addMalException(mb, createException(MAL, "newReturnStmt", "Can 
not allocate return variable"));
        q->barrier= RETURNsymbol;
        pushInstruction(mb, q);
        return q;
@@ -99,14 +80,8 @@ newFcnCall(MalBlkPtr mb, char *mod, char
        InstrPtr q = newAssignment(mb);
        str fcnName, modName;
 
-       if ( q == NULL || mod == NULL || fcn == NULL)
-               return NULL;
        modName = putName(mod);
        fcnName = putName(fcn);
-       if(modName == NULL || fcnName == NULL) {
-               freeInstruction(q);
-               return NULL;
-       }
        setModuleId(q, modName);
        setFunctionId(q, fcnName);
        return q;
@@ -118,23 +93,17 @@ newComment(MalBlkPtr mb, const char *val
        InstrPtr q = newInstruction(mb, NULL, NULL);
        ValRecord cst;
 
-       if (q == NULL)
-               return NULL;
+       assert(q);
        q->token = REMsymbol;
        q->barrier = 0;
        cst.vtype= TYPE_str;
-       if ((cst.val.sval= GDKstrdup(val)) == NULL) {
-               freeInstruction(q);
-               return NULL;
-       }
+       if ((cst.val.sval= GDKstrdup(val)) == NULL) 
+               addMalException(mb, createException(MAL, "newComment", "Can not 
allocate comment"));
+       
        cst.len = strlen(cst.val.sval);
        getArg(q,0) = defConstant(mb,TYPE_str,&cst);
        clrVarConstant(mb,getArg(q,0));
        setVarDisabled(mb,getArg(q,0));
-       if (mb->errors != MAL_SUCCEED) {
-               freeInstruction(q);
-               return NULL;
-       }
        pushInstruction(mb, q);
        return q;
 }
@@ -145,14 +114,11 @@ newCatchStmt(MalBlkPtr mb, str nme)
        InstrPtr q = newAssignment(mb);
        int i= findVariable(mb,nme);
 
-       if ( q == NULL)
-               return NULL;
+       assert(q);
        q->barrier = CATCHsymbol;
        if ( i< 0) {
-               if ((getArg(q,0)= newVariable(mb, nme, strlen(nme),TYPE_str)) < 
0 || mb->errors != MAL_SUCCEED) {
-                       freeInstruction(q);
-                       return NULL;
-               }
+               if ((getArg(q,0)= newVariable(mb, nme, strlen(nme),TYPE_str)) < 
0 )
+                       addMalException(mb, createException(MAL, 
"newCatchStmt", "Can not allocate variable"));
                setVarUDFtype(mb,getArg(q,0));
        } else getArg(q,0) = i;
        return q;
@@ -164,14 +130,11 @@ newRaiseStmt(MalBlkPtr mb, str nme)
        InstrPtr q = newAssignment(mb);
        int i= findVariable(mb,nme);
 
-       if ( q == NULL)
-               return NULL;
+       assert(q);
        q->barrier = RAISEsymbol;
        if ( i< 0) {
-               if ((getArg(q,0)= newVariable(mb, nme, strlen(nme),TYPE_str)) < 
0 || mb->errors != MAL_SUCCEED) {
-                       freeInstruction(q);
-                       return NULL;
-               }
+               if ((getArg(q,0)= newVariable(mb, nme, strlen(nme),TYPE_str)) < 
0 || mb->errors != MAL_SUCCEED) 
+                       addMalException(mb, createException(MAL, 
"newRaiseStmt", "Can not allocate variable"));
        } else
                getArg(q,0) = i;
        return q;
@@ -183,14 +146,11 @@ newExitStmt(MalBlkPtr mb, str nme)
        InstrPtr q = newAssignment(mb);
        int i= findVariable(mb,nme);
 
-       if ( q == NULL)
-               return NULL;
+       assert(q);
        q->barrier = EXITsymbol;
        if ( i< 0) {
-               if ((getArg(q,0)= newVariable(mb, nme,strlen(nme),TYPE_str)) < 
0 || mb->errors != MAL_SUCCEED) {
-                       freeInstruction(q);
-                       return NULL;
-               }
+               if ((getArg(q,0)= newVariable(mb, nme,strlen(nme),TYPE_str)) < 
0 )
+                       addMalException(mb, createException(MAL, "newExitStmt", 
"Can not allocate variable"));
        } else
                getArg(q,0) = i;
        return q;
@@ -199,11 +159,9 @@ newExitStmt(MalBlkPtr mb, str nme)
 InstrPtr
 pushEndInstruction(MalBlkPtr mb)
 {
-    InstrPtr q;
-
-    q = newInstruction(mb,NULL, NULL);
-       if ( q == NULL)
-               return NULL;
+    InstrPtr q = newInstruction(mb,NULL, NULL);
+       
+       assert(q);
     q->token = ENDsymbol;
     q->barrier = 0;
     q->argc = 0;
@@ -264,8 +222,7 @@ pushBte(MalBlkPtr mb, InstrPtr q, bte va
        int _t;
        ValRecord cst;
 
-       if (q == NULL)
-               return NULL;
+       assert(q);
        cst.vtype= TYPE_bte;
        cst.val.btval= val;
        cst.len = 0;
@@ -294,8 +251,7 @@ pushOid(MalBlkPtr mb, InstrPtr q, oid va
        int _t;
        ValRecord cst;
 
-       if (q == NULL)
-               return NULL;
+       assert(q);
        cst.vtype= TYPE_oid;
        cst.val.oval= val;
        cst.len = 0;
@@ -309,8 +265,7 @@ pushVoid(MalBlkPtr mb, InstrPtr q)
        int _t;
        ValRecord cst;
 
-       if (q == NULL)
-               return NULL;
+       assert(q);
        cst.vtype= TYPE_void;
        cst.val.oval= oid_nil;
        cst.len = 0;
@@ -339,8 +294,7 @@ pushLng(MalBlkPtr mb, InstrPtr q, lng va
        int _t;
        ValRecord cst;
 
-       if (q == NULL)
-               return NULL;
+       assert(q);
        cst.vtype= TYPE_lng;
        cst.val.lval= val;
        cst.len = 0;
@@ -369,8 +323,7 @@ pushSht(MalBlkPtr mb, InstrPtr q, sht va
        int _t;
        ValRecord cst;
 
-       if ( q == NULL)
-               return NULL;
+       assert(q);
        cst.vtype= TYPE_sht;
        cst.val.shval= val;
        cst.len = 0;
@@ -400,8 +353,7 @@ pushHge(MalBlkPtr mb, InstrPtr q, hge va
        int _t;
        ValRecord cst;
 
-       if ( q == NULL)
-               return NULL;
+       assert(q);
        cst.vtype= TYPE_hge;
        cst.val.hval= val;
        cst.len = 0;
@@ -431,8 +383,7 @@ pushDbl(MalBlkPtr mb, InstrPtr q, dbl va
        int _t;
        ValRecord cst;
 
-       if (q == NULL)
-               return NULL;
+       assert(q);
        cst.vtype= TYPE_dbl;
        cst.val.dval= val;
        cst.len = 0;
@@ -461,8 +412,7 @@ pushFlt(MalBlkPtr mb, InstrPtr q, flt va
        int _t;
        ValRecord cst;
 
-       if (q == NULL)
-               return NULL;
+       assert(q);
        cst.vtype= TYPE_flt;
        cst.val.fval= val;
        cst.len = 0;
@@ -494,13 +444,10 @@ pushStr(MalBlkPtr mb, InstrPtr q, const 
        int _t;
        ValRecord cst;
 
-       if (q == NULL)
-               return NULL;
+       assert(q);
        cst.vtype= TYPE_str;
-       if ((cst.val.sval= GDKstrdup(Val)) == NULL) {
-               freeInstruction(q);
-               return NULL;
-       }
+       if ((cst.val.sval= GDKstrdup(Val)) == NULL) 
+               addMalException(mb, createException(MAL, "pushStr", "Can not 
allocate string variable"));
        cst.len = strlen(cst.val.sval);
        _t = defConstant(mb,TYPE_str,&cst);
        return pushArgument(mb, q, _t);
@@ -527,8 +474,7 @@ pushBit(MalBlkPtr mb, InstrPtr q, bit va
        int _t;
        ValRecord cst;
 
-       if (q == NULL)
-               return NULL;
+       assert(q);
        cst.vtype= TYPE_bit;
        cst.val.btval= val;
        cst.len = 0;
@@ -543,8 +489,7 @@ pushNil(MalBlkPtr mb, InstrPtr q, int tp
_______________________________________________
checkin-list mailing list
checkin-list@monetdb.org
https://www.monetdb.org/mailman/listinfo/checkin-list

Reply via email to