Merge pull request #5714 from alexischr/update_bockbuild
[mono.git] / ikvm-native / os.c
1 /*
2   Copyright (C) 2004, 2005 Jeroen Frijters
3
4   This software is provided 'as-is', without any express or implied
5   warranty.  In no event will the authors be held liable for any damages
6   arising from the use of this software.
7
8   Permission is granted to anyone to use this software for any purpose,
9   including commercial applications, and to alter it and redistribute it
10   freely, subject to the following restrictions:
11
12   1. The origin of this software must not be misrepresented; you must not
13      claim that you wrote the original software. If you use this software
14      in a product, an acknowledgment in the product documentation would be
15      appreciated but is not required.
16   2. Altered source versions must be plainly marked as such, and must not be
17      misrepresented as being the original software.
18   3. This notice may not be removed or altered from any source distribution.
19
20   Jeroen Frijters
21   jeroen@frijters.net
22   
23 */
24 #ifdef _WIN32
25         #include <windows.h>
26         #include "jni.h"
27
28         JNIEXPORT void* JNICALL ikvm_LoadLibrary(char* psz)
29         {
30                 return LoadLibrary(psz);
31         }
32
33         JNIEXPORT void JNICALL ikvm_FreeLibrary(HMODULE handle)
34         {
35                 FreeLibrary(handle);
36         }
37
38         JNIEXPORT void* JNICALL ikvm_GetProcAddress(HMODULE handle, char* name, jint argc)
39         {
40 #ifdef _WIN64
41                 return GetProcAddress(handle, name);
42 #else
43                 void* pfunc;
44                 char buf[512];
45                 if(strlen(name) > sizeof(buf) - 11)
46                 {
47                         return 0;
48                 }
49                 wsprintf(buf, "_%s@%d", name, argc);
50                 pfunc = GetProcAddress(handle, buf);
51                 if (pfunc)
52                         return pfunc;
53                 // If we didn't find the mangled name, try the unmangled name (this happens if you have an
54                 // explicit EXPORT in the linker def).
55                 return GetProcAddress(handle, name);
56 #endif
57         }
58 #else
59         #include <gmodule.h>
60         #include <sys/types.h>
61         #include <sys/mman.h>
62         #include "jni.h"
63
64         void* JNICALL ikvm_LoadLibrary(char* psz);
65
66         JNIEXPORT void* JNICALL ikvm_LoadLibrary(char* psz)
67         {
68                 return g_module_open(psz, 0);
69         }
70
71         void JNICALL ikvm_FreeLibrary(GModule* handle);
72
73         JNIEXPORT void JNICALL ikvm_FreeLibrary(GModule* handle)
74         {
75                 g_module_close(handle);
76         }
77
78         void* JNICALL ikvm_GetProcAddress(GModule* handle, char* name, jint argc);
79
80         JNIEXPORT void* JNICALL ikvm_GetProcAddress(GModule* handle, char* name, jint argc)
81         {
82                 void *symbol;
83
84                 gboolean res = g_module_symbol(handle, name, &symbol);
85
86                 if (res)
87                         return symbol;
88                 else
89                         return NULL;
90         }
91
92         void* JNICALL ikvm_mmap(int fd, jboolean writeable, jboolean copy_on_write, jlong position, jint size);
93
94         JNIEXPORT void* JNICALL ikvm_mmap(int fd, jboolean writeable, jboolean copy_on_write, jlong position, jint size)
95         {
96                 return mmap(0, size, writeable ? PROT_WRITE | PROT_READ : PROT_READ, copy_on_write ? MAP_PRIVATE : MAP_SHARED, fd, position);
97         }
98
99         int JNICALL ikvm_munmap(void* address, jint size);
100
101         JNIEXPORT int JNICALL ikvm_munmap(void* address, jint size)
102         {
103                 return munmap(address, size);
104         }
105
106         int JNICALL ikvm_msync(void* address, jint size);
107
108         JNIEXPORT int JNICALL ikvm_msync(void* address, jint size)
109         {
110 #if defined(__native_client__) && defined(USE_NEWLIB)
111                 g_assert_not_reached ();
112                 return -1;
113 #else
114                 return msync(address, size, MS_SYNC);
115 #endif
116         }
117 #endif