| 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 "world.h" | 
|---|
| 21 | #include "world_entity.h" | 
|---|
| 22 | #include "null_parent.h" | 
|---|
| 23 |  | 
|---|
| 24 | #include "list.h" | 
|---|
| 25 | #include "object_manager.h" | 
|---|
| 26 |  | 
|---|
| 27 | using namespace std; | 
|---|
| 28 |  | 
|---|
| 29 |  | 
|---|
| 30 | GarbageCollector* GarbageCollector::singletonRef = 0; | 
|---|
| 31 |  | 
|---|
| 32 | /** | 
|---|
| 33 |    \brief standard constructor | 
|---|
| 34 | */ | 
|---|
| 35 | GarbageCollector::GarbageCollector () | 
|---|
| 36 | { | 
|---|
| 37 |    this->setClassID(CL_GARBAGE_COLLECTOR, "GarbageCollector"); | 
|---|
| 38 |    this->setName("GarbageCollector"); | 
|---|
| 39 |  | 
|---|
| 40 |    this->time = 0; | 
|---|
| 41 |    this->delay = 1.5f; /* clean up all 5.0 seconds */ | 
|---|
| 42 | } | 
|---|
| 43 |  | 
|---|
| 44 |  | 
|---|
| 45 | /** | 
|---|
| 46 |    \brief standard deconstructor | 
|---|
| 47 | */ | 
|---|
| 48 | GarbageCollector::~GarbageCollector () | 
|---|
| 49 | { | 
|---|
| 50 |   // delete what has to be deleted here | 
|---|
| 51 | } | 
|---|
| 52 |  | 
|---|
| 53 | /** | 
|---|
| 54 |    \brief this sets the collection delay | 
|---|
| 55 |    \param delay: the delay | 
|---|
| 56 |  | 
|---|
| 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 | 
|---|
| 59 |    at. | 
|---|
| 60 | */ | 
|---|
| 61 | void GarbageCollector::setCollectionDelay(float delay) | 
|---|
| 62 | { | 
|---|
| 63 |   this->delay = delay; | 
|---|
| 64 | } | 
|---|
| 65 |  | 
|---|
| 66 |  | 
|---|
| 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 | */ | 
|---|
| 73 | void GarbageCollector::forceCollection() | 
|---|
| 74 | { | 
|---|
| 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(); | 
|---|
| 79 | } | 
|---|
| 80 |  | 
|---|
| 81 |  | 
|---|
| 82 | /** | 
|---|
| 83 |    \brief this ticks the GarbageCollector to give it the time pulse | 
|---|
| 84 |    \param time: the time passed since last tick | 
|---|
| 85 |  | 
|---|
| 86 |    like every other tick function eg. worldentity | 
|---|
| 87 | */ | 
|---|
| 88 | void GarbageCollector::tick(float time) | 
|---|
| 89 | { | 
|---|
| 90 |   this->time += time; | 
|---|
| 91 | } | 
|---|
| 92 |  | 
|---|
| 93 |  | 
|---|
| 94 | /** | 
|---|
| 95 |    \brief this updated the gargabe collection, if the time is ready | 
|---|
| 96 | */ | 
|---|
| 97 | void GarbageCollector::update() | 
|---|
| 98 | { | 
|---|
| 99 |   if( this->time < this->delay) | 
|---|
| 100 |     return; | 
|---|
| 101 |   /* garbage collect */ | 
|---|
| 102 |   PRINTF(3)("=============================\n"); | 
|---|
| 103 |   PRINTF(3)("Processing Garbage Collection\n"); | 
|---|
| 104 |   PRINTF(3)("=============================\n"); | 
|---|
| 105 |   int counter = 0; | 
|---|
| 106 |   WorldInterface* wi = WorldInterface::getInstance(); | 
|---|
| 107 |   tList<WorldEntity>* list = wi->getEntityList(); | 
|---|
| 108 |  | 
|---|
| 109 |   tIterator<WorldEntity>* iterator = list->getIterator(); | 
|---|
| 110 |   WorldEntity* entity = iterator->nextElement(); | 
|---|
| 111 |   while( entity != NULL) | 
|---|
| 112 |     { | 
|---|
| 113 |       if( entity->isFinalized()) | 
|---|
| 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 */ | 
|---|
| 123 |           //delete entity; | 
|---|
| 124 |           ObjectManager::getInstance()->addToDeadList(entity->getClassID() & CL_MASK_LOWLEVEL_CLASS, entity); | 
|---|
| 125 |         } | 
|---|
| 126 |       entity = iterator->nextElement(); | 
|---|
| 127 |     } | 
|---|
| 128 |  | 
|---|
| 129 |   PRINTF(3)("= collected %i unused objects\n", counter); | 
|---|
| 130 |   PRINTF(3)("=============================\n"); | 
|---|
| 131 |  | 
|---|
| 132 |   //ObjectManager::getInstance()->debug(); | 
|---|
| 133 |  | 
|---|
| 134 |   /* reset time to count again up to this->delay */ | 
|---|
| 135 |   this->time = 0; | 
|---|
| 136 | } | 
|---|