/ Check-in [42b4bf9e]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Additional evidence marks on the malloc() implementation. Update the documentation to explain that mallocs are not necessarily 8-byte aligned if the SQLITE_4_BYTE_ALIGNED_MALLOC compile-time option is used.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: 42b4bf9e72501cf228b4086437c7660443933f74
User & Date: drh 2010-09-11 16:15:56
Context
2010-09-11
16:25
Fix a couple of incorrect evidence marks on malloc(). check-in: f9b5c5cb user: drh tags: trunk
16:15
Additional evidence marks on the malloc() implementation. Update the documentation to explain that mallocs are not necessarily 8-byte aligned if the SQLITE_4_BYTE_ALIGNED_MALLOC compile-time option is used. check-in: 42b4bf9e user: drh tags: trunk
15:54
Add assert() statements to demonstrate that memory allocations are always aligned to an 8-byte boundary (unless SQLITE_4_BYTE_ALIGNED_MALLOC is defined). check-in: 305cc4e6 user: drh tags: trunk
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to src/malloc.c.

   289    289   
   290    290   /*
   291    291   ** Allocate memory.  This routine is like sqlite3_malloc() except that it
   292    292   ** assumes the memory subsystem has already been initialized.
   293    293   */
   294    294   void *sqlite3Malloc(int n){
   295    295     void *p;
   296         -  if( n<=0 || n>=0x7fffff00 ){
          296  +  if( n<=0               /* IMP: R-65312-04917 */ 
          297  +   || n>=0x7fffff00
          298  +  ){
   297    299       /* A memory allocation of a number of bytes which is near the maximum
   298    300       ** signed integer value might cause an integer overflow inside of the
   299    301       ** xMalloc().  Hence we limit the maximum size to 0x7fffff00, giving
   300    302       ** 255 bytes of overhead.  SQLite itself will never use anything near
   301    303       ** this amount.  The only way to reach the limit is with sqlite3_malloc() */
   302    304       p = 0;
   303    305     }else if( sqlite3GlobalConfig.bMemstat ){
................................................................................
   455    457     }
   456    458   }
   457    459   
   458    460   /*
   459    461   ** Free memory previously obtained from sqlite3Malloc().
   460    462   */
   461    463   void sqlite3_free(void *p){
   462         -  if( p==0 ) return;
          464  +  if( p==0 ) return;  /* IMP: R-49053-54554 */
   463    465     assert( sqlite3MemdebugNoType(p, MEMTYPE_DB) );
   464    466     assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
   465    467     if( sqlite3GlobalConfig.bMemstat ){
   466    468       sqlite3_mutex_enter(mem0.mutex);
   467    469       sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, -sqlite3MallocSize(p));
   468    470       sqlite3StatusAdd(SQLITE_STATUS_MALLOC_COUNT, -1);
   469    471       sqlite3GlobalConfig.m.xFree(p);
................................................................................
   502    504   /*
   503    505   ** Change the size of an existing memory allocation
   504    506   */
   505    507   void *sqlite3Realloc(void *pOld, int nBytes){
   506    508     int nOld, nNew;
   507    509     void *pNew;
   508    510     if( pOld==0 ){
   509         -    return sqlite3Malloc(nBytes);
          511  +    return sqlite3Malloc(nBytes); /* IMP: R-28354-25769 */
   510    512     }
   511    513     if( nBytes<=0 ){
   512         -    sqlite3_free(pOld);
          514  +    sqlite3_free(pOld); /* IMP: R-31593-10574 */
   513    515       return 0;
   514    516     }
   515    517     if( nBytes>=0x7fffff00 ){
   516    518       /* The 0x7ffff00 limit term is explained in comments on sqlite3Malloc() */
   517    519       return 0;
   518    520     }
   519    521     nOld = sqlite3MallocSize(pOld);

Changes to src/sqlite.h.in.

  1943   1943   ** ^If M is the size of the prior allocation, then min(N,M) bytes
  1944   1944   ** of the prior allocation are copied into the beginning of buffer returned
  1945   1945   ** by sqlite3_realloc() and the prior allocation is freed.
  1946   1946   ** ^If sqlite3_realloc() returns NULL, then the prior allocation
  1947   1947   ** is not freed.
  1948   1948   **
  1949   1949   ** ^The memory returned by sqlite3_malloc() and sqlite3_realloc()
  1950         -** is always aligned to at least an 8 byte boundary.
         1950  +** is always aligned to at least an 8 byte boundary, or to a
         1951  +** 4 byte boundary if the [SQLITE_4_BYTE_ALIGNED_MALLOC] compile-time
         1952  +** option is used.
  1951   1953   **
  1952   1954   ** In SQLite version 3.5.0 and 3.5.1, it was possible to define
  1953   1955   ** the SQLITE_OMIT_MEMORY_ALLOCATION which would cause the built-in
  1954   1956   ** implementation of these routines to be omitted.  That capability
  1955   1957   ** is no longer provided.  Only built-in memory allocators can be used.
  1956   1958   **
  1957   1959   ** The Windows OS interface layer calls