473,385 Members | 1,908 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Mixed mode '__pin' internals: does '__pin' does anything at all?

Hi,

I am writing a mixed mode application, I have a mixed mode Assembly manipulating a managed byte[] array, to access this array in unmanaged code I '__pin' the array, As I understand, pining an object guarantee that it will not be collected by the GC ( by increasing it's refcount or so ), Taking that in mind, looking at the code generated by the compiler I can't see anything taking care of the GC refcount... following is the pinned variable with the corresponding dis-assembly section:

****************** Mixed mode C++ code line ******************
BYTE __pin *pbtArray = &btArray[0];

******************** The dis-assembly ***********************
00000019 cmp dword ptr [esi+4],0 // makes sure that NULL != &btArray[0]
0000001d ja 00000026 // if it is not NULL go three lines bellow
0000001f xor ecx,ecx
00000021 call 7227F90B // call some exception handling procedure...
00000026 lea eax,[esi+8] // get the address of &btArray[0] to eax
00000029 mov dword ptr [ebp-14h],eax // pbtArray = content of eax
************************************************** ******
Why there is no GC manipulation code? isn't it needed?

The original code:
************************************************** ******
virtual int NotArray(System::Byte btArray __gc[])
{
int i = 0;
BYTE __pin *pbtArray = &btArray[0];
for(i; i < btArray->Length; i++)
pbtArray[i] <<= 1;
return 0;
}

Nadav
http://www.ddevel.com
Nov 16 '05 #1
3 1779
The garbage collection is done by the runtime, not the application, therefor
there is no GC code in your application code.
If Microsoft embedded GC manipulation code in the apllication code, it would
be impossible to change GC policy in the future.

Pinning an object prevents the GC from moving the reference in memory, it
has nothing to do with reference counting. The GC periodically moves things
around so that the memory used is located together in memory.

Chris

"Nadav" <Na***@discussions.microsoft.com> wrote in message
news:DE**********************************@microsof t.com...
Hi,

I am writing a mixed mode application, I have a mixed mode Assembly manipulating a managed byte[] array, to access this array in unmanaged code
I '__pin' the array, As I understand, pining an object guarantee that it
will not be collected by the GC ( by increasing it's refcount or so ),
Taking that in mind, looking at the code generated by the compiler I can't
see anything taking care of the GC refcount... following is the pinned
variable with the corresponding dis-assembly section:
****************** Mixed mode C++ code line ******************
BYTE __pin *pbtArray = &btArray[0];

******************** The dis-assembly ***********************
00000019 cmp dword ptr [esi+4],0 // makes sure that NULL != &btArray[0] 0000001d ja 00000026 // if it is not NULL go three lines bellow 0000001f xor ecx,ecx
00000021 call 7227F90B // call some exception handling procedure... 00000026 lea eax,[esi+8] // get the address of &btArray[0] to eax 00000029 mov dword ptr [ebp-14h],eax // pbtArray = content of eax
************************************************** ******
Why there is no GC manipulation code? isn't it needed?

The original code:
************************************************** ******
virtual int NotArray(System::Byte btArray __gc[])
{
int i = 0;
BYTE __pin *pbtArray = &btArray[0];
for(i; i < btArray->Length; i++)
pbtArray[i] <<= 1;
return 0;
}

Nadav
http://www.ddevel.com

Nov 16 '05 #2
Thanks for your immediate response, concerning the '__pin' keyword causes
the GC to prevent moving the buffer in memory ( and not manipulate any
refcount ), how does it do that? there must be some assembler code to
instruct the GC that this is a '__pined' variable so it would know not to
move it, looking at the assembler code produced by the compiler (provided at
the root query) I can't see anything as such... It seems as the '__pin'
keyword has no effect on the assembler code being generated by the compiler,
why is that? how can it be guaranteed that the memory pointed to by
'pbtArray' will stay valid for the duration it is being used...

(*)Another thing: Taking in mind what you have said concerning the way GC is
operating, Is it possible to prevent the GC from moving memory in the first
place? e.g. lets say I have a performance critical application, I allocate
(apriory) a collection of buffers to be used by the App, those buffers
should be moved as less as possible during runtime, how can I achieve that
using the .NET Framework????

Nadav.
"Christopher Kimbell" <a@b.c> wrote in message
news:40********@news.broadpark.no...
The garbage collection is done by the runtime, not the application, therefor there is no GC code in your application code.
If Microsoft embedded GC manipulation code in the apllication code, it would be impossible to change GC policy in the future.

Pinning an object prevents the GC from moving the reference in memory, it
has nothing to do with reference counting. The GC periodically moves things around so that the memory used is located together in memory.

Chris

"Nadav" <Na***@discussions.microsoft.com> wrote in message
news:DE**********************************@microsof t.com...
Hi,

I am writing a mixed mode application, I have a mixed mode Assembly manipulating a managed byte[] array, to access this array in unmanaged

code I '__pin' the array, As I understand, pining an object guarantee that it
will not be collected by the GC ( by increasing it's refcount or so ),
Taking that in mind, looking at the code generated by the compiler I can't
see anything taking care of the GC refcount... following is the pinned
variable with the corresponding dis-assembly section:

****************** Mixed mode C++ code line ******************
BYTE __pin *pbtArray = &btArray[0];

******************** The dis-assembly ***********************
00000019 cmp dword ptr [esi+4],0 // makes sure that NULL != &btArray[0]
0000001d ja 00000026 // if it is not NULL go

three lines bellow
0000001f xor ecx,ecx
00000021 call 7227F90B // call some exception

handling procedure...
00000026 lea eax,[esi+8] // get the address of

&btArray[0] to eax
00000029 mov dword ptr [ebp-14h],eax // pbtArray = content of eax
************************************************** ******
Why there is no GC manipulation code? isn't it needed?

The original code:
************************************************** ******
virtual int NotArray(System::Byte btArray __gc[])
{
int i = 0;
BYTE __pin *pbtArray = &btArray[0];
for(i; i < btArray->Length; i++)
pbtArray[i] <<= 1;
return 0;
}

Nadav
http://www.ddevel.com


Nov 16 '05 #3
Remember that the code is JIT compiled, the compiler may generate different
code for pinned references than unpinned references.
If performance is that critical, I would consider using something else than
..NET. .NET provides a level of abstraction that comes with a cost. Just out
of interest, if you wrote this application in native C++, how would you
prevent Windows from moving the memory containing the buffers to the swap
file?

If you allocate the objects at the start of program execution, they would be
located at the start of the managed heap. Objects are allocated continuously
from the start of the managed heap, as objects become disposed the GC will
compact the heap to reduce the number of actual memory pages used. This
still wouldn't prevent Windows from paging all the memory to the swap file.

Chris

"Nadav" <not@today> wrote in message
news:ei**************@TK2MSFTNGP12.phx.gbl...
Thanks for your immediate response, concerning the '__pin' keyword causes
the GC to prevent moving the buffer in memory ( and not manipulate any
refcount ), how does it do that? there must be some assembler code to
instruct the GC that this is a '__pined' variable so it would know not to
move it, looking at the assembler code produced by the compiler (provided at the root query) I can't see anything as such... It seems as the '__pin'
keyword has no effect on the assembler code being generated by the compiler, why is that? how can it be guaranteed that the memory pointed to by
'pbtArray' will stay valid for the duration it is being used...

(*)Another thing: Taking in mind what you have said concerning the way GC is operating, Is it possible to prevent the GC from moving memory in the first place? e.g. lets say I have a performance critical application, I allocate
(apriory) a collection of buffers to be used by the App, those buffers
should be moved as less as possible during runtime, how can I achieve that
using the .NET Framework????

Nadav.
"Christopher Kimbell" <a@b.c> wrote in message
news:40********@news.broadpark.no...
The garbage collection is done by the runtime, not the application,

therefor
there is no GC code in your application code.
If Microsoft embedded GC manipulation code in the apllication code, it

would
be impossible to change GC policy in the future.

Pinning an object prevents the GC from moving the reference in memory, it has nothing to do with reference counting. The GC periodically moves

things
around so that the memory used is located together in memory.

Chris

"Nadav" <Na***@discussions.microsoft.com> wrote in message
news:DE**********************************@microsof t.com...
Hi,

I am writing a mixed mode application, I have a mixed mode Assembly

manipulating a managed byte[] array, to access this array in unmanaged

code
I '__pin' the array, As I understand, pining an object guarantee that it
will not be collected by the GC ( by increasing it's refcount or so ),
Taking that in mind, looking at the code generated by the compiler I can't see anything taking care of the GC refcount... following is the pinned
variable with the corresponding dis-assembly section:

****************** Mixed mode C++ code line ******************
BYTE __pin *pbtArray = &btArray[0];

******************** The dis-assembly ***********************
00000019 cmp dword ptr [esi+4],0 // makes sure that NULL !=

&btArray[0]
0000001d ja 00000026 // if it is not NULL go

three
lines bellow
0000001f xor ecx,ecx
00000021 call 7227F90B // call some exception

handling procedure...
00000026 lea eax,[esi+8] // get the address of

&btArray[0] to eax
00000029 mov dword ptr [ebp-14h],eax // pbtArray = content of eax ************************************************** ******
Why there is no GC manipulation code? isn't it needed?

The original code:
************************************************** ******
virtual int NotArray(System::Byte btArray __gc[])
{
int i = 0;
BYTE __pin *pbtArray = &btArray[0];
for(i; i < btArray->Length; i++)
pbtArray[i] <<= 1;
return 0;
}

Nadav
http://www.ddevel.com



Nov 16 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Mike Kamzyuk | last post by:
Hello all. Basically, I need to call a mixed-mode dll's function (which uses managed code) from a native or mixed-mode dll function (which does not use managed code). I'm wondering if this could...
9
by: Edward Diener | last post by:
I received no answers about this the first time I posted, so I will try again. My inability to decipher an MSDN topic may find others who have the same inability and someone who can decipher and...
8
by: Bern McCarty | last post by:
Is it at all possible to leverage mixed-mode assemblies from AppDomains other than the default AppDomain? Is there any means at all of doing this? Mixed-mode is incredibly convenient, but if I...
3
by: | last post by:
Hi, I am making a wrapper class so I can call unmanaged classes from C#, I have these structs that I want to pass and get back from the unmanaged classes, do I... 1) Create a copy of these...
8
by: Nadav | last post by:
Hi, I am writing a performence critical application, this require me to stick to unmanaged C++ as performance is much better using unmanaged C++ ( about 33% better ), Still, I am trying to avoid...
4
by: Lonewolf | last post by:
hi, I'm still in the process of transiting from MFC/VC6 to vs2005, and a lot of things are very alien to me. So hope you could bear with me if my question sounds stupid. Basically I have native...
2
by: Doug Belkofer | last post by:
We have created a fairly complex mixed-mode DLL that we want to use from VB.NET. The mixed-mode DLL is written in C++, and does use the standard C runtime libraries. An unusual thing is happening...
8
by: Edward Diener | last post by:
By reuse, I mean a function in an assembly which is called in another assembly. By a mixed-mode function I mean a function whose signature has one or more CLR types and one or more non-CLR...
0
by: emu | last post by:
Hi All, I have an unmanaged C++ application that references a mixed mode image DLL (mixed managed and unmanaged). Under .NET 1.1 we could trust the dll (the mixed mode dll) by running the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.