// File: /workspace/github_cpp_makefile_star2_O1/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x228, %rsp            # imm = 0x228
movl $0xc80, %edi            # imm = 0xC80
callq 0x20f0
movq %rax, %rbx
leaq 0x20(%rax), %r12
leaq 0x10(%rax), %rsi
movq %rsi, (%rax)
movq $0x0, 0x8(%rax)
movb $0x0, 0x10(%rax)
movq 0x2ccd(%rip), %r14      # 0x4fd8
movq %r14, %rdi
xorl %edx, %edx
callq 0x2110
movq %rax, %r15
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r15, %rdi
movl $0xa, %esi
callq 0x20e0
movsbl %al, %esi
movq %r15, %rdi
callq 0x2040
movq %rax, %rdi
callq 0x20a0
leaq 0x40(%rbx), %r15
leaq 0x30(%rbx), %rax
movq %rax, 0x20(%rbx)
movl $0xa, %esi
movq %r12, %rdi
movl $0x63, %edx
callq 0x2120
movq 0x20(%rbx), %rsi
movq 0x28(%rbx), %rdx
movq %r14, %rdi
callq 0x2110
movq %rax, %r12
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r12, %rdi
movl $0xa, %esi
callq 0x20e0
movsbl %al, %esi
movq %r12, %rdi
callq 0x2040
movq %rax, %rdi
callq 0x20a0
leaq 0x50(%rbx), %r12
movq %r12, 0x40(%rbx)
leaq 0xc5e(%rip), %rsi       # 0x3004
leaq 0xc59(%rip), %rdx       # 0x3006
movq %r15, %rdi
callq 0x275a
movq 0x40(%rbx), %rsi
movq 0x48(%rbx), %rdx
movq %r14, %rdi
callq 0x2110
movq %rax, %r15
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r15, %rdi
movl $0xa, %esi
callq 0x20e0
movsbl %al, %esi
movq %r15, %rdi
callq 0x2040
movq %rax, %rdi
callq 0x20a0
movq (%rbx), %rsi
movq 0x8(%rbx), %rdx
movq %r14, %rdi
callq 0x2110
movq %rax, %r14
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x20e0
movsbl %al, %esi
movq %r14, %rdi
callq 0x2040
movq %rax, %rdi
callq 0x20a0
movq $-0x60, %r14
movq -0x10(%r12), %rdi
cmpq %rdi, %r12
je 0x2445
movq (%r12), %rsi
incq %rsi
callq 0x2100
addq $-0x20, %r12
addq $0x20, %r14
jne 0x242f
movl $0xc80, %esi            # imm = 0xC80
movq %rbx, %rdi
callq 0x2100
movl $0xc80, %edi            # imm = 0xC80
callq 0x20f0
movq %rax, %rbx
leaq -0x38(%rbp), %rax
movq %rax, -0x10(%rax)
movq $0x0, -0x8(%rax)
movb $0x0, (%rax)
leaq 0xb84(%rip), %rsi       # 0x3007
leaq -0x250(%rbp), %rdi
movl $0x8, %edx
callq 0x2170
xorl %r14d, %r14d
leaq -0x48(%rbp), %r12
xorl %r13d, %r13d
leaq -0x250(%rbp), %rdi
movq %r12, %rsi
callq 0x2130
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
jne 0x24f0
cmpq $0xc80, %r13            # imm = 0xC80
je 0x24f0
leaq (%rbx,%r13), %rdi
leaq 0x20(%r13), %r15
leaq (%rbx,%r13), %rax
addq $0x10, %rax
movq %rax, (%rbx,%r13)
movq -0x48(%rbp), %rsi
movq -0x40(%rbp), %rdx
addq %rsi, %rdx
incq %r14
callq 0x27f4
movq %r15, %r13
jmp 0x249e
movq 0x2ae1(%rip), %rdi      # 0x4fd8
leaq 0xb1c(%rip), %rsi       # 0x301a
movl $0x5, %edx
callq 0x2110
movq 0x2ac9(%rip), %rdi      # 0x4fd8
movq %r14, %rsi
callq 0x2090
movq %rax, %r14
leaq 0xaff(%rip), %rsi       # 0x3020
movl $0x8, %edx
movq %rax, %rdi
callq 0x2110
movq (%r14), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x20e0
movsbl %al, %esi
movq %r14, %rdi
callq 0x2040
movq %rax, %rdi
callq 0x20a0
cmpq $0x20, %r13
je 0x2582
addq $-0x10, %r13
leaq (%rbx,%r13), %rax
movq -0x10(%rbx,%r13), %rdi
cmpq %rdi, %rax
je 0x2578
movq (%rax), %rsi
incq %rsi
callq 0x2100
addq $-0x20, %r13
cmpq $0x10, %r13
jne 0x255f
movl $0xc80, %esi            # imm = 0xC80
movq %rbx, %rdi
callq 0x2100
leaq -0x250(%rbp), %rdi
callq 0x2050
leaq 0xa65(%rip), %rsi       # 0x3007
leaq -0x250(%rbp), %rdi
movl $0x8, %edx
callq 0x2080
movl $0xc88, %edi            # imm = 0xC88
callq 0x2030
movq %rax, %rbx
movq $0x64, (%rax)
movl $0x18, %eax
leaq (%rbx,%rax), %rcx
movq %rcx, -0x10(%rbx,%rax)
movq $0x0, -0x8(%rbx,%rax)
movb $0x0, (%rbx,%rax)
addq $0x20, %rax
cmpq $0xc98, %rax            # imm = 0xC98
jne 0x25cc
leaq 0x8(%rbx), %r13
xorl %r14d, %r14d
leaq -0x48(%rbp), %r12
leaq -0x250(%rbp), %rdi
movq %r12, %rsi
callq 0x2130
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
jne 0x2637
cmpq $-0xc80, %r14           # imm = 0xF380
je 0x2637
addq $-0x20, %r14
leaq 0x20(%r13), %r15
movq %r13, %rdi
movq %r12, %rsi
callq 0x2070
movq %r15, %r13
jmp 0x25f9
movq 0x299a(%rip), %rdi      # 0x4fd8
leaq 0x9d5(%rip), %rsi       # 0x301a
movl $0x5, %edx
callq 0x2110
negq %r14
sarq $0x5, %r14
movq 0x297b(%rip), %rdi      # 0x4fd8
movq %r14, %rsi
callq 0x2090
movq %rax, %r14
leaq 0x9b1(%rip), %rsi       # 0x3020
movl $0x8, %edx
movq %rax, %rdi
callq 0x2110
movq (%r14), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x20e0
movsbl %al, %esi
movq %r14, %rdi
callq 0x2040
movq %rax, %rdi
callq 0x20a0
movq (%rbx), %rax
movq %rax, %r15
shlq $0x5, %r15
leaq 0x8(%r15), %r14
testq %rax, %rax
je 0x26dc
movq %rbx, %r12
addq $-0x8, %r12
leaq (%r12,%r15), %rax
movq -0x10(%r12,%r15), %rdi
cmpq %rdi, %rax
je 0x26d6
movq (%rax), %rsi
incq %rsi
callq 0x2100
addq $-0x20, %r15
jne 0x26bd
movq %rbx, %rdi
movq %r14, %rsi
callq 0x2180
leaq -0x250(%rbp), %rdi
callq 0x2060
movq -0x48(%rbp), %rdi
leaq -0x38(%rbp), %rax
cmpq %rax, %rdi
je 0x270c
movq -0x38(%rbp), %rsi
incq %rsi
callq 0x2100
xorl %eax, %eax
addq $0x228, %rsp            # imm = 0x228
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
jmp 0x2738
jmp 0x2729
jmp 0x2729
movq %rax, %rbx
leaq -0x250(%rbp), %rdi
callq 0x2060
movq -0x48(%rbp), %rdi
leaq -0x38(%rbp), %rax
cmpq %rax, %rdi
je 0x2751
movq -0x38(%rbp), %rsi
incq %rsi
callq 0x2100
movq %rbx, %rdi
callq 0x2150
nop