[3647] | 1 | |
---|
| 2 | |
---|
| 3 | /* |
---|
| 4 | orxonox - the future of 3D-vertical-scrollers |
---|
| 5 | |
---|
| 6 | Copyright (C) 2004 orx |
---|
| 7 | |
---|
| 8 | This program is free software; you can redistribute it and/or modify |
---|
| 9 | it under the terms of the GNU General Public License as published by |
---|
| 10 | the Free Software Foundation; either version 2, or (at your option) |
---|
| 11 | any later version. |
---|
| 12 | |
---|
| 13 | ### File Specific: |
---|
| 14 | main-programmer: Patrick Boenzli |
---|
| 15 | co-programmer: ... |
---|
| 16 | */ |
---|
| 17 | |
---|
[3688] | 18 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_GARBAGE_COLLECTOR |
---|
[3647] | 19 | |
---|
| 20 | #include "garbage_collector.h" |
---|
| 21 | |
---|
| 22 | #include "world.h" |
---|
| 23 | #include "world_entity.h" |
---|
| 24 | #include "null_parent.h" |
---|
| 25 | |
---|
| 26 | #include "list.h" |
---|
| 27 | |
---|
| 28 | using namespace std; |
---|
| 29 | |
---|
| 30 | |
---|
| 31 | GarbageCollector* GarbageCollector::singletonRef = 0; |
---|
| 32 | |
---|
| 33 | /** |
---|
| 34 | \brief standard constructor |
---|
| 35 | \todo this constructor is not jet implemented - do it |
---|
| 36 | */ |
---|
| 37 | GarbageCollector::GarbageCollector () |
---|
| 38 | { |
---|
| 39 | this->setClassName ("GarbageCollection"); |
---|
| 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 | |
---|
| 48 | */ |
---|
| 49 | GarbageCollector::~GarbageCollector () |
---|
| 50 | { |
---|
| 51 | // delete what has to be deleted here |
---|
| 52 | } |
---|
| 53 | |
---|
| 54 | |
---|
| 55 | /** |
---|
| 56 | \brief this returns the singleton reference this this class |
---|
| 57 | \returns singleton reference |
---|
| 58 | */ |
---|
| 59 | GarbageCollector* GarbageCollector::getInstance() |
---|
| 60 | { |
---|
| 61 | if( singletonRef == NULL) |
---|
| 62 | singletonRef = new GarbageCollector(); |
---|
| 63 | return singletonRef; |
---|
| 64 | } |
---|
| 65 | |
---|
| 66 | |
---|
[3664] | 67 | /** |
---|
| 68 | \brief this sets the collection delay |
---|
| 69 | \param delay |
---|
| 70 | |
---|
| 71 | after this delay, the garbage collector starts its work and begins to collect unused object |
---|
| 72 | to delete them afterwards. only objects in the worldentity list from the world object are lookded |
---|
| 73 | at. |
---|
| 74 | */ |
---|
[3647] | 75 | void GarbageCollector::setCollectionDelay(float delay) |
---|
[3664] | 76 | { |
---|
| 77 | this->delay = delay; |
---|
| 78 | } |
---|
[3647] | 79 | |
---|
| 80 | |
---|
[3664] | 81 | /** |
---|
| 82 | \brief this foreces a garbage collection |
---|
| 83 | |
---|
| 84 | if this function is called, the gc tries to initiate the garbage collection routines. actually |
---|
| 85 | this should always work. |
---|
| 86 | */ |
---|
[3647] | 87 | void GarbageCollector::forceCollection() |
---|
| 88 | { |
---|
[3664] | 89 | /* just make the time slitely bigger than the delay */ |
---|
| 90 | this->time = this->delay + 1; |
---|
| 91 | /* and update, to get rid of the unused objects */ |
---|
| 92 | this->update(); |
---|
[3647] | 93 | } |
---|
| 94 | |
---|
[3664] | 95 | |
---|
| 96 | /** |
---|
| 97 | \brief this ticks the GarbageCollector to give it the time pulse |
---|
| 98 | \param the time passed since last tick |
---|
| 99 | |
---|
| 100 | like every other tick function eg. worldentity |
---|
| 101 | */ |
---|
[3647] | 102 | void GarbageCollector::tick(float time) |
---|
| 103 | { |
---|
| 104 | this->time += time; |
---|
| 105 | } |
---|
| 106 | |
---|
| 107 | |
---|
[3664] | 108 | /** |
---|
| 109 | \brief this updated the gargabe collection, if the time is ready |
---|
| 110 | |
---|
| 111 | |
---|
| 112 | */ |
---|
[3647] | 113 | void GarbageCollector::update() |
---|
| 114 | { |
---|
| 115 | if( this->time < this->delay) |
---|
| 116 | return; |
---|
| 117 | /* garbage collect */ |
---|
[3669] | 118 | PRINTF(3)("=============================\n"); |
---|
| 119 | PRINTF(3)("Processing Garbage Collection\n"); |
---|
| 120 | PRINTF(3)("=============================\n"); |
---|
| 121 | int counter = 0; |
---|
[3647] | 122 | WorldInterface* wi = WorldInterface::getInstance(); |
---|
| 123 | tList<WorldEntity>* list = wi->getEntityList(); |
---|
[3661] | 124 | |
---|
| 125 | tIterator<WorldEntity>* iterator = list->getIterator(); |
---|
| 126 | WorldEntity* entity = iterator->nextElement(); |
---|
| 127 | while( entity != NULL) |
---|
| 128 | { |
---|
| 129 | if( entity->isFinalized()) |
---|
| 130 | { |
---|
[3669] | 131 | PRINTF(4)("= finalizing object\n"); |
---|
| 132 | ++counter; |
---|
[3662] | 133 | |
---|
[3647] | 134 | /* first remove out of entity list */ |
---|
[3662] | 135 | list->remove(entity); |
---|
[3647] | 136 | /* second remove out of pnode tree */ |
---|
[3669] | 137 | entity->remove(); |
---|
[3670] | 138 | /* then finaly delete reference */ |
---|
[3669] | 139 | delete entity; |
---|
[3661] | 140 | } |
---|
| 141 | entity = iterator->nextElement(); |
---|
| 142 | } |
---|
[3670] | 143 | |
---|
[3669] | 144 | PRINTF(3)("= collected %i unused objects\n", counter); |
---|
| 145 | PRINTF(3)("=============================\n"); |
---|
| 146 | |
---|
[3647] | 147 | /* reset time to count again up to this->delay */ |
---|
| 148 | this->time = 0; |
---|
| 149 | } |
---|