source: src/IdPool_impl.hpp@ 13e5be

stable v1.7.0
Last change on this file since 13e5be was e0e77e, checked in by Frederik Heber <frederik.heber@…>, 4 years ago

Switch off defragmenting of World's atomic id pool.

  • with increased undos that rely on the atom id to remain, we cannot defrag the id pool. Moreover, there should be enough ids in the int range. Hence, there is no direct need for it.
  • In the World the max values for the atomic id pool are set to 0 and if done so, the defragmentation will never be performed.
  • Property mode set to 100644
File size: 3.9 KB
Line 
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
20template <class T, class idpolicy>
21IdPool<T,idpolicy>::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
30template <class T, class idpolicy>
31IdPool<T,idpolicy>::~IdPool()
32{}
33
34template <class T, class idpolicy>
35T IdPool<T,idpolicy>::getNextId()
36{
37 setLastAction(reserve);
38 return idpolicy::getNextId_impl(pool, currId);
39}
40
41template <class T, class idpolicy>
42void IdPool<T,idpolicy>::releaseId(T id)
43{
44 setLastAction(release);
45 pool.insert(makeRange(id,id+1));
46 defragIdPool();
47}
48
49template <class T, class idpolicy>
50bool IdPool<T,idpolicy>::reserveId(T id)
51{
52 setLastAction(reserve);
53 if(id>=currId ) {
54 range<T> newRange = makeRange(currId,id);
55 if(newRange.first<newRange.last)
56 pool.insert(newRange);
57 currId=id+1;
58 defragIdPool();
59 return true;
60 }
61 // look for a range that matches the request
62 for(typename IdPool_t::iterator iter=pool.begin();iter!=pool.end();++iter){
63 if(iter->isBefore(id)){
64 // we have covered all available ranges... nothing to be found here
65 break;
66 }
67 // no need to check first, since it has to be <=id, since otherwise we would have broken out
68 if(!iter->isBeyond(id)){
69 // we found a matching range... get the id from this range
70
71 // split up this range at the point of id
72 range<T> bottomRange = makeRange(iter->first,id);
73 range<T> topRange = makeRange(id+1,iter->last);
74 // remove this range
75 pool.erase(iter);
76 if(bottomRange.first<bottomRange.last){
77 pool.insert(bottomRange);
78 }
79 if(topRange.first<topRange.last){
80 pool.insert(topRange);
81 }
82 defragIdPool();
83 return true;
84 }
85 }
86 // this ID could not be reserved
87 return false;
88}
89
90template <class T, class idpolicy>
91void IdPool<T,idpolicy>::defragIdPool()
92{
93 // check if defragmentation is disabled
94 if ((MAX_FRAGMENTATION_SKIPS == 0) || (MAX_POOL_FRAGMENTATION == 0))
95 return;
96 // check if the situation is bad enough to make defragging neccessary
97 if((numDefragSkips<MAX_FRAGMENTATION_SKIPS) &&
98 (pool.size()<lastPoolSize+MAX_POOL_FRAGMENTATION)) {
99 return;
100 }
101 LOG(3, "DEBUG: Defragmenting id pool.");
102 for(typename IdPool_t::iterator iter = pool.begin();iter!=pool.end();) {
103 // see if this range is adjacent to the next one
104 typename IdPool_t::iterator next = iter;
105 next++;
106 if(next!=pool.end() && (next->first==iter->last)) {
107 // merge the two ranges
108 range<T> newRange = makeRange(iter->first,next->last);
109 pool.erase(iter);
110 pool.erase(next);
111 pair<typename IdPool_t::iterator,bool> res = pool.insert(newRange);
112 ASSERT(res.second,"Id-Pool was confused");
113 iter=res.first;
114 continue;
115 }
116 ++iter;
117 }
118 if(!pool.empty()) {
119 // check if the last range is at the border
120 typename IdPool_t::iterator iter = pool.end();
121 iter--;
122 if(iter->last==currId){
123 currId=iter->first;
124 pool.erase(iter);
125 }
126 }
127 lastPoolSize=pool.size();
128 numDefragSkips=0;
129}
130
131/**
132 * This define allows simple instantiation of the necessary singleton functions
133 * at a chosen place.
134 */
135#define CONSTRUCT_IDPOOL(name, idpolicy) \
136 template name IdPool< name, idpolicy >::getNextId(); \
137 template bool IdPool< name, idpolicy >::reserveId( name ); \
138 template void IdPool< name, idpolicy >::releaseId( name ); \
139 template void IdPool< name, idpolicy >::setLastAction(const enum Actions _action); \
140 template void IdPool< name, idpolicy >::defragIdPool() ;
141
142#endif /* IDPOOL_IMPL_HPP_ */
Note: See TracBrowser for help on using the repository browser.