__gc new and __nogc new

I

Ian Lazarus

Hello,

I tried to confirm that two different heap allocation calls were being made,
i.e., one for __gc and one for __nogc. However, the addresses of the calls
(as seen in disassembly) are not what I expected. As you can see the four
calls in tmain are all to the same routine, while the one in the class
constructor is different. Does anybody know what's going on here?

Thanks

#include "stdafx.h"
#using <mscorlib.dll>
using namespace System;
__value class VClass
{
public:
VClass()
: p(new int(0)) // call F9BB56E3
{ }
int* p;
};

int _tmain()
{
int* p1 = new int(0); // call dword ptr ds:[00965A00h]
int __nogc* p2 = __nogc new int(0); // call dword ptr ds:[00965A00h]
Int32* p3 = new Int32(0); // call dword ptr ds:[00965A00h]
Int32 __gc* p4 = new Int32(0); // call dword ptr ds:[00965A00h]
VClass v;

return 0;
}
 
S

Stu Smith

Ian Lazarus said:
Hello,

I tried to confirm that two different heap allocation calls were being made,
i.e., one for __gc and one for __nogc. However, the addresses of the calls
(as seen in disassembly) are not what I expected. As you can see the four
calls in tmain are all to the same routine, while the one in the class
constructor is different. Does anybody know what's going on here?

This is a guess, and I'd be interested to hear a more definitive answer.

First I used Reflector to have a look at what's going on:

public static unsafe int modopt(CallConvCdecl) main()
{
VClass class1;
int num4 = <Module>.new(4);
int* numPtr2 = (num4 == 0) ? null : ((int*) num4);
int num3 = <Module>.new(4);
int* numPtr1 = (num3 == 0) ? null : ((int*) num3);
int num2 = <Module>.new(4);
ref int numRef2 = (num2 == 0) ? 0 : num2;
int num1 = <Module>.new(4);
ref int numRef1 = (num1 == 0) ? 0 : num1;
class1 = new VClass();
class1 = new VClass();
return 0;
}

[PreserveSig, MethodImpl(MethodImplOptions.Unmanaged,
MethodCodeType=MethodCodeType.Native), SuppressUnmanagedCodeSecurity]
public static unsafe void* modopt(CallConvCdecl) @new(uint);


[StructLayout(LayoutKind.Sequential)]
internal struct VClass
{
public unsafe int* p;
public VClass();
}

public unsafe VClass()
{
int num2;
int num1 = <Module>.new(4);
if (num1 != 0)
{
num1[0] = 0;
num2 = num1;
}
else
{
num2 = 0;
}
this.p = (int*) num2;
}

I think therefore that because main() and new() have the same calling
convention (cdecl), the call can be made directly. However, when calling
from VClass (which presumably uses something different, stdcall maybe?), a
thunk has to be used to change between the calling conventions.

Stu
Thanks

#include "stdafx.h"
#using <mscorlib.dll>
using namespace System;
__value class VClass
{
public:
VClass()
: p(new int(0)) // call F9BB56E3
{ }
int* p;
};

int _tmain()
{
int* p1 = new int(0); // call dword ptr ds:[00965A00h]
int __nogc* p2 = __nogc new int(0); // call dword ptr ds:[00965A00h]
Int32* p3 = new Int32(0); // call dword ptr ds:[00965A00h]
Int32 __gc* p4 = new Int32(0); // call dword ptr ds:[00965A00h]
VClass v;

return 0;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top