Index: src/func.c ================================================================== --- src/func.c +++ src/func.c @@ -14,11 +14,11 @@ ** ** There is only one exported symbol in this file - the function ** sqliteRegisterBuildinFunctions() found at the bottom of the file. ** All other code has file scope. ** -** $Id: func.c,v 1.37 2004/01/19 04:53:25 jplyon Exp $ +** $Id: func.c,v 1.38 2004/01/30 14:49:17 drh Exp $ */ #include #include #include #include @@ -493,18 +493,20 @@ MinMaxCtx *p; p = sqlite_aggregate_context(context, sizeof(*p)); if( p==0 || argc<1 || argv[0]==0 ) return; if( p->z==0 || sqliteCompare(argv[0],p->z)<0 ){ int len; - if( p->z && p->z!=p->zBuf ){ + if( !p->zBuf[0] ){ sqliteFree(p->z); } len = strlen(argv[0]); - if( len < sizeof(p->zBuf) ){ - p->z = p->zBuf; + if( len < sizeof(p->zBuf)-1 ){ + p->z = &p->zBuf[1]; + p->zBuf[0] = 1; }else{ p->z = sqliteMalloc( len+1 ); + p->zBuf[0] = 0; if( p->z==0 ) return; } strcpy(p->z, argv[0]); } } @@ -512,18 +514,20 @@ MinMaxCtx *p; p = sqlite_aggregate_context(context, sizeof(*p)); if( p==0 || argc<1 || argv[0]==0 ) return; if( p->z==0 || sqliteCompare(argv[0],p->z)>0 ){ int len; - if( p->z && p->z!=p->zBuf ){ + if( !p->zBuf[0] ){ sqliteFree(p->z); } len = strlen(argv[0]); - if( len < sizeof(p->zBuf) ){ - p->z = p->zBuf; + if( len < sizeof(p->zBuf)-1 ){ + p->z = &p->zBuf[1]; + p->zBuf[0] = 1; }else{ p->z = sqliteMalloc( len+1 ); + p->zBuf[0] = 0; if( p->z==0 ) return; } strcpy(p->z, argv[0]); } } @@ -531,11 +535,11 @@ MinMaxCtx *p; p = sqlite_aggregate_context(context, sizeof(*p)); if( p && p->z ){ sqlite_set_result_string(context, p->z, strlen(p->z)); } - if( p && p->z && p->z!=p->zBuf ){ + if( p && !p->zBuf[0] ){ sqliteFree(p->z); } } /* Index: src/vdbe.c ================================================================== --- src/vdbe.c +++ src/vdbe.c @@ -41,11 +41,11 @@ ** documentation, headers files, or other derived files. The formatting ** of the code in this file is, therefore, important. See other comments ** in this file for details. If in doubt, do not deviate from existing ** commenting and indentation practices when changing or adding code. ** -** $Id: vdbe.c,v 1.251 2004/01/15 02:44:03 drh Exp $ +** $Id: vdbe.c,v 1.252 2004/01/30 14:49:17 drh Exp $ */ #include "sqliteInt.h" #include "os.h" #include #include "vdbeInt.h" @@ -157,11 +157,11 @@ assert( pOld==pElem ); /* Malloc failed on insert */ sqliteFree(pOld); return 0; } for(i=0; inMem; i++){ - pElem->aMem[i].s.flags = STK_Null; + pElem->aMem[i].flags = MEM_Null; } p->pCurrent = pElem; return 0; } @@ -180,24 +180,24 @@ /* ** Convert the given stack entity into a string if it isn't one ** already. */ -#define Stringify(P,I) if((aStack[I].flags & STK_Str)==0){hardStringify(P,I);} +#define Stringify(P,I) if((aStack[I].flags & MEM_Str)==0){hardStringify(P,I);} static int hardStringify(Vdbe *p, int i){ - Stack *pStack = &p->aStack[i]; + Mem *pStack = &p->aStack[i]; int fg = pStack->flags; - if( fg & STK_Real ){ - sqlite_snprintf(sizeof(pStack->z),pStack->z,"%.15g",pStack->r); - }else if( fg & STK_Int ){ - sqlite_snprintf(sizeof(pStack->z),pStack->z,"%d",pStack->i); + if( fg & MEM_Real ){ + sqlite_snprintf(sizeof(pStack->zShort),pStack->zShort,"%.15g",pStack->r); + }else if( fg & MEM_Int ){ + sqlite_snprintf(sizeof(pStack->zShort),pStack->zShort,"%d",pStack->i); }else{ - pStack->z[0] = 0; + pStack->zShort[0] = 0; } - p->zStack[i] = pStack->z; - pStack->n = strlen(pStack->z)+1; - pStack->flags = STK_Str; + p->aStack[i].z = pStack->zShort; + pStack->n = strlen(pStack->zShort)+1; + pStack->flags = MEM_Str; return 0; } /* ** Convert the given stack entity into a string that has been obtained @@ -204,62 +204,61 @@ ** from sqliteMalloc(). This is different from Stringify() above in that ** Stringify() will use the NBFS bytes of static string space if the string ** will fit but this routine always mallocs for space. ** Return non-zero if we run out of memory. */ -#define Dynamicify(P,I) ((aStack[I].flags & STK_Dyn)==0 ? hardDynamicify(P,I):0) +#define Dynamicify(P,I) ((aStack[I].flags & MEM_Dyn)==0 ? hardDynamicify(P,I):0) static int hardDynamicify(Vdbe *p, int i){ - Stack *pStack = &p->aStack[i]; + Mem *pStack = &p->aStack[i]; int fg = pStack->flags; char *z; - if( (fg & STK_Str)==0 ){ + if( (fg & MEM_Str)==0 ){ hardStringify(p, i); } - assert( (fg & STK_Dyn)==0 ); + assert( (fg & MEM_Dyn)==0 ); z = sqliteMallocRaw( pStack->n ); if( z==0 ) return 1; - memcpy(z, p->zStack[i], pStack->n); - p->zStack[i] = z; - pStack->flags |= STK_Dyn; + memcpy(z, p->aStack[i].z, pStack->n); + p->aStack[i].z = z; + pStack->flags |= MEM_Dyn; return 0; } /* -** An ephemeral string value (signified by the STK_Ephem flag) contains +** An ephemeral string value (signified by the MEM_Ephem flag) contains ** a pointer to a dynamically allocated string where some other entity ** is responsible for deallocating that string. Because the stack entry ** does not control the string, it might be deleted without the stack ** entry knowing it. ** ** This routine converts an ephemeral string into a dynamically allocated ** string that the stack entry itself controls. In other words, it -** converts an STK_Ephem string into an STK_Dyn string. +** converts an MEM_Ephem string into an MEM_Dyn string. */ #define Deephemeralize(P,I) \ - if( ((P)->aStack[I].flags&STK_Ephem)!=0 && hardDeephem(P,I) ){ goto no_mem;} + if( ((P)->aStack[I].flags&MEM_Ephem)!=0 && hardDeephem(P,I) ){ goto no_mem;} static int hardDeephem(Vdbe *p, int i){ - Stack *pStack = &p->aStack[i]; - char **pzStack = &p->zStack[i]; + Mem *pStack = &p->aStack[i]; char *z; - assert( (pStack->flags & STK_Ephem)!=0 ); + assert( (pStack->flags & MEM_Ephem)!=0 ); z = sqliteMallocRaw( pStack->n ); if( z==0 ) return 1; - memcpy(z, *pzStack, pStack->n); - *pzStack = z; - pStack->flags &= ~STK_Ephem; - pStack->flags |= STK_Dyn; + memcpy(z, pStack->z, pStack->n); + pStack->z = z; + pStack->flags &= ~MEM_Ephem; + pStack->flags |= MEM_Dyn; return 0; } /* ** Release the memory associated with the given stack level */ -#define Release(P,I) if((P)->aStack[I].flags&STK_Dyn){ hardRelease(P,I); } +#define Release(P,I) if((P)->aStack[I].flags&MEM_Dyn){ hardRelease(P,I); } static void hardRelease(Vdbe *p, int i){ - sqliteFree(p->zStack[i]); - p->zStack[i] = 0; - p->aStack[i].flags &= ~(STK_Str|STK_Dyn|STK_Static|STK_Ephem); + sqliteFree(p->aStack[i].z); + p->aStack[i].z = 0; + p->aStack[i].flags &= ~(MEM_Str|MEM_Dyn|MEM_Static|MEM_Ephem); } /* ** Return TRUE if zNum is a 32-bit signed integer and write ** the value of the integer into *pNum. If zNum is not an integer @@ -295,41 +294,41 @@ ** ** Any prior string or real representation is invalidated. ** NULLs are converted into 0. */ #define Integerify(P,I) \ - if(((P)->aStack[(I)].flags&STK_Int)==0){ hardIntegerify(P,I); } + if(((P)->aStack[(I)].flags&MEM_Int)==0){ hardIntegerify(P,I); } static void hardIntegerify(Vdbe *p, int i){ - if( p->aStack[i].flags & STK_Real ){ + if( p->aStack[i].flags & MEM_Real ){ p->aStack[i].i = (int)p->aStack[i].r; Release(p, i); - }else if( p->aStack[i].flags & STK_Str ){ - toInt(p->zStack[i], &p->aStack[i].i); + }else if( p->aStack[i].flags & MEM_Str ){ + toInt(p->aStack[i].z, &p->aStack[i].i); Release(p, i); }else{ p->aStack[i].i = 0; } - p->aStack[i].flags = STK_Int; + p->aStack[i].flags = MEM_Int; } /* ** Get a valid Real representation for the given stack element. ** ** Any prior string or integer representation is retained. ** NULLs are converted into 0.0. */ #define Realify(P,I) \ - if(((P)->aStack[(I)].flags&STK_Real)==0){ hardRealify(P,I); } + if(((P)->aStack[(I)].flags&MEM_Real)==0){ hardRealify(P,I); } static void hardRealify(Vdbe *p, int i){ - if( p->aStack[i].flags & STK_Str ){ - p->aStack[i].r = sqliteAtoF(p->zStack[i]); - }else if( p->aStack[i].flags & STK_Int ){ + if( p->aStack[i].flags & MEM_Str ){ + p->aStack[i].r = sqliteAtoF(p->aStack[i].z); + }else if( p->aStack[i].flags & MEM_Int ){ p->aStack[i].r = p->aStack[i].i; }else{ p->aStack[i].r = 0.0; } - p->aStack[i].flags |= STK_Real; + p->aStack[i].flags |= MEM_Real; } /* ** The parameters are pointers to the head of two sorted lists ** of Sorter structures. Merge these two lists together and return @@ -484,12 +483,11 @@ ){ int pc; /* The program counter */ Op *pOp; /* Current operation */ int rc = SQLITE_OK; /* Value to return */ sqlite *db = p->db; /* The database */ - char **zStack = p->zStack; /* Text stack */ - Stack *aStack = p->aStack; /* Additional stack information */ + Mem *aStack = p->aStack; /* The operand stack */ char zBuf[100]; /* Space to sprintf() an integer */ #ifdef VDBE_PROFILE unsigned long long start; /* CPU clock count at start of opcode */ int origPc; /* Program counter at start of opcode */ #endif @@ -660,14 +658,14 @@ ** then it is assumed to be a string representation of the same integer. */ case OP_Integer: { int i = ++p->tos; aStack[i].i = pOp->p1; - aStack[i].flags = STK_Int; + aStack[i].flags = MEM_Int; if( pOp->p3 ){ - zStack[i] = pOp->p3; - aStack[i].flags |= STK_Str | STK_Static; + aStack[i].z = pOp->p3; + aStack[i].flags |= MEM_Str | MEM_Static; aStack[i].n = strlen(pOp->p3)+1; } break; } @@ -679,17 +677,17 @@ case OP_String: { int i = ++p->tos; char *z; z = pOp->p3; if( z==0 ){ - zStack[i] = 0; + aStack[i].z = 0; aStack[i].n = 0; - aStack[i].flags = STK_Null; + aStack[i].flags = MEM_Null; }else{ - zStack[i] = z; + aStack[i].z = z; aStack[i].n = strlen(z) + 1; - aStack[i].flags = STK_Str | STK_Static; + aStack[i].flags = MEM_Str | MEM_Static; } break; } /* Opcode: Variable P1 * * @@ -703,17 +701,17 @@ */ case OP_Variable: { int i = ++p->tos; int j = pOp->p1 - 1; if( j>=0 && jnVar && p->azVar[j]!=0 ){ - zStack[i] = p->azVar[j]; + aStack[i].z = p->azVar[j]; aStack[i].n = p->anVar[j]; - aStack[i].flags = STK_Str | STK_Static; + aStack[i].flags = MEM_Str | MEM_Static; }else{ - zStack[i] = 0; + aStack[i].z = 0; aStack[i].n = 0; - aStack[i].flags = STK_Null; + aStack[i].flags = MEM_Null; } break; } /* Opcode: Pop P1 * * @@ -744,26 +742,26 @@ case OP_Dup: { int i = p->tos - pOp->p1; int j = ++p->tos; VERIFY( if( i<0 ) goto not_enough_stack; ) memcpy(&aStack[j], &aStack[i], sizeof(aStack[i])-NBFS); - if( aStack[j].flags & STK_Str ){ - int isStatic = (aStack[j].flags & STK_Static)!=0; + if( aStack[j].flags & MEM_Str ){ + int isStatic = (aStack[j].flags & MEM_Static)!=0; if( pOp->p2 || isStatic ){ - zStack[j] = zStack[i]; - aStack[j].flags &= ~STK_Dyn; - if( !isStatic ) aStack[j].flags |= STK_Ephem; + aStack[j].z = aStack[i].z; + aStack[j].flags &= ~MEM_Dyn; + if( !isStatic ) aStack[j].flags |= MEM_Ephem; }else if( aStack[i].n<=NBFS ){ - memcpy(aStack[j].z, zStack[i], aStack[j].n); - zStack[j] = aStack[j].z; - aStack[j].flags &= ~(STK_Static|STK_Dyn|STK_Ephem); + memcpy(aStack[j].zShort, aStack[i].z, aStack[j].n); + aStack[j].z = aStack[j].zShort; + aStack[j].flags &= ~(MEM_Static|MEM_Dyn|MEM_Ephem); }else{ - zStack[j] = sqliteMallocRaw( aStack[j].n ); - if( zStack[j]==0 ) goto no_mem; - memcpy(zStack[j], zStack[i], aStack[j].n); - aStack[j].flags &= ~(STK_Static|STK_Ephem); - aStack[j].flags |= STK_Dyn; + aStack[j].z = sqliteMallocRaw( aStack[j].n ); + if( aStack[j].z==0 ) goto no_mem; + memcpy(aStack[j].z, aStack[i].z, aStack[j].n); + aStack[j].flags &= ~(MEM_Static|MEM_Ephem); + aStack[j].flags |= MEM_Dyn; } } break; } @@ -779,33 +777,29 @@ */ case OP_Pull: { int from = p->tos - pOp->p1; int to = p->tos; int i; - Stack ts; - char *tz; + Mem ts; VERIFY( if( from<0 ) goto not_enough_stack; ) Deephemeralize(p, from); ts = aStack[from]; - tz = zStack[from]; Deephemeralize(p, to); for(i=from; itos; int to = p->tos - pOp->p1; VERIFY( if( to<0 ) goto not_enough_stack; ) - if( aStack[to].flags & STK_Dyn ){ - sqliteFree(zStack[to]); + if( aStack[to].flags & MEM_Dyn ){ + sqliteFree(aStack[to].z); } Deephemeralize(p, from); aStack[to] = aStack[from]; - if( aStack[to].flags & (STK_Dyn|STK_Static|STK_Ephem) ){ - zStack[to] = zStack[from]; + if( aStack[to].flags & (MEM_Dyn|MEM_Static|MEM_Ephem) ){ + aStack[to].z = aStack[from].z; }else{ - zStack[to] = aStack[to].z; + aStack[to].z = aStack[to].zShort; } aStack[from].flags = 0; p->tos--; break; } @@ -855,26 +849,27 @@ case OP_Callback: { int i = p->tos - pOp->p1 + 1; int j; VERIFY( if( i<0 ) goto not_enough_stack; ) for(j=i; j<=p->tos; j++){ - if( aStack[j].flags & STK_Null ){ - zStack[j] = 0; + if( aStack[j].flags & MEM_Null ){ + aStack[j].z = 0; }else{ Stringify(p, j); } + p->zArgv[j] = aStack[j].z; } - zStack[p->tos+1] = 0; + p->zArgv[p->tos+1] = 0; if( p->xCallback==0 ){ - p->azResColumn = &zStack[i]; + p->azResColumn = &p->zArgv[i]; p->nResColumn = pOp->p1; p->popStack = pOp->p1; p->pc = pc + 1; return SQLITE_ROW; } if( sqliteSafetyOff(db) ) goto abort_due_to_misuse; - if( p->xCallback(p->pCbArg, pOp->p1, &zStack[i], p->azColName)!=0 ){ + if( p->xCallback(p->pCbArg, pOp->p1, &p->zArgv[i], p->azColName)!=0 ){ rc = SQLITE_ABORT; } if( sqliteSafetyOn(db) ) goto abort_due_to_misuse; p->nCallback++; sqliteVdbePopStack(p, pOp->p1); @@ -937,11 +932,11 @@ if( zSep==0 ) zSep = ""; nSep = strlen(zSep); VERIFY( if( p->tos+1tos-nField+1; i<=p->tos; i++){ - if( aStack[i].flags & STK_Null ){ + if( aStack[i].flags & MEM_Null ){ nByte = -1; break; }else{ Stringify(p, i); nByte += aStack[i].n - 1 + nSep; @@ -948,20 +943,20 @@ } } if( nByte<0 ){ if( pOp->p2==0 ) sqliteVdbePopStack(p, nField); p->tos++; - aStack[p->tos].flags = STK_Null; - zStack[p->tos] = 0; + aStack[p->tos].flags = MEM_Null; + aStack[p->tos].z = 0; break; } zNew = sqliteMallocRaw( nByte ); if( zNew==0 ) goto no_mem; j = 0; for(i=p->tos-nField+1; i<=p->tos; i++){ - if( (aStack[i].flags & STK_Null)==0 ){ - memcpy(&zNew[j], zStack[i], aStack[i].n-1); + if( (aStack[i].flags & MEM_Null)==0 ){ + memcpy(&zNew[j], aStack[i].z, aStack[i].n-1); j += aStack[i].n-1; } if( nSep>0 && itos ){ memcpy(&zNew[j], zSep, nSep); j += nSep; @@ -969,12 +964,12 @@ } zNew[j] = 0; if( pOp->p2==0 ) sqliteVdbePopStack(p, nField); p->tos++; aStack[p->tos].n = nByte; - aStack[p->tos].flags = STK_Str|STK_Dyn; - zStack[p->tos] = zNew; + aStack[p->tos].flags = MEM_Str|MEM_Dyn; + aStack[p->tos].z = zNew; break; } /* Opcode: Add * * * ** @@ -1028,15 +1023,15 @@ case OP_Divide: case OP_Remainder: { int tos = p->tos; int nos = tos - 1; VERIFY( if( nos<0 ) goto not_enough_stack; ) - if( ((aStack[tos].flags | aStack[nos].flags) & STK_Null)!=0 ){ + if( ((aStack[tos].flags | aStack[nos].flags) & MEM_Null)!=0 ){ POPSTACK; Release(p, nos); - aStack[nos].flags = STK_Null; - }else if( (aStack[tos].flags & aStack[nos].flags & STK_Int)==STK_Int ){ + aStack[nos].flags = MEM_Null; + }else if( (aStack[tos].flags & aStack[nos].flags & MEM_Int)==MEM_Int ){ int a, b; a = aStack[tos].i; b = aStack[nos].i; switch( pOp->opcode ){ case OP_Add: b += a; break; @@ -1054,11 +1049,11 @@ } } POPSTACK; Release(p, nos); aStack[nos].i = b; - aStack[nos].flags = STK_Int; + aStack[nos].flags = MEM_Int; }else{ double a, b; Realify(p, tos); Realify(p, nos); a = aStack[tos].r; @@ -1081,18 +1076,18 @@ } } POPSTACK; Release(p, nos); aStack[nos].r = b; - aStack[nos].flags = STK_Real; + aStack[nos].flags = MEM_Real; } break; divide_by_zero: sqliteVdbePopStack(p, 2); p->tos = nos; - aStack[nos].flags = STK_Null; + aStack[nos].flags = MEM_Null; break; } /* Opcode: Function P1 * P3 ** @@ -1108,37 +1103,38 @@ n = pOp->p1; VERIFY( if( n<0 ) goto bad_instruction; ) VERIFY( if( p->tos+1tos-n+1; i<=p->tos; i++){ - if( aStack[i].flags & STK_Null ){ - zStack[i] = 0; + if( aStack[i].flags & MEM_Null ){ + aStack[i].z = 0; }else{ Stringify(p, i); } + p->zArgv[i] = aStack[i].z; } ctx.pFunc = (FuncDef*)pOp->p3; - ctx.s.flags = STK_Null; - ctx.z = 0; + ctx.s.flags = MEM_Null; + ctx.s.z = 0; ctx.isError = 0; ctx.isStep = 0; if( sqliteSafetyOff(db) ) goto abort_due_to_misuse; - (*ctx.pFunc->xFunc)(&ctx, n, (const char**)&zStack[p->tos-n+1]); + (*ctx.pFunc->xFunc)(&ctx, n, (const char**)&p->zArgv[p->tos-n+1]); if( sqliteSafetyOn(db) ) goto abort_due_to_misuse; sqliteVdbePopStack(p, n); p->tos++; aStack[p->tos] = ctx.s; - if( ctx.s.flags & STK_Dyn ){ - zStack[p->tos] = ctx.z; - }else if( ctx.s.flags & STK_Str ){ - zStack[p->tos] = aStack[p->tos].z; + if( ctx.s.flags & MEM_Dyn ){ + aStack[p->tos].z = ctx.s.z; + }else if( ctx.s.flags & MEM_Str ){ + aStack[p->tos].z = aStack[p->tos].zShort; }else{ - zStack[p->tos] = 0; + aStack[p->tos].z = 0; } if( ctx.isError ){ sqliteSetString(&p->zErrMsg, - zStack[p->tos] ? zStack[p->tos] : "user function error", (char*)0); + aStack[p->tos].z ? aStack[p->tos].z : "user function error", (char*)0); rc = SQLITE_ERROR; } break; } @@ -1176,14 +1172,14 @@ case OP_ShiftRight: { int tos = p->tos; int nos = tos - 1; int a, b; VERIFY( if( nos<0 ) goto not_enough_stack; ) - if( (aStack[tos].flags | aStack[nos].flags) & STK_Null ){ + if( (aStack[tos].flags | aStack[nos].flags) & MEM_Null ){ POPSTACK; Release(p,nos); - aStack[nos].flags = STK_Null; + aStack[nos].flags = MEM_Null; break; } Integerify(p, tos); Integerify(p, nos); a = aStack[tos].i; @@ -1196,11 +1192,11 @@ default: /* CANT HAPPEN */ break; } POPSTACK; Release(p, nos); aStack[nos].i = a; - aStack[nos].flags = STK_Int; + aStack[nos].flags = MEM_Int; break; } /* Opcode: AddImm P1 * * ** @@ -1229,28 +1225,28 @@ */ case OP_ForceInt: { int tos = p->tos; int v; VERIFY( if( tos<0 ) goto not_enough_stack; ) - if( (aStack[tos].flags & (STK_Int|STK_Real))==0 - && (zStack[tos]==0 || sqliteIsNumber(zStack[tos])==0) ){ + if( (aStack[tos].flags & (MEM_Int|MEM_Real))==0 + && (aStack[tos].z==0 || sqliteIsNumber(aStack[tos].z)==0) ){ POPSTACK; pc = pOp->p2 - 1; break; } - if( aStack[tos].flags & STK_Int ){ + if( aStack[tos].flags & MEM_Int ){ v = aStack[tos].i + (pOp->p1!=0); }else{ Realify(p, tos); v = (int)aStack[tos].r; if( aStack[tos].r>(double)v ) v++; if( pOp->p1 && aStack[tos].r==(double)v ) v++; } - if( aStack[tos].flags & STK_Dyn ) sqliteFree(zStack[tos]); - zStack[tos] = 0; + if( aStack[tos].flags & MEM_Dyn ) sqliteFree(aStack[tos].z); + aStack[tos].z = 0; aStack[tos].i = v; - aStack[tos].flags = STK_Int; + aStack[tos].flags = MEM_Int; break; } /* Opcode: MustBeInt P1 P2 * ** @@ -1264,28 +1260,28 @@ ** of the stack is unchanged. */ case OP_MustBeInt: { int tos = p->tos; VERIFY( if( tos<0 ) goto not_enough_stack; ) - if( aStack[tos].flags & STK_Int ){ + if( aStack[tos].flags & MEM_Int ){ /* Do nothing */ - }else if( aStack[tos].flags & STK_Real ){ + }else if( aStack[tos].flags & MEM_Real ){ int i = aStack[tos].r; double r = (double)i; if( r!=aStack[tos].r ){ goto mismatch; } aStack[tos].i = i; - }else if( aStack[tos].flags & STK_Str ){ + }else if( aStack[tos].flags & MEM_Str ){ int v; - if( !toInt(zStack[tos], &v) ){ + if( !toInt(aStack[tos].z, &v) ){ double r; - if( !sqliteIsNumber(zStack[tos]) ){ + if( !sqliteIsNumber(aStack[tos].z) ){ goto mismatch; } Realify(p, tos); - assert( (aStack[tos].flags & STK_Real)!=0 ); + assert( (aStack[tos].flags & MEM_Real)!=0 ); v = aStack[tos].r; r = (double)v; if( r!=aStack[tos].r ){ goto mismatch; } @@ -1293,11 +1289,11 @@ aStack[tos].i = v; }else{ goto mismatch; } Release(p, tos); - aStack[tos].flags = STK_Int; + aStack[tos].flags = MEM_Int; break; mismatch: if( pOp->p2==0 ){ rc = SQLITE_MISMATCH; @@ -1431,36 +1427,36 @@ int c, v; int ft, fn; VERIFY( if( nos<0 ) goto not_enough_stack; ) ft = aStack[tos].flags; fn = aStack[nos].flags; - if( (ft | fn) & STK_Null ){ + if( (ft | fn) & MEM_Null ){ POPSTACK; POPSTACK; if( pOp->p2 ){ if( pOp->p1 ) pc = pOp->p2-1; }else{ p->tos++; - aStack[nos].flags = STK_Null; + aStack[nos].flags = MEM_Null; } break; - }else if( (ft & fn & STK_Int)==STK_Int ){ + }else if( (ft & fn & MEM_Int)==MEM_Int ){ c = aStack[nos].i - aStack[tos].i; - }else if( (ft & STK_Int)!=0 && (fn & STK_Str)!=0 && toInt(zStack[nos],&v) ){ + }else if( (ft & MEM_Int)!=0 && (fn & MEM_Str)!=0 && toInt(aStack[nos].z,&v) ){ Release(p, nos); aStack[nos].i = v; - aStack[nos].flags = STK_Int; + aStack[nos].flags = MEM_Int; c = aStack[nos].i - aStack[tos].i; - }else if( (fn & STK_Int)!=0 && (ft & STK_Str)!=0 && toInt(zStack[tos],&v) ){ + }else if( (fn & MEM_Int)!=0 && (ft & MEM_Str)!=0 && toInt(aStack[tos].z,&v) ){ Release(p, tos); aStack[tos].i = v; - aStack[tos].flags = STK_Int; + aStack[tos].flags = MEM_Int; c = aStack[nos].i - aStack[tos].i; }else{ Stringify(p, tos); Stringify(p, nos); - c = sqliteCompare(zStack[nos], zStack[tos]); + c = sqliteCompare(aStack[nos].z, aStack[tos].z); } switch( pOp->opcode ){ case OP_Eq: c = c==0; break; case OP_Ne: c = c!=0; break; case OP_Lt: c = c<0; break; @@ -1472,11 +1468,11 @@ POPSTACK; if( pOp->p2 ){ if( c ) pc = pOp->p2-1; }else{ p->tos++; - aStack[nos].flags = STK_Int; + aStack[nos].flags = MEM_Int; aStack[nos].i = c; } break; } /* INSERT NO CODE HERE! @@ -1594,24 +1590,24 @@ case OP_StrGe: { int tos = p->tos; int nos = tos - 1; int c; VERIFY( if( nos<0 ) goto not_enough_stack; ) - if( (aStack[nos].flags | aStack[tos].flags) & STK_Null ){ + if( (aStack[nos].flags | aStack[tos].flags) & MEM_Null ){ POPSTACK; POPSTACK; if( pOp->p2 ){ if( pOp->p1 ) pc = pOp->p2-1; }else{ p->tos++; - aStack[nos].flags = STK_Null; + aStack[nos].flags = MEM_Null; } break; }else{ Stringify(p, tos); Stringify(p, nos); - c = strcmp(zStack[nos], zStack[tos]); + c = strcmp(aStack[nos].z, aStack[tos].z); } /* The asserts on each case of the following switch are there to verify ** that string comparison opcodes are always exactly 6 greater than the ** corresponding numeric comparison opcodes. The code generator depends ** on this fact. @@ -1628,11 +1624,11 @@ POPSTACK; if( pOp->p2 ){ if( c ) pc = pOp->p2-1; }else{ p->tos++; - aStack[nos].flags = STK_Int; + aStack[nos].flags = MEM_Int; aStack[nos].i = c; } break; } @@ -1653,17 +1649,17 @@ int tos = p->tos; int nos = tos - 1; int v1, v2; /* 0==TRUE, 1==FALSE, 2==UNKNOWN or NULL */ VERIFY( if( nos<0 ) goto not_enough_stack; ) - if( aStack[tos].flags & STK_Null ){ + if( aStack[tos].flags & MEM_Null ){ v1 = 2; }else{ Integerify(p, tos); v1 = aStack[tos].i==0; } - if( aStack[nos].flags & STK_Null ){ + if( aStack[nos].flags & MEM_Null ){ v2 = 2; }else{ Integerify(p, nos); v2 = aStack[nos].i==0; } @@ -1675,14 +1671,14 @@ v1 = or_logic[v1*3+v2]; } POPSTACK; Release(p, nos); if( v1==2 ){ - aStack[nos].flags = STK_Null; + aStack[nos].flags = MEM_Null; }else{ aStack[nos].i = v1==0; - aStack[nos].flags = STK_Int; + aStack[nos].flags = MEM_Int; } break; } /* Opcode: Negative * * * @@ -1699,31 +1695,31 @@ */ case OP_Negative: case OP_AbsValue: { int tos = p->tos; VERIFY( if( tos<0 ) goto not_enough_stack; ) - if( aStack[tos].flags & STK_Real ){ + if( aStack[tos].flags & MEM_Real ){ Release(p, tos); if( pOp->opcode==OP_Negative || aStack[tos].r<0.0 ){ aStack[tos].r = -aStack[tos].r; } - aStack[tos].flags = STK_Real; - }else if( aStack[tos].flags & STK_Int ){ + aStack[tos].flags = MEM_Real; + }else if( aStack[tos].flags & MEM_Int ){ Release(p, tos); if( pOp->opcode==OP_Negative || aStack[tos].i<0 ){ aStack[tos].i = -aStack[tos].i; } - aStack[tos].flags = STK_Int; - }else if( aStack[tos].flags & STK_Null ){ + aStack[tos].flags = MEM_Int; + }else if( aStack[tos].flags & MEM_Null ){ /* Do nothing */ }else{ Realify(p, tos); Release(p, tos); if( pOp->opcode==OP_Negative || aStack[tos].r<0.0 ){ aStack[tos].r = -aStack[tos].r; } - aStack[tos].flags = STK_Real; + aStack[tos].flags = MEM_Real; } break; } /* Opcode: Not * * * @@ -1733,15 +1729,15 @@ ** is unchanged. */ case OP_Not: { int tos = p->tos; VERIFY( if( p->tos<0 ) goto not_enough_stack; ) - if( aStack[tos].flags & STK_Null ) break; /* Do nothing to NULLs */ + if( aStack[tos].flags & MEM_Null ) break; /* Do nothing to NULLs */ Integerify(p, tos); Release(p, tos); aStack[tos].i = !aStack[tos].i; - aStack[tos].flags = STK_Int; + aStack[tos].flags = MEM_Int; break; } /* Opcode: BitNot * * * ** @@ -1750,15 +1746,15 @@ ** value is unchanged. */ case OP_BitNot: { int tos = p->tos; VERIFY( if( p->tos<0 ) goto not_enough_stack; ) - if( aStack[tos].flags & STK_Null ) break; /* Do nothing to NULLs */ + if( aStack[tos].flags & MEM_Null ) break; /* Do nothing to NULLs */ Integerify(p, tos); Release(p, tos); aStack[tos].i = ~aStack[tos].i; - aStack[tos].flags = STK_Int; + aStack[tos].flags = MEM_Int; break; } /* Opcode: Noop * * * ** @@ -1791,11 +1787,11 @@ */ case OP_If: case OP_IfNot: { int c; VERIFY( if( p->tos<0 ) goto not_enough_stack; ) - if( aStack[p->tos].flags & STK_Null ){ + if( aStack[p->tos].flags & MEM_Null ){ c = pOp->p1; }else{ Integerify(p, p->tos); c = aStack[p->tos].i; if( pOp->opcode==OP_IfNot ) c = !c; @@ -1815,11 +1811,11 @@ int i, cnt; cnt = pOp->p1; if( cnt<0 ) cnt = -cnt; VERIFY( if( p->tos+1-cnt<0 ) goto not_enough_stack; ) for(i=0; itos-i].flags & STK_Null ){ + if( aStack[p->tos-i].flags & MEM_Null ){ pc = pOp->p2-1; break; } } if( pOp->p1>0 ) sqliteVdbePopStack(p, cnt); @@ -1835,11 +1831,11 @@ case OP_NotNull: { int i, cnt; cnt = pOp->p1; if( cnt<0 ) cnt = -cnt; VERIFY( if( p->tos+1-cnt<0 ) goto not_enough_stack; ) - for(i=0; itos-i].flags & STK_Null)==0; i++){} + for(i=0; itos-i].flags & MEM_Null)==0; i++){} if( i>=cnt ) pc = pOp->p2-1; if( pOp->p1>0 ) sqliteVdbePopStack(p, cnt); break; } @@ -1895,11 +1891,11 @@ */ nField = pOp->p1; VERIFY( if( p->tos+1tos-nField+1; i<=p->tos; i++){ - if( (aStack[i].flags & STK_Null) ){ + if( (aStack[i].flags & MEM_Null) ){ addUnique = pOp->p2; }else{ Stringify(p, i); nByte += aStack[i].n; } @@ -1931,11 +1927,11 @@ zNewRecord[j++] = (addr>>8)&0xff; if( idxWidth>2 ){ zNewRecord[j++] = (addr>>16)&0xff; } } - if( (aStack[i].flags & STK_Null)==0 ){ + if( (aStack[i].flags & MEM_Null)==0 ){ addr += aStack[i].n; } } zNewRecord[j++] = addr & 0xff; if( idxWidth>1 ){ @@ -1948,27 +1944,27 @@ memcpy(&zNewRecord[j], &p->uniqueCnt, sizeof(p->uniqueCnt)); p->uniqueCnt++; j += sizeof(p->uniqueCnt); } for(i=p->tos-nField+1; i<=p->tos; i++){ - if( (aStack[i].flags & STK_Null)==0 ){ - memcpy(&zNewRecord[j], zStack[i], aStack[i].n); + if( (aStack[i].flags & MEM_Null)==0 ){ + memcpy(&zNewRecord[j], aStack[i].z, aStack[i].n); j += aStack[i].n; } } sqliteVdbePopStack(p, nField); p->tos++; aStack[p->tos].n = nByte; if( nByte<=NBFS ){ assert( zNewRecord==zTemp ); - memcpy(aStack[p->tos].z, zTemp, nByte); - zStack[p->tos] = aStack[p->tos].z; - aStack[p->tos].flags = STK_Str; + memcpy(aStack[p->tos].zShort, zTemp, nByte); + aStack[p->tos].z = aStack[p->tos].zShort; + aStack[p->tos].flags = MEM_Str; }else{ assert( zNewRecord!=zTemp ); - aStack[p->tos].flags = STK_Str | STK_Dyn; - zStack[p->tos] = zNewRecord; + aStack[p->tos].flags = MEM_Str | MEM_Dyn; + aStack[p->tos].z = zNewRecord; } break; } /* Opcode: MakeKey P1 P2 P3 @@ -2059,29 +2055,29 @@ nByte = 0; for(j=0, i=p->tos-nField+1; i<=p->tos; i++, j++){ int flags = aStack[i].flags; int len; char *z; - if( flags & STK_Null ){ + if( flags & MEM_Null ){ nByte += 2; containsNull = 1; }else if( pOp->p3 && pOp->p3[j]=='t' ){ Stringify(p, i); - aStack[i].flags &= ~(STK_Int|STK_Real); + aStack[i].flags &= ~(MEM_Int|MEM_Real); nByte += aStack[i].n+1; - }else if( (flags & (STK_Real|STK_Int))!=0 || sqliteIsNumber(zStack[i]) ){ - if( (flags & (STK_Real|STK_Int))==STK_Int ){ + }else if( (flags & (MEM_Real|MEM_Int))!=0 || sqliteIsNumber(aStack[i].z) ){ + if( (flags & (MEM_Real|MEM_Int))==MEM_Int ){ aStack[i].r = aStack[i].i; - }else if( (flags & (STK_Real|STK_Int))==0 ){ - aStack[i].r = sqliteAtoF(zStack[i]); + }else if( (flags & (MEM_Real|MEM_Int))==0 ){ + aStack[i].r = sqliteAtoF(aStack[i].z); } Release(p, i); - z = aStack[i].z; + z = aStack[i].zShort; sqliteRealToSortable(aStack[i].r, z); len = strlen(z); - zStack[i] = 0; - aStack[i].flags = STK_Real; + aStack[i].z = 0; + aStack[i].flags = MEM_Real; aStack[i].n = len+1; nByte += aStack[i].n+1; }else{ nByte += aStack[i].n+1; } @@ -2097,20 +2093,21 @@ zNewKey = sqliteMallocRaw( nByte ); if( zNewKey==0 ) goto no_mem; } j = 0; for(i=p->tos-nField+1; i<=p->tos; i++){ - if( aStack[i].flags & STK_Null ){ + if( aStack[i].flags & MEM_Null ){ zNewKey[j++] = 'a'; zNewKey[j++] = 0; }else{ - if( aStack[i].flags & (STK_Int|STK_Real) ){ + if( aStack[i].flags & (MEM_Int|MEM_Real) ){ zNewKey[j++] = 'b'; }else{ zNewKey[j++] = 'c'; } - memcpy(&zNewKey[j], zStack[i] ? zStack[i] : aStack[i].z, aStack[i].n); + /*** Is this right? ****/ + memcpy(&zNewKey[j],aStack[i].z?aStack[i].z:aStack[i].zShort,aStack[i].n); j += aStack[i].n; } } if( addRowid ){ u32 iKey; @@ -2124,16 +2121,16 @@ } p->tos++; aStack[p->tos].n = nByte; if( nByte<=NBFS ){ assert( zNewKey==zTemp ); - zStack[p->tos] = aStack[p->tos].z; - memcpy(zStack[p->tos], zTemp, nByte); - aStack[p->tos].flags = STK_Str; + aStack[p->tos].z = aStack[p->tos].zShort; + memcpy(aStack[p->tos].z, zTemp, nByte); + aStack[p->tos].flags = MEM_Str; }else{ - aStack[p->tos].flags = STK_Str|STK_Dyn; - zStack[p->tos] = zNewKey; + aStack[p->tos].flags = MEM_Str|MEM_Dyn; + aStack[p->tos].z = zNewKey; } break; } /* Opcode: IncrKey * * * @@ -2147,18 +2144,18 @@ case OP_IncrKey: { int tos = p->tos; VERIFY( if( tos<0 ) goto bad_instruction ); Stringify(p, tos); - if( aStack[tos].flags & (STK_Static|STK_Ephem) ){ + if( aStack[tos].flags & (MEM_Static|MEM_Ephem) ){ /* CANT HAPPEN. The IncrKey opcode is only applied to keys ** generated by MakeKey or MakeIdxKey and the results of those ** operands are always dynamic strings. */ goto abort_due_to_error; } - zStack[tos][aStack[tos].n-1]++; + aStack[tos].z[aStack[tos].n-1]++; break; } /* Opcode: Checkpoint P1 * * ** @@ -2303,11 +2300,11 @@ assert( pOp->p2p1>=0 && pOp->p1nDb ); assert( db->aDb[pOp->p1].pBt!=0 ); rc = sqliteBtreeGetMeta(db->aDb[pOp->p1].pBt, aMeta); aStack[i].i = aMeta[1+pOp->p2]; - aStack[i].flags = STK_Int; + aStack[i].flags = MEM_Int; break; } /* Opcode: SetCookie P1 P2 * ** @@ -2584,11 +2581,11 @@ assert( i>=0 && inCursor ); pC = &p->aCsr[i]; if( pC->pCursor!=0 ){ int res, oc; pC->nullRow = 0; - if( aStack[tos].flags & STK_Int ){ + if( aStack[tos].flags & MEM_Int ){ int iKey = intToKey(aStack[tos].i); if( pOp->p2==0 && pOp->opcode==OP_MoveTo ){ pC->movetoTarget = iKey; pC->deferredMoveto = 1; POPSTACK; @@ -2597,11 +2594,11 @@ sqliteBtreeMoveto(pC->pCursor, (char*)&iKey, sizeof(int), &res); pC->lastRecno = aStack[tos].i; pC->recnoIsValid = res==0; }else{ Stringify(p, tos); - sqliteBtreeMoveto(pC->pCursor, zStack[tos], aStack[tos].n, &res); + sqliteBtreeMoveto(pC->pCursor, aStack[tos].z, aStack[tos].n, &res); pC->recnoIsValid = 0; } pC->deferredMoveto = 0; sqlite_search_count++; oc = pOp->opcode; @@ -2673,11 +2670,11 @@ Cursor *pC; VERIFY( if( tos<0 ) goto not_enough_stack; ) if( VERIFY( i>=0 && inCursor && ) (pC = &p->aCsr[i])->pCursor!=0 ){ int res, rx; Stringify(p, tos); - rx = sqliteBtreeMoveto(pC->pCursor, zStack[tos], aStack[tos].n, &res); + rx = sqliteBtreeMoveto(pC->pCursor, aStack[tos].z, aStack[tos].n, &res); alreadyExists = rx==SQLITE_OK && res==0; pC->deferredMoveto = 0; } if( pOp->opcode==OP_Found ){ if( alreadyExists ) pc = pOp->p2 - 1; @@ -2730,11 +2727,11 @@ int nKey; /* Number of bytes in K */ /* Make sure K is a string and make zKey point to K */ Stringify(p, nos); - zKey = zStack[nos]; + zKey = aStack[nos].z; nKey = aStack[nos].n; assert( nKey >= 4 ); /* Search for an entry in P1 where all but the last four bytes match K. ** If there is no such entry, jump immediately to P2. @@ -2773,11 +2770,11 @@ ** stack. (These bytes are the record number of an entry that ** violates a UNIQUE constraint.) */ p->tos++; aStack[tos].i = v; - aStack[tos].flags = STK_Int; + aStack[tos].flags = MEM_Int; } break; } /* Opcode: NotExists P1 P2 * @@ -2798,11 +2795,11 @@ int tos = p->tos; BtCursor *pCrsr; VERIFY( if( tos<0 ) goto not_enough_stack; ) if( VERIFY( i>=0 && inCursor && ) (pCrsr = p->aCsr[i].pCursor)!=0 ){ int res, rx, iKey; - assert( aStack[tos].flags & STK_Int ); + assert( aStack[tos].flags & MEM_Int ); iKey = intToKey(aStack[tos].i); rx = sqliteBtreeMoveto(pCrsr, (char*)&iKey, sizeof(int), &res); p->aCsr[i].lastRecno = aStack[tos].i; p->aCsr[i].recnoIsValid = res==0; p->aCsr[i].nullRow = 0; @@ -2910,11 +2907,11 @@ pC->recnoIsValid = 0; pC->deferredMoveto = 0; } p->tos++; aStack[p->tos].i = v; - aStack[p->tos].flags = STK_Int; + aStack[p->tos].flags = MEM_Int; break; } /* Opcode: PutIntKey P1 P2 * ** @@ -2950,13 +2947,13 @@ char *zKey; int nKey, iKey; if( pOp->opcode==OP_PutStrKey ){ Stringify(p, nos); nKey = aStack[nos].n; - zKey = zStack[nos]; + zKey = aStack[nos].z; }else{ - assert( aStack[nos].flags & STK_Int ); + assert( aStack[nos].flags & MEM_Int ); nKey = sizeof(int); iKey = intToKey(aStack[nos].i); zKey = (char*)&iKey; if( pOp->p2 ){ db->nChange++; @@ -2973,24 +2970,24 @@ */ assert( pOp->opcode==OP_PutIntKey ); sqliteFree(pC->pData); pC->iKey = iKey; pC->nData = aStack[tos].n; - if( aStack[tos].flags & STK_Dyn ){ - pC->pData = zStack[tos]; - zStack[tos] = 0; - aStack[tos].flags = STK_Null; + if( aStack[tos].flags & MEM_Dyn ){ + pC->pData = aStack[tos].z; + aStack[tos].z = 0; + aStack[tos].flags = MEM_Null; }else{ pC->pData = sqliteMallocRaw( pC->nData ); if( pC->pData ){ - memcpy(pC->pData, zStack[tos], pC->nData); + memcpy(pC->pData, aStack[tos].z, pC->nData); } } pC->nullRow = 0; }else{ rc = sqliteBtreeInsert(pC->pCursor, zKey, nKey, - zStack[tos], aStack[tos].n); + aStack[tos].z, aStack[tos].n); } pC->recnoIsValid = 0; pC->deferredMoveto = 0; } POPSTACK; @@ -3066,43 +3063,43 @@ int n; assert( i>=0 && inCursor ); pC = &p->aCsr[i]; if( pC->nullRow ){ - aStack[tos].flags = STK_Null; + aStack[tos].flags = MEM_Null; }else if( pC->pCursor!=0 ){ BtCursor *pCrsr = pC->pCursor; sqliteVdbeCursorMoveto(pC); if( pC->nullRow ){ - aStack[tos].flags = STK_Null; + aStack[tos].flags = MEM_Null; break; }else if( pC->keyAsData || pOp->opcode==OP_RowKey ){ sqliteBtreeKeySize(pCrsr, &n); }else{ sqliteBtreeDataSize(pCrsr, &n); } aStack[tos].n = n; if( n<=NBFS ){ - aStack[tos].flags = STK_Str; - zStack[tos] = aStack[tos].z; + aStack[tos].flags = MEM_Str; + aStack[tos].z = aStack[tos].zShort; }else{ char *z = sqliteMallocRaw( n ); if( z==0 ) goto no_mem; - aStack[tos].flags = STK_Str | STK_Dyn; - zStack[tos] = z; + aStack[tos].flags = MEM_Str | MEM_Dyn; + aStack[tos].z = z; } if( pC->keyAsData || pOp->opcode==OP_RowKey ){ - sqliteBtreeKey(pCrsr, 0, n, zStack[tos]); + sqliteBtreeKey(pCrsr, 0, n, aStack[tos].z); }else{ - sqliteBtreeData(pCrsr, 0, n, zStack[tos]); + sqliteBtreeData(pCrsr, 0, n, aStack[tos].z); } }else if( pC->pseudoTable ){ aStack[tos].n = pC->nData; - zStack[tos] = pC->pData; - aStack[tos].flags = STK_Str|STK_Ephem; + aStack[tos].z = pC->pData; + aStack[tos].flags = MEM_Str|MEM_Ephem; }else{ - aStack[tos].flags = STK_Null; + aStack[tos].flags = MEM_Null; } break; } /* Opcode: Column P1 P2 * @@ -3136,12 +3133,12 @@ unsigned char aHdr[10]; assert( inCursor ); if( i<0 ){ VERIFY( if( tos+i<0 ) goto bad_instruction; ) - VERIFY( if( (aStack[tos+i].flags & STK_Str)==0 ) goto bad_instruction; ) - zRec = zStack[tos+i]; + VERIFY( if( (aStack[tos+i].flags & MEM_Str)==0 ) goto bad_instruction; ) + zRec = aStack[tos+i].z; payloadSize = aStack[tos+i].n; }else if( (pC = &p->aCsr[i])->pCursor!=0 ){ sqliteVdbeCursorMoveto(pC); zRec = 0; pCrsr = pC->pCursor; @@ -3162,11 +3159,11 @@ /* Figure out how many bytes in the column data and where the column ** data begins. */ if( payloadSize==0 ){ - aStack[tos].flags = STK_Null; + aStack[tos].flags = MEM_Null; p->tos = tos; break; }else if( payloadSize<256 ){ idxWidth = 1; }else if( payloadSize<65536 ){ @@ -3206,31 +3203,31 @@ /* amt and offset now hold the offset to the start of data and the ** amount of data. Go get the data and put it on the stack. */ if( amt==0 ){ - aStack[tos].flags = STK_Null; + aStack[tos].flags = MEM_Null; }else if( zRec ){ - aStack[tos].flags = STK_Str | STK_Ephem; + aStack[tos].flags = MEM_Str | MEM_Ephem; aStack[tos].n = amt; - zStack[tos] = &zRec[offset]; + aStack[tos].z = &zRec[offset]; }else{ if( amt<=NBFS ){ - aStack[tos].flags = STK_Str; - zStack[tos] = aStack[tos].z; + aStack[tos].flags = MEM_Str; + aStack[tos].z = aStack[tos].zShort; aStack[tos].n = amt; }else{ char *z = sqliteMallocRaw( amt ); if( z==0 ) goto no_mem; - aStack[tos].flags = STK_Str | STK_Dyn; - zStack[tos] = z; + aStack[tos].flags = MEM_Str | MEM_Dyn; + aStack[tos].z = z; aStack[tos].n = amt; } if( pC->keyAsData ){ - sqliteBtreeKey(pCrsr, offset, amt, zStack[tos]); + sqliteBtreeKey(pCrsr, offset, amt, aStack[tos].z); }else{ - sqliteBtreeData(pCrsr, offset, amt, zStack[tos]); + sqliteBtreeData(pCrsr, offset, amt, aStack[tos].z); } } p->tos = tos; break; } @@ -3254,19 +3251,19 @@ if( pC->recnoIsValid ){ v = pC->lastRecno; }else if( pC->pseudoTable ){ v = keyToInt(pC->iKey); }else if( pC->nullRow || pC->pCursor==0 ){ - aStack[tos].flags = STK_Null; + aStack[tos].flags = MEM_Null; break; }else{ assert( pC->pCursor!=0 ); sqliteBtreeKey(pC->pCursor, 0, sizeof(u32), (char*)&v); v = keyToInt(v); } aStack[tos].i = v; - aStack[tos].flags = STK_Int; + aStack[tos].flags = MEM_Int; break; } /* Opcode: FullKey P1 * * ** @@ -3297,17 +3294,17 @@ goto abort_due_to_error; } if( amt>NBFS ){ z = sqliteMallocRaw( amt ); if( z==0 ) goto no_mem; - aStack[tos].flags = STK_Str | STK_Dyn; + aStack[tos].flags = MEM_Str | MEM_Dyn; }else{ - z = aStack[tos].z; - aStack[tos].flags = STK_Str; + z = aStack[tos].zShort; + aStack[tos].flags = MEM_Str; } sqliteBtreeKey(pCrsr, 0, amt, z); - zStack[tos] = z; + aStack[tos].z = z; aStack[tos].n = amt; } break; } @@ -3446,11 +3443,11 @@ int tos = p->tos; BtCursor *pCrsr; VERIFY( if( tos<0 ) goto not_enough_stack; ) if( VERIFY( i>=0 && inCursor && ) (pCrsr = p->aCsr[i].pCursor)!=0 ){ int nKey = aStack[tos].n; - const char *zKey = zStack[tos]; + const char *zKey = aStack[tos].z; if( pOp->p2 ){ int res, n; assert( aStack[tos].n >= 4 ); rc = sqliteBtreeMoveto(pCrsr, zKey, nKey-4, &res); if( rc!=SQLITE_OK ) goto abort_due_to_error; @@ -3492,11 +3489,11 @@ int tos = p->tos; BtCursor *pCrsr; VERIFY( if( tos<0 ) goto not_enough_stack; ) if( VERIFY( i>=0 && inCursor && ) (pCrsr = p->aCsr[i].pCursor)!=0 ){ int rx, res; - rx = sqliteBtreeMoveto(pCrsr, zStack[tos], aStack[tos].n, &res); + rx = sqliteBtreeMoveto(pCrsr, aStack[tos].z, aStack[tos].n, &res); if( rx==SQLITE_OK && res==0 ){ rc = sqliteBtreeDelete(pCrsr); } assert( p->aCsr[i].deferredMoveto==0 ); } @@ -3522,16 +3519,16 @@ int v; int sz; assert( p->aCsr[i].deferredMoveto==0 ); sqliteBtreeKeySize(pCrsr, &sz); if( sz=0 && inCursor && ) (pCrsr = p->aCsr[i].pCursor)!=0 ){ int res, rc; Stringify(p, tos); assert( p->aCsr[i].deferredMoveto==0 ); - rc = sqliteBtreeKeyCompare(pCrsr, zStack[tos], aStack[tos].n, 4, &res); + rc = sqliteBtreeKeyCompare(pCrsr, aStack[tos].z, aStack[tos].n, 4, &res); if( rc!=SQLITE_OK ){ break; } if( pOp->opcode==OP_IdxLT ){ res = -res; @@ -3603,12 +3600,12 @@ int tos = p->tos; int k, n; const char *z; assert( tos>=0 ); - assert( aStack[tos].flags & STK_Str ); - z = zStack[tos]; + assert( aStack[tos].flags & MEM_Str ); + z = aStack[tos].z; n = aStack[tos].n; for(k=0; k0; i--){ if( z[k]=='a' ){ pc = pOp->p2-1; break; @@ -3690,11 +3687,11 @@ }else{ rc = sqliteBtreeCreateIndex(db->aDb[pOp->p2].pBt, &pgno); } if( rc==SQLITE_OK ){ aStack[i].i = pgno; - aStack[i].flags = STK_Int; + aStack[i].flags = MEM_Int; *(u32*)pOp->p3 = pgno; pOp->p3 = 0; } break; } @@ -3737,17 +3734,17 @@ sqliteHashClear(&pSet->hash); pSet->prev = 0; z = sqliteBtreeIntegrityCheck(db->aDb[pOp->p2].pBt, aRoot, nRoot); if( z==0 || z[0]==0 ){ if( z ) sqliteFree(z); - zStack[tos] = "ok"; + aStack[tos].z = "ok"; aStack[tos].n = 3; - aStack[tos].flags = STK_Str | STK_Static; + aStack[tos].flags = MEM_Str | MEM_Static; }else{ - zStack[tos] = z; + aStack[tos].z = z; aStack[tos].n = strlen(z) + 1; - aStack[tos].flags = STK_Str | STK_Dyn; + aStack[tos].flags = MEM_Str | MEM_Dyn; } sqliteFree(aRoot); break; } @@ -3801,12 +3798,12 @@ || pKeylist->nRead>=pKeylist->nUsed || pKeylist->nRead>=pKeylist->nKey ) goto bad_instruction; ) p->tos++; aStack[p->tos].i = pKeylist->aKey[pKeylist->nRead++]; - aStack[p->tos].flags = STK_Int; - zStack[p->tos] = 0; + aStack[p->tos].flags = MEM_Int; + aStack[p->tos].z = 0; if( pKeylist->nRead>=pKeylist->nUsed ){ p->pList = pKeylist->pNext; sqliteFree(pKeylist); } }else{ @@ -3875,23 +3872,23 @@ if( Dynamicify(p, tos) || Dynamicify(p, nos) ) goto no_mem; pSorter = sqliteMallocRaw( sizeof(Sorter) ); if( pSorter==0 ) goto no_mem; pSorter->pNext = p->pSort; p->pSort = pSorter; - assert( aStack[tos].flags & STK_Dyn ); + assert( aStack[tos].flags & MEM_Dyn ); pSorter->nKey = aStack[tos].n; - pSorter->zKey = zStack[tos]; + pSorter->zKey = aStack[tos].z; pSorter->nData = aStack[nos].n; - if( aStack[nos].flags & STK_Dyn ){ - pSorter->pData = zStack[nos]; + if( aStack[nos].flags & MEM_Dyn ){ + pSorter->pData = aStack[nos].z; }else{ - pSorter->pData = sqliteStrDup(zStack[nos]); + pSorter->pData = sqliteStrDup(aStack[nos].z); } aStack[tos].flags = 0; aStack[nos].flags = 0; - zStack[tos] = 0; - zStack[nos] = 0; + aStack[tos].z = 0; + aStack[nos].z = 0; p->tos -= 2; break; } /* Opcode: SortMakeRec P1 * * @@ -3909,33 +3906,33 @@ nField = pOp->p1; VERIFY( if( p->tos+1tos-nField+1; i<=p->tos; i++){ - if( (aStack[i].flags & STK_Null)==0 ){ + if( (aStack[i].flags & MEM_Null)==0 ){ Stringify(p, i); nByte += aStack[i].n; } } nByte += sizeof(char*)*(nField+1); azArg = sqliteMallocRaw( nByte ); if( azArg==0 ) goto no_mem; z = (char*)&azArg[nField+1]; for(j=0, i=p->tos-nField+1; i<=p->tos; i++, j++){ - if( aStack[i].flags & STK_Null ){ + if( aStack[i].flags & MEM_Null ){ azArg[j] = 0; }else{ azArg[j] = z; - strcpy(z, zStack[i]); + strcpy(z, aStack[i].z); z += aStack[i].n; } } sqliteVdbePopStack(p, nField); p->tos++; aStack[p->tos].n = nByte; - zStack[p->tos] = (char*)azArg; - aStack[p->tos].flags = STK_Str|STK_Dyn; + aStack[p->tos].z = (char*)azArg; + aStack[p->tos].flags = MEM_Str|MEM_Dyn; break; } /* Opcode: SortMakeKey * * P3 ** @@ -3960,11 +3957,11 @@ nField = strlen(pOp->p3); VERIFY( if( p->tos+1tos-nField+1; i<=p->tos; i++){ - if( (aStack[i].flags & STK_Null)!=0 ){ + if( (aStack[i].flags & MEM_Null)!=0 ){ nByte += 2; }else{ Stringify(p, i); nByte += aStack[i].n+2; } @@ -3972,28 +3969,28 @@ zNewKey = sqliteMallocRaw( nByte ); if( zNewKey==0 ) goto no_mem; j = 0; k = 0; for(i=p->tos-nField+1; i<=p->tos; i++){ - if( (aStack[i].flags & STK_Null)!=0 ){ + if( (aStack[i].flags & MEM_Null)!=0 ){ zNewKey[j++] = 'N'; zNewKey[j++] = 0; k++; }else{ zNewKey[j++] = pOp->p3[k++]; - memcpy(&zNewKey[j], zStack[i], aStack[i].n-1); + memcpy(&zNewKey[j], aStack[i].z, aStack[i].n-1); j += aStack[i].n-1; zNewKey[j++] = 0; } } zNewKey[j] = 0; assert( jtos++; aStack[p->tos].n = nByte; - aStack[p->tos].flags = STK_Str|STK_Dyn; - zStack[p->tos] = zNewKey; + aStack[p->tos].flags = MEM_Str|MEM_Dyn; + aStack[p->tos].z = zNewKey; break; } /* Opcode: Sort * * * ** @@ -4043,13 +4040,13 @@ Sorter *pSorter = p->pSort; CHECK_FOR_INTERRUPT; if( pSorter!=0 ){ p->pSort = pSorter->pNext; p->tos++; - zStack[p->tos] = pSorter->pData; + aStack[p->tos].z = pSorter->pData; aStack[p->tos].n = pSorter->nData; - aStack[p->tos].flags = STK_Str|STK_Dyn; + aStack[p->tos].flags = MEM_Str|MEM_Dyn; sqliteFree(pSorter->zKey); sqliteFree(pSorter); }else{ pc = pOp->p2 - 1; } @@ -4066,17 +4063,17 @@ case OP_SortCallback: { int i = p->tos; VERIFY( if( i<0 ) goto not_enough_stack; ) if( p->xCallback==0 ){ p->pc = pc+1; - p->azResColumn = (char**)zStack[i]; + p->azResColumn = (char**)aStack[i].z; p->nResColumn = pOp->p1; p->popStack = 1; return SQLITE_ROW; }else{ if( sqliteSafetyOff(db) ) goto abort_due_to_misuse; - if( p->xCallback(p->pCbArg, pOp->p1, (char**)zStack[i], p->azColName)!=0 ){ + if( p->xCallback(p->pCbArg, pOp->p1, (char**)aStack[i].z, p->azColName)!=0){ rc = SQLITE_ABORT; } if( sqliteSafetyOn(db) ) goto abort_due_to_misuse; p->nCallback++; } @@ -4251,16 +4248,16 @@ z = 0; } p->tos++; if( z ){ aStack[p->tos].n = strlen(z) + 1; - zStack[p->tos] = z; - aStack[p->tos].flags = STK_Str; + aStack[p->tos].z = z; + aStack[p->tos].flags = MEM_Str; }else{ aStack[p->tos].n = 0; - zStack[p->tos] = 0; - aStack[p->tos].flags = STK_Null; + aStack[p->tos].z = 0; + aStack[p->tos].flags = MEM_Null; } break; } /* Opcode: MemStore P1 P2 * @@ -4287,45 +4284,45 @@ aMem = sqliteRealloc(p->aMem, p->nMem*sizeof(p->aMem[0])); if( aMem==0 ) goto no_mem; if( aMem!=p->aMem ){ int j; for(j=0; jaMem[j].s.z ){ - aMem[j].z = aMem[j].s.z; + if( aMem[j].z==p->aMem[j].zShort ){ + aMem[j].z = aMem[j].zShort; } } } p->aMem = aMem; if( nOldnMem ){ memset(&p->aMem[nOld], 0, sizeof(p->aMem[0])*(p->nMem-nOld)); } } pMem = &p->aMem[i]; - flags = pMem->s.flags; - if( flags & STK_Dyn ){ + flags = pMem->flags; + if( flags & MEM_Dyn ){ zOld = pMem->z; }else{ zOld = 0; } - pMem->s = aStack[tos]; - flags = pMem->s.flags; - if( flags & (STK_Static|STK_Dyn|STK_Ephem) ){ - if( (flags & STK_Static)!=0 || (pOp->p2 && (flags & STK_Dyn)!=0) ){ - pMem->z = zStack[tos]; - }else if( flags & STK_Str ){ - pMem->z = sqliteMallocRaw( pMem->s.n ); + *pMem = aStack[tos]; + flags = pMem->flags; + if( flags & (MEM_Static|MEM_Dyn|MEM_Ephem) ){ + if( (flags & MEM_Static)!=0 || (pOp->p2 && (flags & MEM_Dyn)!=0) ){ + /* pMem->z = zStack[tos]; *** do nothing */ + }else if( flags & MEM_Str ){ + pMem->z = sqliteMallocRaw( pMem->n ); if( pMem->z==0 ) goto no_mem; - memcpy(pMem->z, zStack[tos], pMem->s.n); - pMem->s.flags |= STK_Dyn; - pMem->s.flags &= ~(STK_Static|STK_Ephem); + memcpy(pMem->z, aStack[tos].z, pMem->n); + pMem->flags |= MEM_Dyn; + pMem->flags &= ~(MEM_Static|MEM_Ephem); } }else{ - pMem->z = pMem->s.z; + pMem->z = pMem->zShort; } if( zOld ) sqliteFree(zOld); if( pOp->p2 ){ - zStack[tos] = 0; + aStack[tos].z = 0; aStack[tos].flags = 0; POPSTACK; } break; } @@ -4341,15 +4338,15 @@ */ case OP_MemLoad: { int tos = ++p->tos; int i = pOp->p1; VERIFY( if( i<0 || i>=p->nMem ) goto bad_instruction; ) - memcpy(&aStack[tos], &p->aMem[i].s, sizeof(aStack[tos])-NBFS);; - if( aStack[tos].flags & STK_Str ){ - zStack[tos] = p->aMem[i].z; - aStack[tos].flags |= STK_Ephem; - aStack[tos].flags &= ~(STK_Dyn|STK_Static); + memcpy(&aStack[tos], &p->aMem[i], sizeof(aStack[tos])-NBFS);; + if( aStack[tos].flags & MEM_Str ){ + /* aStack[tos].z = p->aMem[i].z; */ + aStack[tos].flags |= MEM_Ephem; + aStack[tos].flags &= ~(MEM_Dyn|MEM_Static); } break; } /* Opcode: MemIncr P1 P2 * @@ -4364,13 +4361,13 @@ case OP_MemIncr: { int i = pOp->p1; Mem *pMem; VERIFY( if( i<0 || i>=p->nMem ) goto bad_instruction; ) pMem = &p->aMem[i]; - VERIFY( if( pMem->s.flags != STK_Int ) goto bad_instruction; ) - pMem->s.i++; - if( pOp->p2>0 && pMem->s.i>0 ){ + VERIFY( if( pMem->flags != MEM_Int ) goto bad_instruction; ) + pMem->i++; + if( pOp->p2>0 && pMem->i>0 ){ pc = pOp->p2 - 1; } break; } @@ -4417,30 +4414,31 @@ Mem *pMem; sqlite_func ctx; VERIFY( if( n<0 ) goto bad_instruction; ) VERIFY( if( p->tos+1tos].flags!=STK_Int ) goto bad_instruction; ) + VERIFY( if( aStack[p->tos].flags!=MEM_Int ) goto bad_instruction; ) for(i=p->tos-n; itos; i++){ - if( aStack[i].flags & STK_Null ){ - zStack[i] = 0; + if( aStack[i].flags & MEM_Null ){ + aStack[i].z = 0; }else{ Stringify(p, i); } + p->zArgv[i] = aStack[i].z; } i = aStack[p->tos].i; VERIFY( if( i<0 || i>=p->agg.nMem ) goto bad_instruction; ) ctx.pFunc = (FuncDef*)pOp->p3; pMem = &p->agg.pCurrent->aMem[i]; - ctx.z = pMem->s.z; + ctx.s.z = pMem->zShort; /* Space used for small aggregate contexts */ ctx.pAgg = pMem->z; - ctx.cnt = ++pMem->s.i; + ctx.cnt = ++pMem->i; ctx.isError = 0; ctx.isStep = 1; - (ctx.pFunc->xStep)(&ctx, n, (const char**)&zStack[p->tos-n]); + (ctx.pFunc->xStep)(&ctx, n, (const char**)&p->zArgv[p->tos-n]); pMem->z = ctx.pAgg; - pMem->s.flags = STK_AggCtx; + pMem->flags = MEM_AggCtx; sqliteVdbePopStack(p, n+1); if( ctx.isError ){ rc = SQLITE_ERROR; } break; @@ -4466,11 +4464,11 @@ char *zKey; int nKey; VERIFY( if( tos<0 ) goto not_enough_stack; ) Stringify(p, tos); - zKey = zStack[tos]; + zKey = aStack[tos].z; nKey = aStack[tos].n; pElem = sqliteHashFind(&p->agg.hash, zKey, nKey); if( pElem ){ p->agg.pCurrent = pElem; pc = pOp->p2 - 1; @@ -4494,25 +4492,24 @@ VERIFY( if( tos<0 ) goto not_enough_stack; ) if( pFocus==0 ) goto no_mem; if( VERIFY( i>=0 && ) iagg.nMem ){ Mem *pMem = &pFocus->aMem[i]; char *zOld; - if( pMem->s.flags & STK_Dyn ){ + if( pMem->flags & MEM_Dyn ){ zOld = pMem->z; }else{ zOld = 0; } Deephemeralize(p, tos); - pMem->s = aStack[tos]; - if( pMem->s.flags & STK_Dyn ){ - pMem->z = zStack[tos]; - zStack[tos] = 0; + *pMem = aStack[tos]; + if( pMem->flags & MEM_Dyn ){ + aStack[tos].z = 0; aStack[tos].flags = 0; - }else if( pMem->s.flags & (STK_Static|STK_AggCtx) ){ - pMem->z = zStack[tos]; - }else if( pMem->s.flags & STK_Str ){ - pMem->z = pMem->s.z; + }else if( pMem->flags & (MEM_Static|MEM_AggCtx) ){ + /* pMem->z = zStack[tos]; *** do nothing */ + }else if( pMem->flags & MEM_Str ){ + pMem->z = pMem->zShort; } if( zOld ) sqliteFree(zOld); } POPSTACK; break; @@ -4529,14 +4526,13 @@ int i = pOp->p2; int tos = ++p->tos; if( pFocus==0 ) goto no_mem; if( VERIFY( i>=0 && ) iagg.nMem ){ Mem *pMem = &pFocus->aMem[i]; - aStack[tos] = pMem->s; - zStack[tos] = pMem->z; - aStack[tos].flags &= ~STK_Dyn; - aStack[tos].flags |= STK_Ephem; + aStack[tos] = *pMem; + aStack[tos].flags &= ~MEM_Dyn; + aStack[tos].flags |= MEM_Ephem; } break; } /* Opcode: AggNext * P2 * @@ -4568,26 +4564,25 @@ aMem = p->agg.pCurrent->aMem; for(i=0; iagg.nMem; i++){ int freeCtx; if( p->agg.apFunc[i]==0 ) continue; if( p->agg.apFunc[i]->xFinalize==0 ) continue; - ctx.s.flags = STK_Null; - ctx.z = 0; + ctx.s.flags = MEM_Null; + ctx.s.z = aMem[i].zShort; ctx.pAgg = (void*)aMem[i].z; - freeCtx = aMem[i].z && aMem[i].z!=aMem[i].s.z; - ctx.cnt = aMem[i].s.i; + freeCtx = aMem[i].z && aMem[i].z!=aMem[i].zShort; + ctx.cnt = aMem[i].i; ctx.isStep = 0; ctx.pFunc = p->agg.apFunc[i]; (*p->agg.apFunc[i]->xFinalize)(&ctx); if( freeCtx ){ sqliteFree( aMem[i].z ); } - aMem[i].s = ctx.s; - aMem[i].z = ctx.z; - if( (aMem[i].s.flags & STK_Str) && - (aMem[i].s.flags & (STK_Dyn|STK_Static|STK_Ephem))==0 ){ - aMem[i].z = aMem[i].s.z; + aMem[i] = ctx.s; + if( (aMem[i].flags & MEM_Str) && + (aMem[i].flags & (MEM_Dyn|MEM_Static|MEM_Ephem))==0 ){ + aMem[i].z = aMem[i].zShort; } } } break; } @@ -4614,11 +4609,11 @@ sqliteHashInsert(&p->aSet[i].hash, pOp->p3, strlen(pOp->p3)+1, p); }else{ int tos = p->tos; if( tos<0 ) goto not_enough_stack; Stringify(p, tos); - sqliteHashInsert(&p->aSet[i].hash, zStack[tos], aStack[tos].n, p); + sqliteHashInsert(&p->aSet[i].hash, aStack[tos].z, aStack[tos].n, p); POPSTACK; } if( sqlite_malloc_failed ) goto no_mem; break; } @@ -4633,11 +4628,11 @@ int i = pOp->p1; int tos = p->tos; VERIFY( if( tos<0 ) goto not_enough_stack; ) Stringify(p, tos); if( i>=0 && inSet && - sqliteHashFind(&p->aSet[i].hash, zStack[tos], aStack[tos].n)){ + sqliteHashFind(&p->aSet[i].hash, aStack[tos].z, aStack[tos].n)){ pc = pOp->p2 - 1; } POPSTACK; break; } @@ -4652,11 +4647,11 @@ int i = pOp->p1; int tos = p->tos; VERIFY( if( tos<0 ) goto not_enough_stack; ) Stringify(p, tos); if( i<0 || i>=p->nSet || - sqliteHashFind(&p->aSet[i].hash, zStack[tos], aStack[tos].n)==0 ){ + sqliteHashFind(&p->aSet[i].hash, aStack[tos].z, aStack[tos].n)==0 ){ pc = pOp->p2 - 1; } POPSTACK; break; } @@ -4697,13 +4692,13 @@ }else{ pc = pOp->p2 - 1; } } tos = ++p->tos; - zStack[tos] = sqliteHashKey(pSet->prev); + aStack[tos].z = sqliteHashKey(pSet->prev); aStack[tos].n = sqliteHashKeysize(pSet->prev); - aStack[tos].flags = STK_Str | STK_Ephem; + aStack[tos].flags = MEM_Str | MEM_Ephem; break; } /* Opcode: Vacuum * * * ** @@ -4759,38 +4754,38 @@ } if( p->trace && p->tos>=0 ){ int i; fprintf(p->trace, "Stack:"); for(i=p->tos; i>=0 && i>p->tos-5; i--){ - if( aStack[i].flags & STK_Null ){ + if( aStack[i].flags & MEM_Null ){ fprintf(p->trace, " NULL"); - }else if( (aStack[i].flags & (STK_Int|STK_Str))==(STK_Int|STK_Str) ){ + }else if( (aStack[i].flags & (MEM_Int|MEM_Str))==(MEM_Int|MEM_Str) ){ fprintf(p->trace, " si:%d", aStack[i].i); - }else if( aStack[i].flags & STK_Int ){ + }else if( aStack[i].flags & MEM_Int ){ fprintf(p->trace, " i:%d", aStack[i].i); - }else if( aStack[i].flags & STK_Real ){ + }else if( aStack[i].flags & MEM_Real ){ fprintf(p->trace, " r:%g", aStack[i].r); - }else if( aStack[i].flags & STK_Str ){ + }else if( aStack[i].flags & MEM_Str ){ int j, k; char zBuf[100]; zBuf[0] = ' '; - if( aStack[i].flags & STK_Dyn ){ + if( aStack[i].flags & MEM_Dyn ){ zBuf[1] = 'z'; - assert( (aStack[i].flags & (STK_Static|STK_Ephem))==0 ); - }else if( aStack[i].flags & STK_Static ){ + assert( (aStack[i].flags & (MEM_Static|MEM_Ephem))==0 ); + }else if( aStack[i].flags & MEM_Static ){ zBuf[1] = 't'; - assert( (aStack[i].flags & (STK_Dyn|STK_Ephem))==0 ); - }else if( aStack[i].flags & STK_Ephem ){ + assert( (aStack[i].flags & (MEM_Dyn|MEM_Ephem))==0 ); + }else if( aStack[i].flags & MEM_Ephem ){ zBuf[1] = 'e'; - assert( (aStack[i].flags & (STK_Static|STK_Dyn))==0 ); + assert( (aStack[i].flags & (MEM_Static|MEM_Dyn))==0 ); }else{ zBuf[1] = 's'; } zBuf[2] = '['; k = 3; for(j=0; j<20 && jtos>=0); \ - if( aStack[p->tos].flags & STK_Dyn ) sqliteFree(zStack[p->tos]); \ + if( aStack[p->tos].flags & MEM_Dyn ) sqliteFree(aStack[p->tos].z); \ p->tos--; /* ** Function prototypes */ Index: src/vdbeaux.c ================================================================== --- src/vdbeaux.c +++ src/vdbeaux.c @@ -373,52 +373,52 @@ ** the internals of the sqlite_func structure which is only defined in ** this source file. */ char *sqlite_set_result_string(sqlite_func *p, const char *zResult, int n){ assert( !p->isStep ); - if( p->s.flags & STK_Dyn ){ - sqliteFree(p->z); + if( p->s.flags & MEM_Dyn ){ + sqliteFree(p->s.z); } if( zResult==0 ){ - p->s.flags = STK_Null; + p->s.flags = MEM_Null; n = 0; - p->z = 0; + p->s.z = 0; p->s.n = 0; }else{ if( n<0 ) n = strlen(zResult); if( ns.z, zResult, n); - p->s.z[n] = 0; - p->s.flags = STK_Str; - p->z = p->s.z; + memcpy(p->s.zShort, zResult, n); + p->s.zShort[n] = 0; + p->s.flags = MEM_Str; + p->s.z = p->s.zShort; }else{ - p->z = sqliteMallocRaw( n+1 ); - if( p->z ){ - memcpy(p->z, zResult, n); - p->z[n] = 0; + p->s.z = sqliteMallocRaw( n+1 ); + if( p->s.z ){ + memcpy(p->s.z, zResult, n); + p->s.z[n] = 0; } - p->s.flags = STK_Str | STK_Dyn; + p->s.flags = MEM_Str | MEM_Dyn; } p->s.n = n+1; } - return p->z; + return p->s.z; } void sqlite_set_result_int(sqlite_func *p, int iResult){ assert( !p->isStep ); - if( p->s.flags & STK_Dyn ){ - sqliteFree(p->z); + if( p->s.flags & MEM_Dyn ){ + sqliteFree(p->s.z); } p->s.i = iResult; - p->s.flags = STK_Int; + p->s.flags = MEM_Int; } void sqlite_set_result_double(sqlite_func *p, double rResult){ assert( !p->isStep ); - if( p->s.flags & STK_Dyn ){ - sqliteFree(p->z); + if( p->s.flags & MEM_Dyn ){ + sqliteFree(p->s.z); } p->s.r = rResult; - p->s.flags = STK_Real; + p->s.flags = MEM_Real; } void sqlite_set_result_error(sqlite_func *p, const char *zMsg, int n){ assert( !p->isStep ); sqlite_set_result_string(p, zMsg, n); p->isError = 1; @@ -444,11 +444,12 @@ */ void *sqlite_aggregate_context(sqlite_func *p, int nByte){ assert( p && p->pFunc && p->pFunc->xStep ); if( p->pAgg==0 ){ if( nByte<=NBFS ){ - p->pAgg = (void*)p->z; + p->pAgg = (void*)p->s.z; + memset(p->pAgg, 0, nByte); }else{ p->pAgg = sqliteMalloc( nByte ); } } return p->pAgg; @@ -506,12 +507,12 @@ }; assert( p->popStack==0 ); assert( p->explain ); p->azColName = azColumnNames; - p->azResColumn = p->zStack; - for(i=0; i<5; i++) p->zStack[i] = p->aStack[i].z; + p->azResColumn = p->zArgv; + for(i=0; i<5; i++) p->zArgv[i] = p->aStack[i].zShort; p->rc = SQLITE_OK; for(i=p->pc; p->rc==SQLITE_OK && inOp; i++){ if( db->flags & SQLITE_Interrupt ){ db->flags &= ~SQLITE_Interrupt; if( db->magic!=SQLITE_MAGIC_BUSY ){ @@ -520,31 +521,31 @@ p->rc = SQLITE_INTERRUPT; } sqliteSetString(&p->zErrMsg, sqlite_error_string(p->rc), (char*)0); break; } - sprintf(p->zStack[0],"%d",i); - sprintf(p->zStack[2],"%d", p->aOp[i].p1); - sprintf(p->zStack[3],"%d", p->aOp[i].p2); + sprintf(p->zArgv[0],"%d",i); + sprintf(p->zArgv[2],"%d", p->aOp[i].p1); + sprintf(p->zArgv[3],"%d", p->aOp[i].p2); if( p->aOp[i].p3type==P3_POINTER ){ - sprintf(p->aStack[4].z, "ptr(%#x)", (int)p->aOp[i].p3); - p->zStack[4] = p->aStack[4].z; + sprintf(p->aStack[4].zShort, "ptr(%#x)", (int)p->aOp[i].p3); + p->zArgv[4] = p->aStack[4].zShort; }else{ - p->zStack[4] = p->aOp[i].p3; + p->zArgv[4] = p->aOp[i].p3; } - p->zStack[1] = sqliteOpcodeNames[p->aOp[i].opcode]; + p->zArgv[1] = sqliteOpcodeNames[p->aOp[i].opcode]; if( p->xCallback==0 ){ p->pc = i+1; - p->azResColumn = p->zStack; + p->azResColumn = p->zArgv; p->nResColumn = 5; return SQLITE_ROW; } if( sqliteSafetyOff(db) ){ p->rc = SQLITE_MISUSE; break; } - if( p->xCallback(p->pCbArg, 5, p->zStack, p->azColName) ){ + if( p->xCallback(p->pCbArg, 5, p->zArgv, p->azColName) ){ p->rc = SQLITE_ABORT; } if( sqliteSafetyOn(db) ){ p->rc = SQLITE_MISUSE; } @@ -594,15 +595,15 @@ if( p->aStack==0 ){ p->nVar = nVar; assert( nVar>=0 ); n = isExplain ? 10 : p->nOp; p->aStack = sqliteMalloc( - n*(sizeof(p->aStack[0]) + 2*sizeof(char*)) /* aStack and zStack */ + n*(sizeof(p->aStack[0]) + 2*sizeof(char*)) /* aStack and zArgv */ + p->nVar*(sizeof(char*)+sizeof(int)+1) /* azVar, anVar, abVar */ ); - p->zStack = (char**)&p->aStack[n]; - p->azColName = (char**)&p->zStack[n]; + p->zArgv = (char**)&p->aStack[n]; + p->azColName = (char**)&p->zArgv[n]; p->azVar = (char**)&p->azColName[n]; p->anVar = (int*)&p->azVar[p->nVar]; p->abVar = (u8*)&p->anVar[p->nVar]; } @@ -654,19 +655,16 @@ ** Pop the stack N times. Free any memory associated with the ** popped stack elements. */ void sqliteVdbePopStack(Vdbe *p, int N){ assert( N>=0 ); - if( p->zStack==0 ) return; - assert( p->aStack || sqlite_malloc_failed ); if( p->aStack==0 ) return; while( N-- > 0 ){ - if( p->aStack[p->tos].flags & STK_Dyn ){ - sqliteFree(p->zStack[p->tos]); + if( p->aStack[p->tos].flags & MEM_Dyn ){ + sqliteFree(p->aStack[p->tos].z); } p->aStack[p->tos].flags = 0; - p->zStack[p->tos] = 0; p->tos--; } } /* @@ -684,24 +682,23 @@ for(p = sqliteHashFirst(&pAgg->hash); p; p = sqliteHashNext(p)){ AggElem *pElem = sqliteHashData(p); assert( pAgg->apFunc!=0 ); for(i=0; inMem; i++){ Mem *pMem = &pElem->aMem[i]; - if( pAgg->apFunc[i] && (pMem->s.flags & STK_AggCtx)!=0 ){ + if( pAgg->apFunc[i] && (pMem->flags & MEM_AggCtx)!=0 ){ sqlite_func ctx; ctx.pFunc = pAgg->apFunc[i]; - ctx.s.flags = STK_Null; - ctx.z = 0; + ctx.s.flags = MEM_Null; ctx.pAgg = pMem->z; - ctx.cnt = pMem->s.i; + ctx.cnt = pMem->i; ctx.isStep = 0; ctx.isError = 0; (*pAgg->apFunc[i]->xFinalize)(&ctx); - if( pMem->z!=0 && pMem->z!=pMem->s.z ){ + if( pMem->z!=0 && pMem->z!=pMem->zShort ){ sqliteFree(pMem->z); } - }else if( pMem->s.flags & STK_Dyn ){ + }else if( pMem->flags & MEM_Dyn ){ sqliteFree(pMem->z); } } sqliteFree(pElem); } @@ -763,11 +760,11 @@ int i; sqliteVdbePopStack(p, p->tos+1); closeAllCursors(p); if( p->aMem ){ for(i=0; inMem; i++){ - if( p->aMem[i].s.flags & STK_Dyn ){ + if( p->aMem[i].flags & MEM_Dyn ){ sqliteFree(p->aMem[i].z); } } } sqliteFree(p->aMem);