/ Check-in [d11e8e30]
Login

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

Overview
Comment:Make sure all memory allocations are 8-byte aligned. Ticket #3040. Note that the mem3.c memory allocator returns 4-byte aligned memory allocations. But as mem3.c is intended for use in 32-bit embedded systems, we are not going to change that. (CVS 4978)
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: d11e8e307af906db75ae7aede790464fabd06422
User & Date: drh 2008-04-10 14:57:25
Context
2008-04-10
15:12
Verify that a RAISE(ROLLBACK,...) works like RAISE(FAIL,...) when not inside a transaction. Ticket #3035. (CVS 4979) check-in: 87dc82d0 user: drh tags: trunk
14:57
Make sure all memory allocations are 8-byte aligned. Ticket #3040. Note that the mem3.c memory allocator returns 4-byte aligned memory allocations. But as mem3.c is intended for use in 32-bit embedded systems, we are not going to change that. (CVS 4978) check-in: d11e8e30 user: drh tags: trunk
14:51
Add source file test_osinst.c. A wrapper vfs with instrumentation capabilities. (CVS 4977) check-in: d9a6b653 user: danielk1977 tags: trunk
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to src/mem2.c.

     8      8   **    May you find forgiveness for yourself and forgive others.
     9      9   **    May you share freely, never taking more than you give.
    10     10   **
    11     11   *************************************************************************
    12     12   ** This file contains the C functions that implement a memory
    13     13   ** allocation subsystem for use by SQLite.  
    14     14   **
    15         -** $Id: mem2.c,v 1.25 2008/04/03 10:13:01 danielk1977 Exp $
           15  +** $Id: mem2.c,v 1.26 2008/04/10 14:57:25 drh Exp $
    16     16   */
    17     17   #include "sqliteInt.h"
    18     18   
    19     19   /*
    20     20   ** This version of the memory allocator is used only if the
    21     21   ** SQLITE_MEMDEBUG macro is defined
    22     22   */
................................................................................
    44     44   ** The application code sees only a pointer to the allocation.  We have
    45     45   ** to back up from the allocation pointer to find the MemBlockHdr.  The
    46     46   ** MemBlockHdr tells us the size of the allocation and the number of
    47     47   ** backtrace pointers.  There is also a guard word at the end of the
    48     48   ** MemBlockHdr.
    49     49   */
    50     50   struct MemBlockHdr {
           51  +  i64 iSize;                          /* Size of this allocation */
    51     52     struct MemBlockHdr *pNext, *pPrev;  /* Linked list of all unfreed memory */
    52         -  int iSize;                          /* Size of this allocation */
    53     53     char nBacktrace;                    /* Number of backtraces on this alloc */
    54     54     char nBacktraceSlots;               /* Available backtrace slots */
    55     55     short nTitle;                       /* Bytes of title; includes '\0' */
    56     56     int iForeGuard;                     /* Guard word for sanity */
    57     57   };
    58     58   
    59     59   /*
................................................................................
   214    214     int *pInt;
   215    215     u8 *pU8;
   216    216     int nReserve;
   217    217   
   218    218     p = (struct MemBlockHdr*)pAllocation;
   219    219     p--;
   220    220     assert( p->iForeGuard==FOREGUARD );
   221         -  nReserve = (p->iSize+3)&~3;
          221  +  nReserve = (p->iSize+7)&~7;
   222    222     pInt = (int*)pAllocation;
   223    223     pU8 = (u8*)pAllocation;
   224    224     assert( pInt[nReserve/sizeof(int)]==REARGUARD );
   225    225     assert( (nReserve-0)<=p->iSize || pU8[nReserve-1]==0x65 );
   226    226     assert( (nReserve-1)<=p->iSize || pU8[nReserve-2]==0x65 );
   227    227     assert( (nReserve-2)<=p->iSize || pU8[nReserve-3]==0x65 );
   228    228     return p;
................................................................................
   254    254     if( nByte>0 ){
   255    255       int nReserve;
   256    256       enterMem();
   257    257       assert( mem.disallow==0 );
   258    258       if( mem.alarmCallback!=0 && mem.nowUsed+nByte>=mem.alarmThreshold ){
   259    259         sqlite3MemsysAlarm(nByte);
   260    260       }
   261         -    nReserve = (nByte+3)&~3;
          261  +    nReserve = (nByte+7)&~7;
   262    262       if( nReserve/8>NCSIZE-1 ){
   263    263         mem.sizeCnt[NCSIZE-1]++;
   264    264       }else{
   265    265         mem.sizeCnt[nReserve/8]++;
   266    266       }
   267    267       totalSize = nReserve + sizeof(*pHdr) + sizeof(int) +
   268    268                    mem.nBacktrace*sizeof(void*) + mem.nTitle;
................................................................................
   409    409   */
   410    410   void sqlite3MemdebugSettitle(const char *zTitle){
   411    411     int n = strlen(zTitle) + 1;
   412    412     enterMem();
   413    413     if( n>=sizeof(mem.zTitle) ) n = sizeof(mem.zTitle)-1;
   414    414     memcpy(mem.zTitle, zTitle, n);
   415    415     mem.zTitle[n] = 0;
   416         -  mem.nTitle = (n+3)&~3;
          416  +  mem.nTitle = (n+7)&~7;
   417    417     sqlite3_mutex_leave(mem.mutex);
   418    418   }
   419    419   
   420    420   void sqlite3MemdebugSync(){
   421    421     struct MemBlockHdr *pHdr;
   422    422     for(pHdr=mem.pFirst; pHdr; pHdr=pHdr->pNext){
   423    423       void **pBt = (void**)pHdr;
................................................................................
   440    440       fprintf(stderr, "** Unable to output memory debug output log: %s **\n",
   441    441                       zFilename);
   442    442       return;
   443    443     }
   444    444     for(pHdr=mem.pFirst; pHdr; pHdr=pHdr->pNext){
   445    445       char *z = (char*)pHdr;
   446    446       z -= pHdr->nBacktraceSlots*sizeof(void*) + pHdr->nTitle;
   447         -    fprintf(out, "**** %d bytes at %p from %s ****\n", 
          447  +    fprintf(out, "**** %lld bytes at %p from %s ****\n", 
   448    448               pHdr->iSize, &pHdr[1], pHdr->nTitle ? z : "???");
   449    449       if( pHdr->nBacktrace ){
   450    450         fflush(out);
   451    451         pBt = (void**)pHdr;
   452    452         pBt -= pHdr->nBacktraceSlots;
   453    453         backtrace_symbols_fd(pBt, pHdr->nBacktrace, fileno(out));
   454    454         fprintf(out, "\n");

Changes to src/vdbeaux.c.

  2061   2061   ** and store the result in pMem.  Return the number of bytes read.
  2062   2062   */ 
  2063   2063   int sqlite3VdbeSerialGet(
  2064   2064     const unsigned char *buf,     /* Buffer to deserialize from */
  2065   2065     u32 serial_type,              /* Serial type to deserialize */
  2066   2066     Mem *pMem                     /* Memory cell to write value into */
  2067   2067   ){
         2068  +#ifndef SQLITE_MEMORY_SIZE
         2069  +  assert( (7&(int)pMem)==0 );   /* Verify 8-byte alignment.  Ticket #3040 */
         2070  +#endif
  2068   2071     switch( serial_type ){
  2069   2072       case 10:   /* Reserved for future use */
  2070   2073       case 11:   /* Reserved for future use */
  2071   2074       case 0: {  /* NULL */
  2072   2075         pMem->flags = MEM_Null;
  2073   2076         break;
  2074   2077       }
................................................................................
  2193   2196     const unsigned char *aKey = (const unsigned char *)pKey;
  2194   2197     UnpackedRecord *p;
  2195   2198     int nByte;
  2196   2199     int i, idx, d;
  2197   2200     u32 szHdr;
  2198   2201     Mem *pMem;
  2199   2202     
  2200         -  nByte = sizeof(*p) + sizeof(Mem)*(pKeyInfo->nField+1);
         2203  +  assert( sizeof(Mem)>sizeof(*p) );
         2204  +  nByte = sizeof(Mem)*(pKeyInfo->nField+2);
  2201   2205     if( nByte>szSpace ){
  2202   2206       p = sqlite3DbMallocRaw(pKeyInfo->db, nByte);
  2203   2207       if( p==0 ) return 0;
  2204   2208       p->needFree = 1;
  2205   2209     }else{
  2206   2210       p = pSpace;
  2207   2211       p->needFree = 0;
  2208   2212     }
  2209   2213     p->pKeyInfo = pKeyInfo;
  2210   2214     p->nField = pKeyInfo->nField + 1;
  2211   2215     p->needDestroy = 1;
  2212         -  p->aMem = pMem = (Mem*)&p[1];
         2216  +  p->aMem = pMem = &((Mem*)p)[1];
  2213   2217     idx = GetVarint(aKey, szHdr);
  2214   2218     d = szHdr;
  2215   2219     i = 0;
  2216   2220     while( idx<szHdr && i<p->nField ){
  2217   2221       u32 serial_type;
  2218   2222   
  2219   2223       idx += GetVarint( aKey+idx, serial_type);