// File: Alcaro[P]bridges/arlib/staticmap.cpp
// Function: staticmap::alloc(unsigned long)
uint64_t staticmap::alloc(size_t bytes)
{
//printf("alloc %lu", bytes);
	uint64_t wanted_bucket = ilog2(bytes);
	
	uint64_t current_bucket = wanted_bucket;
	
again:
	uint64_t ptr = rd64(off_r_freespace(current_bucket));
	if (ptr > 1)
	{
		{
			// remove from freelist
			uint64_t next_free = rd64(ptr+off_f_nextobj);
			if (next_free)
				wr64(next_free+off_f_prevptr, off_r_freespace(current_bucket));
			wr64(off_r_freespace(current_bucket), next_free);
		}
		
	split:
		// split it, add new freespaces to their respective buckets
		uint64_t split = wanted_bucket;
		while (split < current_bucket)
		{
			uint64_t ptr_new_free = ptr+(1<<split);
			uint64_t prev_obj = rd64(off_r_freespace(split));
			if (prev_obj == 1)
			{
				for (uint64_t n=split;rd64(off_r_freespace(split))==1;n--)
					wr64(off_r_freespace(split), 0);
				prev_obj = 0;
			}
			if (prev_obj) // if freelist[n]
				wr64(rd64(off_r_freespace(split))+off_f_prevptr, ptr_new_free+off_f_nextobj); // set freelist[n]->prev to new object
			wr64(ptr_new_free, (1<<split)+t_free, prev_obj, off_r_freespace(split)); // create object
			wr64(off_r_freespace(split), ptr_new_free); // add to freelist
			split++;
		}
		// TODO: batch up writes to freelist
		
//printf(" -> %lu\n", ptr);
		return ptr;
	}
	else if (ptr == 0)
	{
		current_bucket++;
		goto again;
	}
	else // else equals 1, every future bucket is empty
	{
		current_bucket = ilog2(mmap.size());
		ptr = mmap.size();
		f.resize(mmap.size()*2, mmap, map_writable);
		goto split;
	}
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x80, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x58(%rbp)
movq -0x10(%rbp), %rdi
callq 0x336f0
movzbl %al, %eax
movq %rax, -0x18(%rbp)
movq -0x18(%rbp), %rax
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rdi
callq 0x336d0
movq -0x58(%rbp), %rdi
movq %rax, %rsi
callq 0x353f0
movq %rax, -0x28(%rbp)
cmpq $0x1, -0x28(%rbp)
jbe 0x33b65
movq -0x58(%rbp), %rdi
movq -0x28(%rbp), %rsi
addq $0x8, %rsi
callq 0x353f0
movq %rax, -0x30(%rbp)
cmpq $0x0, -0x30(%rbp)
je 0x339f8
movq -0x30(%rbp), %rax
addq $0x10, %rax
movq %rax, -0x60(%rbp)
movq -0x20(%rbp), %rdi
callq 0x336d0
movq -0x58(%rbp), %rdi
movq -0x60(%rbp), %rsi
movq %rax, %rdx
callq 0x356d0
movq -0x20(%rbp), %rdi
callq 0x336d0
movq -0x58(%rbp), %rdi
movq %rax, %rsi
movq -0x30(%rbp), %rdx
callq 0x356d0
movq -0x18(%rbp), %rax
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rax
cmpq -0x20(%rbp), %rax
jae 0x33b58
movq -0x28(%rbp), %rax
movq -0x38(%rbp), %rcx
movl $0x1, %edx
shll %cl, %edx
movl %edx, %ecx
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, -0x40(%rbp)
movq -0x38(%rbp), %rdi
callq 0x336d0
movq -0x58(%rbp), %rdi
movq %rax, %rsi
callq 0x353f0
movq %rax, -0x48(%rbp)
cmpq $0x1, -0x48(%rbp)
jne 0x33ab2
movq -0x38(%rbp), %rax
movq %rax, -0x50(%rbp)
movq -0x38(%rbp), %rdi
callq 0x336d0
movq -0x58(%rbp), %rdi
movq %rax, %rsi
callq 0x353f0
cmpq $0x1, %rax
jne 0x33aaa
movq -0x38(%rbp), %rdi
callq 0x336d0
movq -0x58(%rbp), %rdi
movq %rax, %rsi
xorl %edx, %edx
callq 0x35960
movq -0x50(%rbp), %rax
addq $-0x1, %rax
movq %rax, -0x50(%rbp)
jmp 0x33a6a
movq $0x0, -0x48(%rbp)
cmpq $0x0, -0x48(%rbp)
je 0x33ae6
movq -0x38(%rbp), %rdi
callq 0x336d0
movq -0x58(%rbp), %rdi
movq %rax, %rsi
callq 0x353f0
movq -0x58(%rbp), %rdi
movq %rax, %rsi
addq $0x10, %rsi
movq -0x40(%rbp), %rdx
addq $0x8, %rdx
callq 0x356d0
movq -0x40(%rbp), %rax
movq %rax, -0x78(%rbp)
movq -0x38(%rbp), %rax
movl %eax, %ecx
movl $0x1, %eax
shll %cl, %eax
cltq
addq $0x0, %rax
movq %rax, -0x70(%rbp)
movq -0x48(%rbp), %rax
movq %rax, -0x68(%rbp)
movq -0x38(%rbp), %rdi
callq 0x336d0
movq -0x78(%rbp), %rsi
movq -0x70(%rbp), %rdx
movq -0x68(%rbp), %rcx
movq -0x58(%rbp), %rdi
movq %rax, %r8
callq 0x35a00
movq -0x38(%rbp), %rdi
callq 0x336d0
movq -0x58(%rbp), %rdi
movq %rax, %rsi
movq -0x40(%rbp), %rdx
callq 0x356d0
movq -0x38(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x38(%rbp)
jmp 0x33a19
movq -0x28(%rbp), %rax
addq $0x80, %rsp
popq %rbp
retq
cmpq $0x0, -0x28(%rbp)
jne 0x33b7d
movq -0x20(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x20(%rbp)
jmp 0x33993
movq -0x58(%rbp), %rdi
addq $0x8, %rdi
callq 0x6290
movq %rax, %rdi
callq 0x336f0
movq -0x58(%rbp), %rdi
movzbl %al, %eax
movq %rax, -0x20(%rbp)
addq $0x8, %rdi
callq 0x6290
movq -0x58(%rbp), %rdi
movq %rax, -0x28(%rbp)
movq %rdi, %rax
addq $0x4, %rax
movq %rax, -0x80(%rbp)
addq $0x8, %rdi
callq 0x6290
movq -0x80(%rbp), %rdi
movq %rax, %rsi
movq -0x58(%rbp), %rax
shlq %rsi
movq %rax, %rdx
addq $0x8, %rdx
movb 0x18(%rax), %al
andb $0x1, %al
movzbl %al, %ecx
callq 0x35500
jmp 0x33a11
nopl (%rax)