2010-04-16 Sebastien Pouliot <sebastien@ximian.com>
[mono/afaerber.git] / ikvm-native / os.c
blob65d49e2a2aeea33e77a21dc118c4b8359cec3c45
1 /*
2 Copyright (C) 2004, 2005 Jeroen Frijters
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.
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:
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.
20 Jeroen Frijters
21 jeroen@frijters.net
24 #ifdef _WIN32
25 #include <windows.h>
26 #include "jni.h"
28 JNIEXPORT void* JNICALL ikvm_LoadLibrary(char* psz)
30 return LoadLibrary(psz);
33 JNIEXPORT void JNICALL ikvm_FreeLibrary(HMODULE handle)
35 FreeLibrary(handle);
38 JNIEXPORT void* JNICALL ikvm_GetProcAddress(HMODULE handle, char* name, jint argc)
40 #ifdef _WIN64
41 return GetProcAddress(handle, name);
42 #else
43 void* pfunc;
44 char buf[512];
45 if(strlen(name) > sizeof(buf) - 11)
47 return 0;
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
58 #else
59 #include <gmodule.h>
60 #include <sys/types.h>
61 #include <sys/mman.h>
62 #include "jni.h"
64 JNIEXPORT void* JNICALL ikvm_LoadLibrary(char* psz)
66 return g_module_open(psz, 0);
69 JNIEXPORT void JNICALL ikvm_FreeLibrary(GModule* handle)
71 g_module_close(handle);
74 JNIEXPORT void* JNICALL ikvm_GetProcAddress(GModule* handle, char* name, jint argc)
76 void *symbol;
78 gboolean res = g_module_symbol(handle, name, &symbol);
80 if (res)
81 return symbol;
82 else
83 return NULL;
86 JNIEXPORT void* JNICALL ikvm_mmap(int fd, jboolean writeable, jboolean copy_on_write, jlong position, jint size)
88 return mmap(0, size, writeable ? PROT_WRITE | PROT_READ : PROT_READ, copy_on_write ? MAP_PRIVATE : MAP_SHARED, fd, position);
91 JNIEXPORT int JNICALL ikvm_munmap(void* address, jint size)
93 return munmap(address, size);
96 JNIEXPORT int JNICALL ikvm_msync(void* address, jint size)
98 return msync(address, size, MS_SYNC);
100 #endif