// File: /workspace/github_cpp_makefile_star2_O2/MingYueRuYa[P]cprimeprimecode/memory_manager.cpp
// Function: demo03::test_array_new_and_placement_new()
void test_array_new_and_placement_new()
{
	cout << "\ntest_placement_new()............... \n";
	size_t size = 3;
{
//case 1
//模拟memory pool的做法，array new + placement new，崩溃
	A *buf  = (A *)(new char[sizeof(A)*size]);
	A *temp = buf;
	cout << "buf=" << buf << " temp=" << temp << endl;

	for (int i=0; i<size; ++i) {
		new(temp++) A(i);	//3次 copy ctor
	}
	//! delete [] buf; //crash . why?
					   //因为这其实个char array, 看到 delete [] buf; 编译器会企图唤起多次A::~A()
					   //但array memory layout中找到与array元素（本例3）相关的信息
					   //--整个格局都是错乱（从我对VC的认识而言），于是崩溃
					   //error message:*** Error in `./memory_manager': free(): invalid next size (fast): 0x01735040 ***
	delete buf;		   //dtor just one time3, ~[0]
}
{
//case 2
//测试单纯的array new
	A *buf = new A[size]; //default ctor 3 times, [0]->[1]->[2]
						  //A必须有default ctor，否则[Error] no matching function for call to 'demo02::A::A()'
	A *temp = buf;
	cout << "buf=" << buf << " temp=" << temp << endl;

	for (int i=0; i<size; ++i) {
		new(temp++) A(i);	//3次 ctor
	}
	cout << "buf=" << buf << " temp=" << temp << endl;
	delete [] buf;		 //dtor three times (次序逆反，[2]->[1]->[0])
}
{
//case 3
//掌握崩溃的原因，再次模拟memory pool做法，array new + placement new.
//不用做了，因为memory pool只是提供memory，它并不管construction，
//也不管destruction，它只负责回收memory.
//所以它是以void *或char *取得memory，释放的也是void * or char *.
//不像本例case 1 释放的是A*
//
//事实上 memory pool 形式如demo04::test
}
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq 0x4a0b(%rip), %r14      # 0x6fd0
leaq 0x1acc(%rip), %rsi      # 0x4098
movq %r14, %rdi
callq 0x2170
pushq $0xc
popq %rdi
callq 0x2030
movq %rax, %rbx
leaq 0x1ad9(%rip), %rsi      # 0x40bf
movq %r14, %rdi
callq 0x2170
movq %rax, %rdi
movq %rbx, %rsi
callq 0x2100
leaq 0x1ac4(%rip), %rsi      # 0x40c4
movq %rax, %rdi
callq 0x2170
movq %rax, %rdi
movq %rbx, %rsi
callq 0x2100
movq %rax, %rdi
callq 0x2090
xorl %r14d, %r14d
movq %rbx, %rdi
cmpl $0x3, %r14d
je 0x263b
leaq 0x4(%rdi), %r15
movl %r14d, %esi
callq 0x3396
incl %r14d
movq %r15, %rdi
jmp 0x2621
movq %rbx, %rdi
callq 0x33e6
pushq $0x4
popq %rsi
movq %rbx, %rdi
callq 0x2190
pushq $0x14
popq %rdi
callq 0x2030
movq %rax, %rbx
movq $0x3, (%rax)
leaq 0x8(%rax), %r14
xorl %r12d, %r12d
movq %r14, %r15
movq %r15, %rdi
callq 0x343e
addq $-0x4, %r12
addq $0x4, %r15
cmpq $-0xc, %r12
jne 0x266a
movq 0x4949(%rip), %rdi      # 0x6fd0
leaq 0x1a31(%rip), %rsi      # 0x40bf
callq 0x2170
movq %rax, %rdi
movq %r14, %rsi
callq 0x2100
leaq 0x1a1f(%rip), %rsi      # 0x40c4
movq %rax, %rdi
callq 0x2170
movq %rax, %rdi
movq %r14, %rsi
callq 0x2100
movq %rax, %rdi
callq 0x2090
xorl %r12d, %r12d
movq %r14, %r15
cmpl $0x3, %r12d
je 0x26e3
leaq 0x4(%r15), %r13
movq %r15, %rdi
movl %r12d, %esi
callq 0x3396
incl %r12d
movq %r13, %r15
jmp 0x26c6
movq 0x48e6(%rip), %rdi      # 0x6fd0
leaq 0x19ce(%rip), %rsi      # 0x40bf
callq 0x2170
movq %rax, %rdi
movq %r14, %rsi
callq 0x2100
leaq 0x19bc(%rip), %rsi      # 0x40c4
movq %rax, %rdi
callq 0x2170
movq %rax, %rdi
movq %r15, %rsi
callq 0x2100
movq %rax, %rdi
callq 0x2090
movq (%rbx), %rax
leaq (,%rax,4), %r14
testq %rax, %rax
je 0x274c
movq %rbx, %r15
addq $0x4, %r15
movq %r14, %r12
leaq (%r15,%r12), %rdi
callq 0x33e6
addq $-0x4, %r12
jne 0x273d
addq $0x8, %r14
movq %rbx, %rdi
movq %r14, %rsi
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2270
movq %rax, %r14
testq %r12, %r12
je 0x278a
negq %r12
movq %rbx, %r15
addq $0x4, %r15
leaq (%r15,%r12), %rdi
callq 0x33e6
addq $-0x4, %r12
jne 0x277b
pushq $0x14
popq %rsi
movq %rbx, %rdi
callq 0x2270
movq %r14, %rdi
callq 0x2250
nop