extensions/sqlite/sqlite-source/malloc.c
branchrefac-jit
changeset 2284 026da5437111
parent 2192 7602d7fb60d9
child 2288 15c77b21589a
     1.1 --- a/extensions/sqlite/sqlite-source/malloc.c	Fri Jul 11 06:51:21 2008 +0000
     1.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.3 @@ -1,240 +0,0 @@
     1.4 -/*
     1.5 -** 2001 September 15
     1.6 -**
     1.7 -** The author disclaims copyright to this source code.  In place of
     1.8 -** a legal notice, here is a blessing:
     1.9 -**
    1.10 -**    May you do good and not evil.
    1.11 -**    May you find forgiveness for yourself and forgive others.
    1.12 -**    May you share freely, never taking more than you give.
    1.13 -**
    1.14 -*************************************************************************
    1.15 -** Memory allocation functions used throughout sqlite.
    1.16 -**
    1.17 -**
    1.18 -** $Id$
    1.19 -*/
    1.20 -#include "sqliteInt.h"
    1.21 -#include <stdarg.h>
    1.22 -#include <ctype.h>
    1.23 -
    1.24 -/*
    1.25 -** This routine runs when the memory allocator sees that the
    1.26 -** total memory allocation is about to exceed the soft heap
    1.27 -** limit.
    1.28 -*/
    1.29 -static void softHeapLimitEnforcer(
    1.30 -  void *NotUsed, 
    1.31 -  sqlite3_int64 inUse,
    1.32 -  int allocSize
    1.33 -){
    1.34 -  sqlite3_release_memory(allocSize);
    1.35 -}
    1.36 -
    1.37 -/*
    1.38 -** Set the soft heap-size limit for the current thread. Passing a
    1.39 -** zero or negative value indicates no limit.
    1.40 -*/
    1.41 -void sqlite3_soft_heap_limit(int n){
    1.42 -  sqlite3_uint64 iLimit;
    1.43 -  int overage;
    1.44 -  if( n<0 ){
    1.45 -    iLimit = 0;
    1.46 -  }else{
    1.47 -    iLimit = n;
    1.48 -  }
    1.49 -  if( iLimit>0 ){
    1.50 -    sqlite3_memory_alarm(softHeapLimitEnforcer, 0, iLimit);
    1.51 -  }else{
    1.52 -    sqlite3_memory_alarm(0, 0, 0);
    1.53 -  }
    1.54 -  overage = sqlite3_memory_used() - n;
    1.55 -  if( overage>0 ){
    1.56 -    sqlite3_release_memory(overage);
    1.57 -  }
    1.58 -}
    1.59 -
    1.60 -/*
    1.61 -** Release memory held by SQLite instances created by the current thread.
    1.62 -*/
    1.63 -int sqlite3_release_memory(int n){
    1.64 -#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
    1.65 -  return sqlite3PagerReleaseMemory(n);
    1.66 -#else
    1.67 -  return SQLITE_OK;
    1.68 -#endif
    1.69 -}
    1.70 -
    1.71 -
    1.72 -/*
    1.73 -** Allocate and zero memory.
    1.74 -*/ 
    1.75 -void *sqlite3MallocZero(unsigned n){
    1.76 -  void *p = sqlite3_malloc(n);
    1.77 -  if( p ){
    1.78 -    memset(p, 0, n);
    1.79 -  }
    1.80 -  return p;
    1.81 -}
    1.82 -
    1.83 -/*
    1.84 -** Allocate and zero memory.  If the allocation fails, make
    1.85 -** the mallocFailed flag in the connection pointer.
    1.86 -*/
    1.87 -void *sqlite3DbMallocZero(sqlite3 *db, unsigned n){
    1.88 -  void *p = sqlite3DbMallocRaw(db, n);
    1.89 -  if( p ){
    1.90 -    memset(p, 0, n);
    1.91 -  }
    1.92 -  return p;
    1.93 -}
    1.94 -
    1.95 -/*
    1.96 -** Allocate and zero memory.  If the allocation fails, make
    1.97 -** the mallocFailed flag in the connection pointer.
    1.98 -*/
    1.99 -void *sqlite3DbMallocRaw(sqlite3 *db, unsigned n){
   1.100 -  void *p = 0;
   1.101 -  if( !db || db->mallocFailed==0 ){
   1.102 -    p = sqlite3_malloc(n);
   1.103 -    if( !p && db ){
   1.104 -      db->mallocFailed = 1;
   1.105 -    }
   1.106 -  }
   1.107 -  return p;
   1.108 -}
   1.109 -
   1.110 -/*
   1.111 -** Resize the block of memory pointed to by p to n bytes. If the
   1.112 -** resize fails, set the mallocFailed flag inthe connection object.
   1.113 -*/
   1.114 -void *sqlite3DbRealloc(sqlite3 *db, void *p, int n){
   1.115 -  void *pNew = 0;
   1.116 -  if( db->mallocFailed==0 ){
   1.117 -    pNew = sqlite3_realloc(p, n);
   1.118 -    if( !pNew ){
   1.119 -      db->mallocFailed = 1;
   1.120 -    }
   1.121 -  }
   1.122 -  return pNew;
   1.123 -}
   1.124 -
   1.125 -/*
   1.126 -** Attempt to reallocate p.  If the reallocation fails, then free p
   1.127 -** and set the mallocFailed flag in the database connection.
   1.128 -*/
   1.129 -void *sqlite3DbReallocOrFree(sqlite3 *db, void *p, int n){
   1.130 -  void *pNew;
   1.131 -  pNew = sqlite3DbRealloc(db, p, n);
   1.132 -  if( !pNew ){
   1.133 -    sqlite3_free(p);
   1.134 -  }
   1.135 -  return pNew;
   1.136 -}
   1.137 -
   1.138 -/*
   1.139 -** Make a copy of a string in memory obtained from sqliteMalloc(). These 
   1.140 -** functions call sqlite3MallocRaw() directly instead of sqliteMalloc(). This
   1.141 -** is because when memory debugging is turned on, these two functions are 
   1.142 -** called via macros that record the current file and line number in the
   1.143 -** ThreadData structure.
   1.144 -*/
   1.145 -char *sqlite3StrDup(const char *z){
   1.146 -  char *zNew;
   1.147 -  int n;
   1.148 -  if( z==0 ) return 0;
   1.149 -  n = strlen(z)+1;
   1.150 -  zNew = sqlite3_malloc(n);
   1.151 -  if( zNew ) memcpy(zNew, z, n);
   1.152 -  return zNew;
   1.153 -}
   1.154 -char *sqlite3StrNDup(const char *z, int n){
   1.155 -  char *zNew;
   1.156 -  if( z==0 ) return 0;
   1.157 -  zNew = sqlite3_malloc(n+1);
   1.158 -  if( zNew ){
   1.159 -    memcpy(zNew, z, n);
   1.160 -    zNew[n] = 0;
   1.161 -  }
   1.162 -  return zNew;
   1.163 -}
   1.164 -
   1.165 -char *sqlite3DbStrDup(sqlite3 *db, const char *z){
   1.166 -  char *zNew = sqlite3StrDup(z);
   1.167 -  if( z && !zNew ){
   1.168 -    db->mallocFailed = 1;
   1.169 -  }
   1.170 -  return zNew;
   1.171 -}
   1.172 -char *sqlite3DbStrNDup(sqlite3 *db, const char *z, int n){
   1.173 -  char *zNew = sqlite3StrNDup(z, n);
   1.174 -  if( z && !zNew ){
   1.175 -    db->mallocFailed = 1;
   1.176 -  }
   1.177 -  return zNew;
   1.178 -}
   1.179 -
   1.180 -/*
   1.181 -** Create a string from the 2nd and subsequent arguments (up to the
   1.182 -** first NULL argument), store the string in memory obtained from
   1.183 -** sqliteMalloc() and make the pointer indicated by the 1st argument
   1.184 -** point to that string.  The 1st argument must either be NULL or 
   1.185 -** point to memory obtained from sqliteMalloc().
   1.186 -*/
   1.187 -void sqlite3SetString(char **pz, ...){
   1.188 -  va_list ap;
   1.189 -  int nByte;
   1.190 -  const char *z;
   1.191 -  char *zResult;
   1.192 -
   1.193 -  assert( pz!=0 );
   1.194 -  nByte = 1;
   1.195 -  va_start(ap, pz);
   1.196 -  while( (z = va_arg(ap, const char*))!=0 ){
   1.197 -    nByte += strlen(z);
   1.198 -  }
   1.199 -  va_end(ap);
   1.200 -  sqlite3_free(*pz);
   1.201 -  *pz = zResult = sqlite3_malloc(nByte);
   1.202 -  if( zResult==0 ){
   1.203 -    return;
   1.204 -  }
   1.205 -  *zResult = 0;
   1.206 -  va_start(ap, pz);
   1.207 -  while( (z = va_arg(ap, const char*))!=0 ){
   1.208 -    int n = strlen(z);
   1.209 -    memcpy(zResult, z, n);
   1.210 -    zResult += n;
   1.211 -  }
   1.212 -  zResult[0] = 0;
   1.213 -  va_end(ap);
   1.214 -}
   1.215 -
   1.216 -
   1.217 -/*
   1.218 -** This function must be called before exiting any API function (i.e. 
   1.219 -** returning control to the user) that has called sqlite3_malloc or
   1.220 -** sqlite3_realloc.
   1.221 -**
   1.222 -** The returned value is normally a copy of the second argument to this
   1.223 -** function. However, if a malloc() failure has occured since the previous
   1.224 -** invocation SQLITE_NOMEM is returned instead. 
   1.225 -**
   1.226 -** If the first argument, db, is not NULL and a malloc() error has occured,
   1.227 -** then the connection error-code (the value returned by sqlite3_errcode())
   1.228 -** is set to SQLITE_NOMEM.
   1.229 -*/
   1.230 -int sqlite3ApiExit(sqlite3* db, int rc){
   1.231 -  /* If the db handle is not NULL, then we must hold the connection handle
   1.232 -  ** mutex here. Otherwise the read (and possible write) of db->mallocFailed 
   1.233 -  ** is unsafe, as is the call to sqlite3Error().
   1.234 -  */
   1.235 -  assert( !db || sqlite3_mutex_held(db->mutex) );
   1.236 -  if( db && db->mallocFailed ){
   1.237 -    sqlite3Error(db, SQLITE_NOMEM, 0);
   1.238 -    db->mallocFailed = 0;
   1.239 -    rc = SQLITE_NOMEM;
   1.240 -  }
   1.241 -  return rc & (db ? db->errMask : 0xff);
   1.242 -}
   1.243 -