/* ** This is a utility function to allocate and populate a new AsyncWrite ** structure and insert it (via addAsyncWrite() ) into the global list. */ static int addNewAsyncWrite( AsyncFile *pFile, int op, i64 iOffset, int nByte, const char *zByte ){ AsyncWrite *p; if( op!=ASYNC_CLOSE && async.ioError ){ return async.ioError; } p = sqlite3OsMalloc(sizeof(AsyncWrite) + (zByte?nByte:0)); if( !p ){ return SQLITE_NOMEM; } p->op = op; p->iOffset = iOffset; p->nByte = nByte; p->pFile = pFile; p->pNext = 0; if( zByte ){ p->zBuf = (char *)&p[1]; memcpy(p->zBuf, zByte, nByte); }else{ p->zBuf = 0; } addAsyncWrite(p); return SQLITE_OK; }
/* ** This is the test layer's wrapper around sqlite3OsMalloc(). */ static void * OSMALLOC(int n){ #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT sqlite3_nMaxAlloc = MAX(sqlite3_nMaxAlloc, sqlite3ThreadDataReadOnly()->nAlloc); #endif assert( !sqlite3_mallocDisallowed ); if( !sqlite3TestMallocFail() ){ u32 *p; p = (u32 *)sqlite3OsMalloc(n + TESTALLOC_OVERHEAD); assert(p); sqlite3_nMalloc++; applyGuards(p); linkAlloc(p); return (void *)(&p[TESTALLOC_NGUARD + 2*sizeof(void *)/sizeof(u32)]); } return 0; }
/* ** This routine does most of the work of opening a file and building ** the OsFile structure. */ static int asyncOpenFile( const char *zName, /* The name of the file to be opened */ OsFile **pFile, /* Put the OsFile structure here */ OsFile *pBaseRead, /* The real OsFile from the real I/O routine */ int openForWriting /* Open a second file handle for writing if true */ ){ int rc, i, n; AsyncFile *p; OsFile *pBaseWrite = 0; static IoMethod iomethod = { asyncClose, asyncOpenDirectory, asyncRead, asyncWrite, asyncSeek, asyncTruncate, asyncSync, asyncSetFullSync, asyncFileHandle, asyncFileSize, asyncLock, asyncUnlock, asyncLockState, asyncCheckReservedLock, asyncSectorSize, }; if( openForWriting && SQLITE_ASYNC_TWO_FILEHANDLES ){ int dummy; rc = xOrigOpenReadWrite(zName, &pBaseWrite, &dummy); if( rc!=SQLITE_OK ){ goto error_out; } } n = strlen(zName); for(i=n-1; i>=0 && zName[i]!='/'; i--){} p = (AsyncFile *)sqlite3OsMalloc(sizeof(AsyncFile) + n - i); if( !p ){ rc = SQLITE_NOMEM; goto error_out; } memset(p, 0, sizeof(AsyncFile)); p->zName = (char*)&p[1]; strcpy(p->zName, &zName[i+1]); p->nName = n - i; p->pMethod = &iomethod; p->pBaseRead = pBaseRead; p->pBaseWrite = pBaseWrite; *pFile = (OsFile *)p; return SQLITE_OK; error_out: assert(!p); sqlite3OsClose(&pBaseRead); sqlite3OsClose(&pBaseWrite); *pFile = 0; return rc; }
void *sqlite3_malloc(int nByte){ return nByte>0 ? sqlite3OsMalloc(nByte) : 0; }