Chromium Code Reviews| Index: src/d8.h |
| diff --git a/src/d8.h b/src/d8.h |
| index 1bb8a8965ba9a94fbfc738b8663d4eb05eaf20ff..7e9acd7a977e4dfb997223449a0717fb68bd8ded 100644 |
| --- a/src/d8.h |
| +++ b/src/d8.h |
| @@ -5,7 +5,10 @@ |
| #ifndef V8_D8_H_ |
| #define V8_D8_H_ |
| +#include <memory> |
| #include <string> |
| +#include <unordered_set> |
| +#include <vector> |
| #include "src/allocation.h" |
| #include "src/base/hashmap.h" |
| @@ -143,68 +146,51 @@ class SourceGroup { |
| int end_offset_; |
| }; |
| -enum SerializationTag { |
| - kSerializationTagUndefined, |
| - kSerializationTagNull, |
| - kSerializationTagTrue, |
| - kSerializationTagFalse, |
| - kSerializationTagNumber, |
| - kSerializationTagString, |
| - kSerializationTagArray, |
| - kSerializationTagObject, |
| - kSerializationTagArrayBuffer, |
| - kSerializationTagTransferredArrayBuffer, |
| - kSerializationTagTransferredSharedArrayBuffer, |
| -}; |
| - |
| class SerializationData { |
| public: |
| - SerializationData() {} |
| + SerializationData() : data_(nullptr), size_(0) {} |
| ~SerializationData(); |
| - void WriteTag(SerializationTag tag); |
| - void WriteMemory(const void* p, int length); |
| - void WriteArrayBufferContents(const ArrayBuffer::Contents& contents); |
| - void WriteSharedArrayBufferContents( |
| - const SharedArrayBuffer::Contents& contents); |
| - |
| - template <typename T> |
| - void Write(const T& data) { |
| - WriteMemory(&data, sizeof(data)); |
| + uint8_t* data() { return data_.get(); } |
| + size_t size() { return size_; } |
| + const std::vector<ArrayBuffer::Contents>& array_buffer_contents() { |
| + return array_buffer_contents_; |
| } |
| - |
| - SerializationTag ReadTag(int* offset) const; |
| - void ReadMemory(void* p, int length, int* offset) const; |
| - void ReadArrayBufferContents(ArrayBuffer::Contents* contents, |
| - int* offset) const; |
| - void ReadSharedArrayBufferContents(SharedArrayBuffer::Contents* contents, |
| - int* offset) const; |
| - |
| - template <typename T> |
| - T Read(int* offset) const { |
| - T value; |
| - ReadMemory(&value, sizeof(value), offset); |
| - return value; |
| + const std::vector<SharedArrayBuffer::Contents>& |
| + shared_array_buffer_contents() { |
| + return shared_array_buffer_contents_; |
| } |
| + void ClearTransferredArrayBuffers(); |
| + |
| + private: |
| + struct DataDeleter { |
| + void operator()(uint8_t* p) const { free(p); } |
| + }; |
| + |
| + std::unique_ptr<uint8_t, DataDeleter> data_; |
| + size_t size_; |
| + std::vector<ArrayBuffer::Contents> array_buffer_contents_; |
| + std::vector<SharedArrayBuffer::Contents> shared_array_buffer_contents_; |
| + |
| private: |
| - i::List<uint8_t> data_; |
| - i::List<ArrayBuffer::Contents> array_buffer_contents_; |
| - i::List<SharedArrayBuffer::Contents> shared_array_buffer_contents_; |
| + friend class Serializer; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(SerializationData); |
| }; |
| class SerializationDataQueue { |
| public: |
| - void Enqueue(SerializationData* data); |
| - bool Dequeue(SerializationData** data); |
| + void Enqueue(std::unique_ptr<SerializationData> data); |
| + bool Dequeue(std::unique_ptr<SerializationData>* data); |
| bool IsEmpty(); |
| void Clear(); |
| private: |
| base::Mutex mutex_; |
| - i::List<SerializationData*> data_; |
| + std::vector<std::unique_ptr<SerializationData>> data_; |
| }; |
| @@ -219,13 +205,13 @@ class Worker { |
| // Post a message to the worker's incoming message queue. The worker will |
| // take ownership of the SerializationData. |
| // This function should only be called by the thread that created the Worker. |
| - void PostMessage(SerializationData* data); |
| + void PostMessage(std::unique_ptr<SerializationData> data); |
| // Synchronously retrieve messages from the worker's outgoing message queue. |
| // If there is no message in the queue, block until a message is available. |
| // If there are no messages in the queue and the worker is no longer running, |
| // return nullptr. |
| // This function should only be called by the thread that created the Worker. |
| - SerializationData* GetMessage(); |
| + std::unique_ptr<SerializationData> GetMessage(); |
| // Terminate the worker's event loop. Messages from the worker that have been |
| // queued can still be read via GetMessage(). |
| // This function can be called by any thread. |
| @@ -335,16 +321,10 @@ class Shell : public i::AllStatic { |
| static void CollectGarbage(Isolate* isolate); |
| static void EmptyMessageQueues(Isolate* isolate); |
| - // TODO(binji): stupid implementation for now. Is there an easy way to hash an |
| - // object for use in base::HashMap? By pointer? |
| - typedef i::List<Local<Object>> ObjectList; |
| - static bool SerializeValue(Isolate* isolate, Local<Value> value, |
| - const ObjectList& to_transfer, |
| - ObjectList* seen_objects, |
| - SerializationData* out_data); |
| - static MaybeLocal<Value> DeserializeValue(Isolate* isolate, |
| - const SerializationData& data, |
| - int* offset); |
| + static std::unique_ptr<SerializationData> SerializeValue( |
| + Isolate* isolate, Local<Value> value, Local<Value> transfer); |
| + static MaybeLocal<Value> DeserializeValue( |
| + Isolate* isolate, std::unique_ptr<SerializationData> data); |
| static void CleanupWorkers(); |
| static int* LookupCounter(const char* name); |
| static void* CreateHistogram(const char* name, |
| @@ -443,10 +423,27 @@ class Shell : public i::AllStatic { |
| static base::LazyMutex context_mutex_; |
| static const base::TimeTicks kInitialTicks; |
| + struct SharedArrayBufferContentsHash { |
| + size_t operator()(const v8::SharedArrayBuffer::Contents& contents) const { |
| + return std::hash<void*>()(contents.Data()) ^ |
| + std::hash<size_t>()(contents.ByteLength()); |
|
Camillo Bruni
2017/01/26 20:40:14
nit: use base::hash_combine from base/functional.h
binji
2017/01/27 19:33:21
Done.
|
| + } |
| + }; |
| + |
| + struct SharedArrayBufferContentsIsEqual { |
| + bool operator()(const SharedArrayBuffer::Contents& a, |
| + const SharedArrayBuffer::Contents& b) const { |
| + return a.Data() == b.Data() && a.ByteLength() == b.ByteLength(); |
| + } |
| + }; |
| + |
| static base::LazyMutex workers_mutex_; |
| static bool allow_new_workers_; |
| static i::List<Worker*> workers_; |
| - static i::List<SharedArrayBuffer::Contents> externalized_shared_contents_; |
| + static std::unordered_set<SharedArrayBuffer::Contents, |
| + SharedArrayBufferContentsHash, |
| + SharedArrayBufferContentsIsEqual> |
| + externalized_shared_contents_; |
| static void WriteIgnitionDispatchCountersFile(v8::Isolate* isolate); |
| static Counter* GetCounter(const char* name, bool is_histogram); |