Ejemplo n.º 1
0
int ofmem_posix_memalign( void **memptr, size_t alignment, size_t size )
{
    ofmem_t *ofmem = ofmem_arch_get_private();
    alloc_desc_t *d, **pp;
    void *ret;
    ucell top;
    phys_addr_t pa;

    if( !size )
        return ENOMEM;

    if( !ofmem->next_malloc )
        ofmem->next_malloc = (char*)ofmem_arch_get_malloc_base();

    size = align_size(size + sizeof(alloc_desc_t), alignment);

    /* look in the freelist */
    for( pp=&ofmem->mfree; *pp && (**pp).size < size; pp = &(**pp).next ) {
    }

    /* waste at most 4K by taking an entry from the freelist */
    if( *pp && (**pp).size > size + 0x1000 ) {
        /* Alignment should be on physical not virtual address */
        pa = va2pa((uintptr_t)*pp + sizeof(alloc_desc_t));
        pa = align_ptr(pa, alignment);
        ret = (void *)pa2va(pa);

        memset( ret, 0, (**pp).size - sizeof(alloc_desc_t) );
        *pp = (**pp).next;

        *memptr = ret;
        return 0;
    }

    top = ofmem_arch_get_heap_top();

    /* Alignment should be on physical not virtual address */
    pa = va2pa((uintptr_t)ofmem->next_malloc + sizeof(alloc_desc_t));
    pa = align_ptr(pa, alignment);
    ret = (void *)pa2va(pa);

    if( pointer2cell(ret) + size > top ) {
        printk("out of malloc memory (%x)!\n", size );
        return ENOMEM;
    }

    d = (alloc_desc_t*)((uintptr_t)ret - sizeof(alloc_desc_t));
    ofmem->next_malloc += size;

    d->next = NULL;
    d->size = size;

    memset( ret, 0, size - sizeof(alloc_desc_t) );

    *memptr = ret;
    return 0;
}
Ejemplo n.º 2
0
static struct _pool_chain_shared *
pool_chain_new_shared (gsize size)
{
	struct _pool_chain_shared *chain;
	gpointer map;


#if defined(HAVE_MMAP_ANON)
	map = mmap (NULL,
			size + sizeof (struct _pool_chain_shared),
			PROT_READ | PROT_WRITE,
			MAP_ANON | MAP_SHARED,
			-1,
			0);
	if (map == MAP_FAILED) {
		msg_err ("cannot allocate %z bytes of shared memory, aborting", size +
			sizeof (struct _pool_chain));
		abort ();
	}
	chain = (struct _pool_chain_shared *)map;
	chain->begin = ((guint8 *) chain) + sizeof (struct _pool_chain_shared);
#elif defined(HAVE_MMAP_ZERO)
	gint fd;

	fd = open ("/dev/zero", O_RDWR);
	if (fd == -1) {
		return NULL;
	}
	map = mmap (NULL,
			size + sizeof (struct _pool_chain_shared),
			PROT_READ | PROT_WRITE,
			MAP_SHARED,
			fd,
			0);
	if (map == MAP_FAILED) {
		msg_err ("cannot allocate %z bytes, aborting", size +
			sizeof (struct _pool_chain));
		abort ();
	}
	chain = (struct _pool_chain_shared *)map;
	chain->begin = ((guint8 *) chain) + sizeof (struct _pool_chain_shared);
#else
#       error No mmap methods are defined
#endif
	chain->pos = align_ptr (chain->begin, MEM_ALIGNMENT);
	chain->len = size;
	chain->lock = NULL;
	chain->next = NULL;

	g_atomic_int_inc (&mem_pool_stat->shared_chunks_allocated);
	g_atomic_int_add (&mem_pool_stat->bytes_allocated, size);

	return chain;
}
Ejemplo n.º 3
0
static struct _pool_chain *
pool_chain_new (gsize size)
{
	struct _pool_chain *chain;

	g_return_val_if_fail (size > 0, NULL);

	chain = g_slice_alloc (sizeof (struct _pool_chain));
	chain->begin = g_slice_alloc (size);
	chain->pos = align_ptr (chain->begin, MEM_ALIGNMENT);
	chain->len = size;
	chain->next = NULL;
	g_atomic_int_add (&mem_pool_stat->bytes_allocated, size);
	g_atomic_int_inc (&mem_pool_stat->chunks_allocated);

	return chain;
}
Ejemplo n.º 4
0
void *halloc( void *context, size_t size )
{	
	halloc_t *me, *parent;
	if( context )
	{
		char *res;
		char *aligned;
		
#ifdef HALLOC_DEBUG
			
		if( !child_count )
		{
			pid = getpid();
			atexit( &halloc_report );
		}
		 
		child_count++;
		child_size += size;
#endif	
		parent = halloc_from_data( context );

		/*
		  Align memory address 
		*/
		aligned = align_ptr( parent->scratch );

		parent->scratch_free -= (aligned-parent->scratch);

		if( parent->scratch_free < 0 )
			parent->scratch_free=0;
		
		parent->scratch = aligned;

		if( size <= parent->scratch_free )
		{
			res = parent->scratch;
			parent->scratch_free -= size;
			parent->scratch = ((char *)parent->scratch)+size;
		}
		else
		{

#ifdef HALLOC_DEBUG
			alloc_count++;
#endif	
		
			if( parent->scratch_free < HALLOC_SCRAP_SIZE )
			{
#ifdef HALLOC_DEBUG
				alloc_spill += parent->scratch_free;
#endif
				res = calloc( 1, size + HALLOC_BLOCK_SIZE );
				if( !res )
					DIE_MEM();
				parent->scratch = (char *)res + size;
				parent->scratch_free = HALLOC_BLOCK_SIZE;
			}
			else
			{
				res = calloc( 1, size );
				if( !res )
					DIE_MEM();
			}
			al_push_func( &parent->children, &late_free );
			al_push( &parent->children, res );
			
		}
		return res;
	
	}
	else
	{
		me = (halloc_t *)calloc( 1, align_sz(sizeof(halloc_t)) + align_sz(size) + HALLOC_BLOCK_SIZE );
		
		if( !me )
			DIE_MEM();
#ifdef HALLOC_DEBUG
		parent_count++;
#endif		
		me->scratch = ((char *)me) + align_sz(sizeof(halloc_t)) + align_sz(size);
		me->scratch_free = HALLOC_BLOCK_SIZE;
		
		al_init( &me->children );
		return ((char *)me) + align_sz(sizeof(halloc_t));
	}
}
Ejemplo n.º 5
0
static struct _pool_chain *
rspamd_mempool_chain_new (gsize size, enum rspamd_mempool_chain_type pool_type)
{
	struct _pool_chain *chain;
	gpointer map;

	g_return_val_if_fail (size > 0, NULL);

	if (pool_type == RSPAMD_MEMPOOL_SHARED) {
#if defined(HAVE_MMAP_ANON)
		map = mmap (NULL,
				size + sizeof (struct _pool_chain),
				PROT_READ | PROT_WRITE,
				MAP_ANON | MAP_SHARED,
				-1,
				0);
		if (map == MAP_FAILED) {
			msg_err ("cannot allocate %z bytes of shared memory, aborting", size +
					sizeof (struct _pool_chain));
			abort ();
		}
		chain = map;
		chain->begin = ((guint8 *) chain) + sizeof (struct _pool_chain);
#elif defined(HAVE_MMAP_ZERO)
		gint fd;

		fd = open ("/dev/zero", O_RDWR);
		if (fd == -1) {
			return NULL;
		}
		map = mmap (NULL,
				size + sizeof (struct _pool_chain),
				PROT_READ | PROT_WRITE,
				MAP_SHARED,
				fd,
				0);
		if (map == MAP_FAILED) {
			msg_err ("cannot allocate %z bytes, aborting", size +
					sizeof (struct _pool_chain));
			abort ();
		}
		chain = map;
		chain->begin = ((guint8 *) chain) + sizeof (struct _pool_chain);
#else
#error No mmap methods are defined
#endif
		g_atomic_int_inc (&mem_pool_stat->shared_chunks_allocated);
		g_atomic_int_add (&mem_pool_stat->bytes_allocated, size);
	}
	else {
		map = g_slice_alloc (sizeof (struct _pool_chain) + size);
		chain = map;
		chain->begin = ((guint8 *) chain) + sizeof (struct _pool_chain);
		g_atomic_int_add (&mem_pool_stat->bytes_allocated, size);
		g_atomic_int_inc (&mem_pool_stat->chunks_allocated);
	}

	chain->pos = align_ptr (chain->begin, MEM_ALIGNMENT);
	chain->len = size;
	chain->lock = NULL;

	return chain;
}