// File: /workspace/github_cpp_makefile_star2_O2/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 0xf333
imull $0x30, %r14d, %ebx
movq %rdi, %r15
movq %rdx, %r12
movl %r14d, %esi
addl $0x40, %ebx
lock
addq %rbx, 0x13b8(%rdi)
addq $0x20, %rdi
callq 0xf366
testq %rax, %rax
je 0xf289
cmpl $0x4, (%rax)
movq %rax, %rbp
ja 0xf26a
movzwl 0x4(%rbp), %eax
cmpl %r14d, %eax
je 0xf313
leaq 0x14787(%rip), %rdi     # 0x239f8
leaq 0x14429(%rip), %rsi     # 0x236a1
leaq 0x14700(%rip), %rcx     # 0x2397f
movl $0x140, %edx            # imm = 0x140
callq 0x51f0
movq %r12, (%rsp)
pushq $0x20
popq %r12
movq 0x8(%r15), %r13
movl 0x10(%r13), %ebp
leal (%rbp,%rbx), %ecx
cmpl 0xc(%r13), %ecx
jbe 0xf2c4
movq (%r13), %rcx
testq %rcx, %rcx
je 0xf2d0
movq %r13, %rax
lock
cmpxchgq %rcx, 0x8(%r15)
movq %r13, %rax
movq (%r13), %rcx
lock
cmpxchgq %rcx, 0x10(%r15)
jmp 0xf291
movl %ebp, %eax
lock
cmpxchgl %ecx, 0x10(%r13)
jne 0xf291
jmp 0xf30b
movq %r12, %rdi
callq 0x5340
movq %rax, %rbp
movl $0x1000000, %esi        # imm = 0x1000000
movq %rax, %rdi
callq 0xf40c
movq %r13, %rcx
movq (%rcx), %r13
testq %r13, %r13
jne 0xf2e8
movl 0x8(%rcx), %eax
incl %eax
movl %eax, 0x8(%rbp)
xorl %eax, %eax
lock
cmpxchgq %rbp, (%rcx)
jne 0xf2eb
lock
incl 0x18(%r15)
jmp 0xf291
addq 0x18(%r13), %rbp
movq (%rsp), %r12
movq %rbp, %rdi
movl %r14d, %esi
movq %r12, %rdx
callq 0xf39c
movq %rbp, %rax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1462c(%rip), %rdi     # 0x23966
leaq 0x14360(%rip), %rsi     # 0x236a1
leaq 0x14637(%rip), %rcx     # 0x2397f
movl $0x139, %edx            # imm = 0x139
callq 0x51f0
movq %rbp, %rdi
movq %rax, %rbx
callq 0x52f0
movq %rbx, %rdi
callq 0x5650
nop