⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 vdbemem.c

📁 最新的sqlite3.6.2源代码
💻 C
📖 第 1 页 / 共 3 页
字号:
/*** 2004 May 26**** The author disclaims copyright to this source code.  In place of** a legal notice, here is a blessing:****    May you do good and not evil.**    May you find forgiveness for yourself and forgive others.**    May you share freely, never taking more than you give.******************************************************************************* This file contains code use to manipulate "Mem" structure.  A "Mem"** stores a single value in the VDBE.  Mem is an opaque structure visible** only within the VDBE.  Interface routines refer to a Mem using the** name sqlite_value**** $Id: vdbemem.c,v 1.122 2008/08/22 14:41:01 drh Exp $*/#include "sqliteInt.h"#include <ctype.h>#include "vdbeInt.h"/*** Call sqlite3VdbeMemExpandBlob() on the supplied value (type Mem*)** P if required.*/#define expandBlob(P) (((P)->flags&MEM_Zero)?sqlite3VdbeMemExpandBlob(P):0)/*** If pMem is an object with a valid string representation, this routine** ensures the internal encoding for the string representation is** 'desiredEnc', one of SQLITE_UTF8, SQLITE_UTF16LE or SQLITE_UTF16BE.**** If pMem is not a string object, or the encoding of the string** representation is already stored using the requested encoding, then this** routine is a no-op.**** SQLITE_OK is returned if the conversion is successful (or not required).** SQLITE_NOMEM may be returned if a malloc() fails during conversion** between formats.*/int sqlite3VdbeChangeEncoding(Mem *pMem, int desiredEnc){  int rc;  if( !(pMem->flags&MEM_Str) || pMem->enc==desiredEnc ){    return SQLITE_OK;  }  assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );#ifdef SQLITE_OMIT_UTF16  return SQLITE_ERROR;#else  /* MemTranslate() may return SQLITE_OK or SQLITE_NOMEM. If NOMEM is returned,  ** then the encoding of the value may not have changed.  */  rc = sqlite3VdbeMemTranslate(pMem, desiredEnc);  assert(rc==SQLITE_OK    || rc==SQLITE_NOMEM);  assert(rc==SQLITE_OK    || pMem->enc!=desiredEnc);  assert(rc==SQLITE_NOMEM || pMem->enc==desiredEnc);  return rc;#endif}/*** Make sure pMem->z points to a writable allocation of at least ** n bytes.**** If the memory cell currently contains string or blob data** and the third argument passed to this function is true, the ** current content of the cell is preserved. Otherwise, it may** be discarded.  **** This function sets the MEM_Dyn flag and clears any xDel callback.** It also clears MEM_Ephem and MEM_Static. If the preserve flag is ** not set, Mem.n is zeroed.*/int sqlite3VdbeMemGrow(Mem *pMem, int n, int preserve){  assert( 1 >=    ((pMem->zMalloc && pMem->zMalloc==pMem->z) ? 1 : 0) +    (((pMem->flags&MEM_Dyn)&&pMem->xDel) ? 1 : 0) +     ((pMem->flags&MEM_Ephem) ? 1 : 0) +     ((pMem->flags&MEM_Static) ? 1 : 0)  );  if( n<32 ) n = 32;  if( sqlite3DbMallocSize(pMem->db, pMem->zMalloc)<n ){    if( preserve && pMem->z==pMem->zMalloc ){      pMem->z = pMem->zMalloc = sqlite3DbReallocOrFree(pMem->db, pMem->z, n);      if( !pMem->z ){        pMem->flags = MEM_Null;      }      preserve = 0;    }else{      sqlite3DbFree(pMem->db, pMem->zMalloc);      pMem->zMalloc = sqlite3DbMallocRaw(pMem->db, n);    }  }  if( preserve && pMem->z && pMem->zMalloc && pMem->z!=pMem->zMalloc ){    memcpy(pMem->zMalloc, pMem->z, pMem->n);  }  if( pMem->flags&MEM_Dyn && pMem->xDel ){    pMem->xDel((void *)(pMem->z));  }  pMem->z = pMem->zMalloc;  pMem->flags &= ~(MEM_Ephem|MEM_Static);  pMem->xDel = 0;  return (pMem->z ? SQLITE_OK : SQLITE_NOMEM);}/*** Make the given Mem object MEM_Dyn.  In other words, make it so** that any TEXT or BLOB content is stored in memory obtained from** malloc().  In this way, we know that the memory is safe to be** overwritten or altered.**** Return SQLITE_OK on success or SQLITE_NOMEM if malloc fails.*/int sqlite3VdbeMemMakeWriteable(Mem *pMem){  int f;  assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );  expandBlob(pMem);  f = pMem->flags;  if( (f&(MEM_Str|MEM_Blob)) && pMem->z!=pMem->zMalloc ){    if( sqlite3VdbeMemGrow(pMem, pMem->n + 2, 1) ){      return SQLITE_NOMEM;    }    pMem->z[pMem->n] = 0;    pMem->z[pMem->n+1] = 0;    pMem->flags |= MEM_Term;  }  return SQLITE_OK;}/*** If the given Mem* has a zero-filled tail, turn it into an ordinary** blob stored in dynamically allocated space.*/#ifndef SQLITE_OMIT_INCRBLOBint sqlite3VdbeMemExpandBlob(Mem *pMem){  if( pMem->flags & MEM_Zero ){    int nByte;    assert( pMem->flags&MEM_Blob );    assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );    /* Set nByte to the number of bytes required to store the expanded blob. */    nByte = pMem->n + pMem->u.i;    if( nByte<=0 ){      nByte = 1;    }    if( sqlite3VdbeMemGrow(pMem, nByte, 1) ){      return SQLITE_NOMEM;    }    memset(&pMem->z[pMem->n], 0, pMem->u.i);    pMem->n += pMem->u.i;    pMem->flags &= ~(MEM_Zero|MEM_Term);  }  return SQLITE_OK;}#endif/*** Make sure the given Mem is \u0000 terminated.*/int sqlite3VdbeMemNulTerminate(Mem *pMem){  assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );  if( (pMem->flags & MEM_Term)!=0 || (pMem->flags & MEM_Str)==0 ){    return SQLITE_OK;   /* Nothing to do */  }  if( sqlite3VdbeMemGrow(pMem, pMem->n+2, 1) ){    return SQLITE_NOMEM;  }  pMem->z[pMem->n] = 0;  pMem->z[pMem->n+1] = 0;  pMem->flags |= MEM_Term;  return SQLITE_OK;}/*** Add MEM_Str to the set of representations for the given Mem.  Numbers** are converted using sqlite3_snprintf().  Converting a BLOB to a string** is a no-op.**** Existing representations MEM_Int and MEM_Real are *not* invalidated.**** A MEM_Null value will never be passed to this function. This function is** used for converting values to text for returning to the user (i.e. via** sqlite3_value_text()), or for ensuring that values to be used as btree** keys are strings. In the former case a NULL pointer is returned the** user and the later is an internal programming error.*/int sqlite3VdbeMemStringify(Mem *pMem, int enc){  int rc = SQLITE_OK;  int fg = pMem->flags;  const int nByte = 32;  assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );  assert( !(fg&MEM_Zero) );  assert( !(fg&(MEM_Str|MEM_Blob)) );  assert( fg&(MEM_Int|MEM_Real) );  if( sqlite3VdbeMemGrow(pMem, nByte, 0) ){    return SQLITE_NOMEM;  }  /* For a Real or Integer, use sqlite3_mprintf() to produce the UTF-8  ** string representation of the value. Then, if the required encoding  ** is UTF-16le or UTF-16be do a translation.  **   ** FIX ME: It would be better if sqlite3_snprintf() could do UTF-16.  */  if( fg & MEM_Int ){    sqlite3_snprintf(nByte, pMem->z, "%lld", pMem->u.i);  }else{    assert( fg & MEM_Real );    sqlite3_snprintf(nByte, pMem->z, "%!.15g", pMem->r);  }  pMem->n = strlen(pMem->z);  pMem->enc = SQLITE_UTF8;  pMem->flags |= MEM_Str|MEM_Term;  sqlite3VdbeChangeEncoding(pMem, enc);  return rc;}/*** Memory cell pMem contains the context of an aggregate function.** This routine calls the finalize method for that function.  The** result of the aggregate is stored back into pMem.**** Return SQLITE_ERROR if the finalizer reports an error.  SQLITE_OK** otherwise.*/int sqlite3VdbeMemFinalize(Mem *pMem, FuncDef *pFunc){  int rc = SQLITE_OK;  if( pFunc && pFunc->xFinalize ){    sqlite3_context ctx;    assert( (pMem->flags & MEM_Null)!=0 || pFunc==pMem->u.pDef );    assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );    memset(&ctx, 0, sizeof(ctx));    ctx.s.flags = MEM_Null;    ctx.s.db = pMem->db;    ctx.pMem = pMem;    ctx.pFunc = pFunc;    pFunc->xFinalize(&ctx);    assert( 0==(pMem->flags&MEM_Dyn) && !pMem->xDel );    sqlite3DbFree(pMem->db, pMem->zMalloc);    *pMem = ctx.s;    rc = (ctx.isError?SQLITE_ERROR:SQLITE_OK);  }  return rc;}/*** If the memory cell contains a string value that must be freed by** invoking an external callback, free it now. Calling this function** does not free any Mem.zMalloc buffer.*/void sqlite3VdbeMemReleaseExternal(Mem *p){  assert( p->db==0 || sqlite3_mutex_held(p->db->mutex) );  if( p->flags&MEM_Agg ){    sqlite3VdbeMemFinalize(p, p->u.pDef);    assert( (p->flags & MEM_Agg)==0 );    sqlite3VdbeMemRelease(p);  }else if( p->flags&MEM_Dyn && p->xDel ){    p->xDel((void *)p->z);    p->xDel = 0;  }}/*** Release any memory held by the Mem. This may leave the Mem in an** inconsistent state, for example with (Mem.z==0) and** (Mem.type==SQLITE_TEXT).*/void sqlite3VdbeMemRelease(Mem *p){  sqlite3VdbeMemReleaseExternal(p);  sqlite3DbFree(p->db, p->zMalloc);  p->z = 0;  p->zMalloc = 0;  p->xDel = 0;}/*** Convert a 64-bit IEEE double into a 64-bit signed integer.** If the double is too large, return 0x8000000000000000.**** Most systems appear to do this simply by assigning** variables and without the extra range tests.  But** there are reports that windows throws an expection** if the floating point value is out of range. (See ticket #2880.)** Because we do not completely understand the problem, we will** take the conservative approach and always do range tests** before attempting the conversion.*/static i64 doubleToInt64(double r){  /*  ** Many compilers we encounter do not define constants for the  ** minimum and maximum 64-bit integers, or they define them  ** inconsistently.  And many do not understand the "LL" notation.  ** So we define our own static constants here using nothing  ** larger than a 32-bit integer constant.  */  static const i64 maxInt = LARGEST_INT64;  static const i64 minInt = SMALLEST_INT64;  if( r<(double)minInt ){    return minInt;  }else if( r>(double)maxInt ){    return minInt;  }else{    return (i64)r;  }}/*** Return some kind of integer value which is the best we can do** at representing the value that *pMem describes as an integer.** If pMem is an integer, then the value is exact.  If pMem is** a floating-point then the value returned is the integer part.** If pMem is a string or blob, then we make an attempt to convert** it into a integer and return that.  If pMem is NULL, return 0.**** If pMem is a string, its encoding might be changed.*/i64 sqlite3VdbeIntValue(Mem *pMem){  int flags;  assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );  flags = pMem->flags;  if( flags & MEM_Int ){    return pMem->u.i;  }else if( flags & MEM_Real ){    return doubleToInt64(pMem->r);  }else if( flags & (MEM_Str|MEM_Blob) ){    i64 value;    pMem->flags |= MEM_Str;    if( sqlite3VdbeChangeEncoding(pMem, SQLITE_UTF8)       || sqlite3VdbeMemNulTerminate(pMem) ){      return 0;    }    assert( pMem->z );    sqlite3Atoi64(pMem->z, &value);    return value;  }else{    return 0;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -