Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / third_party / sqlite / sqlite-src-3080704 / src / memjournal.c
blob6452cecc379ff0d8fc47126ace27a34e552d252b
1 /*
2 ** 2008 October 7
3 **
4 ** The author disclaims copyright to this source code. In place of
5 ** a legal notice, here is a blessing:
6 **
7 ** May you do good and not evil.
8 ** May you find forgiveness for yourself and forgive others.
9 ** May you share freely, never taking more than you give.
11 *************************************************************************
13 ** This file contains code use to implement an in-memory rollback journal.
14 ** The in-memory rollback journal is used to journal transactions for
15 ** ":memory:" databases and when the journal_mode=MEMORY pragma is used.
17 #include "sqliteInt.h"
19 /* Forward references to internal structures */
20 typedef struct MemJournal MemJournal;
21 typedef struct FilePoint FilePoint;
22 typedef struct FileChunk FileChunk;
24 /* Space to hold the rollback journal is allocated in increments of
25 ** this many bytes.
27 ** The size chosen is a little less than a power of two. That way,
28 ** the FileChunk object will have a size that almost exactly fills
29 ** a power-of-two allocation. This minimizes wasted space in power-of-two
30 ** memory allocators.
32 #define JOURNAL_CHUNKSIZE ((int)(1024-sizeof(FileChunk*)))
35 ** The rollback journal is composed of a linked list of these structures.
37 struct FileChunk {
38 FileChunk *pNext; /* Next chunk in the journal */
39 u8 zChunk[JOURNAL_CHUNKSIZE]; /* Content of this chunk */
43 ** An instance of this object serves as a cursor into the rollback journal.
44 ** The cursor can be either for reading or writing.
46 struct FilePoint {
47 sqlite3_int64 iOffset; /* Offset from the beginning of the file */
48 FileChunk *pChunk; /* Specific chunk into which cursor points */
52 ** This subclass is a subclass of sqlite3_file. Each open memory-journal
53 ** is an instance of this class.
55 struct MemJournal {
56 sqlite3_io_methods *pMethod; /* Parent class. MUST BE FIRST */
57 FileChunk *pFirst; /* Head of in-memory chunk-list */
58 FilePoint endpoint; /* Pointer to the end of the file */
59 FilePoint readpoint; /* Pointer to the end of the last xRead() */
63 ** Read data from the in-memory journal file. This is the implementation
64 ** of the sqlite3_vfs.xRead method.
66 static int memjrnlRead(
67 sqlite3_file *pJfd, /* The journal file from which to read */
68 void *zBuf, /* Put the results here */
69 int iAmt, /* Number of bytes to read */
70 sqlite_int64 iOfst /* Begin reading at this offset */
72 MemJournal *p = (MemJournal *)pJfd;
73 u8 *zOut = zBuf;
74 int nRead = iAmt;
75 int iChunkOffset;
76 FileChunk *pChunk;
78 /* SQLite never tries to read past the end of a rollback journal file */
79 assert( iOfst+iAmt<=p->endpoint.iOffset );
81 if( p->readpoint.iOffset!=iOfst || iOfst==0 ){
82 sqlite3_int64 iOff = 0;
83 for(pChunk=p->pFirst;
84 ALWAYS(pChunk) && (iOff+JOURNAL_CHUNKSIZE)<=iOfst;
85 pChunk=pChunk->pNext
87 iOff += JOURNAL_CHUNKSIZE;
89 }else{
90 pChunk = p->readpoint.pChunk;
93 iChunkOffset = (int)(iOfst%JOURNAL_CHUNKSIZE);
94 do {
95 int iSpace = JOURNAL_CHUNKSIZE - iChunkOffset;
96 int nCopy = MIN(nRead, (JOURNAL_CHUNKSIZE - iChunkOffset));
97 memcpy(zOut, &pChunk->zChunk[iChunkOffset], nCopy);
98 zOut += nCopy;
99 nRead -= iSpace;
100 iChunkOffset = 0;
101 } while( nRead>=0 && (pChunk=pChunk->pNext)!=0 && nRead>0 );
102 p->readpoint.iOffset = iOfst+iAmt;
103 p->readpoint.pChunk = pChunk;
105 return SQLITE_OK;
109 ** Write data to the file.
111 static int memjrnlWrite(
112 sqlite3_file *pJfd, /* The journal file into which to write */
113 const void *zBuf, /* Take data to be written from here */
114 int iAmt, /* Number of bytes to write */
115 sqlite_int64 iOfst /* Begin writing at this offset into the file */
117 MemJournal *p = (MemJournal *)pJfd;
118 int nWrite = iAmt;
119 u8 *zWrite = (u8 *)zBuf;
121 /* An in-memory journal file should only ever be appended to. Random
122 ** access writes are not required by sqlite.
124 assert( iOfst==p->endpoint.iOffset );
125 UNUSED_PARAMETER(iOfst);
127 while( nWrite>0 ){
128 FileChunk *pChunk = p->endpoint.pChunk;
129 int iChunkOffset = (int)(p->endpoint.iOffset%JOURNAL_CHUNKSIZE);
130 int iSpace = MIN(nWrite, JOURNAL_CHUNKSIZE - iChunkOffset);
132 if( iChunkOffset==0 ){
133 /* New chunk is required to extend the file. */
134 FileChunk *pNew = sqlite3_malloc(sizeof(FileChunk));
135 if( !pNew ){
136 return SQLITE_IOERR_NOMEM;
138 pNew->pNext = 0;
139 if( pChunk ){
140 assert( p->pFirst );
141 pChunk->pNext = pNew;
142 }else{
143 assert( !p->pFirst );
144 p->pFirst = pNew;
146 p->endpoint.pChunk = pNew;
149 memcpy(&p->endpoint.pChunk->zChunk[iChunkOffset], zWrite, iSpace);
150 zWrite += iSpace;
151 nWrite -= iSpace;
152 p->endpoint.iOffset += iSpace;
155 return SQLITE_OK;
159 ** Truncate the file.
161 static int memjrnlTruncate(sqlite3_file *pJfd, sqlite_int64 size){
162 MemJournal *p = (MemJournal *)pJfd;
163 FileChunk *pChunk;
164 assert(size==0);
165 UNUSED_PARAMETER(size);
166 pChunk = p->pFirst;
167 while( pChunk ){
168 FileChunk *pTmp = pChunk;
169 pChunk = pChunk->pNext;
170 sqlite3_free(pTmp);
172 sqlite3MemJournalOpen(pJfd);
173 return SQLITE_OK;
177 ** Close the file.
179 static int memjrnlClose(sqlite3_file *pJfd){
180 memjrnlTruncate(pJfd, 0);
181 return SQLITE_OK;
186 ** Sync the file.
188 ** Syncing an in-memory journal is a no-op. And, in fact, this routine
189 ** is never called in a working implementation. This implementation
190 ** exists purely as a contingency, in case some malfunction in some other
191 ** part of SQLite causes Sync to be called by mistake.
193 static int memjrnlSync(sqlite3_file *NotUsed, int NotUsed2){
194 UNUSED_PARAMETER2(NotUsed, NotUsed2);
195 return SQLITE_OK;
199 ** Query the size of the file in bytes.
201 static int memjrnlFileSize(sqlite3_file *pJfd, sqlite_int64 *pSize){
202 MemJournal *p = (MemJournal *)pJfd;
203 *pSize = (sqlite_int64) p->endpoint.iOffset;
204 return SQLITE_OK;
208 ** Table of methods for MemJournal sqlite3_file object.
210 static const struct sqlite3_io_methods MemJournalMethods = {
211 1, /* iVersion */
212 memjrnlClose, /* xClose */
213 memjrnlRead, /* xRead */
214 memjrnlWrite, /* xWrite */
215 memjrnlTruncate, /* xTruncate */
216 memjrnlSync, /* xSync */
217 memjrnlFileSize, /* xFileSize */
218 0, /* xLock */
219 0, /* xUnlock */
220 0, /* xCheckReservedLock */
221 0, /* xFileControl */
222 0, /* xSectorSize */
223 0, /* xDeviceCharacteristics */
224 0, /* xShmMap */
225 0, /* xShmLock */
226 0, /* xShmBarrier */
227 0, /* xShmUnmap */
228 0, /* xFetch */
229 0 /* xUnfetch */
233 ** Open a journal file.
235 void sqlite3MemJournalOpen(sqlite3_file *pJfd){
236 MemJournal *p = (MemJournal *)pJfd;
237 assert( EIGHT_BYTE_ALIGNMENT(p) );
238 memset(p, 0, sqlite3MemJournalSize());
239 p->pMethod = (sqlite3_io_methods*)&MemJournalMethods;
243 ** Return true if the file-handle passed as an argument is
244 ** an in-memory journal
246 int sqlite3IsMemJournal(sqlite3_file *pJfd){
247 return pJfd->pMethods==&MemJournalMethods;
251 ** Return the number of bytes required to store a MemJournal file descriptor.
253 int sqlite3MemJournalSize(void){
254 return sizeof(MemJournal);