// File: MingYueRuYa[P]cprimeprimecode/memory_manager.cpp
// Function: demo07::test_overload_placement_new()
void test_overload_placement_new()
{
	cout << "\n\n\ntest_overload_placement_new()..........\n";

	Foo start; 							//Foo:Foo
	cout << "Foo start" << endl;

	Foo *p1 = new Foo;					//op-new(size_t)
	cout << "new Foo" << endl;

	Foo *p2 = new (&start)Foo;			//op-new(size_t, void *)
	cout << "new (&start)Foo" << endl;

	Foo *p3 = new (100)Foo;				//op-new(size_t, long)
	cout << "new (100)Foo" << endl;

  	Foo *p4 = new (100, 'a')Foo;		//op-new(size_t, long, char)
	cout << "new (100, 'a')Foo" << endl;

	Foo *p5 = new (100) Foo(1);			//op-new(size_t, long) op-del(void *,long)
	cout << "new (100) Foo(1)" << endl;

	Foo *p6 = new (100, 'a') Foo(2);
	cout << "new (100, 'a') Foo(2)" << endl;

	Foo *p7 = new (&start) Foo(1);
	cout << "new (&start) Foo(1)" << endl;

	Foo *p8 = new Foo(1);
	cout << "new Foo(1)" << endl;
	//result:
		// test_overload_placement_new()..........
		// Foo:Foo()
		// Foo start
		// operator new(size_t size), size=4
		// Foo:Foo()
		// new Foo
		// operator new(size_t size, void *start), size= 4 start=0x7e95e58c
		// Foo:Foo()
		// new (&start)Foo
		// operator new(size_t size, long extra) size=4 extra:100
		// Foo:Foo()
		// new (100)Foo
		// operator new(size_t size, long extra, char init) size=4 extra= 100 init=a
		// Foo:Foo()
		// new (100, 'a')Foo
		// operator new(size_t size, long extra) size=4 extra:100
		// Foo:Foo(int)
		// new (100) Foo(1)
		// operator new(size_t size, long extra, char init) size=4 extra= 100 init=a
		// Foo:Foo(int)
		// new (100, 'a') Foo(2)
		// operator new(size_t size, void *start), size= 4 start=0x7e95e58c
		// Foo:Foo(int)
		// new (&start) Foo(1)
		// operator new(size_t size), size=4
		// Foo:Foo(int)
		// new Foo(1)
		// [notice:在4.9上当ctor爆出异常时G4.9没有调用对应的operator delete(void *, long)]
		// [但在G2.9确实是可以的]
		// terminate called after throwing an instance of 'demo07::Bad'
		// Aborted
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xa0, %rsp
movq 0x5666(%rip), %rdi      # 0x8fd8
movq %rdi, -0x68(%rbp)
leaq 0x2872(%rip), %rsi      # 0x61ef
callq 0x2190
leaq -0x4(%rbp), %rdi
callq 0x47d0
movq -0x68(%rbp), %rdi
leaq 0x2885(%rip), %rsi      # 0x621b
callq 0x2190
movq %rax, %rdi
movq 0x5613(%rip), %rsi      # 0x8fb8
callq 0x21c0
movl $0x4, %edi
callq 0x4810
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0x60(%rbp)
callq 0x47d0
jmp 0x39c5
movq -0x60(%rbp), %rax
movq %rax, -0x10(%rbp)
movq 0x5604(%rip), %rdi      # 0x8fd8
leaq 0x284a(%rip), %rsi      # 0x6225
callq 0x2190
movq %rax, %rdi
movq 0x55ce(%rip), %rsi      # 0x8fb8
callq 0x21c0
movl $0x4, %edi
leaq -0x4(%rbp), %rsi
callq 0x48b0
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0x70(%rbp)
callq 0x47d0
jmp 0x3a0e
movq -0x70(%rbp), %rax
movq %rax, -0x28(%rbp)
movq 0x55bb(%rip), %rdi      # 0x8fd8
leaq 0x2809(%rip), %rsi      # 0x622d
callq 0x2190
movq %rax, %rdi
movq 0x5585(%rip), %rsi      # 0x8fb8
callq 0x21c0
movl $0x4, %edi
movl $0x64, %esi
callq 0x4970
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0x78(%rbp)
callq 0x47d0
jmp 0x3a58
movq -0x78(%rbp), %rax
movq %rax, -0x30(%rbp)
movq 0x5571(%rip), %rdi      # 0x8fd8
leaq 0x27cf(%rip), %rsi      # 0x623d
callq 0x2190
movq %rax, %rdi
movq 0x553b(%rip), %rsi      # 0x8fb8
callq 0x21c0
movl $0x4, %edi
movl $0x64, %esi
movl $0x61, %edx
callq 0x4a30
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0x80(%rbp)
callq 0x47d0
jmp 0x3aa7
movq -0x80(%rbp), %rax
movq %rax, -0x38(%rbp)
movq 0x5522(%rip), %rdi      # 0x8fd8
leaq 0x278d(%rip), %rsi      # 0x624a
callq 0x2190
movq %rax, %rdi
movq 0x54ec(%rip), %rsi      # 0x8fb8
callq 0x21c0
movl $0x4, %edi
movl $0x64, %esi
callq 0x4970
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0x88(%rbp)
movl $0x1, %esi
callq 0x4b10
jmp 0x3af9
movq -0x88(%rbp), %rax
movq %rax, -0x40(%rbp)
movq 0x54cd(%rip), %rdi      # 0x8fd8
leaq 0x274a(%rip), %rsi      # 0x625c
callq 0x2190
movq %rax, %rdi
movq 0x5497(%rip), %rsi      # 0x8fb8
callq 0x21c0
movl $0x4, %edi
movl $0x64, %esi
movl $0x61, %edx
callq 0x4a30
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0x90(%rbp)
movl $0x2, %esi
callq 0x4b10
jmp 0x3b53
movq -0x90(%rbp), %rax
movq %rax, -0x48(%rbp)
movq 0x5473(%rip), %rdi      # 0x8fd8
leaq 0x2701(%rip), %rsi      # 0x626d
callq 0x2190
movq %rax, %rdi
movq 0x543d(%rip), %rsi      # 0x8fb8
callq 0x21c0
movl $0x4, %edi
leaq -0x4(%rbp), %rsi
callq 0x48b0
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0x98(%rbp)
movl $0x1, %esi
callq 0x4b10
jmp 0x3ba7
movq -0x98(%rbp), %rax
movq %rax, -0x50(%rbp)
movq 0x541f(%rip), %rdi      # 0x8fd8
leaq 0x26c3(%rip), %rsi      # 0x6283
callq 0x2190
movq %rax, %rdi
movq 0x53e9(%rip), %rsi      # 0x8fb8
callq 0x21c0
movl $0x4, %edi
callq 0x4810
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0xa0(%rbp)
movl $0x1, %esi
callq 0x4b10
jmp 0x3bf7
movq -0xa0(%rbp), %rax
movq %rax, -0x58(%rbp)
movq 0x53cf(%rip), %rdi      # 0x8fd8
leaq 0x2687(%rip), %rsi      # 0x6297
callq 0x2190
movq %rax, %rdi
movq 0x5399(%rip), %rsi      # 0x8fb8
callq 0x21c0
addq $0xa0, %rsp
popq %rbp
retq
movq -0x60(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x18(%rbp)
movl %eax, -0x1c(%rbp)
movl $0x4, %esi
callq 0x4860
jmp 0x3d28
movq -0x70(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x18(%rbp)
movl %eax, -0x1c(%rbp)
leaq -0x4(%rbp), %rsi
callq 0x4920
jmp 0x3d28
movq -0x78(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x18(%rbp)
movl %eax, -0x1c(%rbp)
movl $0x64, %esi
callq 0x49e0
jmp 0x3d28
movq -0x80(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x18(%rbp)
movl %eax, -0x1c(%rbp)
movl $0x64, %esi
movl $0x61, %edx
callq 0x4ac0
jmp 0x3d28
movq -0x88(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x18(%rbp)
movl %eax, -0x1c(%rbp)
movl $0x64, %esi
callq 0x49e0
jmp 0x3d28
movq -0x90(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x18(%rbp)
movl %eax, -0x1c(%rbp)
movl $0x64, %esi
movl $0x61, %edx
callq 0x4ac0
jmp 0x3d28
movq -0x98(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x18(%rbp)
movl %eax, -0x1c(%rbp)
leaq -0x4(%rbp), %rsi
callq 0x4920
jmp 0x3d28
movq -0xa0(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x18(%rbp)
movl %eax, -0x1c(%rbp)
movl $0x4, %esi
callq 0x4860
movq -0x18(%rbp), %rdi
callq 0x2300
nopw %cs:(%rax,%rax)