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

📄 vdbemem.c

📁 sqlite最新源码
💻 C
📖 第 1 页 / 共 3 页
字号:
    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;  }}/*** Return the best representation of pMem that we can get into a** double.  If pMem is already a double or an integer, return its** value.  If it is a string or blob, try to convert it to a double.** If it is a NULL, return 0.0.*/double sqlite3VdbeRealValue(Mem *pMem){  assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );  if( pMem->flags & MEM_Real ){    return pMem->r;  }else if( pMem->flags & MEM_Int ){    return (double)pMem->u.i;  }else if( pMem->flags & (MEM_Str|MEM_Blob) ){    /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */    double val = (double)0;    pMem->flags |= MEM_Str;    if( sqlite3VdbeChangeEncoding(pMem, SQLITE_UTF8)       || sqlite3VdbeMemNulTerminate(pMem) ){      /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */      return (double)0;    }    assert( pMem->z );    sqlite3AtoF(pMem->z, &val);    return val;  }else{    /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */    return (double)0;  }}/*** The MEM structure is already a MEM_Real.  Try to also make it a** MEM_Int if we can.*/void sqlite3VdbeIntegerAffinity(Mem *pMem){  assert( pMem->flags & MEM_Real );  assert( (pMem->flags & MEM_RowSet)==0 );  assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );  pMem->u.i = doubleToInt64(pMem->r);  if( pMem->r==(double)pMem->u.i ){    pMem->flags |= MEM_Int;  }}/*** Convert pMem to type integer.  Invalidate any prior representations.*/int sqlite3VdbeMemIntegerify(Mem *pMem){  assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );  assert( (pMem->flags & MEM_RowSet)==0 );  pMem->u.i = sqlite3VdbeIntValue(pMem);  MemSetTypeFlag(pMem, MEM_Int);  return SQLITE_OK;}/*** Convert pMem so that it is of type MEM_Real.** Invalidate any prior representations.*/int sqlite3VdbeMemRealify(Mem *pMem){  assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );  pMem->r = sqlite3VdbeRealValue(pMem);  MemSetTypeFlag(pMem, MEM_Real);  return SQLITE_OK;}/*** Convert pMem so that it has types MEM_Real or MEM_Int or both.** Invalidate any prior representations.*/int sqlite3VdbeMemNumerify(Mem *pMem){  double r1, r2;  i64 i;  assert( (pMem->flags & (MEM_Int|MEM_Real|MEM_Null))==0 );  assert( (pMem->flags & (MEM_Blob|MEM_Str))!=0 );  assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );  r1 = sqlite3VdbeRealValue(pMem);  i = doubleToInt64(r1);  r2 = (double)i;  if( r1==r2 ){    sqlite3VdbeMemIntegerify(pMem);  }else{    pMem->r = r1;    MemSetTypeFlag(pMem, MEM_Real);  }  return SQLITE_OK;}/*** Delete any previous value and set the value stored in *pMem to NULL.*/void sqlite3VdbeMemSetNull(Mem *pMem){  if( pMem->flags & MEM_RowSet ){    sqlite3RowSetClear(pMem->u.pRowSet);  }  MemSetTypeFlag(pMem, MEM_Null);  pMem->type = SQLITE_NULL;}/*** Delete any previous value and set the value to be a BLOB of length** n containing all zeros.*/void sqlite3VdbeMemSetZeroBlob(Mem *pMem, int n){  sqlite3VdbeMemRelease(pMem);  pMem->flags = MEM_Blob|MEM_Zero;  pMem->type = SQLITE_BLOB;  pMem->n = 0;  if( n<0 ) n = 0;  pMem->u.nZero = n;  pMem->enc = SQLITE_UTF8;}/*** Delete any previous value and set the value stored in *pMem to val,** manifest type INTEGER.*/void sqlite3VdbeMemSetInt64(Mem *pMem, i64 val){  sqlite3VdbeMemRelease(pMem);  pMem->u.i = val;  pMem->flags = MEM_Int;  pMem->type = SQLITE_INTEGER;}/*** Delete any previous value and set the value stored in *pMem to val,** manifest type REAL.*/void sqlite3VdbeMemSetDouble(Mem *pMem, double val){  if( sqlite3IsNaN(val) ){    sqlite3VdbeMemSetNull(pMem);  }else{    sqlite3VdbeMemRelease(pMem);    pMem->r = val;    pMem->flags = MEM_Real;    pMem->type = SQLITE_FLOAT;  }}/*** Delete any previous value and set the value of pMem to be an** empty boolean index.*/void sqlite3VdbeMemSetRowSet(Mem *pMem){  sqlite3 *db = pMem->db;  assert( db!=0 );  if( pMem->flags & MEM_RowSet ){    sqlite3RowSetClear(pMem->u.pRowSet);  }else{    sqlite3VdbeMemRelease(pMem);    pMem->zMalloc = sqlite3DbMallocRaw(db, 64);  }  if( db->mallocFailed ){    pMem->flags = MEM_Null;  }else{    assert( pMem->zMalloc );    pMem->u.pRowSet = sqlite3RowSetInit(db, pMem->zMalloc,                                        sqlite3DbMallocSize(db, pMem->zMalloc));    assert( pMem->u.pRowSet!=0 );    pMem->flags = MEM_RowSet;  }}/*** Return true if the Mem object contains a TEXT or BLOB that is** too large - whose size exceeds SQLITE_MAX_LENGTH.*/int sqlite3VdbeMemTooBig(Mem *p){  assert( p->db!=0 );  if( p->flags & (MEM_Str|MEM_Blob) ){    int n = p->n;    if( p->flags & MEM_Zero ){      n += p->u.nZero;    }    return n>p->db->aLimit[SQLITE_LIMIT_LENGTH];  }  return 0; }/*** Size of struct Mem not including the Mem.zMalloc member.*/#define MEMCELLSIZE (size_t)(&(((Mem *)0)->zMalloc))/*** Make an shallow copy of pFrom into pTo.  Prior contents of** pTo are freed.  The pFrom->z field is not duplicated.  If** pFrom->z is used, then pTo->z points to the same thing as pFrom->z** and flags gets srcType (either MEM_Ephem or MEM_Static).*/void sqlite3VdbeMemShallowCopy(Mem *pTo, const Mem *pFrom, int srcType){  assert( (pFrom->flags & MEM_RowSet)==0 );  sqlite3VdbeMemReleaseExternal(pTo);  memcpy(pTo, pFrom, MEMCELLSIZE);  pTo->xDel = 0;  if( (pFrom->flags&MEM_Dyn)!=0 || pFrom->z==pFrom->zMalloc ){    pTo->flags &= ~(MEM_Dyn|MEM_Static|MEM_Ephem);    assert( srcType==MEM_Ephem || srcType==MEM_Static );    pTo->flags |= srcType;  }}/*** Make a full copy of pFrom into pTo.  Prior contents of pTo are** freed before the copy is made.*/int sqlite3VdbeMemCopy(Mem *pTo, const Mem *pFrom){  int rc = SQLITE_OK;  assert( (pFrom->flags & MEM_RowSet)==0 );  sqlite3VdbeMemReleaseExternal(pTo);  memcpy(pTo, pFrom, MEMCELLSIZE);  pTo->flags &= ~MEM_Dyn;  if( pTo->flags&(MEM_Str|MEM_Blob) ){    if( 0==(pFrom->flags&MEM_Static) ){      pTo->flags |= MEM_Ephem;      rc = sqlite3VdbeMemMakeWriteable(pTo);    }  }  return rc;}/*** Transfer the contents of pFrom to pTo. Any existing value in pTo is** freed. If pFrom contains ephemeral data, a copy is made.**** pFrom contains an SQL NULL when this routine returns.*/void sqlite3VdbeMemMove(Mem *pTo, Mem *pFrom){  assert( pFrom->db==0 || sqlite3_mutex_held(pFrom->db->mutex) );  assert( pTo->db==0 || sqlite3_mutex_held(pTo->db->mutex) );  assert( pFrom->db==0 || pTo->db==0 || pFrom->db==pTo->db );  sqlite3VdbeMemRelease(pTo);  memcpy(pTo, pFrom, sizeof(Mem));  pFrom->flags = MEM_Null;  pFrom->xDel = 0;  pFrom->zMalloc = 0;}/*** Change the value of a Mem to be a string or a BLOB.**** The memory management strategy depends on the value of the xDel** parameter. If the value passed is SQLITE_TRANSIENT, then the ** string is copied into a (possibly existing) buffer managed by the ** Mem structure. Otherwise, any existing buffer is freed and the** pointer copied.*/int sqlite3VdbeMemSetStr(  Mem *pMem,          /* Memory cell to set to string value */  const char *z,      /* String pointer */  int n,              /* Bytes in string, or negative */  u8 enc,             /* Encoding of z.  0 for BLOBs */  void (*xDel)(void*) /* Destructor function */){  int nByte = n;      /* New value for pMem->n */  int iLimit;         /* Maximum allowed string or blob size */  u16 flags = 0;      /* New value for pMem->flags */  assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );  assert( (pMem->flags & MEM_RowSet)==0 );  /* If z is a NULL pointer, set pMem to contain an SQL NULL. */  if( !z ){    sqlite3VdbeMemSetNull(pMem);    return SQLITE_OK;  }  if( pMem->db ){    iLimit = pMem->db->aLimit[SQLITE_LIMIT_LENGTH];  }else{    iLimit = SQLITE_MAX_LENGTH;  }  flags = (enc==0?MEM_Blob:MEM_Str);  if( nByte<0 ){    assert( enc!=0 );    if( enc==SQLITE_UTF8 ){      for(nByte=0; nByte<=iLimit && z[nByte]; nByte++){}    }else{      for(nByte=0; nByte<=iLimit && (z[nByte] | z[nByte+1]); nByte+=2){}    }    flags |= MEM_Term;  }  /* The following block sets the new values of Mem.z and Mem.xDel. It  ** also sets a flag in local variable "flags" to indicate the memory  ** management (one of MEM_Dyn or MEM_Static).  */  if( xDel==SQLITE_TRANSIENT ){    int nAlloc = nByte;    if( flags&MEM_Term ){      nAlloc += (enc==SQLITE_UTF8?1:2);    }    if( nByte>iLimit ){      return SQLITE_TOOBIG;    }    if( sqlite3VdbeMemGrow(pMem, nAlloc, 0) ){      return SQLITE_NOMEM;    }    memcpy(pMem->z, z, nAlloc);  }else if( xDel==SQLITE_DYNAMIC ){    sqlite3VdbeMemRelease(pMem);    pMem->zMalloc = pMem->z = (char *)z;    pMem->xDel = 0;  }else{    sqlite3VdbeMemRelease(pMem);    pMem->z = (char *)z;    pMem->xDel = xDel;    flags |= ((xDel==SQLITE_STATIC)?MEM_Static:MEM_Dyn);  }  if( nByte>iLimit ){    return SQLITE_TOOBIG;  }  pMem->n = nByte;  pMem->flags = flags;  pMem->enc = (enc==0 ? SQLITE_UTF8 : enc);  pMem->type = (enc==0 ? SQLITE_BLOB : SQLITE_TEXT);#ifndef SQLITE_OMIT_UTF16  if( pMem->enc!=SQLITE_UTF8 && sqlite3VdbeMemHandleBom(pMem) ){    return SQLITE_NOMEM;  }#endif  return SQLITE_OK;}/*** Compare the values contained by the two memory cells, returning

⌨️ 快捷键说明

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