V8 API Reference, 7.2.502.16 (for Deno 0.2.4)
v8-platform.h
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 PostIdleTask(std::unique_ptr<IdleTask> task) = 0;
82 
86  virtual bool IdleTasksEnabled() = 0;
87 
91  virtual bool NonNestableTasksEnabled() const { return false; }
92 
93  TaskRunner() = default;
94  virtual ~TaskRunner() = default;
95 
96  private:
97  TaskRunner(const TaskRunner&) = delete;
98  TaskRunner& operator=(const TaskRunner&) = delete;
99 };
100 
105  public:
106  virtual ~ConvertableToTraceFormat() = default;
107 
114  virtual void AppendAsTraceFormat(std::string* out) const = 0;
115 };
116 
123  public:
124  virtual ~TracingController() = default;
125 
133  virtual const uint8_t* GetCategoryGroupEnabled(const char* name) {
134  static uint8_t no = 0;
135  return &no;
136  }
137 
146  virtual uint64_t AddTraceEvent(
147  char phase, const uint8_t* category_enabled_flag, const char* name,
148  const char* scope, uint64_t id, uint64_t bind_id, int32_t num_args,
149  const char** arg_names, const uint8_t* arg_types,
150  const uint64_t* arg_values,
151  std::unique_ptr<ConvertableToTraceFormat>* arg_convertables,
152  unsigned int flags) {
153  return 0;
154  }
155  virtual uint64_t AddTraceEventWithTimestamp(
156  char phase, const uint8_t* category_enabled_flag, const char* name,
157  const char* scope, uint64_t id, uint64_t bind_id, int32_t num_args,
158  const char** arg_names, const uint8_t* arg_types,
159  const uint64_t* arg_values,
160  std::unique_ptr<ConvertableToTraceFormat>* arg_convertables,
161  unsigned int flags, int64_t timestamp) {
162  return 0;
163  }
164 
169  virtual void UpdateTraceEventDuration(const uint8_t* category_enabled_flag,
170  const char* name, uint64_t handle) {}
171 
173  public:
174  virtual ~TraceStateObserver() = default;
175  virtual void OnTraceEnabled() = 0;
176  virtual void OnTraceDisabled() = 0;
177  };
178 
181 
184 };
185 
192  public:
193  virtual ~PageAllocator() = default;
194 
199  virtual size_t AllocatePageSize() = 0;
200 
205  virtual size_t CommitPageSize() = 0;
206 
211  virtual void SetRandomMmapSeed(int64_t seed) = 0;
212 
217  virtual void* GetRandomMmapAddr() = 0;
218 
222  enum Permission {
223  kNoAccess,
224  kRead,
225  kReadWrite,
226  // TODO(hpayer): Remove this flag. Memory should never be rwx.
227  kReadWriteExecute,
228  kReadExecute
229  };
230 
234  virtual void* AllocatePages(void* address, size_t length, size_t alignment,
235  Permission permissions) = 0;
236 
240  virtual bool FreePages(void* address, size_t length) = 0;
241 
245  virtual bool ReleasePages(void* address, size_t length,
246  size_t new_length) = 0;
247 
251  virtual bool SetPermissions(void* address, size_t length,
252  Permission permissions) = 0;
253 
259  virtual bool DiscardSystemPages(void* address, size_t size) { return true; }
260 };
261 
268 class Platform {
269  public:
270  virtual ~Platform() = default;
271 
276  // TODO(bbudge) Make this abstract after all embedders implement this.
277  return nullptr;
278  }
279 
287  virtual void OnCriticalMemoryPressure() {
288  // TODO(bbudge) Remove this when embedders override the following method.
289  // See crbug.com/634547.
290  }
291 
301  virtual bool OnCriticalMemoryPressure(size_t length) { return false; }
302 
310  virtual int NumberOfWorkerThreads() = 0;
311 
316  virtual std::shared_ptr<v8::TaskRunner> GetForegroundTaskRunner(
317  Isolate* isolate) = 0;
318 
322  virtual void CallOnWorkerThread(std::unique_ptr<Task> task) = 0;
323 
328  virtual void CallBlockingTaskOnWorkerThread(std::unique_ptr<Task> task) {
329  // Embedders may optionally override this to process these tasks in a high
330  // priority pool.
331  CallOnWorkerThread(std::move(task));
332  }
333 
338  virtual void CallDelayedOnWorkerThread(std::unique_ptr<Task> task,
339  double delay_in_seconds) = 0;
340 
347  "Use a taskrunner acquired by GetForegroundTaskRunner instead.",
348  virtual void CallOnForegroundThread(Isolate* isolate, Task* task)) = 0;
349 
357  "Use a taskrunner acquired by GetForegroundTaskRunner instead.",
358  virtual void CallDelayedOnForegroundThread(Isolate* isolate, Task* task,
359  double delay_in_seconds)) = 0;
360 
370  "Use a taskrunner acquired by GetForegroundTaskRunner instead.",
371  virtual void CallIdleOnForegroundThread(Isolate* isolate,
372  IdleTask* task)) {
373  // This must be overriden if |IdleTasksEnabled()|.
374  abort();
375  }
376 
380  virtual bool IdleTasksEnabled(Isolate* isolate) {
381  return false;
382  }
383 
391  virtual double MonotonicallyIncreasingTime() = 0;
392 
397  virtual double CurrentClockTimeMillis() = 0;
398 
399  typedef void (*StackTracePrinter)();
400 
405  virtual StackTracePrinter GetStackTracePrinter() { return nullptr; }
406 
411 
416  virtual void DumpWithoutCrashing() {}
417 
418  protected:
424  static double SystemClockTimeMillis();
425 };
426 
427 } // namespace v8
428 
429 #endif // V8_V8_PLATFORM_H_
virtual void OnCriticalMemoryPressure()
Definition: v8-platform.h:287
virtual bool NonNestableTasksEnabled() const
Definition: v8-platform.h:91
virtual PageAllocator * GetPageAllocator()
Definition: v8-platform.h:275
virtual void PostTask(std::unique_ptr< Task > task)=0
virtual void PostDelayedTask(std::unique_ptr< Task > task, double delay_in_seconds)=0
virtual void AppendAsTraceFormat(std::string *out) const =0
virtual bool FreePages(void *address, size_t length)=0
static double SystemClockTimeMillis()
Definition: v8.cc:154
virtual void CallBlockingTaskOnWorkerThread(std::unique_ptr< Task > task)
Definition: v8-platform.h:328
V8_DEPRECATE_SOON("Use a taskrunner acquired by GetForegroundTaskRunner instead.", virtual void CallIdleOnForegroundThread(Isolate *isolate, IdleTask *task))
Definition: v8-platform.h:369
V8_DEPRECATE_SOON("Use a taskrunner acquired by GetForegroundTaskRunner instead.", virtual void CallOnForegroundThread(Isolate *isolate, Task *task))=0
virtual void SetRandomMmapSeed(int64_t seed)=0
virtual bool OnCriticalMemoryPressure(size_t length)
Definition: v8-platform.h:301
virtual void UpdateTraceEventDuration(const uint8_t *category_enabled_flag, const char *name, uint64_t handle)
Definition: v8-platform.h:169
virtual bool IdleTasksEnabled()=0
virtual void PostIdleTask(std::unique_ptr< IdleTask > task)=0
virtual void CallOnWorkerThread(std::unique_ptr< Task > task)=0
virtual void * GetRandomMmapAddr()=0
virtual void CallDelayedOnWorkerThread(std::unique_ptr< Task > task, double delay_in_seconds)=0
Definition: libplatform.h:13
virtual bool ReleasePages(void *address, size_t length, size_t new_length)=0
virtual const uint8_t * GetCategoryGroupEnabled(const char *name)
Definition: v8-platform.h:133
virtual void * AllocatePages(void *address, size_t length, size_t alignment, Permission permissions)=0
virtual int NumberOfWorkerThreads()=0
virtual void AddTraceStateObserver(TraceStateObserver *)
Definition: v8-platform.h:180
virtual void PostNonNestableTask(std::unique_ptr< Task > task)
Definition: v8-platform.h:63
virtual StackTracePrinter GetStackTracePrinter()
Definition: v8-platform.h:405
virtual bool IdleTasksEnabled(Isolate *isolate)
Definition: v8-platform.h:380
virtual TracingController * GetTracingController()=0
virtual void DumpWithoutCrashing()
Definition: v8-platform.h:416
virtual size_t AllocatePageSize()=0
virtual double CurrentClockTimeMillis()=0
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:146
virtual bool DiscardSystemPages(void *address, size_t size)
Definition: v8-platform.h:259
virtual std::shared_ptr< v8::TaskRunner > GetForegroundTaskRunner(Isolate *isolate)=0
virtual double MonotonicallyIncreasingTime()=0
virtual size_t CommitPageSize()=0
virtual void RemoveTraceStateObserver(TraceStateObserver *)
Definition: v8-platform.h:183
virtual bool SetPermissions(void *address, size_t length, Permission permissions)=0