Пример #1
0
/*
** Free memory.
*/
void sqlite3_free(void *pPrior) {
    struct MemBlockHdr *pHdr;
    void **pBt;
    char *z;
    if( pPrior==0 ) {
        return;
    }
    assert( mem.mutex!=0 );
    pHdr = sqlite3MemsysGetHeader(pPrior);
    pBt = (void**)pHdr;
    pBt -= pHdr->nBacktraceSlots;
    sqlite3_mutex_enter(mem.mutex);
    mem.nowUsed -= pHdr->iSize;
    if( pHdr->pPrev ) {
        assert( pHdr->pPrev->pNext==pHdr );
        pHdr->pPrev->pNext = pHdr->pNext;
    } else {
        assert( mem.pFirst==pHdr );
        mem.pFirst = pHdr->pNext;
    }
    if( pHdr->pNext ) {
        assert( pHdr->pNext->pPrev==pHdr );
        pHdr->pNext->pPrev = pHdr->pPrev;
    } else {
        assert( mem.pLast==pHdr );
        mem.pLast = pHdr->pPrev;
    }
    z = (char*)pBt;
    z -= pHdr->nTitle;
    memset(z, 0x2b, sizeof(void*)*pHdr->nBacktraceSlots + sizeof(*pHdr) +
           pHdr->iSize + sizeof(int) + pHdr->nTitle);
    free(z);
    sqlite3_mutex_leave(mem.mutex);
}
Пример #2
0
/*
** Free memory.
*/
static void sqlite3MemFree(void *pPrior){
  struct MemBlockHdr *pHdr;
  void **pBt;
  char *z;
  assert( sqlite3Config.bMemstat || mem.mutex!=0 );
  pHdr = sqlite3MemsysGetHeader(pPrior);
  pBt = (void**)pHdr;
  pBt -= pHdr->nBacktraceSlots;
  sqlite3_mutex_enter(mem.mutex);
  if( pHdr->pPrev ){
    assert( pHdr->pPrev->pNext==pHdr );
    pHdr->pPrev->pNext = pHdr->pNext;
  }else{
    assert( mem.pFirst==pHdr );
    mem.pFirst = pHdr->pNext;
  }
  if( pHdr->pNext ){
    assert( pHdr->pNext->pPrev==pHdr );
    pHdr->pNext->pPrev = pHdr->pPrev;
  }else{
    assert( mem.pLast==pHdr );
    mem.pLast = pHdr->pPrev;
  }
  z = (char*)pBt;
  z -= pHdr->nTitle;
  adjustStats(pHdr->iSize, -1);
  memset(z, 0x2b, sizeof(void*)*pHdr->nBacktraceSlots + sizeof(*pHdr) +
                  pHdr->iSize + sizeof(int) + pHdr->nTitle);
  free(z);
  sqlite3_mutex_leave(mem.mutex);  
}
Пример #3
0
/*
** Return the number of bytes currently allocated at address p.
*/
static int sqlite3MemSize(void *p){
  struct MemBlockHdr *pHdr;
  if( !p ){
    return 0;
  }
  pHdr = sqlite3MemsysGetHeader(p);
  return pHdr->iSize;
}
Пример #4
0
/*
** Change the size of an existing memory allocation.
**
** For this debugging implementation, we *always* make a copy of the
** allocation into a new place in memory.  In this way, if the 
** higher level code is using pointer to the old allocation, it is 
** much more likely to break and we are much more liking to find
** the error.
*/
static void *sqlite3MemRealloc(void *pPrior, int nByte){
  struct MemBlockHdr *pOldHdr;
  void *pNew;
  assert( mem.disallow==0 );
  pOldHdr = sqlite3MemsysGetHeader(pPrior);
  pNew = sqlite3MemMalloc(nByte);
  if( pNew ){
    memcpy(pNew, pPrior, nByte<pOldHdr->iSize ? nByte : pOldHdr->iSize);
    if( nByte>pOldHdr->iSize ){
      memset(&((char*)pNew)[pOldHdr->iSize], 0x2b, nByte - pOldHdr->iSize);
    }
    sqlite3MemFree(pPrior);
  }
  return pNew;
}