| [3e4fb6] | 1 | /* | 
|---|
|  | 2 | * IdPool_impl.hpp | 
|---|
|  | 3 | * | 
|---|
|  | 4 | *  Created on: Dec 23, 2011 | 
|---|
|  | 5 | *      Author: heber | 
|---|
|  | 6 | */ | 
|---|
|  | 7 |  | 
|---|
|  | 8 | #ifndef IDPOOL_IMPL_HPP_ | 
|---|
|  | 9 | #define IDPOOL_IMPL_HPP_ | 
|---|
|  | 10 |  | 
|---|
|  | 11 | // include config.h | 
|---|
|  | 12 | #ifdef HAVE_CONFIG_H | 
|---|
|  | 13 | #include <config.h> | 
|---|
|  | 14 | #endif | 
|---|
|  | 15 |  | 
|---|
|  | 16 | #include "IdPool.hpp" | 
|---|
|  | 17 |  | 
|---|
|  | 18 | #include "CodePatterns/Log.hpp" | 
|---|
|  | 19 |  | 
|---|
|  | 20 | template <class T> | 
|---|
|  | 21 | IdPool<T>::IdPool(const T _currId, const unsigned int _max_skips, const unsigned int _max_size) : | 
|---|
|  | 22 | currId(_currId), | 
|---|
|  | 23 | MAX_FRAGMENTATION_SKIPS(_max_skips), | 
|---|
|  | 24 | MAX_POOL_FRAGMENTATION(_max_size) | 
|---|
|  | 25 | {} | 
|---|
|  | 26 |  | 
|---|
|  | 27 | template <class T> | 
|---|
|  | 28 | IdPool<T>::~IdPool() | 
|---|
|  | 29 | {} | 
|---|
|  | 30 |  | 
|---|
|  | 31 | template <class T> | 
|---|
|  | 32 | T IdPool<T>::getNextId() | 
|---|
|  | 33 | { | 
|---|
|  | 34 | setLastAction(reserve); | 
|---|
|  | 35 | // try to find an Id in the pool; | 
|---|
|  | 36 | if(!pool.empty()) { | 
|---|
|  | 37 | typename IdPool_t::iterator iter=pool.begin(); | 
|---|
|  | 38 | T id = iter->first; | 
|---|
|  | 39 | range<T> newRange = makeRange(id+1,iter->last); | 
|---|
|  | 40 | // we wont use this iterator anymore, so we don't care about invalidating | 
|---|
|  | 41 | pool.erase(iter); | 
|---|
|  | 42 | if(newRange.first<newRange.last) | 
|---|
|  | 43 | pool.insert(newRange); | 
|---|
|  | 44 | return id; | 
|---|
|  | 45 | } | 
|---|
|  | 46 | // Nothing in the pool... we are out of luck | 
|---|
|  | 47 | return currId++; | 
|---|
|  | 48 | } | 
|---|
|  | 49 |  | 
|---|
|  | 50 | template <class T> | 
|---|
|  | 51 | void IdPool<T>::releaseId(T id) | 
|---|
|  | 52 | { | 
|---|
|  | 53 | setLastAction(release); | 
|---|
|  | 54 | pool.insert(makeRange(id,id+1)); | 
|---|
|  | 55 | defragIdPool(); | 
|---|
|  | 56 | } | 
|---|
|  | 57 |  | 
|---|
|  | 58 | template <class T> | 
|---|
|  | 59 | bool IdPool<T>::reserveId(T id) | 
|---|
|  | 60 | { | 
|---|
|  | 61 | setLastAction(reserve); | 
|---|
|  | 62 | if(id>=currId ) { | 
|---|
|  | 63 | range<T> newRange = makeRange(currId,id); | 
|---|
|  | 64 | if(newRange.first<newRange.last) | 
|---|
|  | 65 | pool.insert(newRange); | 
|---|
|  | 66 | currId=id+1; | 
|---|
|  | 67 | defragIdPool(); | 
|---|
|  | 68 | return true; | 
|---|
|  | 69 | } | 
|---|
|  | 70 | // look for a range that matches the request | 
|---|
|  | 71 | for(typename IdPool_t::iterator iter=pool.begin();iter!=pool.end();++iter){ | 
|---|
|  | 72 | if(iter->isBefore(id)){ | 
|---|
|  | 73 | // we have covered all available ranges... nothing to be found here | 
|---|
|  | 74 | break; | 
|---|
|  | 75 | } | 
|---|
|  | 76 | // no need to check first, since it has to be <=id, since otherwise we would have broken out | 
|---|
|  | 77 | if(!iter->isBeyond(id)){ | 
|---|
|  | 78 | // we found a matching range... get the id from this range | 
|---|
|  | 79 |  | 
|---|
|  | 80 | // split up this range at the point of id | 
|---|
|  | 81 | range<T> bottomRange = makeRange(iter->first,id); | 
|---|
|  | 82 | range<T> topRange = makeRange(id+1,iter->last); | 
|---|
|  | 83 | // remove this range | 
|---|
|  | 84 | pool.erase(iter); | 
|---|
|  | 85 | if(bottomRange.first<bottomRange.last){ | 
|---|
|  | 86 | pool.insert(bottomRange); | 
|---|
|  | 87 | } | 
|---|
|  | 88 | if(topRange.first<topRange.last){ | 
|---|
|  | 89 | pool.insert(topRange); | 
|---|
|  | 90 | } | 
|---|
|  | 91 | defragIdPool(); | 
|---|
|  | 92 | return true; | 
|---|
|  | 93 | } | 
|---|
|  | 94 | } | 
|---|
|  | 95 | // this ID could not be reserved | 
|---|
|  | 96 | return false; | 
|---|
|  | 97 | } | 
|---|
|  | 98 |  | 
|---|
|  | 99 | template <class T> | 
|---|
|  | 100 | void IdPool<T>::defragIdPool() | 
|---|
|  | 101 | { | 
|---|
|  | 102 | // check if the situation is bad enough to make defragging neccessary | 
|---|
|  | 103 | if((numDefragSkips<MAX_FRAGMENTATION_SKIPS) && | 
|---|
|  | 104 | (pool.size()<lastPoolSize+MAX_POOL_FRAGMENTATION)) { | 
|---|
|  | 105 | return; | 
|---|
|  | 106 | } | 
|---|
|  | 107 | LOG(1, "STATUS: Defragmenting id pool."); | 
|---|
|  | 108 | for(typename IdPool_t::iterator iter = pool.begin();iter!=pool.end();) { | 
|---|
|  | 109 | // see if this range is adjacent to the next one | 
|---|
|  | 110 | typename IdPool_t::iterator next = iter; | 
|---|
|  | 111 | next++; | 
|---|
|  | 112 | if(next!=pool.end() && (next->first==iter->last)) { | 
|---|
|  | 113 | // merge the two ranges | 
|---|
|  | 114 | range<T> newRange = makeRange(iter->first,next->last); | 
|---|
|  | 115 | pool.erase(iter); | 
|---|
|  | 116 | pool.erase(next); | 
|---|
|  | 117 | pair<typename IdPool_t::iterator,bool> res = pool.insert(newRange); | 
|---|
|  | 118 | ASSERT(res.second,"Id-Pool was confused"); | 
|---|
|  | 119 | iter=res.first; | 
|---|
|  | 120 | continue; | 
|---|
|  | 121 | } | 
|---|
|  | 122 | ++iter; | 
|---|
|  | 123 | } | 
|---|
|  | 124 | if(!pool.empty()) { | 
|---|
|  | 125 | // check if the last range is at the border | 
|---|
|  | 126 | typename IdPool_t::iterator iter = pool.end(); | 
|---|
|  | 127 | iter--; | 
|---|
|  | 128 | if(iter->last==currId){ | 
|---|
|  | 129 | currId=iter->first; | 
|---|
|  | 130 | pool.erase(iter); | 
|---|
|  | 131 | } | 
|---|
|  | 132 | } | 
|---|
|  | 133 | lastPoolSize=pool.size(); | 
|---|
|  | 134 | numDefragSkips=0; | 
|---|
|  | 135 | } | 
|---|
|  | 136 |  | 
|---|
|  | 137 | /** | 
|---|
|  | 138 | * This define allows simple instantiation of the necessary singleton functions | 
|---|
|  | 139 | * at a chosen place. | 
|---|
|  | 140 | */ | 
|---|
|  | 141 | #define CONSTRUCT_IDPOOL(name) \ | 
|---|
|  | 142 | template name IdPool< name >::getNextId(); \ | 
|---|
|  | 143 | template bool IdPool< name >::reserveId( name ); \ | 
|---|
|  | 144 | template void IdPool< name >::releaseId( name ); \ | 
|---|
|  | 145 | template void IdPool< name >::setLastAction(const enum Actions _action); \ | 
|---|
|  | 146 | template void IdPool< name >::defragIdPool() ; | 
|---|
|  | 147 |  | 
|---|
|  | 148 | #endif /* IDPOOL_IMPL_HPP_ */ | 
|---|