@@ -70,7 +70,6 @@ struct MemJournal {
70
70
int nChunkSize ; /* In-memory chunk-size */
71
71
72
72
int nSpill ; /* Bytes of data before flushing */
73
- int nSize ; /* Bytes of data currently in memory */
74
73
FileChunk * pFirst ; /* Head of in-memory chunk-list */
75
74
FilePoint endpoint ; /* Pointer to the end of the file */
76
75
FilePoint readpoint ; /* Pointer to the end of the last xRead() */
@@ -131,14 +130,13 @@ static int memjrnlRead(
131
130
/*
132
131
** Free the list of FileChunk structures headed at MemJournal.pFirst.
133
132
*/
134
- static void memjrnlFreeChunks (MemJournal * p ){
133
+ static void memjrnlFreeChunks (FileChunk * pFirst ){
135
134
FileChunk * pIter ;
136
135
FileChunk * pNext ;
137
- for (pIter = p -> pFirst ; pIter ; pIter = pNext ){
136
+ for (pIter = pFirst ; pIter ; pIter = pNext ){
138
137
pNext = pIter -> pNext ;
139
138
sqlite3_free (pIter );
140
139
}
141
- p -> pFirst = 0 ;
142
140
}
143
141
144
142
/*
@@ -165,7 +163,7 @@ static int memjrnlCreateFile(MemJournal *p){
165
163
}
166
164
if ( rc == SQLITE_OK ){
167
165
/* No error has occurred. Free the in-memory buffers. */
168
- memjrnlFreeChunks (& copy );
166
+ memjrnlFreeChunks (copy . pFirst );
169
167
}
170
168
}
171
169
if ( rc != SQLITE_OK ){
@@ -248,30 +246,37 @@ static int memjrnlWrite(
248
246
nWrite -= iSpace ;
249
247
p -> endpoint .iOffset += iSpace ;
250
248
}
251
- p -> nSize = iAmt + iOfst ;
252
249
}
253
250
}
254
251
255
252
return SQLITE_OK ;
256
253
}
257
254
258
255
/*
259
- ** Truncate the file.
260
- **
261
- ** If the journal file is already on disk, truncate it there. Or, if it
262
- ** is still in main memory but is being truncated to zero bytes in size,
263
- ** ignore
256
+ ** Truncate the in-memory file.
264
257
*/
265
258
static int memjrnlTruncate (sqlite3_file * pJfd , sqlite_int64 size ){
266
259
MemJournal * p = (MemJournal * )pJfd ;
267
- if ( ALWAYS (size == 0 ) ){
268
- memjrnlFreeChunks (p );
269
- p -> nSize = 0 ;
270
- p -> endpoint .pChunk = 0 ;
271
- p -> endpoint .iOffset = 0 ;
272
- p -> readpoint .pChunk = 0 ;
273
- p -> readpoint .iOffset = 0 ;
260
+ FileChunk * pIter = 0 ;
261
+
262
+ if ( size == 0 ){
263
+ memjrnlFreeChunks (p -> pFirst );
264
+ p -> pFirst = 0 ;
265
+ }else {
266
+ i64 iOff = p -> nChunkSize ;
267
+ for (pIter = p -> pFirst ; ALWAYS (pIter ) && iOff <=size ; pIter = pIter -> pNext ){
268
+ iOff += p -> nChunkSize ;
269
+ }
270
+ if ( pIter ){
271
+ memjrnlFreeChunks (pIter -> pNext );
272
+ pIter -> pNext = 0 ;
273
+ }
274
274
}
275
+
276
+ p -> endpoint .pChunk = pIter ;
277
+ p -> endpoint .iOffset = size ;
278
+ p -> readpoint .pChunk = 0 ;
279
+ p -> readpoint .iOffset = 0 ;
275
280
return SQLITE_OK ;
276
281
}
277
282
@@ -280,7 +285,7 @@ static int memjrnlTruncate(sqlite3_file *pJfd, sqlite_int64 size){
280
285
*/
281
286
static int memjrnlClose (sqlite3_file * pJfd ){
282
287
MemJournal * p = (MemJournal * )pJfd ;
283
- memjrnlFreeChunks (p );
288
+ memjrnlFreeChunks (p -> pFirst );
284
289
return SQLITE_OK ;
285
290
}
286
291
0 commit comments