os.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. void* JNICALL ikvm_LoadLibrary(char* psz);
  56. JNIEXPORT void* JNICALL ikvm_LoadLibrary(char* psz)
  57. {
  58. return g_module_open(psz, 0);
  59. }
  60. void JNICALL ikvm_FreeLibrary(GModule* handle);
  61. JNIEXPORT void JNICALL ikvm_FreeLibrary(GModule* handle)
  62. {
  63. g_module_close(handle);
  64. }
  65. void* JNICALL ikvm_GetProcAddress(GModule* handle, char* name, jint argc);
  66. JNIEXPORT void* JNICALL ikvm_GetProcAddress(GModule* handle, char* name, jint argc)
  67. {
  68. void *symbol;
  69. gboolean res = g_module_symbol(handle, name, &symbol);
  70. if (res)
  71. return symbol;
  72. else
  73. return NULL;
  74. }
  75. void* JNICALL ikvm_mmap(int fd, jboolean writeable, jboolean copy_on_write, jlong position, jint size);
  76. JNIEXPORT void* JNICALL ikvm_mmap(int fd, jboolean writeable, jboolean copy_on_write, jlong position, jint size)
  77. {
  78. return mmap(0, size, writeable ? PROT_WRITE | PROT_READ : PROT_READ, copy_on_write ? MAP_PRIVATE : MAP_SHARED, fd, position);
  79. }
  80. int JNICALL ikvm_munmap(void* address, jint size);
  81. JNIEXPORT int JNICALL ikvm_munmap(void* address, jint size)
  82. {
  83. return munmap(address, size);
  84. }
  85. int JNICALL ikvm_msync(void* address, jint size);
  86. JNIEXPORT int JNICALL ikvm_msync(void* address, jint size)
  87. {
  88. #if defined(__native_client__) && defined(USE_NEWLIB)
  89. g_assert_not_reached ();
  90. return -1;
  91. #else
  92. return msync(address, size, MS_SYNC);
  93. #endif
  94. }
  95. #endif