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