473,888 Members | 1,551 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1801
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***@discussi ons.microsoft.c om> wrote in message
news:DE******** *************** ***********@mic rosoft.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.
"Christophe r 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***@discussi ons.microsoft.c om> wrote in message
news:DE******** *************** ***********@mic rosoft.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******** ******@TK2MSFTN GP12.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.
"Christophe r 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***@discussi ons.microsoft.c om> wrote in message
news:DE******** *************** ***********@mic rosoft.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
2208
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 be accomplished and how. Here's the problem. We have a third party app (TPA) capable of loading native and mixed-mode dlls somehow (we don't know how). It loads our native dll (OND) and allows us to use our code inside the app (that is, we...
9
2600
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 explain it. I have some questions about the instructions for creating a mixed mode DLL in the MSDN topic "Converting Managed Extensions for C++ Projects from Pure Intermediate Language to Mixed Mode" in the "Managed Extensions for C++ Reference"....
8
3527
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 cannot load/unload/reload extensions into my large and slow-to-load application during development without restarting the process then the disadvantages may outweigh the advantages. I've got a mixed-mode program in which I create a new AppDomain...
3
5130
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 structs for the managed world like public __value struct SomeStruct {
8
2006
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 the usage of old style COM, my alternative is to expose my unmanaged interface through the CLI, to achieve that I have created a mixed mode DLL in which my unmanaged class are defined. When referencing the DLL just described in another mixed mode EXE...
4
2246
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 codes written in VC6 which I want to encapsulate in a managed assembly using C++/CLI so that I can use it in C# easily without all the interop codes. So, my question is, what is the replacement for MFC's TRACE macro in VS2005's C++/CLI in...
2
1654
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 in that when we look at this DLL from the Object Browser in Visual Studio, it seems to be exporting several items related to the standard C runtime libraries. One example is that there is a namespace called "std" in the Object Browser, and in that...
8
2332
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 types. The problem: I have a number of mixed-mode functions which I want reuse. These functions revolve around converting a CLR String to a C++ std::string or
0
3005
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 following command line: caspol.exe -polchgprompt off -machine -addgroup 1 -url "file://<UNC path to dll>\mixedMode.dll" FullTrust ame "GroupName" -polchgprompt on
0
9961
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9800
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11178
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10882
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10438
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9597
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
7148
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5817
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
4243
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.