1 | // Licensed to the .NET Foundation under one or more agreements. |
2 | // The .NET Foundation licenses this file to you under the MIT license. |
3 | // See the LICENSE file in the project root for more information. |
4 | // |
5 | // File: stubcache.h |
6 | // |
7 | |
8 | // |
9 | // Base class for caching stubs. |
10 | // |
11 | |
12 | |
13 | #ifndef __mlcache_h__ |
14 | #define __mlcache_h__ |
15 | |
16 | |
17 | #include "vars.hpp" |
18 | #include "util.hpp" |
19 | #include "crst.h" |
20 | |
21 | class Stub; |
22 | class StubLinker; |
23 | |
24 | class StubCacheBase : private CClosedHashBase |
25 | { |
26 | private: |
27 | //--------------------------------------------------------- |
28 | // Hash entry for CClosedHashBase. |
29 | //--------------------------------------------------------- |
30 | struct STUBHASHENTRY |
31 | { |
32 | // Values: |
33 | // NULL = free |
34 | // -1 = deleted |
35 | // other = used |
36 | Stub *m_pStub; |
37 | |
38 | // Offset where the RawStub begins (the RawStub can be |
39 | // preceded by native stub code.) |
40 | UINT16 m_offsetOfRawStub; |
41 | }; |
42 | |
43 | |
44 | public: |
45 | //--------------------------------------------------------- |
46 | // Constructor |
47 | //--------------------------------------------------------- |
48 | StubCacheBase(LoaderHeap *heap = 0); |
49 | |
50 | //--------------------------------------------------------- |
51 | // Destructor |
52 | //--------------------------------------------------------- |
53 | virtual ~StubCacheBase(); |
54 | |
55 | //--------------------------------------------------------- |
56 | // Returns the equivalent hashed Stub, creating a new hash |
57 | // entry if necessary. If the latter, will call out to CompileStub. |
58 | // |
59 | // Throws on out of memory or other fatal error. |
60 | //--------------------------------------------------------- |
61 | Stub *Canonicalize(const BYTE *pRawStub); |
62 | |
63 | protected: |
64 | //--------------------------------------------------------- |
65 | // OVERRIDE. |
66 | // Compile a native (ASM) version of the stub. |
67 | // |
68 | // This method should compile into the provided stublinker (but |
69 | // not call the Link method.) |
70 | // |
71 | // It should return the chosen compilation mode. |
72 | // |
73 | // If the method fails for some reason, it should return |
74 | // INTERPRETED so that the EE can fall back on the already |
75 | // created ML code. |
76 | //--------------------------------------------------------- |
77 | virtual void CompileStub(const BYTE *pRawStub, |
78 | StubLinker *psl) = 0; |
79 | |
80 | //--------------------------------------------------------- |
81 | // OVERRIDE |
82 | // Tells the StubCacheBase the length of a stub. |
83 | //--------------------------------------------------------- |
84 | virtual UINT Length(const BYTE *pRawStub) = 0; |
85 | |
86 | //--------------------------------------------------------- |
87 | // OVERRIDE (OPTIONAL) |
88 | // Notifies the various derived classes that a new stub has been created |
89 | //--------------------------------------------------------- |
90 | virtual void AddStub(const BYTE* pRawStub, Stub* pNewStub); |
91 | |
92 | |
93 | private: |
94 | // *** OVERRIDES FOR CClosedHashBase ***/ |
95 | |
96 | //***************************************************************************** |
97 | // Hash is called with a pointer to an element in the table. You must override |
98 | // this method and provide a hash algorithm for your element type. |
99 | //***************************************************************************** |
100 | virtual unsigned int Hash( // The key value. |
101 | void const *pData); // Raw data to hash. |
102 | |
103 | //***************************************************************************** |
104 | // Compare is used in the typical memcmp way, 0 is eqaulity, -1/1 indicate |
105 | // direction of miscompare. In this system everything is always equal or not. |
106 | //***************************************************************************** |
107 | virtual unsigned int Compare( // 0, -1, or 1. |
108 | void const *pData, // Raw key data on lookup. |
109 | BYTE *pElement); // The element to compare data against. |
110 | |
111 | //***************************************************************************** |
112 | // Return true if the element is free to be used. |
113 | //***************************************************************************** |
114 | virtual ELEMENTSTATUS Status( // The status of the entry. |
115 | BYTE *pElement); // The element to check. |
116 | |
117 | //***************************************************************************** |
118 | // Sets the status of the given element. |
119 | //***************************************************************************** |
120 | virtual void SetStatus( |
121 | BYTE *pElement, // The element to set status for. |
122 | ELEMENTSTATUS eStatus); // New status. |
123 | |
124 | //***************************************************************************** |
125 | // Returns the internal key value for an element. |
126 | //***************************************************************************** |
127 | virtual void *GetKey( // The data to hash on. |
128 | BYTE *pElement); // The element to return data ptr for. |
129 | |
130 | |
131 | |
132 | |
133 | private: |
134 | Crst m_crst; |
135 | LoaderHeap* m_heap; |
136 | }; |
137 | |
138 | |
139 | #endif // __mlcache_h__ |
140 | |