Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/orxonox/trunk/src/util/garbage_collector.cc @ 4951

Last change on this file since 4951 was 4942, checked in by bensch, 19 years ago

orxonox/trunk: garbage-collection works perfectly.
also taken out the finalized-tag from BaseObject
@patrick: i hope, you agree, that it is not used anymore, and i hope you don't mind me having ereased it.

File size: 4.6 KB
Line 
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#include "object_manager.h"
26
27using namespace std;
28
29
30GarbageCollector* GarbageCollector::singletonRef = 0;
31
32/**
33 *  standard constructor
34*/
35GarbageCollector::GarbageCollector ()
36{
37   this->setClassID(CL_GARBAGE_COLLECTOR, "GarbageCollector");
38   this->setName("GarbageCollector");
39
40   this->collectedObjects = NULL;
41   this->unusedContainers = NULL;
42
43   this->time = 0;
44   this->delay = 5.0f; /* clean up all 5.0 seconds */
45}
46
47
48/**
49 *  standard deconstructor
50*/
51GarbageCollector::~GarbageCollector ()
52{
53  // delete what has to be deleted here
54}
55
56/**
57 *  this sets the collection delay
58 * @param delay: the delay
59
60   after this delay, the garbage collector starts its work and begins to collect unused object
61   to delete them afterwards. only objects in the worldentity list from the world object are lookded
62   at.
63*/
64void GarbageCollector::setCollectionDelay(float delay)
65{
66  this->delay = delay;
67}
68
69
70/**
71 *  this foreces a garbage collection
72
73   if this function is called, the gc tries to initiate the garbage collection routines. actually
74   this should always work.
75*/
76void GarbageCollector::forceCollection()
77{
78  /* just make the time slitely bigger than the delay */
79  this->time = this->delay + 1;
80  /* and update, to get rid of the unused objects */
81  this->update();
82}
83
84
85/**
86 * collect an Object, that should be scheduled for clearing.
87 * @param object the Object to schedule.
88 */
89void GarbageCollector::collect(BaseObject* object)
90{
91  State::getWorldEntityList()->remove(dynamic_cast<WorldEntity*>(object));
92  FastObjectMember* tmpC;
93  if (unlikely(this->unusedContainers == NULL))
94  {
95    tmpC = new FastObjectMember;
96  }
97  else
98  {
99    tmpC = this->unusedContainers;
100    this->unusedContainers = this->unusedContainers->next;
101  }
102
103  tmpC->next = this->collectedObjects;
104  tmpC->objectPointer = object;
105  this->collectedObjects = tmpC;
106}
107
108/**
109 *  this ticks the GarbageCollector to give it the time pulse
110 * @param time: the time passed since last tick
111
112   like every other tick function eg. worldentity
113*/
114void GarbageCollector::tick(float time)
115{
116  this->time += time;
117}
118
119
120void GarbageCollector::update()
121{
122  if (this->time < this->delay)
123  {
124    return;
125  }
126  if (this->collectedObjects == NULL)
127    return;
128  else
129  {
130    FastObjectMember* tmpC = this->collectedObjects;
131    FastObjectMember* moveC;
132    while (tmpC != NULL)
133    {
134      WorldEntity* entity = dynamic_cast<WorldEntity*>(tmpC->objectPointer);
135      //State::getWorldEntityList()->remove(entity);
136      entity->remove();
137      FastFactory::kill(entity, true);
138
139      moveC = tmpC->next;
140      tmpC->next = this->unusedContainers;
141      this->unusedContainers = tmpC;
142      tmpC = moveC;
143    }
144    this->collectedObjects = NULL;
145  }
146}
147
148/**
149 *  this updated the gargabe collection, if the time is ready
150*/
151// void GarbageCollector::update()
152// {
153//   if( this->time < this->delay)
154//     return;
155//   /* garbage collect */
156//   PRINTF(3)("=============================\n");
157//   PRINTF(3)("Processing Garbage Collection\n");
158//   PRINTF(3)("=============================\n");
159//   int counter = 0;
160//
161//   tList<WorldEntity>* list = State::getWorldEntityList();
162//
163//   tIterator<WorldEntity>* iterator = list->getIterator();
164//   WorldEntity* entity = iterator->nextElement();
165//   while( entity != NULL)
166//     {
167//       if( entity->isFinalized())
168//         {
169//           PRINTF(4)("= finalizing object\n");
170//           ++counter;
171//
172//           /* first remove out of entity list */
173//           list->remove(entity);
174//           /* second remove out of pnode tree */
175//           entity->remove();
176//           /* then finaly delete reference */
177//           //delete entity;
178//           //FastFactory::kill();
179//           //ObjectManager::getInstance()->addToDeadList(entity->getClassID() & CL_MASK_LOWLEVEL_CLASS, entity);
180//         }
181//       entity = iterator->nextElement();
182//     }
183//
184//   PRINTF(3)("= collected %i unused objects\n", counter);
185//   PRINTF(3)("=============================\n");
186//
187//   //ObjectManager::getInstance()->debug();
188//
189//   /* reset time to count again up to this->delay */
190//   this->time = 0;
191// }
Note: See TracBrowser for help on using the repository browser.