src/utils/malloc.c
void *malloc_arena(size_t mem)
{
	assert(mem > 0);
	// Round to multiple of 16
	size_t oldmem = mem;
	mem = (mem + 15u) & ~15ull;
	assert(mem >= oldmem);
	if (mem >= BUCKET_SIZE / 4)
	{
		void *ret = malloc(mem);
		ASSERT(ret, "Out of memory.");
		return malloc(mem);
	}
	if (current_use + mem > BUCKET_SIZE)
	{
		if (arena_buckets_used == arena_buckets_array_size)
		{
			arena_buckets_array_size *= 2;
			arena_buckets = realloc(arena_buckets, arena_buckets_array_size * sizeof(void *));
			ASSERT(arena_buckets, "Ran out of memory after allocating %ld KB", BUCKET_SIZE * arena_buckets_used / KB);
		}
		current_arena = malloc(BUCKET_SIZE);
		ASSERT(current_arena, "Ran out of memory after allocating %ld KB", BUCKET_SIZE * arena_buckets_used / KB);
		arena_buckets[arena_buckets_used++] = current_arena;
		current_use = 0;
	}
	uint8_t *ptr = current_arena + current_use;
	current_use += mem;
	allocations_done++;
	if (mem > 4096)
	{
		// printf("Allocated large chunk %llu\n", (unsigned long long)mem);
	}
	return (void *)ptr;

}
// Simple bump allocator with buckets.
Simple bump allocator with buckets.
Simple bump allocator with buckets .
void *malloc_arena(size_t mem)
{
	assert(mem > 0);
	size_t oldmem = mem;
	mem = (mem + 15u) & ~15ull;
	assert(mem >= oldmem);
	if (mem >= BUCKET_SIZE / 4)
	{
		void *ret = malloc(mem);
		ASSERT(ret, "Out of memory.");
		return malloc(mem);
	}
	if (current_use + mem > BUCKET_SIZE)
	{
		if (arena_buckets_used == arena_buckets_array_size)
		{
			arena_buckets_array_size *= 2;
			arena_buckets = realloc(arena_buckets, arena_buckets_array_size * sizeof(void *));
			ASSERT(arena_buckets, "Ran out of memory after allocating %ld KB", BUCKET_SIZE * arena_buckets_used / KB);
		}
		current_arena = malloc(BUCKET_SIZE);
		ASSERT(current_arena, "Ran out of memory after allocating %ld KB", BUCKET_SIZE * arena_buckets_used / KB);
		arena_buckets[arena_buckets_used++] = current_arena;
		current_use = 0;
	}
	uint8_t *ptr = current_arena + current_use;
	current_use += mem;
	allocations_done++;
	if (mem > 4096)
	{
	}
	return (void *)ptr;
}
void * malloc_arena ( size_t mem ) { assert ( mem > 0 ) ; size_t oldmem = mem ; mem = ( mem + 15u ) & ~ 15ull ; assert ( mem >= oldmem ) ; if ( mem >= BUCKET_SIZE / 4 ) { void * ret = malloc ( mem ) ; ASSERT ( ret , " " ) ; return malloc ( mem ) ; } if ( current_use + mem > BUCKET_SIZE ) { if ( arena_buckets_used == arena_buckets_array_size ) { arena_buckets_array_size *= 2 ; arena_buckets = realloc ( arena_buckets , arena_buckets_array_size * sizeof ( void * ) ) ; ASSERT ( arena_buckets , " " , BUCKET_SIZE * arena_buckets_used / KB ) ; } current_arena = malloc ( BUCKET_SIZE ) ; ASSERT ( current_arena , " " , BUCKET_SIZE * arena_buckets_used / KB ) ; arena_buckets [ arena_buckets_used ++ ] = current_arena ; current_use = 0 ; } uint8_t * ptr = current_arena + current_use ; current_use += mem ; allocations_done ++ ; if ( mem > 4096 ) { } return ( void * ) ptr ; }
Simple bump allocator with buckets.
Simple bump allocator with buckets .
// Round to multiple of 16 // printf("Allocated large chunk %llu\n", (unsigned long long)mem);