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