os.c 2.6 KB

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