v8  7.4.288(node12.0.0)
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 
63  virtual void PostNonNestableTask(std::unique_ptr<Task> task) {}
64 
70  virtual void PostDelayedTask(std::unique_ptr<Task> task,
71  double delay_in_seconds) = 0;
72 
81  virtual void PostNonNestableDelayedTask(std::unique_ptr<Task> task,
82  double delay_in_seconds) {}
83 
92  virtual void PostIdleTask(std::unique_ptr<IdleTask> task) = 0;
93 
97  virtual bool IdleTasksEnabled() = 0;
98 
102  virtual bool NonNestableTasksEnabled() const { return false; }
103 
107  virtual bool NonNestableDelayedTasksEnabled() const { return false; }
108 
109  TaskRunner() = default;
110  virtual ~TaskRunner() = default;
111 
112  private:
113  TaskRunner(const TaskRunner&) = delete;
114  TaskRunner& operator=(const TaskRunner&) = delete;
115 };
116 
121  public:
122  virtual ~ConvertableToTraceFormat() = default;
123 
130  virtual void AppendAsTraceFormat(std::string* out) const = 0;
131 };
132 
139  public:
140  virtual ~TracingController() = default;
141 
149  virtual const uint8_t* GetCategoryGroupEnabled(const char* name) {
150  static uint8_t no = 0;
151  return &no;
152  }
153 
162  virtual uint64_t AddTraceEvent(
163  char phase, const uint8_t* category_enabled_flag, const char* name,
164  const char* scope, uint64_t id, uint64_t bind_id, int32_t num_args,
165  const char** arg_names, const uint8_t* arg_types,
166  const uint64_t* arg_values,
167  std::unique_ptr<ConvertableToTraceFormat>* arg_convertables,
168  unsigned int flags) {
169  return 0;
170  }
171  virtual uint64_t AddTraceEventWithTimestamp(
172  char phase, const uint8_t* category_enabled_flag, const char* name,
173  const char* scope, uint64_t id, uint64_t bind_id, int32_t num_args,
174  const char** arg_names, const uint8_t* arg_types,
175  const uint64_t* arg_values,
176  std::unique_ptr<ConvertableToTraceFormat>* arg_convertables,
177  unsigned int flags, int64_t timestamp) {
178  return 0;
179  }
180 
185  virtual void UpdateTraceEventDuration(const uint8_t* category_enabled_flag,
186  const char* name, uint64_t handle) {}
187 
189  public:
190  virtual ~TraceStateObserver() = default;
191  virtual void OnTraceEnabled() = 0;
192  virtual void OnTraceDisabled() = 0;
193  };
194 
197 
200 };
201 
208  public:
209  virtual ~PageAllocator() = default;
210 
215  virtual size_t AllocatePageSize() = 0;
216 
221  virtual size_t CommitPageSize() = 0;
222 
227  virtual void SetRandomMmapSeed(int64_t seed) = 0;
228 
233  virtual void* GetRandomMmapAddr() = 0;
234 
238  enum Permission {
242  // TODO(hpayer): Remove this flag. Memory should never be rwx.
245  };
246 
250  virtual void* AllocatePages(void* address, size_t length, size_t alignment,
251  Permission permissions) = 0;
252 
256  virtual bool FreePages(void* address, size_t length) = 0;
257 
261  virtual bool ReleasePages(void* address, size_t length,
262  size_t new_length) = 0;
263 
267  virtual bool SetPermissions(void* address, size_t length,
268  Permission permissions) = 0;
269 
275  virtual bool DiscardSystemPages(void* address, size_t size) { return true; }
276 };
277 
284 class Platform {
285  public:
286  virtual ~Platform() = default;
287 
292  // TODO(bbudge) Make this abstract after all embedders implement this.
293  return nullptr;
294  }
295 
303  virtual void OnCriticalMemoryPressure() {
304  // TODO(bbudge) Remove this when embedders override the following method.
305  // See crbug.com/634547.
306  }
307 
317  virtual bool OnCriticalMemoryPressure(size_t length) { return false; }
318 
326  virtual int NumberOfWorkerThreads() = 0;
327 
332  virtual std::shared_ptr<v8::TaskRunner> GetForegroundTaskRunner(
333  Isolate* isolate) = 0;
334 
338  virtual void CallOnWorkerThread(std::unique_ptr<Task> task) = 0;
339 
344  virtual void CallBlockingTaskOnWorkerThread(std::unique_ptr<Task> task) {
345  // Embedders may optionally override this to process these tasks in a high
346  // priority pool.
347  CallOnWorkerThread(std::move(task));
348  }
349 
353  virtual void CallLowPriorityTaskOnWorkerThread(std::unique_ptr<Task> task) {
354  // Embedders may optionally override this to process these tasks in a low
355  // priority pool.
356  CallOnWorkerThread(std::move(task));
357  }
358 
363  virtual void CallDelayedOnWorkerThread(std::unique_ptr<Task> task,
364  double delay_in_seconds) = 0;
365 
372  "Use a taskrunner acquired by GetForegroundTaskRunner instead.",
373  virtual void CallOnForegroundThread(Isolate* isolate, Task* task)) = 0;
374 
382  "Use a taskrunner acquired by GetForegroundTaskRunner instead.",
383  virtual void CallDelayedOnForegroundThread(Isolate* isolate, Task* task,
384  double delay_in_seconds)) = 0;
385 
395  "Use a taskrunner acquired by GetForegroundTaskRunner instead.",
396  virtual void CallIdleOnForegroundThread(Isolate* isolate,
397  IdleTask* task)) {
398  // This must be overriden if |IdleTasksEnabled()|.
399  abort();
400  }
401 
405  virtual bool IdleTasksEnabled(Isolate* isolate) {
406  return false;
407  }
408 
416  virtual double MonotonicallyIncreasingTime() = 0;
417 
422  virtual double CurrentClockTimeMillis() = 0;
423 
424  typedef void (*StackTracePrinter)();
425 
430  virtual StackTracePrinter GetStackTracePrinter() { return nullptr; }
431 
436 
441  virtual void DumpWithoutCrashing() {}
442 
443  protected:
449  V8_EXPORT static double SystemClockTimeMillis();
450 };
451 
452 } // namespace v8
453 
454 #endif // V8_V8_PLATFORM_H_
virtual void RemoveTraceStateObserver(TraceStateObserver *)
Definition: v8-platform.h:199
virtual bool DiscardSystemPages(void *address, size_t size)
Definition: v8-platform.h:275
virtual size_t AllocatePageSize()=0
#define V8_EXPORT
Definition: v8config.h:377
virtual ~Platform()=default
virtual void PostNonNestableDelayedTask(std::unique_ptr< Task > task, double delay_in_seconds)
Definition: v8-platform.h:81
static V8_EXPORT double SystemClockTimeMillis()
virtual StackTracePrinter GetStackTracePrinter()
Definition: v8-platform.h:430
virtual void Run()=0
virtual bool NonNestableDelayedTasksEnabled() const
Definition: v8-platform.h:107
virtual void PostIdleTask(std::unique_ptr< IdleTask > task)=0
virtual void CallBlockingTaskOnWorkerThread(std::unique_ptr< Task > task)
Definition: v8-platform.h:344
virtual void CallLowPriorityTaskOnWorkerThread(std::unique_ptr< Task > task)
Definition: v8-platform.h:353
virtual ~TaskRunner()=default
V8_DEPRECATE_SOON("Use a taskrunner acquired by GetForegroundTaskRunner instead.", virtual void CallIdleOnForegroundThread(Isolate *isolate, IdleTask *task))
Definition: v8-platform.h:394
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:149
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:162
virtual void AddTraceStateObserver(TraceStateObserver *)
Definition: v8-platform.h:196
virtual void AppendAsTraceFormat(std::string *out) const =0
virtual std::shared_ptr< v8::TaskRunner > GetForegroundTaskRunner(Isolate *isolate)=0
void(* StackTracePrinter)()
Definition: v8-platform.h:424
Definition: libplatform.h:13
virtual bool NonNestableTasksEnabled() const
Definition: v8-platform.h:102
virtual ~TracingController()=default
virtual ~Task()=default
virtual ~ConvertableToTraceFormat()=default
virtual PageAllocator * GetPageAllocator()
Definition: v8-platform.h:291
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:405
virtual bool SetPermissions(void *address, size_t length, Permission permissions)=0
virtual bool FreePages(void *address, size_t length)=0
virtual double MonotonicallyIncreasingTime()=0
virtual void DumpWithoutCrashing()
Definition: v8-platform.h:441
virtual void * GetRandomMmapAddr()=0
virtual ~IdleTask()=default
virtual void OnCriticalMemoryPressure()
Definition: v8-platform.h:303
virtual void UpdateTraceEventDuration(const uint8_t *category_enabled_flag, const char *name, uint64_t handle)
Definition: v8-platform.h:185
TaskRunner()=default
virtual void SetRandomMmapSeed(int64_t seed)=0
virtual void Run(double deadline_in_seconds)=0
V8_DEPRECATE_SOON("Use a taskrunner acquired by GetForegroundTaskRunner instead.", virtual void CallOnForegroundThread(Isolate *isolate, Task *task))=0
virtual void PostNonNestableTask(std::unique_ptr< Task > task)
Definition: v8-platform.h:63
virtual TracingController * GetTracingController()=0
virtual bool OnCriticalMemoryPressure(size_t length)
Definition: v8-platform.h:317
virtual void CallOnWorkerThread(std::unique_ptr< Task > task)=0
virtual int NumberOfWorkerThreads()=0
virtual ~PageAllocator()=default
virtual void PostDelayedTask(std::unique_ptr< 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:171