説明を見る。00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #ifndef V8_SERIALIZE_H_
00029 #define V8_SERIALIZE_H_
00030
00031 #include "hashmap.h"
00032
00033 namespace v8 { namespace internal {
00034
00035
00036
00037 enum TypeCode {
00038 UNCLASSIFIED,
00039 BUILTIN,
00040 RUNTIME_FUNCTION,
00041 IC_UTILITY,
00042 DEBUG_ADDRESS,
00043 STATS_COUNTER,
00044 TOP_ADDRESS,
00045 C_BUILTIN,
00046 EXTENSION,
00047 ACCESSOR,
00048 RUNTIME_ENTRY,
00049 STUB_CACHE_TABLE
00050 };
00051
00052 const int kTypeCodeCount = STUB_CACHE_TABLE + 1;
00053 const int kFirstTypeCode = UNCLASSIFIED;
00054
00055 const int kReferenceIdBits = 16;
00056 const int kReferenceIdMask = (1 << kReferenceIdBits) - 1;
00057 const int kReferenceTypeShift = kReferenceIdBits;
00058 const int kDebugRegisterBits = 4;
00059 const int kDebugIdShift = kDebugRegisterBits;
00060
00061
00062 class ExternalReferenceEncoder {
00063 public:
00064 ExternalReferenceEncoder();
00065
00066 uint32_t Encode(Address key) const;
00067
00068 const char* NameOfAddress(Address key) const;
00069
00070 private:
00071 HashMap encodings_;
00072 static uint32_t Hash(Address key) {
00073 return reinterpret_cast<uint32_t>(key) >> 2;
00074 }
00075
00076 int IndexOf(Address key) const;
00077
00078 static bool Match(void* key1, void* key2) { return key1 == key2; }
00079
00080 void Put(Address key, int index);
00081 };
00082
00083
00084 class ExternalReferenceDecoder {
00085 public:
00086 ExternalReferenceDecoder();
00087 ~ExternalReferenceDecoder();
00088
00089 Address Decode(uint32_t key) const {
00090 if (key == 0) return NULL;
00091 return *Lookup(key);
00092 }
00093
00094 private:
00095 Address** encodings_;
00096
00097 Address* Lookup(uint32_t key) const {
00098 int type = key >> kReferenceTypeShift;
00099 ASSERT(kFirstTypeCode <= type && type < kTypeCodeCount);
00100 int id = key & kReferenceIdMask;
00101 return &encodings_[type][id];
00102 }
00103
00104 void Put(uint32_t key, Address value) {
00105 *Lookup(key) = value;
00106 }
00107 };
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120 class RelativeAddress;
00121 class SimulatedHeapSpace;
00122 class SnapshotWriter;
00123 class ReferenceUpdater;
00124
00125
00126 class Serializer: public ObjectVisitor {
00127 public:
00128 Serializer();
00129
00130 virtual ~Serializer();
00131
00132
00133
00134 void Serialize();
00135
00136
00137
00138 void Finalize(char** str, int* len);
00139
00140 int roots() { return roots_; }
00141 int objects() { return objects_; }
00142
00143 #ifdef DEBUG
00144
00145 virtual void Synchronize(const char* tag);
00146 #endif
00147
00148 static bool enabled() { return serialization_enabled_; }
00149
00150 static void disable() { serialization_enabled_ = false; }
00151
00152 private:
00153 friend class ReferenceUpdater;
00154
00155 virtual void VisitPointers(Object** start, Object** end);
00156
00157 bool IsVisited(HeapObject* obj);
00158
00159 Address GetSavedAddress(HeapObject* obj);
00160
00161 void SaveAddress(HeapObject* obj, Address addr);
00162
00163 void PutEncodedAddress(Address addr);
00164
00165 void PutFlags();
00166
00167 void PutHeader();
00168
00169 void PutLog();
00170
00171 Address PutObject(HeapObject* obj);
00172
00173 void PutGlobalHandleStack(const List<Handle<Object> >& stack);
00174
00175 void PutContextStack();
00176
00177
00178
00179
00180
00181 Address Encode(Object* o, bool* serialized);
00182
00183
00184
00185 RelativeAddress Allocate(HeapObject* obj);
00186
00187 void InitializeAllocators();
00188
00189 SnapshotWriter* writer_;
00190 bool root_;
00191 int roots_;
00192 int objects_;
00193
00194 static bool serialization_enabled_;
00195
00196 int flags_end_;
00197
00198
00199 SimulatedHeapSpace* allocator_[LAST_SPACE+1];
00200
00201 List<Object**> global_handles_;
00202
00203 ExternalReferenceEncoder* reference_encoder_;
00204
00205 HashMap saved_addresses_;
00206
00207 DISALLOW_COPY_AND_ASSIGN(Serializer);
00208 };
00209
00210
00211
00212 class SnapshotReader {
00213 public:
00214 SnapshotReader(const char* str, int len): str_(str), end_(str + len) {}
00215
00216 void ExpectC(char expected) {
00217 int c = GetC();
00218 USE(c);
00219 ASSERT(c == expected);
00220 }
00221
00222 int GetC() {
00223 if (str_ >= end_) return EOF;
00224 return *str_++;
00225 }
00226
00227 int GetInt() {
00228 int result = *reinterpret_cast<const int*>(str_);
00229 str_ += sizeof(result);
00230 return result;
00231 }
00232
00233 void GetBytes(Address a, int size) {
00234 ASSERT(str_ + size <= end_);
00235 memcpy(a, str_, size);
00236 str_ += size;
00237 }
00238
00239 char* GetString() {
00240 ExpectC('[');
00241 int size = GetInt();
00242 ExpectC(']');
00243 char* s = NewArray<char>(size + 1);
00244 GetBytes(reinterpret_cast<Address>(s), size);
00245 s[size] = 0;
00246 return s;
00247 }
00248
00249 private:
00250 const char* str_;
00251 const char* end_;
00252 };
00253
00254
00255
00256
00257 class Deserializer: public ObjectVisitor {
00258 public:
00259
00260 Deserializer(const char* str, int len);
00261
00262 virtual ~Deserializer();
00263
00264
00265
00266 void GetFlags();
00267
00268
00269 void GetLog();
00270
00271
00272 void Deserialize();
00273
00274 int roots() { return roots_; }
00275 int objects() { return objects_; }
00276
00277 #ifdef DEBUG
00278
00279 virtual void Synchronize(const char* tag);
00280 #endif
00281
00282 private:
00283 virtual void VisitPointers(Object** start, Object** end);
00284 virtual void VisitExternalReferences(Address* start, Address* end);
00285 virtual void VisitRuntimeEntry(RelocInfo* rinfo);
00286
00287 Address GetEncodedAddress();
00288
00289
00290 void GetHeader();
00291
00292 void GetGlobalHandleStack(List<Handle<Object> >* stack);
00293
00294 void GetContextStack();
00295
00296 Object* GetObject();
00297
00298
00299
00300 void ExpectEncodedAddress(Address expected);
00301
00302
00303
00304
00305 Object* Resolve(Address encoded_address);
00306
00307 SnapshotReader reader_;
00308 bool root_;
00309 int roots_;
00310 int objects_;
00311
00312 bool has_log_;
00313
00314
00315 List<Page*> map_pages_;
00316 List<Page*> old_pointer_pages_;
00317 List<Page*> old_data_pages_;
00318 List<Page*> code_pages_;
00319 List<Object*> large_objects_;
00320
00321 List<Object**> global_handles_;
00322
00323 ExternalReferenceDecoder* reference_decoder_;
00324
00325 #ifdef DEBUG
00326 bool expect_debug_information_;
00327 #endif
00328
00329 DISALLOW_COPY_AND_ASSIGN(Deserializer);
00330 };
00331
00332 } }
00333
00334 #endif // V8_SERIALIZE_H_