Example #1
0
	__forceinline char * stack_allocator::allocate(int size_bytes){
			//round size_bytes up to multiple of 4
			if(size_bytes==0){
			    perror("Allocating 0 bytes?\n");fflush(stdout);
			    abort();
			};
			int size = (size_bytes+3)>>2;
			if(cap_free()<size+overhead){//check if it fits
				throw std::bad_alloc();
			};
			int * P = beg()-size;
			block_size(P) = size;
			mark_block_used(P);
			_beg = P-overhead;
			return (char*)P;
	};
Example #2
0
BD_t * mem_bd(uint32_t blocks, uint16_t blocksize)
{
	struct mem_info * info = malloc(sizeof(*info));
	BD_t * bd;
	struct JOSFS_File * f;
	struct JOSFS_Super * s;
	int i;
	
	if(blocks < 1)
		return NULL;

	if(!info)
		return NULL;
	bd = &info->my_bd;
	
	bd->numblocks = blocks;
	bd->blocksize = blocksize;
	bd->atomicsize = blocksize;

	/* When running in the Linux kernel, we can't allocate this much
	 * memory with kmalloc(). So, we use vmalloc() instead. */
	info->blocks = vmalloc(blocks * blocksize);
	if(!info->blocks)
	{
		free(info);
		return NULL;
	}
	if(blockman_init(&info->blockman) < 0)
	{
		free(info->blocks);
		free(info);
		return NULL;
	}

	memset(info->blocks, 0, blocks * blocksize);

	// Set up JOS fs on the mem device... such a hack
	s = (struct JOSFS_Super *) &info->blocks[blocksize];
	s->s_magic = JOSFS_FS_MAGIC;
	s->s_nblocks = blocks;

	f = &s->s_root;
	strcpy(f->f_name, "/");
	f->f_size = 0;
	f->f_type = JOSFS_TYPE_DIR;
	for(i = 0; i < JOSFS_NDIRECT; i++)
		f->f_direct[i] = 0;
	f->f_indirect = 0;

	for(i = 0; i < blocks; i++)
		mark_block_free(&info->blocks[blocksize * 2], i);
	mark_block_used(&info->blocks[blocksize * 2], 0);
	mark_block_used(&info->blocks[blocksize * 2], 1);
	for(i = 0; i < (blocks + JOSFS_BLKBITSIZE - 1) / JOSFS_BLKBITSIZE; i++)
		mark_block_used(&info->blocks[blocksize * 2], i + 2);
	// done setting up JOS fs

	BD_INIT(bd, mem_bd);
	bd->level = 0;
	bd->graph_index = 0;
	if(bd->graph_index >= NBDINDEX)
	{
		DESTROY(bd);
		return NULL;
	}
		
	if(modman_add_anon_bd(bd, __FUNCTION__))
	{
		DESTROY(bd);
		return NULL;
	}
	
	return bd;
}