+std::size_t Resource::getNumberOfFreePlace(int nbCoresPerCont) const
+{
+ std::size_t ret(0),pos(0);
+ while( pos < _occupied.size() )
+ {
+ bool isChunckFree(true);
+ int posInChunck(0);
+ for( ; ( posInChunck < nbCoresPerCont ) && ( pos < _occupied.size() ) ; ++posInChunck, ++pos)
+ if(_occupied[pos])
+ isChunckFree = false;
+ if( isChunckFree && (posInChunck == nbCoresPerCont) )
+ ret++;
+ }
+ return ret;
+}
+
+std::vector<std::size_t> Resource::allocateFor(std::size_t& nbOfPlacesToTake, int nbCoresPerCont) const
+{
+ std::vector<std::size_t> ret;
+ std::size_t pos(0),curWorkerId(0);
+ while( ( pos < _occupied.size() ) && ( nbOfPlacesToTake > 0 ) )
+ {
+ bool isChunckFree(true);
+ int posInChunck(0);
+ for( ; ( posInChunck < nbCoresPerCont ) && ( pos < _occupied.size() ) ; ++posInChunck, ++pos)
+ if(_occupied[pos])
+ isChunckFree = false;
+ if( isChunckFree && (posInChunck == nbCoresPerCont) )
+ {
+ for(int i = 0 ; i < nbCoresPerCont ; ++i)
+ _occupied[pos-nbCoresPerCont+i] = true;
+ ret.push_back(curWorkerId);
+ --nbOfPlacesToTake;
+ }
+ ++curWorkerId;
+ }
+ return ret;
+}
+
+void Resource::release(std::size_t workerId, int nbCoresPerCont) const
+{
+ if(workerId >= this->getNumberOfWorkers(nbCoresPerCont))
+ throw Exception("Resource::release : invalid worker id !");
+ std::size_t pos(workerId*static_cast<std::size_t>(nbCoresPerCont));
+ for(int i = 0 ; i < nbCoresPerCont ; ++i)
+ {
+ if(!_occupied[pos + static_cast<std::size_t>(i)])
+ throw Exception("Resource::release : internal error ! A core is expected to be occupied !");
+ _occupied[pos + static_cast<std::size_t>(i)] = false;
+ }
+}
+
+std::size_t Resource::getNumberOfWorkers(int nbCoresPerCont) const
+{
+ return static_cast<std::size_t>(this->nbCores())/static_cast<std::size_t>(nbCoresPerCont);
+}
+
+void Resource::printSelf(std::ostream& oss) const
+{
+ oss << this->name() << " (" << this->nbCores() << ") : ";
+ for(auto it : this->_occupied)
+ {
+ if(it)
+ oss << "1";
+ else
+ oss << "0";
+ }
+}