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