ScummVM API documentation
as_gc.h
1 /*
2  AngelCode Scripting Library
3  Copyright (c) 2003-2018 Andreas Jonsson
4 
5  This software is provided 'as-is', without any express or implied
6  warranty. In no event will the authors be held liable for any
7  damages arising from the use of this software.
8 
9  Permission is granted to anyone to use this software for any
10  purpose, including commercial applications, and to alter it and
11  redistribute it freely, subject to the following restrictions:
12 
13  1. The origin of this software must not be misrepresented; you
14  must not claim that you wrote the original software. If you use
15  this software in a product, an acknowledgment in the product
16  documentation would be appreciated but is not required.
17 
18  2. Altered source versions must be plainly marked as such, and
19  must not be misrepresented as being the original software.
20 
21  3. This notice may not be removed or altered from any source
22  distribution.
23 
24  The original version of this library can be located at:
25  http://www.angelcode.com/angelscript/
26 
27  Andreas Jonsson
28  andreas@angelcode.com
29 */
30 
31 
32 //
33 // as_gc.h
34 //
35 // The garbage collector is used to resolve cyclic references
36 //
37 
38 
39 
40 #ifndef AS_GC_H
41 #define AS_GC_H
42 
43 #include "as_config.h"
44 #include "as_memory.h"
45 #include "as_map.h"
46 #include "as_thread.h"
47 
48 BEGIN_AS_NAMESPACE
49 
50 class asCScriptEngine;
51 class asCObjectType;
52 
54 public:
57 
58  int GarbageCollect(asDWORD flags, asUINT iterations);
59  void GetStatistics(asUINT *currentSize, asUINT *totalDestroyed, asUINT *totalDetected, asUINT *newObjects, asUINT *totalNewDestroyed) const;
60  void GCEnumCallback(void *reference);
61  int AddScriptObjectToGC(void *obj, asCObjectType *objType);
62  int GetObjectInGC(asUINT idx, asUINT *seqNbr, void **obj, asITypeInfo **type);
63 
64  int ReportAndReleaseUndestroyedObjects();
65 
66  asCScriptEngine *engine;
67 
68  // Callback for when circular reference are detected
69  asCIRCULARREFFUNC_t circularRefDetectCallbackFunc;
70  void *circularRefDetectCallbackParam;
71 
72 protected:
73  struct asSObjTypePair {
74  void *obj;
75  asCObjectType *type;
76  asUINT seqNbr;
77  };
78  struct asSIntTypePair {
79  int i;
80  asCObjectType *type;
81  };
83 
84  enum egcDestroyState {
85  destroyGarbage_init = 0,
86  destroyGarbage_loop,
87  destroyGarbage_haveMore
88  };
89 
90  enum egcDetectState {
91  clearCounters_init = 0,
92  clearCounters_loop,
93  buildMap_init,
94  buildMap_loop,
95  countReferences_init,
96  countReferences_loop,
97  detectGarbage_init,
98  detectGarbage_loop1,
99  detectGarbage_loop2,
100  verifyUnmarked_init,
101  verifyUnmarked_loop,
102  breakCircles_init,
103  breakCircles_loop,
104  breakCircles_haveGarbage
105  };
106 
107  int DestroyNewGarbage();
108  int DestroyOldGarbage();
109  int IdentifyGarbageWithCyclicRefs();
110  asSObjTypePair GetNewObjectAtIdx(int idx);
111  asSObjTypePair GetOldObjectAtIdx(int idx);
112  void RemoveNewObjectAtIdx(int idx);
113  void RemoveOldObjectAtIdx(int idx);
114  void MoveObjectToOldList(int idx);
115  void MoveAllObjectsToOldList();
116 
117  // Holds all the objects known by the garbage collector
118  asCArray<asSObjTypePair> gcNewObjects;
119  asCArray<asSObjTypePair> gcOldObjects;
120 
121  // This array temporarily holds references to objects known to be live objects
122  asCArray<void *> liveObjects;
123 
124  // This map holds objects currently being searched for cyclic references, it also holds a
125  // counter that gives the number of references to the object that the GC can't reach
127 
128  // State variables
129  egcDestroyState destroyNewState;
130  egcDestroyState destroyOldState;
131  asUINT destroyNewIdx;
132  asUINT destroyOldIdx;
133  asUINT numDestroyed;
134  asUINT numNewDestroyed;
135  egcDetectState detectState;
136  asUINT detectIdx;
137  asUINT numDetected;
138  asUINT numAdded;
139  asUINT seqAtSweepStart[3];
140  asSMapNode_t *gcMapCursor;
141  bool isProcessing;
142 
143  // We'll keep a pool of nodes to avoid allocating memory all the time
144  asSMapNode_t *GetNode(void *obj, asSIntTypePair it);
145  void ReturnNode(asSMapNode_t *node);
146  asCArray<asSMapNode_t *> freeNodes;
147 
148  // Critical section for multithreaded access
149  DECLARECRITICALSECTION(gcCritical) // Used for adding/removing objects
150  DECLARECRITICALSECTION(gcCollecting) // Used for processing
151 };
152 
153 END_AS_NAMESPACE
154 
155 #endif
Definition: as_objecttype.h:100
Definition: as_map.h:44
Definition: as_array.h:47
Definition: as_map.h:42
Definition: as_gc.h:53
Definition: as_scriptengine.h:64
Definition: angelscript.h:1011