Re: Garbage collectable pinned arrays!
- From: "Ben Voigt [C++ MVP]" <rbv@xxxxxxxxxxxxx>
- Date: Tue, 12 Feb 2008 18:16:08 -0600
Willy Denoyette [MVP] wrote:
"Ben Voigt [C++ MVP]" <rbv@xxxxxxxxxxxxx> wrote in message
news:%23bjU84abIHA.5400@xxxxxxxxxxxxxxxxxxxxxxx
This is not what pinned means in this context. The objects on the
LOH (all version of the CLR) are at a fixed address for their life
time, but that doesn't mean they are pinned. Pinning is an explicit
action
Ok, but then they don't require pinning. In any case they
inherently have the fixed address which requires pinning to obtain
for normal objects.
As I said before, pinning is an action performed by the Interop layer
*when the GC initiates a scan, and it's not done by means of a call to
GCHandle.Alloc. You only need to pin "explicitely" when you are
passing an object to unmanaged code and you need to keep the object
alive and at fixed location after the PInvoke call returned, during
the call the PInvoke layer takes care of eventual pinning.
That's not an unusual case. I've already given two examples of APIs in
widespread use which require a buffer to stay in one position after the
initial function call which accepts the pointer.
Also, you keep ignoring my remark that the fact that addresses of
*Large* objects are fixed is a convenience of the current version of
the CLR, nothing stops MS from changing this.
Which is why the OP is asking for a keyword / MSIL flag that will let the
runtime know that the object is intended to be fixed for as long as it
lives. It would be an implementation detail whether the memory is allocated
from the LOH, OLE task allocator, etc, etc. Also I don't think that
sacrificing GC for such objects would necessarily be a big loss, they either
will live to the end of the process anyway, or they can be explicitly freed.
Hence my suggestion of T[] Marshal.AllocCoTaskMem<T>(int elementCount), T
required to be a "reference free" value type, meaning all members are
"reference free" value types.
It would be useful to request that a particular buffer not be
subject to relocation by the GC. Probably the easiest way to do
this would be to place it in the LOH. The OLE task allocator or
HGlobal allocator, both of which are already exposed by the Marshal
class in a typeless way, would be other options. It could be as
simple as adding a T[] Marshal.AllocCoTaskMem<T>(int elementCount)
override.
But now you are allocating from the unmanaged heap (COM heap or CRT
heap or whatever). So now you will incur the costs of copying back
and forth, again this depends on the semantics, but might be a
solution when you need to pass large data chunks to unmanaged land.
Why? .NET could create a proper array descriptor storing the
metadata alongside and access it directly.
Where? outside of the GC heap? Who's going to "manage" these objects
then? As you may know, there are other cheap means to pass fixed
buffers to managed code.
The CLR has no trouble with data outside the GC heap, it just can't hold
references to objects inside the GC heap (because then objects could be
reachable but the GC wouldn't know that). For example, MSIL instructions
have no trouble accessing structures on the stack.
Willy.
.
- Follow-Ups:
- Re: Garbage collectable pinned arrays!
- From: Willy Denoyette [MVP]
- Re: Garbage collectable pinned arrays!
- References:
- Re: Garbage collectable pinned arrays!
- From: Jesse McGrew
- Re: Garbage collectable pinned arrays!
- From: Atmapuri
- Re: Garbage collectable pinned arrays!
- From: Willy Denoyette [MVP]
- Re: Garbage collectable pinned arrays!
- From: Ben Voigt [C++ MVP]
- Re: Garbage collectable pinned arrays!
- From: Willy Denoyette [MVP]
- Re: Garbage collectable pinned arrays!
- From: Ben Voigt [C++ MVP]
- Re: Garbage collectable pinned arrays!
- From: Willy Denoyette [MVP]
- Re: Garbage collectable pinned arrays!
- Prev by Date: Re: Can you use two monitors with Viual Studio 2008?
- Next by Date: How to find out the content of the HTTP Request when a web form is automaically submitted?
- Previous by thread: Re: Garbage collectable pinned arrays!
- Next by thread: Re: Garbage collectable pinned arrays!
- Index(es):
Relevant Pages
|
Loading