// File: /workspace/github_cpp_makefile_star2_O2/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 $0x238, %rsp            # imm = 0x238
movl $0xc80, %edi            # imm = 0xC80
callq 0x10f0
movq %rax, %rbx
leaq 0x20(%rax), %r12
addq $0x10, %rax
movq %rax, (%rbx)
andq $0x0, 0x8(%rbx)
movb $0x0, 0x10(%rbx)
movq 0x2ce0(%rip), %r14      # 0x3fd8
movq %r14, %rdi
movq %rbx, %rsi
callq 0x10d0
movq %rax, %rdi
callq 0x1080
leaq 0x40(%rbx), %r15
leaq 0x30(%rbx), %rax
movq %rax, 0x20(%rbx)
pushq $0xa
popq %rsi
pushq $0x63
popq %rdx
movq %r12, %rdi
callq 0x1110
movq %r14, %rdi
movq %r12, %rsi
callq 0x10d0
movq %rax, %rdi
callq 0x1080
leaq 0xcc5(%rip), %rdx       # 0x2004
leaq -0x29(%rbp), %rdi
movq %r15, %rsi
callq 0x15d0
movq %r14, %rdi
movq %r15, %rsi
callq 0x10d0
movq %rax, %rdi
callq 0x1080
movq %r14, %rdi
movq %rbx, %rsi
callq 0x10d0
movq %rax, %rdi
callq 0x1080
pushq $0x40
popq %r14
cmpq $-0x20, %r14
je 0x138a
leaq (%rbx,%r14), %rdi
callq 0x1070
addq $-0x20, %r14
jmp 0x1375
movl $0xc80, %esi            # imm = 0xC80
movq %rbx, %rdi
callq 0x1100
movl $0xc80, %edi            # imm = 0xC80
callq 0x10f0
movq %rax, %rbx
leaq -0x40(%rbp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb $0x0, (%rax)
leaq 0xc4c(%rip), %rsi       # 0x2007
leaq -0x258(%rbp), %rdi
pushq $0x8
popq %rdx
callq 0x1160
pushq $-0x20
popq %r13
xorl %r14d, %r14d
leaq -0x258(%rbp), %r15
leaq -0x50(%rbp), %r12
movq %r15, %rdi
movq %r12, %rsi
callq 0x1120
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
jne 0x1417
cmpq $0xc60, %r13            # imm = 0xC60
je 0x1417
leaq (%rbx,%r13), %rdi
addq $0x20, %rdi
addq $0x20, %r13
incq %r14
movq %r12, %rsi
callq 0x1130
jmp 0x13dc
movq 0x2bba(%rip), %rdi      # 0x3fd8
leaq 0xbf5(%rip), %rsi       # 0x201a
callq 0x10e0
movq %rax, %rdi
movq %r14, %rsi
callq 0x10b0
leaq 0xbe4(%rip), %rsi       # 0x2020
movq %rax, %rdi
callq 0x10e0
movq %rax, %rdi
callq 0x1080
jmp 0x145b
leaq (%rbx,%r13), %rdi
callq 0x1070
addq $-0x20, %r13
testq %r13, %r13
jne 0x144e
movl $0xc80, %esi            # imm = 0xC80
movq %rbx, %rdi
callq 0x1100
leaq -0x258(%rbp), %rdi
callq 0x1040
leaq 0xb87(%rip), %rsi       # 0x2007
leaq -0x258(%rbp), %rdi
pushq $0x8
popq %rdx
callq 0x10a0
movl $0xc88, %edi            # imm = 0xC88
callq 0x1030
movq %rax, %rbx
movq $0x64, (%rax)
pushq $0x18
popq %rax
leaq (%rbx,%rax), %rcx
movq %rcx, -0x10(%rbx,%rax)
andq $0x0, -0x8(%rbx,%rax)
movb $0x0, (%rbx,%rax)
addq $0x20, %rax
cmpq $0xc98, %rax            # imm = 0xC98
jne 0x14a6
leaq 0x8(%rbx), %r13
xorl %r14d, %r14d
leaq -0x50(%rbp), %r12
leaq -0x258(%rbp), %rdi
movq %r12, %rsi
callq 0x1120
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
jne 0x150e
cmpq $-0xc80, %r14           # imm = 0xF380
je 0x150e
addq $-0x20, %r14
leaq 0x20(%r13), %r15
movq %r13, %rdi
movq %r12, %rsi
callq 0x1090
movq %r15, %r13
jmp 0x14d0
movq 0x2ac3(%rip), %rdi      # 0x3fd8
leaq 0xafe(%rip), %rsi       # 0x201a
callq 0x10e0
negq %r14
sarq $0x5, %r14
movq %rax, %rdi
movq %r14, %rsi
callq 0x10b0
leaq 0xae6(%rip), %rsi       # 0x2020
movq %rax, %rdi
callq 0x10e0
movq %rax, %rdi
callq 0x1080
movq (%rbx), %rax
movq %rax, %r15
shlq $0x5, %r15
leaq 0x8(%r15), %r14
testq %rax, %rax
je 0x1573
movq %rbx, %r12
addq $-0x18, %r12
leaq (%r12,%r15), %rdi
callq 0x1070
addq $-0x20, %r15
jne 0x1564
movq %rbx, %rdi
movq %r14, %rsi
callq 0x1170
leaq -0x258(%rbp), %rdi
callq 0x1050
leaq -0x50(%rbp), %rdi
callq 0x1070
xorl %eax, %eax
addq $0x238, %rsp            # imm = 0x238
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
jmp 0x15bf
jmp 0x15b0
jmp 0x15b0
movq %rax, %rbx
leaq -0x258(%rbp), %rdi
callq 0x1050
leaq -0x50(%rbp), %rdi
callq 0x1070
movq %rbx, %rdi
callq 0x1150