void MemoryManager::loadHelper(const std::string &processName, int size, MemoryBlock *block) { // Memory is full, sorry folks if( !block ) { // cerr << "Null block !" << endl; output() << "Impossible de trouver un bloc appropriƩ pour notre processus" << endl; return; } // Ignore used blocks if( block->isUsed() ) { // cerr << "Block is used" << endl; loadHelper( processName, size, block->next() ); } // Block can fit only into the block, use it else if( size > (block->blockSize() >> 1) && size <= block->blockSize() ) { // cerr << "Block size is between " << (block->blockSize() >> 1) << " and " << block->blockSize() << endl; block->use(processName, size); } // Make sure that the block can contain at least the process else if( block->blockSize() < size ) { // cerr << "Block of size " << block->blockSize() << " is too small for " << size << endl; loadHelper( processName, size, block->next() ); } // Divise the block in two else { // cerr << "Divide and conquer block of size " << block->blockSize() << endl; // Break current memory block into two int newSize = block->blockSize() >> 1; int startOffset = block->startOffset(); MemoryBlock *firstBlock = new MemoryBlock(startOffset, newSize, d->memory); MemoryBlock *secondBlock = new MemoryBlock(startOffset+newSize, newSize, d->memory); // Notifiy previous and next of current block // of the two new blocks MemoryBlock *previous = block->previous(); if( previous ) { previous->setNext(firstBlock); } MemoryBlock *next = block->next(); if( next ) { next->setPrevious( secondBlock ); } // Create the links between the blocks firstBlock->setPrevious( previous ); firstBlock->setNext( secondBlock ); secondBlock->setPrevious( firstBlock ); secondBlock->setNext( block->next() ); // Do not lost the link of the head block if( block == d->headBlock ) { d->headBlock = firstBlock; } // Clean current block from memory delete block; // Try to insert into the first block else // divise it again if it's too big for the process if( size > (newSize>>1) && size <= newSize ) { // cerr << "First Block size is between " << (newSize>>1) << " and " << newSize << endl; firstBlock->use(processName, size); } else { loadHelper( processName, size, firstBlock ); } } }