Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(5)

Side by Side Diff: third_party/sqlite/sqlite-src-3080704/src/memjournal.c

Issue 949043002: Add //third_party/sqlite to dirs_to_snapshot, remove net_sql.patch (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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.
10 **
11 *************************************************************************
12 **
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.
16 */
17 #include "sqliteInt.h"
18
19 /* Forward references to internal structures */
20 typedef struct MemJournal MemJournal;
21 typedef struct FilePoint FilePoint;
22 typedef struct FileChunk FileChunk;
23
24 /* Space to hold the rollback journal is allocated in increments of
25 ** this many bytes.
26 **
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.
31 */
32 #define JOURNAL_CHUNKSIZE ((int)(1024-sizeof(FileChunk*)))
33
34 /*
35 ** The rollback journal is composed of a linked list of these structures.
36 */
37 struct FileChunk {
38 FileChunk *pNext; /* Next chunk in the journal */
39 u8 zChunk[JOURNAL_CHUNKSIZE]; /* Content of this chunk */
40 };
41
42 /*
43 ** An instance of this object serves as a cursor into the rollback journal.
44 ** The cursor can be either for reading or writing.
45 */
46 struct FilePoint {
47 sqlite3_int64 iOffset; /* Offset from the beginning of the file */
48 FileChunk *pChunk; /* Specific chunk into which cursor points */
49 };
50
51 /*
52 ** This subclass is a subclass of sqlite3_file. Each open memory-journal
53 ** is an instance of this class.
54 */
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() */
60 };
61
62 /*
63 ** Read data from the in-memory journal file. This is the implementation
64 ** of the sqlite3_vfs.xRead method.
65 */
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 */
71 ){
72 MemJournal *p = (MemJournal *)pJfd;
73 u8 *zOut = zBuf;
74 int nRead = iAmt;
75 int iChunkOffset;
76 FileChunk *pChunk;
77
78 /* SQLite never tries to read past the end of a rollback journal file */
79 assert( iOfst+iAmt<=p->endpoint.iOffset );
80
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
86 ){
87 iOff += JOURNAL_CHUNKSIZE;
88 }
89 }else{
90 pChunk = p->readpoint.pChunk;
91 }
92
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;
104
105 return SQLITE_OK;
106 }
107
108 /*
109 ** Write data to the file.
110 */
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 */
116 ){
117 MemJournal *p = (MemJournal *)pJfd;
118 int nWrite = iAmt;
119 u8 *zWrite = (u8 *)zBuf;
120
121 /* An in-memory journal file should only ever be appended to. Random
122 ** access writes are not required by sqlite.
123 */
124 assert( iOfst==p->endpoint.iOffset );
125 UNUSED_PARAMETER(iOfst);
126
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);
131
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;
137 }
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;
145 }
146 p->endpoint.pChunk = pNew;
147 }
148
149 memcpy(&p->endpoint.pChunk->zChunk[iChunkOffset], zWrite, iSpace);
150 zWrite += iSpace;
151 nWrite -= iSpace;
152 p->endpoint.iOffset += iSpace;
153 }
154
155 return SQLITE_OK;
156 }
157
158 /*
159 ** Truncate the file.
160 */
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);
171 }
172 sqlite3MemJournalOpen(pJfd);
173 return SQLITE_OK;
174 }
175
176 /*
177 ** Close the file.
178 */
179 static int memjrnlClose(sqlite3_file *pJfd){
180 memjrnlTruncate(pJfd, 0);
181 return SQLITE_OK;
182 }
183
184
185 /*
186 ** Sync the file.
187 **
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.
192 */
193 static int memjrnlSync(sqlite3_file *NotUsed, int NotUsed2){
194 UNUSED_PARAMETER2(NotUsed, NotUsed2);
195 return SQLITE_OK;
196 }
197
198 /*
199 ** Query the size of the file in bytes.
200 */
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;
205 }
206
207 /*
208 ** Table of methods for MemJournal sqlite3_file object.
209 */
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 */
230 };
231
232 /*
233 ** Open a journal file.
234 */
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;
240 }
241
242 /*
243 ** Return true if the file-handle passed as an argument is
244 ** an in-memory journal
245 */
246 int sqlite3IsMemJournal(sqlite3_file *pJfd){
247 return pJfd->pMethods==&MemJournalMethods;
248 }
249
250 /*
251 ** Return the number of bytes required to store a MemJournal file descriptor.
252 */
253 int sqlite3MemJournalSize(void){
254 return sizeof(MemJournal);
255 }
OLDNEW
« no previous file with comments | « third_party/sqlite/sqlite-src-3080704/src/mem5.c ('k') | third_party/sqlite/sqlite-src-3080704/src/mutex.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698