summaryrefslogtreecommitdiff
path: root/src/vm/custommarshalerinfo.h
blob: 01af33cc0e0901826bffbb41177d527f89bda320 (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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
// 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.
// 
// File: CustomMarshalerInfo.h
// 

// 
// Custom marshaler information used when marshaling
// a parameter with a custom marshaler. 
// 


#ifndef _CUSTOMMARSHALERINFO_H_
#define _CUSTOMMARSHALERINFO_H_


#include "vars.hpp"
#include "slist.h"


// This enumeration is used to retrieve a method desc from CustomMarshalerInfo::GetCustomMarshalerMD().
enum EnumCustomMarshalerMethods
{
    CustomMarshalerMethods_MarshalNativeToManaged = 0,
    CustomMarshalerMethods_MarshalManagedToNative,
    CustomMarshalerMethods_CleanUpNativeData,
    CustomMarshalerMethods_CleanUpManagedData,
    CustomMarshalerMethods_GetNativeDataSize,
    CustomMarshalerMethods_GetInstance,
    CustomMarshalerMethods_LastMember
};


class CustomMarshalerInfo
{
public:
    // Constructor and destructor.
    CustomMarshalerInfo(BaseDomain* pDomain, TypeHandle hndCustomMarshalerType, TypeHandle hndManagedType, LPCUTF8 strCookie, DWORD cCookieStrBytes);
    ~CustomMarshalerInfo();

    // CustomMarshalerInfo's are always allocated on the loader heap so we need to redefine
    // the new and delete operators to ensure this.
    void* operator      new(size_t size, LoaderHeap* pHeap);
    void  operator      delete(void* pMem);

    // Helpers used to invoke the different methods in the ICustomMarshaler interface.
    OBJECTREF           InvokeMarshalNativeToManagedMeth(void* pNative);
    void*               InvokeMarshalManagedToNativeMeth(OBJECTREF MngObj);
    void                InvokeCleanUpNativeMeth(void* pNative);
    void                InvokeCleanUpManagedMeth(OBJECTREF MngObj);

    // Accessors.
    int GetNativeSize()
    {
        LIMITED_METHOD_CONTRACT;
        return m_NativeSize;
    }

    int GetManagedSize()
    {
        WRAPPER_NO_CONTRACT;
        return m_hndManagedType.GetSize();
    }

    TypeHandle GetManagedType()
    {
        LIMITED_METHOD_CONTRACT;
        return m_hndManagedType;
    }

    BOOL IsDataByValue()
    {
        LIMITED_METHOD_CONTRACT;
        return m_bDataIsByValue;
    }

    OBJECTHANDLE GetCustomMarshaler()
    {
        LIMITED_METHOD_CONTRACT;
        return m_hndCustomMarshaler;
    }

    TypeHandle GetCustomMarshalerType()
    {
        CONTRACTL
        {
            NOTHROW;
            GC_NOTRIGGER;
            SO_TOLERANT;
            MODE_COOPERATIVE;
        }
        CONTRACTL_END;
        return ObjectFromHandle(m_hndCustomMarshaler)->GetTypeHandle();
    }

    // Helper function to retrieve a custom marshaler method desc.
    static MethodDesc*  GetCustomMarshalerMD(EnumCustomMarshalerMethods Method, TypeHandle hndCustomMarshalertype); 

    // Link used to contain this CM info in a linked list.
    SLink               m_Link;

private:
    int                 m_NativeSize;
    TypeHandle          m_hndManagedType;
    OBJECTHANDLE        m_hndCustomMarshaler;
    MethodDesc*         m_pMarshalNativeToManagedMD;
    MethodDesc*         m_pMarshalManagedToNativeMD;
    MethodDesc*         m_pCleanUpNativeDataMD;
    MethodDesc*         m_pCleanUpManagedDataMD;
    BOOL                m_bDataIsByValue;
};


typedef SList<CustomMarshalerInfo, true> CMINFOLIST;


class EECMHelperHashtableKey
{
public:
    EECMHelperHashtableKey(DWORD cMarshalerTypeNameBytes, LPCSTR strMarshalerTypeName, DWORD cCookieStrBytes, LPCSTR strCookie, Instantiation instantiation, BOOL bSharedHelper) 
    : m_cMarshalerTypeNameBytes(cMarshalerTypeNameBytes)
    , m_strMarshalerTypeName(strMarshalerTypeName)
    , m_cCookieStrBytes(cCookieStrBytes)
    , m_strCookie(strCookie)
    , m_Instantiation(instantiation)
    , m_bSharedHelper(bSharedHelper)
    {
        LIMITED_METHOD_CONTRACT;
    }

    inline DWORD GetMarshalerTypeNameByteCount() const
    {
        LIMITED_METHOD_CONTRACT;
        return m_cMarshalerTypeNameBytes;
    }
    inline LPCSTR GetMarshalerTypeName() const
    {
        LIMITED_METHOD_CONTRACT;
        return m_strMarshalerTypeName;
    }
    inline LPCSTR GetCookieString() const
    {
        LIMITED_METHOD_CONTRACT;
        return m_strCookie;
    }
    inline ULONG GetCookieStringByteCount() const
    {
        LIMITED_METHOD_CONTRACT;
        return m_cCookieStrBytes;
    }
    inline Instantiation GetMarshalerInstantiation() const
    {
        LIMITED_METHOD_CONTRACT;
        return m_Instantiation;
    }
    inline BOOL IsSharedHelper() const
    {
        LIMITED_METHOD_CONTRACT;
        return m_bSharedHelper;
    }


    DWORD           m_cMarshalerTypeNameBytes;
    LPCSTR          m_strMarshalerTypeName;
    DWORD           m_cCookieStrBytes;
    LPCSTR          m_strCookie;
    Instantiation   m_Instantiation;
    BOOL            m_bSharedHelper;
};


class EECMHelperHashtableHelper
{
public:
    static EEHashEntry_t*  AllocateEntry(EECMHelperHashtableKey* pKey, BOOL bDeepCopy, AllocationHeap Heap);
    static void            DeleteEntry(EEHashEntry_t* pEntry, AllocationHeap Heap);
    static BOOL            CompareKeys(EEHashEntry_t* pEntry, EECMHelperHashtableKey* pKey);
    static DWORD           Hash(EECMHelperHashtableKey* pKey);
};


typedef EEHashTable<EECMHelperHashtableKey*, EECMHelperHashtableHelper, TRUE> EECMHelperHashTable;


class CustomMarshalerHelper
{
public:
    // Helpers used to invoke the different methods in the ICustomMarshaler interface.
    OBJECTREF           InvokeMarshalNativeToManagedMeth(void* pNative);
    void*               InvokeMarshalManagedToNativeMeth(OBJECTREF MngObj);
    void                InvokeCleanUpNativeMeth(void* pNative);
    void                InvokeCleanUpManagedMeth(OBJECTREF MngObj);

    // Accessors.
    int GetNativeSize()
    {
        WRAPPER_NO_CONTRACT;
        return GetCustomMarshalerInfo()->GetNativeSize();
    }
    
    int GetManagedSize()
    {
        WRAPPER_NO_CONTRACT;
        return GetCustomMarshalerInfo()->GetManagedSize();
    }
    
    TypeHandle GetManagedType()
    {
        WRAPPER_NO_CONTRACT;
        return GetCustomMarshalerInfo()->GetManagedType();
    }
    
    BOOL IsDataByValue()
    {
        WRAPPER_NO_CONTRACT;
        return GetCustomMarshalerInfo()->IsDataByValue();
    }

    // Helper function to retrieve the custom marshaler object.
    virtual CustomMarshalerInfo* GetCustomMarshalerInfo() = 0;

protected:
    ~CustomMarshalerHelper( void )
    {
        LIMITED_METHOD_CONTRACT;
    }
};


class NonSharedCustomMarshalerHelper : public CustomMarshalerHelper
{
public:
    // Constructor.
    NonSharedCustomMarshalerHelper(CustomMarshalerInfo* pCMInfo) : m_pCMInfo(pCMInfo)
    {
        WRAPPER_NO_CONTRACT;
    }

    // CustomMarshalerHelpers's are always allocated on the loader heap so we need to redefine
    // the new and delete operators to ensure this.
    void *operator new(size_t size, LoaderHeap *pHeap);
    void operator delete(void* pMem);

protected:
    // Helper function to retrieve the custom marshaler object.
    virtual CustomMarshalerInfo* GetCustomMarshalerInfo()
    {
        LIMITED_METHOD_CONTRACT;
        return m_pCMInfo;
    }

private:
    CustomMarshalerInfo* m_pCMInfo;
};


class SharedCustomMarshalerHelper : public CustomMarshalerHelper
{
public:
    // Constructor.
    SharedCustomMarshalerHelper(Assembly* pAssembly, TypeHandle hndManagedType, LPCUTF8 strMarshalerTypeName, DWORD cMarshalerTypeNameBytes, LPCUTF8 strCookie, DWORD cCookieStrBytes);

    // CustomMarshalerHelpers's are always allocated on the loader heap so we need to redefine
    // the new and delete operators to ensure this.
    void* operator new(size_t size, LoaderHeap* pHeap);
    void  operator delete(void* pMem);

    // Accessors.
    inline Assembly* GetAssembly()
    {
        LIMITED_METHOD_CONTRACT;
        return m_pAssembly;
    }

    inline TypeHandle GetManagedType()
    {
        LIMITED_METHOD_CONTRACT;
        return m_hndManagedType;
    }

    inline DWORD GetMarshalerTypeNameByteCount()
    {
        LIMITED_METHOD_CONTRACT;
        return m_cMarshalerTypeNameBytes;
    }

    inline LPCSTR GetMarshalerTypeName()
    {
        LIMITED_METHOD_CONTRACT;
        return m_strMarshalerTypeName;
    }

    inline LPCSTR GetCookieString()
    {
        LIMITED_METHOD_CONTRACT;
        return m_strCookie;
    }

    inline ULONG GetCookieStringByteCount()
    {
        LIMITED_METHOD_CONTRACT;
        return m_cCookieStrBytes;
    }

protected:
    // Helper function to retrieve the custom marshaler object.
    virtual CustomMarshalerInfo* GetCustomMarshalerInfo();

private:
    Assembly*       m_pAssembly;
    TypeHandle      m_hndManagedType;
    DWORD           m_cMarshalerTypeNameBytes;
    LPCUTF8         m_strMarshalerTypeName;
    DWORD           m_cCookieStrBytes;
    LPCUTF8         m_strCookie;
};


#endif // _CUSTOMMARSHALERINFO_H_