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