// File: /workspace/github_cpp_makefile_star2_O1/tewalds[P]morat/hex/../lib/compacttree.h
// Function: Morat::CompactTree<Morat::Hex::AgentMCTS::Node>::alloc(unsigned int, Morat::CompactTree<Morat::Hex::AgentMCTS::Node>::Data**)
Data * alloc(unsigned int num, Data ** parent){
		assert(num > 0 && num < MAX_NUM);

		unsigned int size = sizeof(Data) + sizeof(Node)*num;
		PLUS(memused, size);

	//check freelist
		if(Data * t = freelist.pop(num)){
			assert(t->empty() && t->capacity == num);
			return new(t) Data(num, parent);
		}

	//allocate new memory
		while(1){
			Chunk * c = current;
			uint32_t used = c->used;
			if(used + size <= c->capacity){ //if there is room, try to use it
				if(CAS(c->used, used, used+size))
					return new((Data *)(c->mem + used)) Data(num, parent);
				else
					continue;
			}else if(c->next != NULL){ //if there is a next chunk, advance to it and try again
				CAS(current, c, c->next); //CAS to avoid skipping a chunk
				CAS(last, c, c->next); //most last forward too
				continue;
			}else{ //need to allocate a new chunk
				Chunk * next = new Chunk(CHUNK_SIZE);

				while(1){
					while(c->next != NULL) //advance to the end
						c = c->next;

					next->id = c->id+1;
					if(CAS(c->next, (Chunk *)NULL, next)){ //put it in place
						INCR(numchunks);
						//note that this doesn't move current forward since this may not be the next chunk
						// if there is a race condition where two threads allocate chunks at the same time
						break;
					}
				}
				continue;
			}
		}
		assert(false && "How'd CompactTree::alloc get here?");
		return NULL;
	}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rdx, (%rsp)
movl %esi, %r14d
leal -0x1(%r14), %eax
cmpl $0x271, %eax            # imm = 0x271
jae 0x13bc3
movq %rdi, %r15
leal (%r14,%r14,2), %ebx
shll $0x4, %ebx
addl $0x40, %ebx
lock
addq %rbx, 0x13b8(%rdi)
movl $0x1, %eax
xchgl %eax, 0x13b0(%r15)
testl %eax, %eax
jne 0x13a5e
movl %r14d, %eax
movq 0x20(%r15,%rax,8), %r12
testq %r12, %r12
je 0x13abe
movq 0x8(%r12), %rcx
movq %rcx, 0x20(%r15,%rax,8)
movl $0x0, 0x13b0(%r15)
cmpl $0x4, (%r12)
ja 0x13ba4
movzwl 0x4(%r12), %eax
cmpl %r14d, %eax
jne 0x13ba4
movq %r12, %rdi
movl %r14d, %esi
movq (%rsp), %rdx
callq 0x13bf6
jmp 0x13b92
movl $0x0, 0x13b0(%r15)
movq 0x8(%r15), %r13
movl 0x10(%r13), %ebp
leal (%rbp,%rbx), %ecx
cmpl 0xc(%r13), %ecx
jbe 0x13aff
movq (%r13), %rcx
testq %rcx, %rcx
je 0x13b25
movq %r13, %rax
lock
cmpxchgq %rcx, 0x8(%r15)
movq (%r13), %rcx
movq %r13, %rax
lock
cmpxchgq %rcx, 0x10(%r15)
jmp 0x13b88
movl %ebp, %eax
lock
cmpxchgl %ecx, 0x10(%r13)
movb $0x1, %al
jne 0x13b8a
addq 0x18(%r13), %rbp
movq %rbp, %rdi
movl %r14d, %esi
movq (%rsp), %rdx
callq 0x13bf6
xorl %eax, %eax
movq %rbp, %r12
jmp 0x13b8a
movl $0x20, %edi
callq 0x5320
movq %rax, %rbp
movq $0x0, 0x18(%rax)
vxorps %xmm0, %xmm0, %xmm0
vmovups %xmm0, (%rax)
movl $0x0, 0x10(%rax)
movq %rax, %rdi
callq 0x13c8a
movq $0x1000000, 0xc(%rbp)   # imm = 0x1000000
movl $0x1000000, %edi        # imm = 0x1000000
callq 0x5030
movq %rax, 0x18(%rbp)
movq %r13, %rcx
movq (%rcx), %r13
testq %r13, %r13
jne 0x13b67
movl 0x8(%rcx), %eax
incl %eax
movl %eax, 0x8(%rbp)
xorl %eax, %eax
lock
cmpxchgq %rbp, (%rcx)
jne 0x13b6a
lock
incl 0x18(%r15)
movb $0x1, %al
testb %al, %al
jne 0x13ac9
movq %r12, %rax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1eff1(%rip), %rdi     # 0x32b9c
leaq 0x1ec93(%rip), %rsi     # 0x32845
leaq 0x1ef6a(%rip), %rcx     # 0x32b23
movl $0x140, %edx            # imm = 0x140
callq 0x51d0
leaq 0x1ef40(%rip), %rdi     # 0x32b0a
leaq 0x1ec74(%rip), %rsi     # 0x32845
leaq 0x1ef4b(%rip), %rcx     # 0x32b23
movl $0x139, %edx            # imm = 0x139
callq 0x51d0
movq %rax, %rbx
movq %rbp, %rdi
callq 0x52e0
movq %rbx, %rdi
callq 0x5670
nop