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