// File: /workspace/github_cpp_makefile_star2_O3/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
movl %esi, %r14d
leal -0x1(%r14), %eax
cmpl $0x271, %eax            # imm = 0x271
jae 0x13cc1
leal (%r14,%r14,2), %ebp
movq %rdx, %rbx
movq %rdi, %r15
shll $0x4, %ebp
addl $0x40, %ebp
lock
addq %rbp, 0x13b8(%rdi)
movl $0x1, %eax
xchgl %eax, 0x13b0(%r15)
testl %eax, %eax
jne 0x13b7f
movl %r14d, %eax
movq 0x20(%r15,%rax,8), %r13
testq %r13, %r13
je 0x13be4
movq 0x8(%r13), %rcx
movq %rcx, 0x20(%r15,%rax,8)
movl $0x0, 0x13b0(%r15)
cmpl $0x4, (%r13)
ja 0x13bc5
movzwl 0x4(%r13), %eax
cmpl %r14d, %eax
je 0x13ca1
leaq 0x1efd0(%rip), %rdi     # 0x32b9c
leaq 0x1ec72(%rip), %rsi     # 0x32845
leaq 0x1ef49(%rip), %rcx     # 0x32b23
movl $0x140, %edx            # imm = 0x140
callq 0x51d0
movl $0x0, 0x13b0(%r15)
movq 0x8(%r15), %r12
movl 0x10(%r12), %r13d
leal (%r13,%rbp), %ecx
cmpl 0xc(%r12), %ecx
jbe 0x13c25
movq (%r12), %rcx
testq %rcx, %rcx
je 0x13c33
movq %r12, %rax
lock
cmpxchgq %rcx, 0x8(%r15)
movq %r12, %rax
movq (%r12), %rcx
lock
cmpxchgq %rcx, 0x10(%r15)
jmp 0x13bef
movl %r13d, %eax
lock
cmpxchgl %ecx, 0x10(%r12)
jne 0x13bef
jmp 0x13c9c
movl $0x20, %edi
callq 0x5320
vxorps %xmm0, %xmm0, %xmm0
movq $0x0, 0x18(%rax)
movq %rax, %rdi
movq %rax, %r13
movl $0x0, 0x10(%rax)
vmovups %xmm0, (%rax)
callq 0x13d88
movq $0x1000000, 0xc(%r13)   # imm = 0x1000000
movl $0x1000000, %edi        # imm = 0x1000000
callq 0x5030
movq %rax, 0x18(%r13)
movq %r12, %rcx
movq (%rcx), %r12
testq %r12, %r12
jne 0x13c75
movl 0x8(%rcx), %eax
incl %eax
movl %eax, 0x8(%r13)
xorl %eax, %eax
lock
cmpxchgq %r13, (%rcx)
jne 0x13c78
lock
incl 0x18(%r15)
jmp 0x13bef
addq 0x18(%r12), %r13
movq %r13, %rdi
movl %r14d, %esi
movq %rbx, %rdx
callq 0x13cf4
movq %r13, %rax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1ee42(%rip), %rdi     # 0x32b0a
leaq 0x1eb76(%rip), %rsi     # 0x32845
leaq 0x1ee4d(%rip), %rcx     # 0x32b23
movl $0x139, %edx            # imm = 0x139
callq 0x51d0
movq %r13, %rdi
movq %rax, %rbx
callq 0x52e0
movq %rbx, %rdi
callq 0x5670
nop