v8  6.8.275(node10.15.3)
V8 is Google's open source JavaScript engine
v8-platform.h
Go to the documentation of this file.
1 // Copyright 2013 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef V8_V8_PLATFORM_H_
6 #define V8_V8_PLATFORM_H_
7 
8 #include <stddef.h>
9 #include <stdint.h>
10 #include <stdlib.h> // For abort.
11 #include <memory>
12 #include <string>
13 
14 #include "v8config.h" // NOLINT(build/include)
15 
16 namespace v8 {
17 
18 class Isolate;
19 
23 class Task {
24  public:
25  virtual ~Task() = default;
26 
27  virtual void Run() = 0;
28 };
29 
36 class IdleTask {
37  public:
38  virtual ~IdleTask() = default;
39  virtual void Run(double deadline_in_seconds) = 0;
40 };
41 
48 class TaskRunner {
49  public:
54  virtual void PostTask(std::unique_ptr<Task> task) = 0;
55 
61  virtual void PostDelayedTask(std::unique_ptr<Task> task,
62  double delay_in_seconds) = 0;
63 
72  virtual void PostIdleTask(std::unique_ptr<IdleTask> task) = 0;
73 
77  virtual bool IdleTasksEnabled() = 0;
78 
79  TaskRunner() = default;
80  virtual ~TaskRunner() = default;
81 
82  private:
83  TaskRunner(const TaskRunner&) = delete;
84  TaskRunner& operator=(const TaskRunner&) = delete;
85 };
86 
91  public:
92  virtual ~ConvertableToTraceFormat() = default;
93 
100  virtual void AppendAsTraceFormat(std::string* out) const = 0;
101 };
102 
109  public:
110  virtual ~TracingController() = default;
111 
119  virtual const uint8_t* GetCategoryGroupEnabled(const char* name) {
120  static uint8_t no = 0;
121  return &no;
122  }
123 
132  virtual uint64_t AddTraceEvent(
133  char phase, const uint8_t* category_enabled_flag, const char* name,
134  const char* scope, uint64_t id, uint64_t bind_id, int32_t num_args,
135  const char** arg_names, const uint8_t* arg_types,
136  const uint64_t* arg_values,
137  std::unique_ptr<ConvertableToTraceFormat>* arg_convertables,
138  unsigned int flags) {
139  return 0;
140  }
141  virtual uint64_t AddTraceEventWithTimestamp(
142  char phase, const uint8_t* category_enabled_flag, const char* name,
143  const char* scope, uint64_t id, uint64_t bind_id, int32_t num_args,
144  const char** arg_names, const uint8_t* arg_types,
145  const uint64_t* arg_values,
146  std::unique_ptr<ConvertableToTraceFormat>* arg_convertables,
147  unsigned int flags, int64_t timestamp) {
148  return 0;
149  }
150 
155  virtual void UpdateTraceEventDuration(const uint8_t* category_enabled_flag,
156  const char* name, uint64_t handle) {}
157 
159  public:
160  virtual ~TraceStateObserver() = default;
161  virtual void OnTraceEnabled() = 0;
162  virtual void OnTraceDisabled() = 0;
163  };
164 
167 
170 };
171 
178  public:
179  virtual ~PageAllocator() = default;
180 
185  virtual size_t AllocatePageSize() = 0;
186 
191  virtual size_t CommitPageSize() = 0;
192 
197  virtual void SetRandomMmapSeed(int64_t seed) = 0;
198 
203  virtual void* GetRandomMmapAddr() = 0;
204 
208  enum Permission {
212  // TODO(hpayer): Remove this flag. Memory should never be rwx.
215  };
216 
220  virtual void* AllocatePages(void* address, size_t length, size_t alignment,
221  Permission permissions) = 0;
222 
226  virtual bool FreePages(void* address, size_t length) = 0;
227 
231  virtual bool ReleasePages(void* address, size_t length,
232  size_t new_length) = 0;
233 
237  virtual bool SetPermissions(void* address, size_t length,
238  Permission permissions) = 0;
239 };
240 
247 class Platform {
248  public:
249  virtual ~Platform() = default;
250 
255  // TODO(bbudge) Make this abstract after all embedders implement this.
256  return nullptr;
257  }
258 
266  virtual void OnCriticalMemoryPressure() {
267  // TODO(bbudge) Remove this when embedders override the following method.
268  // See crbug.com/634547.
269  }
270 
280  virtual bool OnCriticalMemoryPressure(size_t length) { return false; }
281 
289  virtual int NumberOfWorkerThreads() = 0;
290 
295  virtual std::shared_ptr<v8::TaskRunner> GetForegroundTaskRunner(
296  Isolate* isolate) = 0;
297 
301  virtual void CallOnWorkerThread(std::unique_ptr<Task> task) = 0;
302 
307  virtual void CallBlockingTaskOnWorkerThread(std::unique_ptr<Task> task) {
308  // Embedders may optionally override this to process these tasks in a high
309  // priority pool.
310  CallOnWorkerThread(std::move(task));
311  }
312 
317  virtual void CallDelayedOnWorkerThread(std::unique_ptr<Task> task,
318  double delay_in_seconds) = 0;
319 
325  virtual void CallOnForegroundThread(Isolate* isolate, Task* task) = 0;
326 
333  virtual void CallDelayedOnForegroundThread(Isolate* isolate, Task* task,
334  double delay_in_seconds) = 0;
335 
344  virtual void CallIdleOnForegroundThread(Isolate* isolate, IdleTask* task) {
345  // This must be overriden if |IdleTasksEnabled()|.
346  abort();
347  }
348 
352  virtual bool IdleTasksEnabled(Isolate* isolate) {
353  return false;
354  }
355 
363  virtual double MonotonicallyIncreasingTime() = 0;
364 
369  virtual double CurrentClockTimeMillis() = 0;
370 
371  typedef void (*StackTracePrinter)();
372 
377  virtual StackTracePrinter GetStackTracePrinter() { return nullptr; }
378 
383 
384  protected:
390  static double SystemClockTimeMillis();
391 };
392 
393 } // namespace v8
394 
395 #endif // V8_V8_PLATFORM_H_
virtual void RemoveTraceStateObserver(TraceStateObserver *)
Definition: v8-platform.h:169
virtual size_t AllocatePageSize()=0
virtual ~Platform()=default
virtual StackTracePrinter GetStackTracePrinter()
Definition: v8-platform.h:377
virtual void Run()=0
virtual void PostIdleTask(std::unique_ptr< IdleTask > task)=0
virtual void CallBlockingTaskOnWorkerThread(std::unique_ptr< Task > task)
Definition: v8-platform.h:307
virtual ~TaskRunner()=default
virtual bool IdleTasksEnabled()=0
virtual void * AllocatePages(void *address, size_t length, size_t alignment, Permission permissions)=0
virtual double CurrentClockTimeMillis()=0
virtual size_t CommitPageSize()=0
virtual const uint8_t * GetCategoryGroupEnabled(const char *name)
Definition: v8-platform.h:119
virtual uint64_t AddTraceEvent(char phase, const uint8_t *category_enabled_flag, const char *name, const char *scope, uint64_t id, uint64_t bind_id, int32_t num_args, const char **arg_names, const uint8_t *arg_types, const uint64_t *arg_values, std::unique_ptr< ConvertableToTraceFormat > *arg_convertables, unsigned int flags)
Definition: v8-platform.h:132
virtual void AddTraceStateObserver(TraceStateObserver *)
Definition: v8-platform.h:166
virtual void AppendAsTraceFormat(std::string *out) const =0
virtual std::shared_ptr< v8::TaskRunner > GetForegroundTaskRunner(Isolate *isolate)=0
void(* StackTracePrinter)()
Definition: v8-platform.h:371
Definition: libplatform.h:13
virtual ~TracingController()=default
virtual ~Task()=default
virtual ~ConvertableToTraceFormat()=default
virtual PageAllocator * GetPageAllocator()
Definition: v8-platform.h:254
virtual void CallDelayedOnWorkerThread(std::unique_ptr< Task > task, double delay_in_seconds)=0
virtual void PostTask(std::unique_ptr< Task > task)=0
virtual bool IdleTasksEnabled(Isolate *isolate)
Definition: v8-platform.h:352
virtual bool SetPermissions(void *address, size_t length, Permission permissions)=0
virtual bool FreePages(void *address, size_t length)=0
virtual double MonotonicallyIncreasingTime()=0
static double SystemClockTimeMillis()
virtual void * GetRandomMmapAddr()=0
virtual ~IdleTask()=default
virtual void OnCriticalMemoryPressure()
Definition: v8-platform.h:266
virtual void UpdateTraceEventDuration(const uint8_t *category_enabled_flag, const char *name, uint64_t handle)
Definition: v8-platform.h:155
TaskRunner()=default
virtual void SetRandomMmapSeed(int64_t seed)=0
virtual void CallIdleOnForegroundThread(Isolate *isolate, IdleTask *task)
Definition: v8-platform.h:344
virtual void Run(double deadline_in_seconds)=0
virtual TracingController * GetTracingController()=0
virtual bool OnCriticalMemoryPressure(size_t length)
Definition: v8-platform.h:280
virtual void CallOnWorkerThread(std::unique_ptr< Task > task)=0
virtual int NumberOfWorkerThreads()=0
virtual void CallOnForegroundThread(Isolate *isolate, Task *task)=0
virtual ~PageAllocator()=default
virtual void PostDelayedTask(std::unique_ptr< Task > task, double delay_in_seconds)=0
virtual void CallDelayedOnForegroundThread(Isolate *isolate, Task *task, double delay_in_seconds)=0
virtual bool ReleasePages(void *address, size_t length, size_t new_length)=0
virtual uint64_t AddTraceEventWithTimestamp(char phase, const uint8_t *category_enabled_flag, const char *name, const char *scope, uint64_t id, uint64_t bind_id, int32_t num_args, const char **arg_names, const uint8_t *arg_types, const uint64_t *arg_values, std::unique_ptr< ConvertableToTraceFormat > *arg_convertables, unsigned int flags, int64_t timestamp)
Definition: v8-platform.h:141