Welcome to mirror list, hosted at ThFree Co, Russian Federation.

GCHandle.cs « InteropServices « Runtime « System « src « System.Private.CoreLib « src - github.com/mono/corert.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 53a51ae19d22c099bdc27b531321103892c94eba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using System.Runtime.CompilerServices;
using System.Threading;

using Internal.Runtime.CompilerServices;

namespace System.Runtime.InteropServices
{
    // This class allows you to create an opaque, GC handle to any 
    // COM+ object. A GC handle is used when an object reference must be
    // reachable from unmanaged memory.  There are 3 kinds of roots:
    // Normal - keeps the object from being collected.
    // Weak - allows object to be collected and handle contents will be zeroed.
    //          Weak references are zeroed before the finalizer runs, so if the
    //          object is resurrected in the finalizer the weak reference is
    //          still zeroed.
    // WeakTrackResurrection - Same as weak, but stays until after object is
    //          really gone.
    // Pinned - same as normal, but allows the address of the actual object
    //          to be taken.
    //

    [StructLayout(LayoutKind.Sequential)]
    public struct GCHandle
    {
        // IMPORTANT: This must be kept in sync with the GCHandleType enum.
        private const GCHandleType MaxHandleType = GCHandleType.Pinned;

        // Allocate a handle storing the object and the type.
        internal GCHandle(object value, GCHandleType type)
        {
            // Make sure the type parameter is within the valid range for the enum.
            if ((uint)type > (uint)MaxHandleType)
            {
                throw new ArgumentOutOfRangeException(); // "type", SR.ArgumentOutOfRange_Enum;
            }

            if (type == GCHandleType.Pinned)
                GCHandleValidatePinnedObject(value);

            _handle = RuntimeImports.RhHandleAlloc(value, type);

            // Record if the handle is pinned.
            if (type == GCHandleType.Pinned)
                SetIsPinned();
        }

        // Used in the conversion functions below.
        internal GCHandle(IntPtr handle)
        {
            _handle = handle;
        }

        // Creates a new GC handle for an object.
        //
        // value - The object that the GC handle is created for.
        // type - The type of GC handle to create.
        // 
        // returns a new GC handle that protects the object.
        public static GCHandle Alloc(object value)
        {
            return new GCHandle(value, GCHandleType.Normal);
        }

        public static GCHandle Alloc(object value, GCHandleType type)
        {
            return new GCHandle(value, type);
        }

        // Frees a GC handle.
        [MethodImplAttribute(MethodImplOptions.NoInlining)]
        public void Free()
        {
            // Copy the handle instance member to a local variable. This is required to prevent
            // race conditions releasing the handle.
            IntPtr handle = _handle;

            // Free the handle if it hasn't already been freed.
            if (handle != default(IntPtr) && Interlocked.CompareExchange(ref _handle, default(IntPtr), handle) == handle)
            {
#if BIT64
                RuntimeImports.RhHandleFree((IntPtr)(((long)handle) & ~1L));
#else
                RuntimeImports.RhHandleFree((IntPtr)(((int)handle) & ~1));
#endif
            }
            else
            {
                throw new InvalidOperationException(); // SR.InvalidOperation_HandleIsNotInitialized);
            }
        }

        // Target property - allows getting / updating of the handle's referent.
        public object Target
        {
            get
            {
                // Check if the handle was never initialized or was freed.
                if (_handle == default(IntPtr))
                {
                    throw new InvalidOperationException(); // SR.InvalidOperation_HandleIsNotInitialized);
                }

                return RuntimeImports.RhHandleGet(GetHandleValue());
            }

            set
            {
                // Check if the handle was never initialized or was freed.
                if (_handle == default(IntPtr))
                {
                    throw new InvalidOperationException(); // SR.InvalidOperation_HandleIsNotInitialized);
                }

                if (IsPinned())
                    GCHandleValidatePinnedObject(value);

                RuntimeImports.RhHandleSet(GetHandleValue(), value);
            }
        }

        // Retrieve the address of an object in a Pinned handle.  This throws
        // an exception if the handle is any type other than Pinned.
        public IntPtr AddrOfPinnedObject()
        {
            // Check if the handle was not a pinned handle.
            if (!IsPinned())
            {
                // Check if the handle was never initialized for was freed.
                if (_handle == default(IntPtr))
                {
                    throw new InvalidOperationException(); // SR.InvalidOperation_HandleIsNotInitialized);
                }

                // You can only get the address of pinned handles.
                throw new InvalidOperationException(); // SR.InvalidOperation_HandleIsNotPinned);
            }

            unsafe
            {
                // Get the address of the pinned object.
                // The layout of String and Array is different from Object

                object target = this.Target;

                if (target == null)
                    return default(IntPtr);

                if (target is string targetAsString)
                {
                    return (IntPtr)Unsafe.AsPointer(ref targetAsString.GetRawStringData());
                }

                if (target is Array targetAsArray)
                {
                    return (IntPtr)Unsafe.AsPointer(ref targetAsArray.GetRawArrayData());
                }

                return (IntPtr)Unsafe.AsPointer(ref target.GetRawData());
            }
        }

        // Determine whether this handle has been allocated or not.
        public bool IsAllocated
        {
            get
            {
                return _handle != default(IntPtr);
            }
        }

        // Used to create a GCHandle from an int.  This is intended to
        // be used with the reverse conversion.
        public static explicit operator GCHandle(IntPtr value)
        {
            return FromIntPtr(value);
        }

        public static GCHandle FromIntPtr(IntPtr value)
        {
            if (value == default(IntPtr))
            {
                throw new InvalidOperationException(); // SR.InvalidOperation_HandleIsNotInitialized);
            }

            return new GCHandle(value);
        }

        // Used to get the internal integer representation of the handle out.
        public static explicit operator IntPtr(GCHandle value)
        {
            return ToIntPtr(value);
        }

        public static IntPtr ToIntPtr(GCHandle value)
        {
            return value._handle;
        }

        public override int GetHashCode()
        {
            return _handle.GetHashCode();
        }

        public override bool Equals(object o)
        {
            GCHandle hnd;

            // Check that o is a GCHandle first
            if (o == null || !(o is GCHandle))
                return false;
            else
                hnd = (GCHandle)o;

            return _handle == hnd._handle;
        }

        public static bool operator ==(GCHandle a, GCHandle b)
        {
            return a._handle == b._handle;
        }

        public static bool operator !=(GCHandle a, GCHandle b)
        {
            return a._handle != b._handle;
        }

        internal IntPtr GetHandleValue()
        {
#if BIT64
            return new IntPtr(((long)_handle) & ~1L);
#else
            return new IntPtr(((int)_handle) & ~1);
#endif
        }

        internal bool IsPinned()
        {
#if BIT64
            return (((long)_handle) & 1) != 0;
#else
            return (((int)_handle) & 1) != 0;
#endif
        }

        internal void SetIsPinned()
        {
#if BIT64
            _handle = new IntPtr(((long)_handle) | 1L);
#else
            _handle = new IntPtr(((int)_handle) | 1);
#endif
        }

        private static void GCHandleValidatePinnedObject(object obj)
        {
            if (obj != null && !obj.IsBlittable())
                throw new ArgumentException(SR.Argument_NotIsomorphic);
        }

        // The actual integer handle value that the EE uses internally.
        private IntPtr _handle;
    }
}