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

📄 util.c

📁 sqlite嵌入式数据库源码
💻 C
📖 第 1 页 / 共 3 页
字号:
#endif  assert( !sqlite3_mallocDisallowed );  if( !sqlite3TestMallocFail() ){    u32 *p = (u32 *)getOsPointer(pRealloc);    checkGuards(p);    p = sqlite3OsRealloc(p, n + TESTALLOC_OVERHEAD);    applyGuards(p);    relinkAlloc(p);    return (void *)(&p[TESTALLOC_NGUARD + 2*sizeof(void *)/sizeof(u32)]);  }  return 0;}static void OSMALLOC_FAILED(){  sqlite3_isFail = 0;}#else/* Define macros to call the sqlite3OsXXX interface directly if ** the SQLITE_MEMDEBUG macro is not defined.*/#define OSMALLOC(x)        sqlite3OsMalloc(x)#define OSREALLOC(x,y)     sqlite3OsRealloc(x,y)#define OSFREE(x)          sqlite3OsFree(x)#define OSSIZEOF(x)        sqlite3OsAllocationSize(x)#define OSMALLOC_FAILED()#endif  /* SQLITE_MEMDEBUG *//*** End code for memory allocation system test layer.**--------------------------------------------------------------------------*//*** This routine is called when we are about to allocate n additional bytes** of memory.  If the new allocation will put is over the soft allocation** limit, then invoke sqlite3_release_memory() to try to release some** memory before continuing with the allocation.**** This routine also makes sure that the thread-specific-data (TSD) has** be allocated.  If it has not and can not be allocated, then return** false.  The updateMemoryUsedCount() routine below will deallocate** the TSD if it ought to be.**** If SQLITE_ENABLE_MEMORY_MANAGEMENT is not defined, this routine is** a no-op*/ #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENTstatic int enforceSoftLimit(int n){  ThreadData *pTsd = sqlite3ThreadData();  if( pTsd==0 ){    return 0;  }  assert( pTsd->nAlloc>=0 );  if( n>0 && pTsd->nSoftHeapLimit>0 ){    while( pTsd->nAlloc+n>pTsd->nSoftHeapLimit && sqlite3_release_memory(n) ){}  }  return 1;}#else# define enforceSoftLimit(X)  1#endif/*** Update the count of total outstanding memory that is held in** thread-specific-data (TSD).  If after this update the TSD is** no longer being used, then deallocate it.**** If SQLITE_ENABLE_MEMORY_MANAGEMENT is not defined, this routine is** a no-op*/#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENTstatic void updateMemoryUsedCount(int n){  ThreadData *pTsd = sqlite3ThreadData();  if( pTsd ){    pTsd->nAlloc += n;    assert( pTsd->nAlloc>=0 );    if( pTsd->nAlloc==0 && pTsd->nSoftHeapLimit==0 ){      sqlite3ReleaseThreadData();    }  }}#else#define updateMemoryUsedCount(x)  /* no-op */#endif/*** Allocate and return N bytes of uninitialised memory by calling** sqlite3OsMalloc(). If the Malloc() call fails, attempt to free memory ** by calling sqlite3_release_memory().*/void *sqlite3MallocRaw(int n, int doMemManage){  void *p = 0;  if( n>0 && !sqlite3MallocFailed() && (!doMemManage || enforceSoftLimit(n)) ){    while( (p = OSMALLOC(n))==0 && sqlite3_release_memory(n) ){}    if( !p ){      sqlite3FailedMalloc();      OSMALLOC_FAILED();    }else if( doMemManage ){      updateMemoryUsedCount(OSSIZEOF(p));    }  }  return p;}/*** Resize the allocation at p to n bytes by calling sqlite3OsRealloc(). The** pointer to the new allocation is returned.  If the Realloc() call fails,** attempt to free memory by calling sqlite3_release_memory().*/void *sqlite3Realloc(void *p, int n){  if( sqlite3MallocFailed() ){    return 0;  }  if( !p ){    return sqlite3Malloc(n, 1);  }else{    void *np = 0;#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT    int origSize = OSSIZEOF(p);#endif    if( enforceSoftLimit(n - origSize) ){      while( (np = OSREALLOC(p, n))==0 && sqlite3_release_memory(n) ){}      if( !np ){        sqlite3FailedMalloc();        OSMALLOC_FAILED();      }else{        updateMemoryUsedCount(OSSIZEOF(np) - origSize);      }    }    return np;  }}/*** Free the memory pointed to by p. p must be either a NULL pointer or a ** value returned by a previous call to sqlite3Malloc() or sqlite3Realloc().*/void sqlite3FreeX(void *p){  if( p ){    updateMemoryUsedCount(0 - OSSIZEOF(p));    OSFREE(p);  }}/*** A version of sqliteMalloc() that is always a function, not a macro.** Currently, this is used only to alloc to allocate the parser engine.*/void *sqlite3MallocX(int n){  return sqliteMalloc(n);}/*** sqlite3Malloc** sqlite3ReallocOrFree**** These two are implemented as wrappers around sqlite3MallocRaw(), ** sqlite3Realloc() and sqlite3Free().*/ void *sqlite3Malloc(int n, int doMemManage){  void *p = sqlite3MallocRaw(n, doMemManage);  if( p ){    memset(p, 0, n);  }  return p;}void sqlite3ReallocOrFree(void **pp, int n){  void *p = sqlite3Realloc(*pp, n);  if( !p ){    sqlite3FreeX(*pp);  }  *pp = p;}/*** sqlite3ThreadSafeMalloc() and sqlite3ThreadSafeFree() are used in those** rare scenarios where sqlite may allocate memory in one thread and free** it in another. They are exactly the same as sqlite3Malloc() and ** sqlite3Free() except that:****   * The allocated memory is not included in any calculations with **     respect to the soft-heap-limit, and****   * sqlite3ThreadSafeMalloc() must be matched with ThreadSafeFree(),**     not sqlite3Free(). Calling sqlite3Free() on memory obtained from**     ThreadSafeMalloc() will cause an error somewhere down the line.*/#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENTvoid *sqlite3ThreadSafeMalloc(int n){  (void)ENTER_MALLOC;  return sqlite3Malloc(n, 0);}void sqlite3ThreadSafeFree(void *p){  (void)ENTER_MALLOC;  if( p ){    OSFREE(p);  }}#endif/*** Return the number of bytes allocated at location p. p must be either ** a NULL pointer (in which case 0 is returned) or a pointer returned by ** sqlite3Malloc(), sqlite3Realloc() or sqlite3ReallocOrFree().**** The number of bytes allocated does not include any overhead inserted by ** any malloc() wrapper functions that may be called. So the value returned** is the number of bytes that were available to SQLite using pointer p, ** regardless of how much memory was actually allocated.*/#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENTint sqlite3AllocSize(void *p){  return OSSIZEOF(p);}#endif/*** Make a copy of a string in memory obtained from sqliteMalloc(). These ** functions call sqlite3MallocRaw() directly instead of sqliteMalloc(). This** is because when memory debugging is turned on, these two functions are ** called via macros that record the current file and line number in the** ThreadData structure.*/char *sqlite3StrDup(const char *z){  char *zNew;  if( z==0 ) return 0;  zNew = sqlite3MallocRaw(strlen(z)+1, 1);  if( zNew ) strcpy(zNew, z);  return zNew;}char *sqlite3StrNDup(const char *z, int n){  char *zNew;  if( z==0 ) return 0;  zNew = sqlite3MallocRaw(n+1, 1);  if( zNew ){    memcpy(zNew, z, n);    zNew[n] = 0;  }  return zNew;}/*** Create a string from the 2nd and subsequent arguments (up to the** first NULL argument), store the string in memory obtained from** sqliteMalloc() and make the pointer indicated by the 1st argument** point to that string.  The 1st argument must either be NULL or ** point to memory obtained from sqliteMalloc().*/void sqlite3SetString(char **pz, ...){  va_list ap;  int nByte;  const char *z;  char *zResult;  if( pz==0 ) return;  nByte = 1;  va_start(ap, pz);  while( (z = va_arg(ap, const char*))!=0 ){    nByte += strlen(z);  }  va_end(ap);  sqliteFree(*pz);  *pz = zResult = sqliteMallocRaw( nByte );  if( zResult==0 ){    return;  }  *zResult = 0;  va_start(ap, pz);  while( (z = va_arg(ap, const char*))!=0 ){    strcpy(zResult, z);    zResult += strlen(zResult);  }  va_end(ap);}/*** Set the most recent error code and error string for the sqlite** handle "db". The error code is set to "err_code".**** If it is not NULL, string zFormat specifies the format of the** error string in the style of the printf functions: The following** format characters are allowed:****      %s      Insert a string**      %z      A string that should be freed after use**      %d      Insert an integer**      %T      Insert a token**      %S      Insert the first element of a SrcList**** zFormat and any string tokens that follow it are assumed to be** encoded in UTF-8.**** To clear the most recent error for sqlite handle "db", sqlite3Error** should be called with err_code set to SQLITE_OK and zFormat set** to NULL.*/void sqlite3Error(sqlite3 *db, int err_code, const char *zFormat, ...){  if( db && (db->pErr || (db->pErr = sqlite3ValueNew())!=0) ){    db->errCode = err_code;    if( zFormat ){      char *z;      va_list ap;      va_start(ap, zFormat);      z = sqlite3VMPrintf(zFormat, ap);      va_end(ap);      sqlite3ValueSetStr(db->pErr, -1, z, SQLITE_UTF8, sqlite3FreeX);    }else{      sqlite3ValueSetStr(db->pErr, 0, 0, SQLITE_UTF8, SQLITE_STATIC);    }  }}/*** Add an error message to pParse->zErrMsg and increment pParse->nErr.** The following formatting characters are allowed:****      %s      Insert a string**      %z      A string that should be freed after use**      %d      Insert an integer**      %T      Insert a token**      %S      Insert the first element of a SrcList**** This function should be used to report any error that occurs whilst** compiling an SQL statement (i.e. within sqlite3_prepare()). The** last thing the sqlite3_prepare() function does is copy the error** stored by this function into the database handle using sqlite3Error().** Function sqlite3Error() should be used during statement execution** (sqlite3_step() etc.).*/void sqlite3ErrorMsg(Parse *pParse, const char *zFormat, ...){  va_list ap;  pParse->nErr++;  sqliteFree(pParse->zErrMsg);  va_start(ap, zFormat);  pParse->zErrMsg = sqlite3VMPrintf(zFormat, ap);  va_end(ap);}/*** Clear the error message in pParse, if any*/void sqlite3ErrorClear(Parse *pParse){  sqliteFree(pParse->zErrMsg);  pParse->zErrMsg = 0;  pParse->nErr = 0;}/*** Convert an SQL-style quoted string into a normal string by removing** the quote characters.  The conversion is done in-place.  If the** input does not begin with a quote character, then this routine** is a no-op.**** 2002-Feb-14: This routine is extended to remove MS-Access style** brackets from around identifers.  For example:  "[a-b-c]" becomes** "a-b-c".*/void sqlite3Dequote(char *z){  int quote;  int i, j;  if( z==0 ) return;  quote = z[0];  switch( quote ){    case '\'':  break;    case '"':   break;    case '`':   break;                /* For MySQL compatibility */    case '[':   quote = ']';  break;  /* For MS SqlServer compatibility */    default:    return;  }  for(i=1, j=0; z[i]; i++){    if( z[i]==quote ){      if( z[i+1]==quote ){        z[j++] = quote;        i++;      }else{        z[j++] = 0;        break;      }    }else{      z[j++] = z[i];    }  }}/* An array to map all upper-case characters into their corresponding** lower-case character. */const unsigned char sqlite3UpperToLower[] = {#ifdef SQLITE_ASCII      0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16, 17,     18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35,     36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53,     54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 97, 98, 99,100,101,102,103,    104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,    122, 91, 92, 93, 94, 95, 96, 97, 98, 99,100,101,102,103,104,105,106,107,    108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,125,    126,127,128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,    144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,160,161,    162,163,164,165,166,167,168,169,170,171,172,173,174,175,176,177,178,179,    180,181,182,183,184,185,186,187,188,189,190,191,192,193,194,195,196,197,    198,199,200,201,202,203,204,205,206,207,208,209,210,211,212,213,214,215,    216,217,218,219,220,221,222,223,224,225,226,227,228,229,230,231,232,233,    234,235,236,237,238,239,240,241,242,243,244,245,246,247,248,249,250,251,    252,253,254,255#endif#ifdef SQLITE_EBCDIC      0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, /* 0x */     16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, /* 1x */     32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, /* 2x */     48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, /* 3x */     64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, /* 4x */     80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, /* 5x */     96, 97, 66, 67, 68, 69, 70, 71, 72, 73,106,107,108,109,110,111, /* 6x */    112, 81, 82, 83, 84, 85, 86, 87, 88, 89,122,123,124,125,126,127, /* 7x */    128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143, /* 8x */    144,145,146,147,148,149,150,151,152,153,154,155,156,157,156,159, /* 9x */    160,161,162,163,164,165,166,167,168,169,170,171,140,141,142,175, /* Ax */    176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191, /* Bx */    192,129,130,131,132,133,134,135,136,137,202,203,204,205,206,207, /* Cx */    208,145,146,147,148,149,150,151,152,153,218,219,220,221,222,223, /* Dx */    224,225,162,163,164,165,166,167,168,169,232,203,204,205,206,207, /* Ex */    239,240,241,242,243,244,245,246,247,248,249,219,220,221,222,255, /* Fx */#endif};#define UpperToLower sqlite3UpperToLower/*** Some systems have stricmp().  Others have strcasecmp().  Because** there is no consistency, we will define our own.*/int sqlite3StrICmp(const char *zLeft, const char *zRight){  register unsigned char *a, *b;  a = (unsigned char *)zLeft;  b = (unsigned char *)zRight;  while( *a!=0 && UpperToLower[*a]==UpperToLower[*b]){ a++; b++; }  return UpperToLower[*a] - UpperToLower[*b];}int sqlite3StrNICmp(const char *zLeft, const char *zRight, int N){  register unsigned char *a, *b;  a = (unsigned char *)zLeft;  b = (unsigned char *)zRight;  while( N-- > 0 && *a!=0 && UpperToLower[*a]==UpperToLower[*b]){ a++; b++; }  return N<0 ? 0 : UpperToLower[*a] - UpperToLower[*b];}/*** Return TRUE if z is a pure numeric string.  Return FALSE if the** string contains any character which is not part of a number. If** the string is numeric and contains the '.' character, set *realnum** to TRUE (otherwise FALSE).**** An empty string is considered non-numeric.*/int sqlite3IsNumber(const char *z, int *realnum, u8 enc){  int incr = (enc==SQLITE_UTF8?1:2);  if( enc==SQLITE_UTF16BE ) z++;  if( *z=='-' || *z=='+' ) z += incr;  if( !isdigit(*(u8*)z) ){    return 0;  }  z += incr;  if( realnum ) *realnum = 0;  while( isdigit(*(u8*)z) ){ z += incr; }  if( *z=='.' ){    z += incr;    if( !isdigit(*(u8*)z) ) return 0;    while( isdigit(*(u8*)z) ){ z += incr; }    if( realnum ) *realnum = 1;  }  if( *z=='e' || *z=='E' ){    z += incr;    if( *z=='+' || *z=='-' ) z += incr;    if( !isdigit(*(u8*)z) ) return 0;    while( isdigit(*(u8*)z) ){ z += incr; }    if( realnum ) *realnum = 1;  }  return *z==0;}/*** The string z[] is an ascii representation of a real number.** Convert this string to a double.**** This routine assumes that z[] really is a valid number.  If it** is not, the result is undefined.**** This routine is used instead of the library atof() function because** the library atof() might want to use "," as the decimal point instead** of "." depending on how locale is set.  But that would cause problems** for SQL.  So this routine always uses "." regardless of locale.*/int sqlite3AtoF(const char *z, double *pResult){#ifndef SQLITE_OMIT_FLOATING_POINT  int sign = 1;

⌨️ 快捷键说明

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