Pyrogenesis  trunk
ScriptRuntime.h
Go to the documentation of this file.
1 /* Copyright (C) 2016 Wildfire Games.
2  * This file is part of 0 A.D.
3  *
4  * 0 A.D. is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * 0 A.D. is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #ifndef INCLUDED_SCRIPTRUNTIME
19 #define INCLUDED_SCRIPTRUNTIME
20 
21 #include <sstream>
22 
23 #include "ScriptTypes.h"
24 #include "ScriptExtraHeaders.h"
25 
26 #define STACK_CHUNK_SIZE 8192
27 
28 /**
29  * Abstraction around a SpiderMonkey JSRuntime.
30  * Each ScriptRuntime can be used to initialize several ScriptInterface
31  * contexts which can then share data, but a single ScriptRuntime should
32  * only be used on a single thread.
33  *
34  * (One means to share data between threads and runtimes is to create
35  * a ScriptInterface::StructuredClone.)
36  */
37 
39 {
40 public:
41  ScriptRuntime(shared_ptr<ScriptRuntime> parentRuntime, int runtimeSize, int heapGrowthBytesGCTrigger);
43 
44  /**
45  * MaybeIncrementalRuntimeGC tries to determine whether a runtime-wide garbage collection would free up enough memory to
46  * be worth the amount of time it would take. It does this with our own logic and NOT some predefined JSAPI logic because
47  * such functionality currently isn't available out of the box.
48  * It does incremental GC which means it will collect one slice each time it's called until the garbage collection is done.
49  * This can and should be called quite regularly. The delay parameter allows you to specify a minimum time since the last GC
50  * in seconds (the delay should be a fraction of a second in most cases though).
51  * It will only start a new incremental GC or another GC slice if this time is exceeded. The user of this function is
52  * responsible for ensuring that GC can run with a small enough delay to get done with the work.
53  */
54  void MaybeIncrementalGC(double delay);
55  void ShrinkingGC();
56 
57  void RegisterContext(JSContext* cx);
58  void UnRegisterContext(JSContext* cx);
59 
60  /**
61  * Registers an object to be freed/finalized by the ScriptRuntime. Freeing is
62  * guaranteed to happen after the next minor GC has completed, but might also
63  * happen a bit later. This is only needed in very special situations
64  * and you should only use it if you know exactly why you need it!
65  * Specify a deleter for the shared_ptr to free the void pointer correctly
66  * (by casting to the right type before calling delete for example).
67  */
68  void AddDeferredFinalizationObject(const std::shared_ptr<void>& obj);
69 
70  JSRuntime* m_rt;
71 
72 private:
73 
75  void GCCallbackMember();
76 
77  std::list<JSContext*> m_Contexts;
78  std::vector<std::shared_ptr<void> > m_FinalizationListObjectIdCache;
79 
83  double m_LastGCCheck;
84 
85  static void GCCallback(JSRuntime *rt, JSGCStatus status, void *data);
86 };
87 
88 #endif // INCLUDED_SCRIPTRUNTIME
void UnRegisterContext(JSContext *cx)
Definition: ScriptRuntime.cpp:149
void GCCallbackMember()
Definition: ScriptRuntime.cpp:98
ScriptRuntime(shared_ptr< ScriptRuntime > parentRuntime, int runtimeSize, int heapGrowthBytesGCTrigger)
Definition: ScriptRuntime.cpp:108
std::list< JSContext * > m_Contexts
Definition: ScriptRuntime.h:77
void RegisterContext(JSContext *cx)
Definition: ScriptRuntime.cpp:144
void PrepareContextsForIncrementalGC()
Definition: ScriptRuntime.cpp:265
double m_LastGCCheck
Definition: ScriptRuntime.h:83
void ShrinkingGC()
Definition: ScriptRuntime.cpp:257
int m_RuntimeSize
Definition: ScriptRuntime.h:80
void AddDeferredFinalizationObject(const std::shared_ptr< void > &obj)
Registers an object to be freed/finalized by the ScriptRuntime.
Definition: ScriptRuntime.cpp:103
void MaybeIncrementalGC(double delay)
MaybeIncrementalRuntimeGC tries to determine whether a runtime-wide garbage collection would free up ...
Definition: ScriptRuntime.cpp:155
static void GCCallback(JSRuntime *rt, JSGCStatus status, void *data)
Definition: ScriptRuntime.cpp:92
Abstraction around a SpiderMonkey JSRuntime.
Definition: ScriptRuntime.h:38
~ScriptRuntime()
Definition: ScriptRuntime.cpp:134
std::vector< std::shared_ptr< void > > m_FinalizationListObjectIdCache
Definition: ScriptRuntime.h:78
JSRuntime * m_rt
Definition: ScriptRuntime.h:70
int m_HeapGrowthBytesGCTrigger
Definition: ScriptRuntime.h:81
int m_LastGCBytes
Definition: ScriptRuntime.h:82