Пример #1
0
        VboBlock* Vbo::allocBlock(size_t capacity) {
            assert(capacity > 0);
            
#ifdef _DEBUG_VBO
            checkBlockChain();
            checkFreeBlocks();
#endif

            size_t index = findFreeBlock(0, capacity);
            if (index >= m_freeBlocks.size()) {
                SetVboState mapVbo(*this, VboMapped);
                pack();

                if (capacity > m_freeCapacity) {
                    const size_t usedCapacity = m_totalCapacity - m_freeCapacity;
                    size_t newCapacity = m_totalCapacity;
                    size_t newFreeCapacity = m_freeCapacity;
                    while (capacity > newFreeCapacity) {
                        newCapacity *= 2;
                        newFreeCapacity = newCapacity - usedCapacity;
                    }
                    resizeVbo(newCapacity);
                }
                
                index = findFreeBlock(0, capacity);
                assert(index < m_freeBlocks.size());
            }
            
            VboBlock* block = m_freeBlocks[index];
            std::vector<VboBlock*>::iterator it = m_freeBlocks.begin();
            std::advance(it, index);
            m_freeBlocks.erase(it);
            
            // split block
            if (capacity < block->capacity()) {
                VboBlock* remainder = new VboBlock(*this, block->address() + capacity, block->capacity() - capacity);
                remainder->insertBetween(block, block->m_next);
                block->m_capacity = capacity;
                insertFreeBlock(*remainder);
                if (m_last == block) m_last = remainder;
            }
            
            m_freeCapacity -= block->capacity();
            block->m_free = false;

#ifdef _DEBUG_VBO
            checkBlockChain();
            checkFreeBlocks();
#endif

            return block;
        }
Пример #2
0
void		*malloc(int size)
{
  t_header	*tmp;
  int		total_size;

  if (size <= 0)
    return (NULL);
  lock_thread();
  if ((size % sizeof(int)) != 0)
    size += (sizeof(int) - (size % sizeof(int)));
  if ((tmp = (t_header *)findFreeBlock(size)) == NULL)
    tmp = (t_header *)getMoreMem(size + sizeof(*tmp));
  else
    {
      if (tmp->size - size >= THRESHOLD)
	{
	  total_size = tmp->size + sizeof(*tmp);
	  tmp->size = size;
	  split_mid(tmp, total_size);
	}
      deleteFromFreeList(tmp);
    }
  unlock_thread();
  if (tmp == NULL)
    return (NULL);
  return ((void *)((int)tmp + sizeof(*tmp)));
}
// Ёкспортируемые функции
int _malloc (VirtualAddress * ptr, size_t szBlock){
	MemoryBlock * freeBlock;
	MemoryBlock * newBlock;
	VirtualAddress adress;
	//printf("malloc\n");
	if(!szBlock){
		return -1;
	}
	freeBlock = findFreeBlock(szBlock);
	if(!freeBlock){
		return -2;
	}
	newBlock = (MemoryBlock *)malloc(sizeof(MemoryBlock));
	adress = freeBlock->blockAdress;
	freeBlock->blockAdress = adress + szBlock;
	freeBlock->blockSize = freeBlock->blockSize - szBlock;
	newBlock->blockAdress = adress;
	newBlock->blockSize = szBlock;
	newBlock->isFree = false;
	if(!(freeBlock->previous)){
		newBlock->next = freeBlock;
		freeBlock->previous = newBlock;
		newBlock->previous = NULL;
		memoryManager.firstMemoryBlock = newBlock;
	}
	else{
		switchBlocks(freeBlock, newBlock);
	}
	*ptr = adress;
	return 0;
}
Пример #4
0
        void Vbo::removeFreeBlock(VboBlock& block) {
            assert(block.free());
            size_t index = findFreeBlock(block.address(), block.capacity());
            assert(index < m_freeBlocks.size());
            assert(m_freeBlocks[index] == &block);
            std::vector<VboBlock*>::iterator it = m_freeBlocks.begin();
            std::advance(it, index);
            m_freeBlocks.erase(it);
#ifdef _DEBUG_VBO
            checkFreeBlocks();
#endif
        }
Пример #5
0
	void* Allocator::alloc(size_t size) { 
	
		//Find a free block of memory.
		char* pBlock = findFreeBlock(size);
		if(!pBlock)
			throw std::bad_alloc();
		
		//Allocate the block
		allocateBlock(pBlock, size);
		
		return (void*)pBlock;
	}
Пример #6
0
        void Vbo::insertFreeBlock(VboBlock& block) {
            assert(block.free());
            size_t index = findFreeBlock(block.address(), block.capacity());
            assert(index <= m_freeBlocks.size());
            if (index < m_freeBlocks.size()) {
                std::vector<VboBlock*>::iterator it = m_freeBlocks.begin();
                std::advance(it, index);
                m_freeBlocks.insert(it, &block);
            } else {
                m_freeBlocks.push_back(&block);
            }
#ifdef _DEBUG_VBO
            checkFreeBlocks();
#endif
        }
Пример #7
0
process_id_t process_spawn(char const *executable) {
  TID_t newThread;
  int i = findFreeBlock();
  if(i < 0)  
    return PROCESS_PTABLE_FULL;
    
  process_table[i].exec = executable;
  process_table[i].parent_id = process_get_current_process(); 
  process_table[i].state = RUNNING;
  newThread = thread_create((void*)process_start,(uint32_t)i);
  thread_run(newThread);
//  kprintf("PROCESS SPAWN ER STARTET\n");

  return i; /* pid of new process */
}
Пример #8
0
int insertKey(char *key,int key_len,char *val,int val_len,int ttl)
{
	
	long len_t=val_len;
	int count=(val_len/BLOCK_SIZE)+1;
	if(global_head->freeBlockNum<count||global_head->freeNodeNum==0)
		return -1;
	int already=findKey(key,key_len);
	if(already!=-1)
		removeKey(already);
	int index=findFreeNode();
	if(index==-1)
		return -1;
	int i;
	int sum=0;
	for (i = 0; i < count; ++i)
	{
		int t=findFreeBlock();
		long ttt=len_t;
		if(t==-1)
			return -1;
		(global_head->p[index])->block[i]=t;
		if(ttt>BLOCK_SIZE)
			ttt=BLOCK_SIZE;
		memcpy(AT(t),val+sum,ttt);
		//long tt=snprintf(AT(t),ttt+1,"%s",val+sum);
		sum+=ttt;
		len_t-=ttt;
		setBlockUsed(t);
	}
	(global_head->p[index])->block[i]=-1;
	global_head->freeBlockNum-=count;
	global_head->usedBlockNum+=count;
	global_head->freeNodeNum--;
	global_head->usedNodeNum++;
	global_head->p[index]->size=val_len;
	global_head->p[index]->is_free=0;
	global_head->p[index]->ttl=time(0)+ttl;

	snprintf(global_head->p[index]->key,key_len+1,"%s",key);
	global_head->p[index]->key_len=key_len;
	for (i = 0; i < count; ++i)
	{
		setBlockUsed((global_head->p[index])->block[i]);	
	}
	return 1;
}
Пример #9
0
MemBlock * ListAllocator::findFreeBlock(Size size, bool askParent = true)
{
    Size sz;
    Address addr;

    /* Loop all available memory blocks. */
    for (MemRegion *r = regions; r; r = r->next)
    {
	/* Does this region have enough memory? */
	if (r->free >= size)
	{
    	    /* Search all it's available blocks. */
    	    for (MemBlock *b = r->blocks; b; b = b->next)
	    {
		/* Must be a sane memory block. */
	        assert(b->magic  == LISTALLOC_MAGIC);
	        assert(b->next   != b);
	        assert(b->prev   != b);
		assert(b->region == r);
    
		/* Is this block big enough? */
	        if (b->free && b->size >= size)
		{
		    return b;
		}
	    }
	}
    }
    /* If no blocks are available, allocate from parent. */
    if (parent && askParent)
    {
	/* We want a new memory block from our parent. */
	sz = size + sizeof(MemRegion) + sizeof(MemBlock);
	
	/* Ask for more blocks from our parent. */
	if ((addr = parent->allocate(&sz)))
	{
	    region(addr, sz);
	}
	/* Try again. */
	return findFreeBlock(size, false);
    }
    /* Out of memory. */
    return ZERO;
}
Пример #10
0
Address ListAllocator::allocate(Size *size)
{
    MemBlock *b, *n;
    Address a;
    
    REVALIDATE();
    
    /* First try to find a free MemBlock. */
    if ((b = findFreeBlock(*size)) == ZERO)
    {
	return ZERO;
    }
    /* Split it if possible. */
    if (*size + sizeof(MemBlock) * 4 < b->size)
    {
	/* Create new block. */
	a  = (Address) (b + 1);
	a += *size;
	n  = (MemBlock *) a;
	
	/* Fill it. */
	n->magic  = LISTALLOC_MAGIC;
	n->size   = b->size - (*size) - sizeof(MemBlock);
	n->region = b->region;
	b->size   = *size;
	n->free   = TRUE;
	n->next   = b->next;
	n->prev   = b;
	if (b->next != ZERO)
	    b->next->prev = n;
	b->next   = n;

	REVALIDATE();
    }
    /* Return the block. */
    b->region->free -= *size;
    b->free  = FALSE;
    
    REVALIDATE();
    
    /* Success. */
    return (Address) (b + 1);
}
Пример #11
0
	int allocateBlock() {
		int block = findFreeBlock();
		if (block >= 0)
			blocks[block] = true;
		return block;
	}
Пример #12
0
unsigned int FSManager::saveFile(string filePath)
{
    char * fileContent;
    fstream file;
    if (!diskFile.is_open())
    {
        cout << "\n BLAD: saveFile(): dysk nie jest otwarty";
        return -1;
    }
    file.open(filePath.c_str(), fstream::in | fstream::binary);
    if (file.fail() || file.eof())
    {
        cout << "\n BLAD: saveFile(): wystapil eof lub fail \n";
        return -1;
    }


    fileContent = new char[blkSize + 1];
    int i = 1;
    int bytesRead;
    int freeBlock;
    int firstBlock = findFreeBlock();

    while (1)
    {
        freeBlock = findFreeBlock();
        // cout << "saveFile() : findFreeBlock() zwrocil: " << freeBlock << std::endl;
        this->freeSpace -= blkSize;
        file.read(fileContent, blkSize - sizeof (Block));
        bytesRead = file.gcount();
        // cout << "Odczytalem " << bytesRead << " bajtow z pliku\n";
        //cout << "Ustawiam pozycje " << blkSize << "*" << freeBlock << "+" <<sizeof(DiskInfo) << "= " << blkSize * freeBlock + sizeof (DiskInfo) << "\n";
        diskFile.seekp(blkSize * freeBlock + sizeof (DiskInfo));
        // cout << "ustawilo sie " << diskFile.tellp() << "\n";
        Block block;
        block.current = freeBlock;
        if (bytesRead != (blkSize - sizeof (Block)))
        {
            // cout << "dane zmieszcza sie w jednym pliku\n";
            block.next = 0;
            block.size = bytesRead;
            //cout << "Pozycja przed zapisem: " <<diskFile.tellp() << "\n";
            diskFile.write((char *) & block, sizeof (block));
            //cout << "Pozycja przed zapisem: " <<diskFile.tellp() << "\n";
            diskFile.write(fileContent, bytesRead);
            break;
        } else
        {
            block.next = findFreeBlock(freeBlock + 1);
            //        cout << "saveFile() : findFreeBlock() zwrocil: " << block.next << std::endl;
            block.size = bytesRead;
            diskFile.seekp(blkSize * freeBlock + sizeof (DiskInfo));
            //cout << "Pozycja przed zapisem: " <<diskFile.tellp() << "\n";

            diskFile.write((char *) & block, sizeof (block));
            // cout << "Pozycja przed zapisem: " <<diskFile.tellp() << "\n";
            diskFile.write(fileContent, bytesRead);
        }
        i++;
    }
    file.close();
    delete[] fileContent;
    return firstBlock;
}
Пример #13
0
int createFile(char *name, enum fileTypes type, securityIdType securityID) {

int i;
unsigned int catalogBlock;


	// if no filename give, return error
	if ( name == 0 ) {

		return ERROR_BAD_VALUE;

	}

	// if the root directory is invalid, return error
	if ( rootDir == 0 ) {

		return ERROR_INIT;

	}

	// if the file already exists, return error
	if ( statusFile(name, 0) == 0 ) {

		return ERROR_FILE_EXISTS;

	}

	// find the first empty directory entry

	for ( i = 0; i < masterBlocks->mblock0.dirEntriesPerBlock; ++i ) {

		if ( rootDir->entry[i].name[0] == 0) {

			strncpy(rootDir->entry[i].name, name, MAX_FILENAME_LEN);
			rootDir->entry[i].fileSize = 0;
			rootDir->entry[i].fileType = type;
			rootDir->entry[i].securityID = securityID;
			rootDir->entry[i].othersPermissions = 0;

			// now allocate a block for its first catalog block

			if (findFreeBlock(&catalogBlock) == 0 ) {

				eraseBlock(catalogBlock);

				setBlockInUse(catalogBlock);

				rootDir->entry[i].firstCatalogBlock = catalogBlock;

			}
			else {

				return ERROR_NO_BLOCKS;
			
			}

			rootDir->numEntries++;
			return NO_ERROR;

		} // if strcmp

	}  // for

	return ERROR_MAX_EXCEEDED;

}
Пример #14
0
int makeMemoryFile(char *name, unsigned int address, unsigned int length, char accessType, securityIdType securityID ) {

int i;
unsigned int catalogBlock;
struct { 
	unsigned int address;
	unsigned int size;
	char accessType;

	} *memoryFileInfo;

	// if no filename give, return error
	if ( name == 0 ) {

		return ERROR_BAD_VALUE;

	}

	// if the root directory is invalid, return error
	if ( rootDir == 0 ) {

		return ERROR_INIT;

	}

	// if the file already exists, return error
	if ( statusFile(name, 0) == 0 ) {

		return ERROR_FILE_EXISTS;

	}


	// find the first empty directory entry
	for ( i = 0; i < rootDir->maxEntries; ++i ) {

		if ( rootDir->entry[i].name[0] == 0) {

			strncpy(rootDir->entry[i].name, name, MAX_FILENAME_LEN);
			rootDir->entry[i].fileSize = length;
			rootDir->entry[i].fileType = 0x3 + accessType;
			rootDir->entry[i].securityID = securityID;
			rootDir->entry[i].othersPermissions = 0;

			// now allocate a block for its first catalog block

			if (findFreeBlock(&catalogBlock) == 0 ) {

				eraseBlock(catalogBlock);

				setBlockInUse(catalogBlock);

				rootDir->entry[i].firstCatalogBlock = catalogBlock;

			}
			else {

				return ERROR_NO_BLOCKS;
			
			}

			rootDir->numEntries++;

			readBlock(catalogBlock, (void **)&memoryFileInfo);

			memoryFileInfo->address = address;
			memoryFileInfo->size = length;
			memoryFileInfo->accessType = accessType;

			writeBlock(memoryFileInfo, catalogBlock);

			return NO_ERROR;

		} // if strcmp

	}  // for

	return ERROR_MAX_EXCEEDED;

}
Пример #15
0
unsigned int addNewBlock(fileHandleType fh) {

unsigned int newBlockNum;
void *catalogBlock;
unsigned int dirEntry;
int i;
int retval;
	
	if (fh > MAX_OPEN_FILES) {

		return ERROR_BAD_HANDLE;

	}

	if (fileCursors[fh].inUse == 0) {

		return ERROR_BAD_HANDLE;

	}

	dirEntry = fileCursors[fh].dirEntryNum;

	// read the catalog for this file
	retval = readBlock(rootDir->entry[dirEntry].firstCatalogBlock, &catalogBlock);

	if (retval != 0) {

		return ERROR_READ_ERROR;

	}

	for (i=0; i < masterBlocks->mblock0.blockEntriesPerCatalog; ++i) {

		if (*((unsigned int *)(catalogBlock)+i) == 0)
			break;
	}

	if (i == masterBlocks->mblock0.blockEntriesPerCatalog) {

		return -1;

	}

	if (findFreeBlock(&newBlockNum) != NO_ERROR) {

		return ERROR_NO_BLOCKS;

	}

	setBlockInUse(newBlockNum);

	// add this new block to the catalog for this file
	*((unsigned int *)catalogBlock+i) = newBlockNum;

	// and write the block back 
	writeBlock(catalogBlock, rootDir->entry[dirEntry].firstCatalogBlock);

	// release the memory allocated by readBlock
	deallocate(catalogBlock, masterBlocks->mblock0.blockSize);

	return newBlockNum;

}