Merge pull request #1841 from caslan/supportforBSTR
[platform/upstream/coreclr.git] / src / pal / src / include / pal / map.hpp
1 //
2 // Copyright (c) Microsoft. All rights reserved.
3 // Licensed under the MIT license. See LICENSE file in the project root for full license information. 
4 //
5
6 /*++
7
8
9
10 Module Name:
11
12     include/pal/map.hpp
13
14 Abstract:
15
16     Header file for file mapping functions.
17
18
19
20 --*/
21
22 #ifndef _PAL_MAP_H_
23 #define _PAL_MAP_H_
24
25 #include "corunix.hpp"
26 #include <sys/param.h>
27
28 extern "C"
29 {
30 #include "list.h"
31
32 #ifndef NO_INO
33 #define NO_INO ((ino_t)-1)
34 #endif
35
36     /*++
37     Function :
38         MapInitialize
39
40         Initialize the critical sections.
41
42     Return value:
43         TRUE  if initialization succeeded
44         FALSE otherwise        
45     --*/
46     BOOL MAPInitialize( void );
47
48     /*++
49     Function :
50         MapCleanup
51
52         Deletes the critical sections.
53
54     --*/
55     void MAPCleanup( void );
56
57     /*++
58     Function :
59         MAPGetRegionInfo
60
61         Parameters: 
62         lpAddress: pointer to the starting memory location, not necessary
63                    to be rounded to the page location
64
65         lpBuffer: if this function finds information about the specified address,
66                   the information is stored in this struct
67
68         Note: This function is to be used in virtual.c
69               
70         Returns TRUE if this function finds information about the specified address
71     --*/
72
73     BOOL MAPGetRegionInfo(LPVOID lpAddress, PMEMORY_BASIC_INFORMATION lpBuffer);
74
75     /*++
76         MAPMapPEFile -
77
78         Map a PE format file into memory like Windows LoadLibrary() would do.
79         Doesn't apply base relocations if the function is relocated.
80
81     Parameters:
82         IN hFile - file to map
83
84     Return value:
85         non-NULL - the base address of the mapped image
86         NULL - error, with last error set.
87     --*/
88
89     void * MAPMapPEFile(HANDLE hFile);
90
91     /*++
92     Function :
93         MAPUnmapPEFile - unmap a PE file, and remove it from the recorded list of PE files mapped
94
95         returns TRUE if successful, FALSE otherwise
96     --*/
97     BOOL MAPUnmapPEFile(LPCVOID lpAddress);
98 }
99
100 namespace CorUnix
101 {
102     extern CObjectType otFileMapping;
103
104 #if ONE_SHARED_MAPPING_PER_FILEREGION_PER_PROCESS
105     typedef struct _NativeMapHolder
106     {
107         Volatile<LONG> ref_count;
108         LPVOID address;
109         SIZE_T size;
110         SIZE_T offset; /* for future use */
111     } NativeMapHolder;
112 #endif
113
114     /* Process specific information. This 
115     structure is not stored in shared memory.*/
116     typedef struct _MVL
117     {
118         LIST_ENTRY Link;
119         
120         //
121         // Each MVL entry holds a reference to its parent file
122         // mapping object.
123         //
124         
125         IPalObject *pFileMapping;
126         
127 #if ONE_SHARED_MAPPING_PER_FILEREGION_PER_PROCESS
128         NativeMapHolder * pNMHolder; /* Ref-counted holder for memory mapping */
129         dev_t   MappedFileDevNum;           /* ID of device containing the file to be mapped */
130         ino_t   MappedFileInodeNum;         /* Inode number of file to be mapped.
131                                                These two fields are used used to uniquely 
132                                                identify files on systems that do not allow 
133                                                more than one shared mmapping per region of 
134                                                physical file, per process */
135 #endif
136         LPVOID lpAddress;           /* The pointer to the mapped memory. */
137         SIZE_T NumberOfBytesToMap;  /* Number of bytes to map. */
138         DWORD dwDesiredAccess;      /* Desired access. */
139         LPVOID lpPEBaseAddress;     /* If this mapping is part of a PE file mapping, this is the
140                                        base address pointer of the PE file (used to find all
141                                        parts of the PE file mapping to allow PE file unload).
142                                        Otherwise, it is NULL. */
143     } MAPPED_VIEW_LIST, * PMAPPED_VIEW_LIST;
144
145     class CFileMappingImmutableData
146     {
147     public:
148         CHAR szFileName[MAXPATHLEN];
149         UINT MaxSize;               // The max size of the file mapping object
150         DWORD flProtect;            // Protection desired for the file view
151         BOOL bPALCreatedTempFile;   // TRUE if it's a PAL created file
152         DWORD dwDesiredAccessWhenOpened;  // FILE_MAP_WRITE etc
153     };
154
155     class CFileMappingProcessLocalData 
156     {
157     public:
158         INT     UnixFd;                     /* File descriptor. */
159         
160 #if ONE_SHARED_MAPPING_PER_FILEREGION_PER_PROCESS
161         dev_t   MappedFileDevNum;           /* ID of device containing the file to be mapped */
162         ino_t   MappedFileInodeNum;         /* Inode number of file to be mapped.
163                                                These two fields are used used to uniquely 
164                                                identify files on systems that do not allow 
165                                                more than one shared mmapping per region of 
166                                                physical file, per process */
167 #endif
168     };
169
170     PAL_ERROR
171     InternalCreateFileMapping(
172         CPalThread *pThread,
173         HANDLE hFile,
174         LPSECURITY_ATTRIBUTES lpFileMappingAttributes,
175         DWORD flProtect,
176         DWORD dwMaximumSizeHigh,
177         DWORD dwMaximumSizeLow,
178         LPCWSTR lpName,
179         HANDLE *phMapping
180         );
181
182     PAL_ERROR
183     InternalOpenFileMapping(
184         CPalThread *pThread,
185         DWORD dwDesiredAccess,
186         BOOL bInheritHandle,
187         LPCWSTR lpName,
188         HANDLE *phMapping
189         );
190
191     PAL_ERROR
192     InternalMapViewOfFile(
193         CPalThread *pThread,
194         HANDLE hFileMappingObject,
195         DWORD dwDesiredAccess,
196         DWORD dwFileOffsetHigh,
197         DWORD dwFileOffsetLow,
198         SIZE_T dwNumberOfBytesToMap,
199         LPVOID *ppvBaseAddress
200         );
201
202     PAL_ERROR
203     InternalUnmapViewOfFile(
204         CPalThread *pThread,
205         LPCVOID lpBaseAddress
206         );
207
208 }
209
210 #endif /* _PAL_MAP_H_ */