// File: iZhangHui[P]CppPrimer/CH12_DynamicMemory/examples/usealloc.cc
// Function: main
int main() 
{
	const size_t n = 100;
	allocator<string> alloc;      // object that can allocate strings
	auto p = alloc.allocate(n);   // allocate n unconstructed strings
	
	auto q = p; // q will point to one past the last constructed element
	alloc.construct(q++);         // *q is the empty string
	cout << *(q-1) << endl;
	
	alloc.construct(q++, 10, 'c'); // *q is cccccccccc
	cout << *(q - 1) << endl;
	
	alloc.construct(q++, "hi");    // *q is hi!
	cout << *(q - 1) << endl;
	
	cout << *p << endl;  // ok: uses the string output operator
	while (q != p)
		alloc.destroy(--q);  // free the strings we actually allocated
	
	alloc.deallocate(p, n);  // return the memory we allocated
	
	p = alloc.allocate(n);   // allocate n unconstructed strings
	string s;
	q = p;                   // q points to the memory for first string
	ifstream in("data/storyDataFile");
	while (in >> s && q != p + n)
		alloc.construct(q++, s); // construct only as many strings as we need
	size_t size = q - p;         // remember how many strings we read

	// use the array

	cout << "read " << size << " strings" << endl;
	
	for (q = p + size - 1; q != p; --q)
		alloc.destroy(q);         // free the strings we allocated
	alloc.deallocate(p, n);       // return the memory we allocated

	in.close();
	in.open("data/storyDataFile");
	p = new string[n];            // construct n empty strings
	q = p;                        // q points to the first string
	while (in >> s && q != p + n)
		*q++ = s;                 // assign a new value to *q
	size = q - p;                 // remember how many strings we read

	cout << "read " << size << " strings" << endl;

	// use the array

	delete[] p;  // p points to an array; must remember to use delete[]
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x350, %rsp            # imm = 0x350
movl $0x0, -0x4(%rbp)
movq $0x64, -0x10(%rbp)
leaq -0x11(%rbp), %rdi
movq %rdi, -0x280(%rbp)
callq 0x2980
movq -0x280(%rbp), %rdi
xorl %eax, %eax
movl %eax, %edx
movl $0x64, %esi
callq 0x29a0
movq %rax, -0x278(%rbp)
jmp 0x2368
movq -0x278(%rbp), %rax
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rax
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rsi
movq %rsi, %rax
addq $0x20, %rax
movq %rax, -0x38(%rbp)
leaq -0x11(%rbp), %rdi
callq 0x2a00
movq -0x38(%rbp), %rsi
addq $-0x20, %rsi
movq 0x2c36(%rip), %rdi      # 0x4fd8
callq 0x20f0
movq %rax, -0x288(%rbp)
jmp 0x23b0
movq -0x288(%rbp), %rdi
movq 0x2c0a(%rip), %rsi      # 0x4fc8
callq 0x2130
jmp 0x23c5
movq -0x38(%rbp), %rsi
movq %rsi, %rax
addq $0x20, %rax
movq %rax, -0x38(%rbp)
movl $0xa, -0x3c(%rbp)
movb $0x63, -0x3d(%rbp)
leaq -0x11(%rbp), %rdi
leaq -0x3c(%rbp), %rdx
leaq -0x3d(%rbp), %rcx
callq 0x2a20
jmp 0x23f2
movq -0x38(%rbp), %rsi
addq $-0x20, %rsi
movq 0x2bd7(%rip), %rdi      # 0x4fd8
callq 0x20f0
movq %rax, -0x290(%rbp)
jmp 0x240f
movq -0x290(%rbp), %rdi
movq 0x2bab(%rip), %rsi      # 0x4fc8
callq 0x2130
jmp 0x2424
movq -0x38(%rbp), %rsi
movq %rsi, %rax
addq $0x20, %rax
movq %rax, -0x38(%rbp)
leaq 0xbca(%rip), %rdx       # 0x3004
leaq -0x11(%rbp), %rdi
callq 0x2ab0
jmp 0x2445
movq -0x38(%rbp), %rsi
addq $-0x20, %rsi
movq 0x2b84(%rip), %rdi      # 0x4fd8
callq 0x20f0
movq %rax, -0x298(%rbp)
jmp 0x2462
movq -0x298(%rbp), %rdi
movq 0x2b58(%rip), %rsi      # 0x4fc8
callq 0x2130
jmp 0x2477
movq -0x20(%rbp), %rsi
movq 0x2b56(%rip), %rdi      # 0x4fd8
callq 0x20f0
movq %rax, -0x2a0(%rbp)
jmp 0x2490
movq -0x2a0(%rbp), %rdi
movq 0x2b2a(%rip), %rsi      # 0x4fc8
callq 0x2130
jmp 0x24a5
jmp 0x24a7
movq -0x38(%rbp), %rax
cmpq -0x20(%rbp), %rax
je 0x24d9
movq -0x38(%rbp), %rsi
addq $-0x20, %rsi
movq %rsi, -0x38(%rbp)
leaq -0x11(%rbp), %rdi
callq 0x2b30
jmp 0x24a7
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x28(%rbp)
movl %eax, -0x2c(%rbp)
jmp 0x2961
movq -0x20(%rbp), %rsi
leaq -0x11(%rbp), %rdi
movl $0x64, %edx
callq 0x2b50
jmp 0x24ed
xorl %eax, %eax
movl %eax, %edx
leaq -0x11(%rbp), %rdi
movl $0x64, %esi
callq 0x29a0
movq %rax, -0x2a8(%rbp)
jmp 0x2508
movq -0x2a8(%rbp), %rax
movq %rax, -0x20(%rbp)
leaq -0x60(%rbp), %rdi
callq 0x2180
movq -0x20(%rbp), %rax
movq %rax, -0x38(%rbp)
leaq 0xadc(%rip), %rsi       # 0x3007
leaq -0x268(%rbp), %rdi
movl $0x8, %edx
callq 0x21c0
jmp 0x253e
jmp 0x2540
leaq -0x268(%rbp), %rdi
leaq -0x60(%rbp), %rsi
callq 0x2160
movq %rax, -0x2b0(%rbp)
jmp 0x2559
movq -0x2b0(%rbp), %rdi
movq (%rdi), %rax
movq -0x18(%rax), %rax
addq %rax, %rdi
callq 0x2150
movb %al, -0x2b1(%rbp)
jmp 0x2577
movb -0x2b1(%rbp), %cl
xorl %eax, %eax
testb $0x1, %cl
movb %al, -0x2b2(%rbp)
jne 0x258c
jmp 0x25a7
movq -0x38(%rbp), %rax
movq -0x20(%rbp), %rcx
addq $0xc80, %rcx            # imm = 0xC80
cmpq %rcx, %rax
setne %al
movb %al, -0x2b2(%rbp)
movb -0x2b2(%rbp), %al
testb $0x1, %al
jne 0x25b3
jmp 0x2604
movq -0x38(%rbp), %rsi
movq %rsi, %rax
addq $0x20, %rax
movq %rax, -0x38(%rbp)
leaq -0x11(%rbp), %rdi
leaq -0x60(%rbp), %rdx
callq 0x2b80
jmp 0x25d1
jmp 0x2540
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x28(%rbp)
movl %eax, -0x2c(%rbp)
jmp 0x2958
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x28(%rbp)
movl %eax, -0x2c(%rbp)
leaq -0x268(%rbp), %rdi
callq 0x2070
jmp 0x2958
movq -0x38(%rbp), %rax
movq -0x20(%rbp), %rcx
subq %rcx, %rax
sarq $0x5, %rax
movq %rax, -0x270(%rbp)
movq 0x29b7(%rip), %rdi      # 0x4fd8
leaq 0x9f2(%rip), %rsi       # 0x301a
callq 0x2100
movq %rax, -0x2c0(%rbp)
jmp 0x2636
movq -0x2c0(%rbp), %rdi
movq -0x270(%rbp), %rsi
callq 0x2060
movq %rax, -0x2c8(%rbp)
jmp 0x2652
movq -0x2c8(%rbp), %rdi
leaq 0x9c0(%rip), %rsi       # 0x3020
callq 0x2100
movq %rax, -0x2d0(%rbp)
jmp 0x266e
movq -0x2d0(%rbp), %rdi
movq 0x294c(%rip), %rsi      # 0x4fc8
callq 0x2130
jmp 0x2683
movq -0x20(%rbp), %rax
movq -0x270(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
addq $-0x20, %rax
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rax
cmpq -0x20(%rbp), %rax
je 0x26c2
movq -0x38(%rbp), %rsi
leaq -0x11(%rbp), %rdi
callq 0x2b30
movq -0x38(%rbp), %rax
addq $-0x20, %rax
movq %rax, -0x38(%rbp)
jmp 0x269d
movq -0x20(%rbp), %rsi
leaq -0x11(%rbp), %rdi
movl $0x64, %edx
callq 0x2b50
jmp 0x26d6
leaq -0x268(%rbp), %rdi
callq 0x2050
jmp 0x26e4
leaq 0x91c(%rip), %rsi       # 0x3007
leaq -0x268(%rbp), %rdi
movl $0x8, %edx
callq 0x20b0
jmp 0x26fe
movl $0xc88, %edi            # imm = 0xC88
callq 0x2030
movq %rax, -0x2d8(%rbp)
jmp 0x2711
movq -0x2d8(%rbp), %rax
movq $0x64, (%rax)
addq $0x8, %rax
movq %rax, -0x2f0(%rbp)
movq %rax, %rcx
addq $0xc80, %rcx            # imm = 0xC80
movq %rcx, -0x2e8(%rbp)
movq %rax, -0x2e0(%rbp)
movq -0x2e0(%rbp), %rdi
movq %rdi, -0x2f8(%rbp)
callq 0x2180
movq -0x2f8(%rbp), %rax
movq -0x2e8(%rbp), %rcx
addq $0x20, %rax
cmpq %rcx, %rax
movq %rax, -0x2e0(%rbp)
jne 0x2742
movq -0x2f0(%rbp), %rax
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rax
movq %rax, -0x38(%rbp)
leaq -0x268(%rbp), %rdi
leaq -0x60(%rbp), %rsi
callq 0x2160
movq %rax, -0x300(%rbp)
jmp 0x279f
movq -0x300(%rbp), %rdi
movq (%rdi), %rax
movq -0x18(%rax), %rax
addq %rax, %rdi
callq 0x2150
movb %al, -0x301(%rbp)
jmp 0x27bd
movb -0x301(%rbp), %cl
xorl %eax, %eax
testb $0x1, %cl
movb %al, -0x302(%rbp)
jne 0x27d2
jmp 0x27ed
movq -0x38(%rbp), %rax
movq -0x20(%rbp), %rcx
addq $0xc80, %rcx            # imm = 0xC80
cmpq %rcx, %rax
setne %al
movb %al, -0x302(%rbp)
movb -0x302(%rbp), %al
testb $0x1, %al
jne 0x27f9
jmp 0x2818
movq -0x38(%rbp), %rdi
movq %rdi, %rax
addq $0x20, %rax
movq %rax, -0x38(%rbp)
leaq -0x60(%rbp), %rsi
callq 0x2040
jmp 0x2813
jmp 0x2786
movq -0x38(%rbp), %rax
movq -0x20(%rbp), %rcx
subq %rcx, %rax
sarq $0x5, %rax
movq %rax, -0x270(%rbp)
movq 0x27a3(%rip), %rdi      # 0x4fd8
leaq 0x7de(%rip), %rsi       # 0x301a
callq 0x2100
movq %rax, -0x310(%rbp)
jmp 0x284a
movq -0x310(%rbp), %rdi
movq -0x270(%rbp), %rsi
callq 0x2060
movq %rax, -0x318(%rbp)
jmp 0x2866
movq -0x318(%rbp), %rdi
leaq 0x7ac(%rip), %rsi       # 0x3020
callq 0x2100
movq %rax, -0x320(%rbp)
jmp 0x2882
movq -0x320(%rbp), %rdi
movq 0x2738(%rip), %rsi      # 0x4fc8
callq 0x2130
jmp 0x2897
movq -0x20(%rbp), %rax
movq %rax, -0x328(%rbp)
cmpq $0x0, %rax
je 0x292e
movq -0x328(%rbp), %rcx
movq %rcx, %rax
addq $-0x8, %rax
movq %rax, -0x340(%rbp)
movq -0x8(%rcx), %rdx
movq %rdx, -0x338(%rbp)
shlq $0x5, %rdx
movq %rcx, %rax
addq %rdx, %rax
cmpq %rax, %rcx
movq %rax, -0x330(%rbp)
je 0x2913
movq -0x330(%rbp), %rdi
addq $-0x20, %rdi
movq %rdi, -0x348(%rbp)
callq 0x20a0
movq -0x328(%rbp), %rcx
movq -0x348(%rbp), %rax
cmpq %rcx, %rax
movq %rax, -0x330(%rbp)
jne 0x28e2
movq -0x340(%rbp), %rdi
movq -0x338(%rbp), %rsi
shlq $0x5, %rsi
addq $0x8, %rsi
callq 0x21d0
leaq -0x268(%rbp), %rdi
callq 0x2070
leaq -0x60(%rbp), %rdi
callq 0x20a0
leaq -0x11(%rbp), %rdi
callq 0x2bb0
movl -0x4(%rbp), %eax
addq $0x350, %rsp            # imm = 0x350
popq %rbp
retq
leaq -0x60(%rbp), %rdi
callq 0x20a0
leaq -0x11(%rbp), %rdi
callq 0x2bb0
movq -0x28(%rbp), %rdi
callq 0x21a0
nopw %cs:(%rax,%rax)
nopl (%rax)