// File: /workspace/github_cpp_makefile_star2_O1/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 %r12
pushq %rbx
movq 0x58bb(%rip), %r14      # 0x7fd0
leaq 0x297c(%rip), %rsi      # 0x5098
movl $0x26, %edx
movq %r14, %rdi
callq 0x21a0
movl $0xc, %edi
callq 0x2030
movq %rax, %rbx
leaq 0x2982(%rip), %rsi      # 0x50bf
movl $0x4, %edx
movq %r14, %rdi
callq 0x21a0
movq %r14, %rdi
movq %rbx, %rsi
callq 0x2100
movq %rax, %r14
leaq 0x2965(%rip), %rsi      # 0x50c4
movl $0x6, %edx
movq %rax, %rdi
callq 0x21a0
movq %r14, %rdi
movq %rbx, %rsi
callq 0x2100
movq %rax, %r14
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x2160
movsbl %al, %esi
movq %r14, %rdi
callq 0x2040
movq %rax, %rdi
callq 0x20d0
xorl %r14d, %r14d
movq %rbx, %rdi
leaq 0x4(%rdi), %r15
movl %r14d, %esi
callq 0x3a3c
incl %r14d
movq %r15, %rdi
cmpl $0x3, %r14d
jne 0x27a7
movq %rbx, %rdi
callq 0x3abe
movl $0x4, %esi
movq %rbx, %rdi
callq 0x2190
movl $0x14, %edi
callq 0x2030
movq %rax, %rbx
movq $0x3, (%rax)
leaq 0x8(%rax), %r14
xorl %r12d, %r12d
movq %r14, %r15
movq %r15, %rdi
callq 0x3b48
addq $-0x4, %r12
addq $0x4, %r15
cmpq $-0xc, %r12
jne 0x27f2
movq 0x57c1(%rip), %r15      # 0x7fd0
leaq 0x28a9(%rip), %rsi      # 0x50bf
movl $0x4, %edx
movq %r15, %rdi
callq 0x21a0
movq %r15, %rdi
movq %r14, %rsi
callq 0x2100
movq %rax, %r15
leaq 0x288c(%rip), %rsi      # 0x50c4
movl $0x6, %edx
movq %rax, %rdi
callq 0x21a0
movq %r15, %rdi
movq %r14, %rsi
callq 0x2100
movq %rax, %r15
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r15, %rdi
movl $0xa, %esi
callq 0x2160
movsbl %al, %esi
movq %r15, %rdi
callq 0x2040
movq %rax, %rdi
callq 0x20d0
xorl %r12d, %r12d
movq %r14, %r15
movq %r15, %rdi
addq $0x4, %r15
movl %r12d, %esi
callq 0x3a3c
incl %r12d
cmpl $0x3, %r12d
jne 0x2880
movq 0x5731(%rip), %r12      # 0x7fd0
leaq 0x2819(%rip), %rsi      # 0x50bf
movl $0x4, %edx
movq %r12, %rdi
callq 0x21a0
movq %r12, %rdi
movq %r14, %rsi
callq 0x2100
movq %rax, %r14
leaq 0x27fc(%rip), %rsi      # 0x50c4
movl $0x6, %edx
movq %rax, %rdi
callq 0x21a0
movq %r14, %rdi
movq %r15, %rsi
callq 0x2100
movq %rax, %r14
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x2160
movsbl %al, %esi
movq %r14, %rdi
callq 0x2040
movq %rax, %rdi
callq 0x20d0
movq (%rbx), %rax
leaq (,%rax,4), %r14
testq %rax, %rax
je 0x2933
movq %rbx, %r15
addq $0x4, %r15
movq %r14, %r12
leaq (%r15,%r12), %rdi
callq 0x3abe
addq $-0x4, %r12
jne 0x2924
addq $0x8, %r14
movq %rbx, %rdi
movq %r14, %rsi
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
jmp 0x2270
movq %rax, %r14
testq %r12, %r12
je 0x296b
negq %r12
movq %rbx, %r15
addq $0x4, %r15
leaq (%r15,%r12), %rdi
callq 0x3abe
addq $-0x4, %r12
jne 0x295c
movl $0x14, %esi
movq %rbx, %rdi
callq 0x2270
movq %r14, %rdi
callq 0x2250