Пример #1
0
/* XX could be made smarter, so it doesn't always copy to a new block.  */
void *realloc(void *buf, vm_size_t new_size)
{
	vm_size_t *op;
	vm_size_t old_size;
	vm_size_t *np;

	if (buf == 0)
		return malloc(new_size);

	op = (vm_size_t*)buf;
	old_size = *--op;

	new_size += sizeof(vm_size_t);
	while (!(np = lmm_alloc(&malloc_lmm, new_size, 0)))
	{
		if (!morecore(new_size))
			return 0;
	}

	memcpy(np, op, old_size < new_size ? old_size : new_size);

	lmm_free(&malloc_lmm, op, old_size);

	*np++ = new_size;
	return np;
}
Пример #2
0
/**
 * @brief Free a region of memory
 *
 * @param buf The address of the memory to free
 * @param size The size of the memory to free
 */
void kfree(void *buf, size_t size)
{
	unsigned long flags;

	kheap_used -= size;

	spin_lock_irq(&kmalloc_lock, &flags);

	lmm_free(&kheap_lmm, buf, size);

	spin_unlock_irq(&kmalloc_lock, flags);
}
Пример #3
0
void lmm_add_free(lmm_t *lmm, void *block, oskit_size_t size)
{
	struct lmm_region *reg;
	oskit_addr_t min = (oskit_addr_t)block;
	oskit_addr_t max = min + size;

	/* Restrict the min and max further to be properly aligned.
	   Note that this is the opposite of what lmm_free() does,
	   because lmm_free() assumes the block was allocated with lmm_alloc()
	   and thus would be a subset of a larger, already-aligned free block.
	   Here we can assume no such thing.  */
	min = (min + ALIGN_MASK) & ~ALIGN_MASK;
	max &= ~ALIGN_MASK;
	assert(max >= min);

	/* If after alignment we have nothing left, we're done.  */
	if (max == min)
		return;

	/* Add the block to the free list(s) of whatever region(s) it overlaps.
	   If some or all of the block doesn't fall into any existing region,
	   then that memory is simply dropped on the floor.  */
	for (reg = lmm->regions; reg; reg = reg->next)
	{
		assert(reg->min < reg->max);
		assert((reg->min & ALIGN_MASK) == 0);
		assert((reg->max & ALIGN_MASK) == 0);

		if ((max > reg->min) && (min < reg->max))
		{
			oskit_addr_t new_min = min, new_max = max;

			/* Only add the part of the block
			   that actually falls within this region.  */
			if (new_min < reg->min)
				new_min = reg->min;
			if (new_max > reg->max)
				new_max = reg->max;
			assert(new_max > new_min);

			/* Add the block.  */
			lmm_free(lmm, (void*)new_min, new_max - new_min);
		}
	}
}
void lmm_free_page(lmm_t *lmm, void *page)
{
	return lmm_free(lmm, page, PAGE_SIZE);
}
Пример #5
0
void free(void *chunk_ptr)
{
	size_t *chunk = (size_t*)chunk_ptr - 1;

	lmm_free(&malloc_lmm, chunk, *chunk);
}