1 | /* |
---|
2 | orxonox - the future of 3D-vertical-scrollers |
---|
3 | |
---|
4 | Copyright (C) 2004 orx |
---|
5 | |
---|
6 | This program is free software; you can redistribute it and/or modify |
---|
7 | it under the terms of the GNU General Public License as published by |
---|
8 | the Free Software Foundation; either version 2, or (at your option) |
---|
9 | any later version. |
---|
10 | |
---|
11 | ### File Specific: |
---|
12 | main-programmer: Patrick Boenzli |
---|
13 | co-programmer: ... |
---|
14 | */ |
---|
15 | |
---|
16 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_GARBAGE_COLLECTOR |
---|
17 | |
---|
18 | #include "garbage_collector.h" |
---|
19 | |
---|
20 | #include "state.h" |
---|
21 | #include "world_entity.h" |
---|
22 | #include "null_parent.h" |
---|
23 | |
---|
24 | #include "list.h" |
---|
25 | |
---|
26 | using namespace std; |
---|
27 | |
---|
28 | GarbageCollector* GarbageCollector::singletonRef = 0; |
---|
29 | |
---|
30 | /** |
---|
31 | * standard constructor |
---|
32 | */ |
---|
33 | GarbageCollector::GarbageCollector () |
---|
34 | { |
---|
35 | this->setClassID(CL_GARBAGE_COLLECTOR, "GarbageCollector"); |
---|
36 | this->setName("GarbageCollector"); |
---|
37 | |
---|
38 | this->collectedObjects = NULL; |
---|
39 | this->unusedContainers = NULL; |
---|
40 | |
---|
41 | this->time = 0; |
---|
42 | this->delay = 5.0f; /* clean up all 5.0 seconds */ |
---|
43 | } |
---|
44 | |
---|
45 | |
---|
46 | /** |
---|
47 | * standard deconstructor |
---|
48 | */ |
---|
49 | GarbageCollector::~GarbageCollector () |
---|
50 | { |
---|
51 | // delete what has to be deleted here |
---|
52 | } |
---|
53 | |
---|
54 | /** |
---|
55 | * this sets the collection delay |
---|
56 | * @param delay: the delay |
---|
57 | |
---|
58 | after this delay, the garbage collector starts its work and begins to collect unused object |
---|
59 | to delete them afterwards. only objects in the worldentity list from the world object are lookded |
---|
60 | at. |
---|
61 | */ |
---|
62 | void GarbageCollector::setCollectionDelay(float delay) |
---|
63 | { |
---|
64 | this->delay = delay; |
---|
65 | } |
---|
66 | |
---|
67 | |
---|
68 | /** |
---|
69 | * this foreces a garbage collection |
---|
70 | |
---|
71 | if this function is called, the gc tries to initiate the garbage collection routines. actually |
---|
72 | this should always work. |
---|
73 | */ |
---|
74 | void GarbageCollector::forceCollection() |
---|
75 | { |
---|
76 | /* just make the time slitely bigger than the delay */ |
---|
77 | this->time = this->delay + 1; |
---|
78 | /* and update, to get rid of the unused objects */ |
---|
79 | this->update(); |
---|
80 | } |
---|
81 | |
---|
82 | |
---|
83 | /** |
---|
84 | * collect an Object, that should be scheduled for clearing. |
---|
85 | * @param object the Object to schedule. |
---|
86 | */ |
---|
87 | void GarbageCollector::collect(BaseObject* object) |
---|
88 | { |
---|
89 | State::getWorldEntityList()->remove(dynamic_cast<WorldEntity*>(object)); |
---|
90 | FastObjectMember* tmpC; |
---|
91 | if (unlikely(this->unusedContainers == NULL)) |
---|
92 | { |
---|
93 | tmpC = new FastObjectMember; |
---|
94 | } |
---|
95 | else |
---|
96 | { |
---|
97 | tmpC = this->unusedContainers; |
---|
98 | this->unusedContainers = this->unusedContainers->next; |
---|
99 | } |
---|
100 | |
---|
101 | tmpC->next = this->collectedObjects; |
---|
102 | tmpC->objectPointer = object; |
---|
103 | this->collectedObjects = tmpC; |
---|
104 | } |
---|
105 | |
---|
106 | /** |
---|
107 | * this ticks the GarbageCollector to give it the time pulse |
---|
108 | * @param time: the time passed since last tick |
---|
109 | |
---|
110 | like every other tick function eg. worldentity |
---|
111 | */ |
---|
112 | void GarbageCollector::tick(float time) |
---|
113 | { |
---|
114 | this->time += time; |
---|
115 | } |
---|
116 | |
---|
117 | |
---|
118 | void GarbageCollector::update() |
---|
119 | { |
---|
120 | if (this->time < this->delay || this->collectedObjects == NULL) |
---|
121 | return; |
---|
122 | else |
---|
123 | { |
---|
124 | FastObjectMember* tmpC = this->collectedObjects; |
---|
125 | FastObjectMember* moveC; |
---|
126 | while (tmpC != NULL) |
---|
127 | { |
---|
128 | //WorldEntity* entity = dynamic_cast<WorldEntity*>(tmpC->objectPointer); |
---|
129 | //State::getWorldEntityList()->remove(entity); |
---|
130 | //entity->remove(); |
---|
131 | FastFactory::kill(tmpC->objectPointer, true); |
---|
132 | |
---|
133 | moveC = tmpC->next; |
---|
134 | tmpC->next = this->unusedContainers; |
---|
135 | this->unusedContainers = tmpC; |
---|
136 | tmpC = moveC; |
---|
137 | } |
---|
138 | this->collectedObjects = NULL; |
---|
139 | } |
---|
140 | } |
---|
141 | |
---|
142 | /** |
---|
143 | * this updated the gargabe collection, if the time is ready |
---|
144 | */ |
---|
145 | // void GarbageCollector::update() |
---|
146 | // { |
---|
147 | // if( this->time < this->delay) |
---|
148 | // return; |
---|
149 | // /* garbage collect */ |
---|
150 | // PRINTF(3)("=============================\n"); |
---|
151 | // PRINTF(3)("Processing Garbage Collection\n"); |
---|
152 | // PRINTF(3)("=============================\n"); |
---|
153 | // int counter = 0; |
---|
154 | // |
---|
155 | // tList<WorldEntity>* list = State::getWorldEntityList(); |
---|
156 | // |
---|
157 | // tIterator<WorldEntity>* iterator = list->getIterator(); |
---|
158 | // WorldEntity* entity = iterator->nextElement(); |
---|
159 | // while( entity != NULL) |
---|
160 | // { |
---|
161 | // if( entity->isFinalized()) |
---|
162 | // { |
---|
163 | // PRINTF(4)("= finalizing object\n"); |
---|
164 | // ++counter; |
---|
165 | // |
---|
166 | // /* first remove out of entity list */ |
---|
167 | // list->remove(entity); |
---|
168 | // /* second remove out of pnode tree */ |
---|
169 | // entity->remove(); |
---|
170 | // /* then finaly delete reference */ |
---|
171 | // //delete entity; |
---|
172 | // //FastFactory::kill(); |
---|
173 | // //ObjectManager::getInstance()->addToDeadList(entity->getClassID() & CL_MASK_LOWLEVEL_CLASS, entity); |
---|
174 | // } |
---|
175 | // entity = iterator->nextElement(); |
---|
176 | // } |
---|
177 | // |
---|
178 | // PRINTF(3)("= collected %i unused objects\n", counter); |
---|
179 | // PRINTF(3)("=============================\n"); |
---|
180 | // |
---|
181 | // //ObjectManager::getInstance()->debug(); |
---|
182 | // |
---|
183 | // /* reset time to count again up to this->delay */ |
---|
184 | // this->time = 0; |
---|
185 | // } |
---|