[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" |
---|
[5355] | 22 | #include "fast_factory.h" |
---|
[3647] | 23 | |
---|
| 24 | #include "list.h" |
---|
| 25 | |
---|
| 26 | using namespace std; |
---|
| 27 | |
---|
| 28 | GarbageCollector* GarbageCollector::singletonRef = 0; |
---|
| 29 | |
---|
| 30 | /** |
---|
[4836] | 31 | * standard constructor |
---|
[3647] | 32 | */ |
---|
[4592] | 33 | GarbageCollector::GarbageCollector () |
---|
[3647] | 34 | { |
---|
[4320] | 35 | this->setClassID(CL_GARBAGE_COLLECTOR, "GarbageCollector"); |
---|
[4597] | 36 | this->setName("GarbageCollector"); |
---|
| 37 | |
---|
[4941] | 38 | this->collectedObjects = NULL; |
---|
| 39 | this->unusedContainers = NULL; |
---|
| 40 | |
---|
[3647] | 41 | this->time = 0; |
---|
[4938] | 42 | this->delay = 5.0f; /* clean up all 5.0 seconds */ |
---|
[3647] | 43 | } |
---|
| 44 | |
---|
| 45 | |
---|
| 46 | /** |
---|
[4836] | 47 | * standard deconstructor |
---|
[3647] | 48 | */ |
---|
[4592] | 49 | GarbageCollector::~GarbageCollector () |
---|
[3647] | 50 | { |
---|
| 51 | // delete what has to be deleted here |
---|
| 52 | } |
---|
| 53 | |
---|
| 54 | /** |
---|
[4836] | 55 | * this sets the collection delay |
---|
| 56 | * @param delay: the delay |
---|
[4592] | 57 | |
---|
[3664] | 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 |
---|
[4592] | 60 | at. |
---|
[3664] | 61 | */ |
---|
[3647] | 62 | void GarbageCollector::setCollectionDelay(float delay) |
---|
[3664] | 63 | { |
---|
| 64 | this->delay = delay; |
---|
| 65 | } |
---|
[3647] | 66 | |
---|
| 67 | |
---|
[3664] | 68 | /** |
---|
[4836] | 69 | * this foreces a garbage collection |
---|
[3664] | 70 | |
---|
| 71 | if this function is called, the gc tries to initiate the garbage collection routines. actually |
---|
| 72 | this should always work. |
---|
| 73 | */ |
---|
[3647] | 74 | void GarbageCollector::forceCollection() |
---|
| 75 | { |
---|
[3664] | 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(); |
---|
[3647] | 80 | } |
---|
| 81 | |
---|
[3664] | 82 | |
---|
| 83 | /** |
---|
[4941] | 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 | /** |
---|
[4836] | 107 | * this ticks the GarbageCollector to give it the time pulse |
---|
| 108 | * @param time: the time passed since last tick |
---|
[3664] | 109 | |
---|
| 110 | like every other tick function eg. worldentity |
---|
| 111 | */ |
---|
[3647] | 112 | void GarbageCollector::tick(float time) |
---|
| 113 | { |
---|
| 114 | this->time += time; |
---|
| 115 | } |
---|
| 116 | |
---|
| 117 | |
---|
| 118 | void GarbageCollector::update() |
---|
| 119 | { |
---|
[4979] | 120 | if (this->time < this->delay || this->collectedObjects == NULL) |
---|
[3647] | 121 | return; |
---|
[4941] | 122 | else |
---|
| 123 | { |
---|
| 124 | FastObjectMember* tmpC = this->collectedObjects; |
---|
| 125 | FastObjectMember* moveC; |
---|
| 126 | while (tmpC != NULL) |
---|
[4592] | 127 | { |
---|
[4979] | 128 | //WorldEntity* entity = dynamic_cast<WorldEntity*>(tmpC->objectPointer); |
---|
[4941] | 129 | //State::getWorldEntityList()->remove(entity); |
---|
[4979] | 130 | //entity->remove(); |
---|
| 131 | FastFactory::kill(tmpC->objectPointer, true); |
---|
[4592] | 132 | |
---|
[4941] | 133 | moveC = tmpC->next; |
---|
| 134 | tmpC->next = this->unusedContainers; |
---|
| 135 | this->unusedContainers = tmpC; |
---|
| 136 | tmpC = moveC; |
---|
[3661] | 137 | } |
---|
[4941] | 138 | this->collectedObjects = NULL; |
---|
| 139 | } |
---|
| 140 | } |
---|
[3670] | 141 | |
---|
[4941] | 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(); |
---|
[5115] | 158 | // WorldEntity* entity = iterator->firstElement(); |
---|
[4941] | 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 | // } |
---|