| 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 "world.h" | 
|---|
| 21 | #include "world_entity.h" | 
|---|
| 22 | #include "null_parent.h" | 
|---|
| 23 |  | 
|---|
| 24 | #include "list.h" | 
|---|
| 25 | #include "object_manager.h" | 
|---|
| 26 |  | 
|---|
| 27 | using namespace std; | 
|---|
| 28 |  | 
|---|
| 29 |  | 
|---|
| 30 | GarbageCollector* GarbageCollector::singletonRef = 0; | 
|---|
| 31 |  | 
|---|
| 32 | /** | 
|---|
| 33 |    \brief standard constructor | 
|---|
| 34 | */ | 
|---|
| 35 | GarbageCollector::GarbageCollector ()  | 
|---|
| 36 | { | 
|---|
| 37 |    this->setClassID(CL_GARBAGE_COLLECTOR, "GarbageCollector"); | 
|---|
| 38 |    this->time = 0; | 
|---|
| 39 |    this->delay = 1.5f; /* clean up all 5.0 seconds */ | 
|---|
| 40 | } | 
|---|
| 41 |  | 
|---|
| 42 |  | 
|---|
| 43 | /** | 
|---|
| 44 |    \brief standard deconstructor | 
|---|
| 45 | */ | 
|---|
| 46 | GarbageCollector::~GarbageCollector ()  | 
|---|
| 47 | { | 
|---|
| 48 |   // delete what has to be deleted here | 
|---|
| 49 | } | 
|---|
| 50 |  | 
|---|
| 51 |  | 
|---|
| 52 | /** | 
|---|
| 53 |    \brief this returns the singleton reference this this class | 
|---|
| 54 |    \returns singleton reference | 
|---|
| 55 | */ | 
|---|
| 56 | GarbageCollector* GarbageCollector::getInstance() | 
|---|
| 57 | { | 
|---|
| 58 |   if( singletonRef == NULL) | 
|---|
| 59 |     singletonRef = new GarbageCollector(); | 
|---|
| 60 |   return singletonRef; | 
|---|
| 61 | } | 
|---|
| 62 |  | 
|---|
| 63 |  | 
|---|
| 64 | /** | 
|---|
| 65 |    \brief this sets the collection delay | 
|---|
| 66 |    \param delay: the delay | 
|---|
| 67 |     | 
|---|
| 68 |    after this delay, the garbage collector starts its work and begins to collect unused object | 
|---|
| 69 |    to delete them afterwards. only objects in the worldentity list from the world object are lookded | 
|---|
| 70 |    at.  | 
|---|
| 71 | */ | 
|---|
| 72 | void GarbageCollector::setCollectionDelay(float delay) | 
|---|
| 73 | { | 
|---|
| 74 |   this->delay = delay; | 
|---|
| 75 | } | 
|---|
| 76 |  | 
|---|
| 77 |  | 
|---|
| 78 | /** | 
|---|
| 79 |    \brief this foreces a garbage collection | 
|---|
| 80 |  | 
|---|
| 81 |    if this function is called, the gc tries to initiate the garbage collection routines. actually | 
|---|
| 82 |    this should always work. | 
|---|
| 83 | */ | 
|---|
| 84 | void GarbageCollector::forceCollection() | 
|---|
| 85 | { | 
|---|
| 86 |   /* just make the time slitely bigger than the delay */ | 
|---|
| 87 |   this->time = this->delay + 1; | 
|---|
| 88 |   /* and update, to get rid of the unused objects */ | 
|---|
| 89 |   this->update(); | 
|---|
| 90 | } | 
|---|
| 91 |  | 
|---|
| 92 |  | 
|---|
| 93 | /** | 
|---|
| 94 |    \brief this ticks the GarbageCollector to give it the time pulse | 
|---|
| 95 |    \param time: the time passed since last tick | 
|---|
| 96 |  | 
|---|
| 97 |    like every other tick function eg. worldentity | 
|---|
| 98 | */ | 
|---|
| 99 | void GarbageCollector::tick(float time) | 
|---|
| 100 | { | 
|---|
| 101 |   this->time += time; | 
|---|
| 102 | } | 
|---|
| 103 |  | 
|---|
| 104 |  | 
|---|
| 105 | /** | 
|---|
| 106 |    \brief this updated the gargabe collection, if the time is ready | 
|---|
| 107 | */ | 
|---|
| 108 | void GarbageCollector::update() | 
|---|
| 109 | { | 
|---|
| 110 |   if( this->time < this->delay) | 
|---|
| 111 |     return; | 
|---|
| 112 |   /* garbage collect */ | 
|---|
| 113 |   PRINTF(3)("=============================\n"); | 
|---|
| 114 |   PRINTF(3)("Processing Garbage Collection\n"); | 
|---|
| 115 |   PRINTF(3)("=============================\n"); | 
|---|
| 116 |   int counter = 0; | 
|---|
| 117 |   WorldInterface* wi = WorldInterface::getInstance(); | 
|---|
| 118 |   tList<WorldEntity>* list = wi->getEntityList(); | 
|---|
| 119 |  | 
|---|
| 120 |   tIterator<WorldEntity>* iterator = list->getIterator(); | 
|---|
| 121 |   WorldEntity* entity = iterator->nextElement(); | 
|---|
| 122 |   while( entity != NULL)  | 
|---|
| 123 |     {  | 
|---|
| 124 |       if( entity->isFinalized()) | 
|---|
| 125 |         { | 
|---|
| 126 |           PRINTF(4)("= finalizing object\n"); | 
|---|
| 127 |           ++counter; | 
|---|
| 128 |            | 
|---|
| 129 |           /* first remove out of entity list */ | 
|---|
| 130 |           list->remove(entity); | 
|---|
| 131 |           /* second remove out of pnode tree */ | 
|---|
| 132 |           entity->remove(); | 
|---|
| 133 |           /* then finaly delete reference */ | 
|---|
| 134 |           //delete entity; | 
|---|
| 135 |           ObjectManager::getInstance()->addToDeadList(entity->getClassID(), entity); | 
|---|
| 136 |         } | 
|---|
| 137 |       entity = iterator->nextElement(); | 
|---|
| 138 |     } | 
|---|
| 139 |  | 
|---|
| 140 |   PRINTF(3)("= collected %i unused objects\n", counter); | 
|---|
| 141 |   PRINTF(3)("=============================\n"); | 
|---|
| 142 |  | 
|---|
| 143 |   //ObjectManager::getInstance()->debug(); | 
|---|
| 144 |  | 
|---|
| 145 |   /* reset time to count again up to this->delay */ | 
|---|
| 146 |   this->time = 0; | 
|---|
| 147 | } | 
|---|