Пример #1
0
void *
crt_memalign (size_t alignment, size_t size)
{
	void *mem, *base;
	unsigned long tot_addr, tot_end_addr, addr, end_addr;
	struct heap_free_area **heap = &__malloc_heap;

	/* Make SIZE something we like.  */
	size = HEAP_ADJUST_SIZE (size);

	/* Use malloc to do the initial allocation, since it deals with getting
	system memory.  We over-allocate enough to be sure that we'll get
	enough memory to hold a properly aligned block of size SIZE,
	_somewhere_ in the result.  */
	mem = crt_malloc (size + 2 * alignment);
	if (! mem)
		/* Allocation failed, we can't do anything.  */
		return 0;
	if (alignment < MALLOC_ALIGNMENT)
		return mem;

	/* Remember the base-address, of the allocation, although we normally
	use the user-address for calculations, since that's where the
	alignment matters.  */
	base = MALLOC_BASE (mem);

	/* The bounds of the initial allocation.  */
	tot_addr = (unsigned long)mem;
	tot_end_addr = (unsigned long)base + MALLOC_SIZE (mem);

	/* Find a likely place inside MEM with the right alignment.  */
	addr = MALLOC_ROUND_UP (tot_addr, alignment);

	/* Unless TOT_ADDR was already aligned correctly, we need to return the
	initial part of MEM to the heap.  */
	if (addr != tot_addr)
	{
		size_t init_size = addr - tot_addr;

		/* Ensure that memory returned to the heap is large enough.  */
		if (init_size < HEAP_MIN_SIZE)
		{
			addr = MALLOC_ROUND_UP (tot_addr + HEAP_MIN_SIZE, alignment);
			init_size = addr - tot_addr;
		}

		__heap_free (heap, base, init_size);

		/* Remember that we've freed the initial part of MEM.  */
		(char*)base += init_size;
	}

	/* Return the end part of MEM to the heap, unless it's too small.  */
	end_addr = addr + size;
	if (end_addr + MALLOC_REALLOC_MIN_FREE_SIZE < tot_end_addr)
		__heap_free (heap, (void *)end_addr, tot_end_addr - end_addr);
	else
		/* We didn't free the end, so include it in the size.  */
		end_addr = tot_end_addr;

	return MALLOC_SETUP (base, end_addr - (unsigned long)base);
}
Пример #2
0
// Modify the local functions below should you wish to use some other memory routines
// for malloc(), free()
static void* XXH_malloc(size_t s) { return crt_malloc(s); }