-
Notifications
You must be signed in to change notification settings - Fork 23
/
SaveImage.cpp
269 lines (220 loc) · 6.93 KB
/
SaveImage.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
/******************************************************************************
File: image.cpp
Description:
Object Memory management image save method
definitions for Dolphin Smalltalk
******************************************************************************/
#include "ist.h"
#if defined(CANSAVEIMAGE)
#include "binstream.h"
#include "zfbinstream.h"
#include "objmem.h"
#include "ObjMemPriv.inl"
#include "interprt.h"
#include "rc_vm.h"
// Smalltalk classes
#include "STProcess.h" // The stacks are patched on loading to new addresses
#include "STString.h"
#include "STCharacter.h"
#include "STClassDesc.h"
#include "Utf16StringBuf.h"
#ifdef NDEBUG
#pragma optimize("s", on)
#pragma auto_inline(off)
#endif
#ifdef _DEBUG
#define PROFILE_IMAGELOADSAVE
#endif
//////////////////////////////////////////////////////////////////////////////
// Image Save Methods
int __stdcall ObjectMemory::SaveImageFile(const wchar_t* szFileName, bool bBackup, int nCompressionLevel, unsigned nMaxObjects)
{
// Answer:
// NULL = success
// ZeroPointer = general save error
if (!szFileName)
return 2;
if (nMaxObjects == 0)
{
nMaxObjects = m_nOTMax;
}
if (nMaxObjects < m_nOTSize + OTMinHeadroom)
{
return 4;
}
if (nMaxObjects > OTMaxLimit)
{
return 5;
}
int nRet = 3;
const wchar_t* saveName;
wchar_t bak[_MAX_PATH];
if (bBackup)
{
wchar_t folder[_MAX_PATH];
wchar_t fname[_MAX_FNAME];
{
wchar_t dir[_MAX_DIR];
wchar_t drive[_MAX_DRIVE];
_wsplitpath_s(szFileName, drive, _MAX_DRIVE, dir, _MAX_DIR, fname, _MAX_FNAME, NULL, 0);
_wmakepath(folder, drive, dir, NULL, NULL);
_wmakepath(bak, drive, dir, fname, L"bak");
}
saveName = _wtempnam(folder, fname);
}
else
saveName = szFileName;
int fd;
int err = ::_wsopen_s(&fd, (const wchar_t*)saveName, _O_WRONLY|_O_BINARY|_O_CREAT|_O_TRUNC|_O_SEQUENTIAL, _SH_DENYRW, _S_IWRITE|_S_IREAD);
if (err != 0)
{
char buf[256];
strerror_s(buf, errno);
TRACE(L"Failed to open image file for save %d:'%s'\n", errno, buf);
return 2;
}
#ifdef PROFILE_IMAGELOADSAVE
TRACESTREAM<< L"Saving image to '" << saveName<< L"' ..." << std::endl;
DWORD dwStartTicks = GetTickCount();
#endif
::_write(fd, ISTHDRTYPE, sizeof(ISTHDRTYPE));
VS_FIXEDFILEINFO versionInfo;
::GetVersionInfo(&versionInfo);
ImageHeader header;
memset(&header, 0, sizeof(header));
header.versionMS = versionInfo.dwProductVersionMS;
SMALLINTEGER imageVersionMinor = isIntegerObject(_Pointers.ImageVersionMinor) ? integerValueOf(_Pointers.ImageVersionMinor) : 0;
header.versionLS = imageVersionMinor == 0 ? LOWORD(versionInfo.dwProductVersionLS) << 16 : imageVersionMinor;
header.flags.bIsCompressed = nCompressionLevel != 0;
header.nGlobalPointers = NumPointers;
// By saving down the base offset with the image, we don't need
// to convert on save, only load
ASSERT(sizeof(OTE*) == sizeof(DWORD));
header.BasePointer = m_pOT;
// Saving down current hash counter is not 100% necessary, but ensures
// consistent behavior each time image is loaded.
header.nNextIdHash = m_nNextIdHash;
// User may have modified max table size
header.nMaxTableSize = nMaxObjects;
// Set the OT size
unsigned i = lastOTEntry();
// Find the last used entry
ASSERT(i > NumPermanent);
header.nTableSize = i + 1;
::_write(fd, &header, sizeof(ImageHeader));
bool bSaved;
{
BYTE buf[dwAllocationGranularity];
if (header.flags.bIsCompressed)
{
zfbinstream stream;
//stream.rdbuf()->setbuf(buf, sizeof(buf));
//stream.clrlock();
stream.attach(fd, "wb", 0, true);
//stream << setcompressionlevel(nCompressionLevel);
bSaved = SaveImage(stream, &header, nRet);
}
else
{
fbinstream stream;
// We don't need thread synchronisation
//stream.clrlock();
stream.attach(fd, "wb");
stream.setbuf(buf, sizeof(buf));
bSaved = SaveImage(stream, &header, nRet);
}
#ifdef PROFILE_IMAGELOADSAVE
DWORD msToRun = GetTickCount() - dwStartTicks;
TRACESTREAM << L" done (" << (bSaved ? "Succeeded" : "Failed")<< L"), binstreams time " << long(msToRun)<< L"mS" << std::endl;
#endif
}
if (bBackup)
{
if (bSaved)
{
_wremove(bak);
_wrename(szFileName, bak);
nRet = _wrename(saveName, szFileName);
}
else
{
_wremove(saveName);
}
}
else
{
if (bSaved)
nRet = 0;
}
return nRet;
}
bool __stdcall ObjectMemory::SaveImage(obinstream& imageFile, const ImageHeader* pHeader, int nRet)
{
EmptyZct(Interpreter::m_registers.m_stackPointer);
// Do the save.
bool bResult = imageFile.good() != 0
&& (nRet == 3)
&& SaveObjectTable(imageFile, pHeader)
// From VM 7.0.54 two bytes are allowed for null terminators to correctly accommodate wide strings;
// Prior to 7.0.54 null terminators were always 1 byte (which was not really sufficient for wide strings).
&& (pHeader->HasSingleByteNullTerms()
? SaveObjects<sizeof(char)>(imageFile, pHeader)
: SaveObjects<sizeof(WCHAR)>(imageFile, pHeader))
&& imageFile.flush().good();
PopulateZct(Interpreter::m_registers.m_stackPointer);
return bResult;
}
// Quick and dirty - save the whole OT wasting space used by empty
// entries
bool __stdcall ObjectMemory::SaveObjectTable(obinstream& imageFile, const ImageHeader* pHeader)
{
return imageFile.write(m_pOT, sizeof(OTE)*pHeader->nTableSize);
}
template <MWORD ImageNullTerms> bool __stdcall ObjectMemory::SaveObjects(obinstream& imageFile, const ImageHeader* pHeader)
{
#ifdef _DEBUG
unsigned numObjects = 0;
unsigned nFree = 0;
#endif
DWORD dwDataSize = 0;
const OTE* pEnd = m_pOT+pHeader->nTableSize; // Loop invariant
for (OTE* ote=m_pOT; ote < pEnd; ote++)
{
if (!ote->isFree())
{
#ifdef _DEBUG
numObjects++;
#endif
POBJECT obj = ote->m_location;
if (ote->heapSpace() == OTEFlags::VirtualSpace)
{
VirtualObject* vObj = static_cast<VirtualObject*>(obj);
VirtualObjectHeader* pObjHeader = vObj->getHeader();
// We only write the max allocation size from the header. The rest of the info (fxstate) is not preserved across image saves.
imageFile.write(pObjHeader, sizeof(MWORD));
dwDataSize += sizeof(MWORD);
}
MWORD bytesToWrite = ote->getSize() + (ote->isNullTerminated() * ImageNullTerms);
imageFile.write(obj, bytesToWrite);
if (imageFile.good() == 0)
return false;
dwDataSize += bytesToWrite;
}
else
{
#ifdef _DEBUG
nFree++;
#endif
}
}
#ifdef _DEBUG
TRACESTREAM << numObjects<< L" objects saved totalling " << std::dec << dwDataSize
<< L" bytes, " << nFree<< L" free OTEs"
// Compressed stream does not support seeking and sets to bad state
//<< L", writing checksum at offset " << imageFile.tellp()
<< std::endl;
#endif
// Append the amount of data written as a checksum.
return imageFile.write(&dwDataSize, sizeof(DWORD));
}
#endif