* src/native/jni.hpp: We typedef the JNI types to the CACAO internal
[cacao.git] / src / native / vm / openjdk / hpi.c
1 /* src/native/vm/openjdk/hpi.c - HotSpot HPI interface functions
2
3    Copyright (C) 2008
4    CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
5
6    This file is part of CACAO.
7
8    This program is free software; you can redistribute it and/or
9    modify it under the terms of the GNU General Public License as
10    published by the Free Software Foundation; either version 2, or (at
11    your option) any later version.
12
13    This program is distributed in the hope that it will be useful, but
14    WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16    General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21    02110-1301, USA.
22
23 */
24
25
26 #include "config.h"
27
28 // Include our JNI header before the HPI headers, because the HPI
29 // headers include jni.h and we want to override the typedefs in
30 // jni.h.
31 #include "native/jni.hpp"
32
33 // We include hpi_md.h before hpi.h as the latter includes the former.
34 #include INCLUDE_HPI_MD_H
35 #include INCLUDE_HPI_H
36
37 #include "mm/memory.h"
38
39 #include "native/native.hpp"
40
41 #include "native/vm/openjdk/hpi.h"
42
43 #include "vm/options.h"
44 #include "vm/os.hpp"
45 #include "vm/properties.hpp"
46 #include "vm/utf8.h"
47 #include "vm/vm.hpp"
48
49
50 /* VM callback functions ******************************************************/
51
52 static vm_calls_t callbacks = {
53         /* TODO What should we use here? */
54 /*   jio_fprintf, */
55 /*   unimplemented_panic, */
56 /*   unimplemented_monitorRegister, */
57         NULL,
58         NULL,
59         NULL,
60         
61         NULL, /* unused */
62         NULL, /* unused */
63         NULL  /* unused */
64 };
65
66
67 /* HPI interfaces *************************************************************/
68
69 GetInterfaceFunc      hpi_get_interface = NULL;
70 HPI_FileInterface    *hpi_file          = NULL;
71 HPI_SocketInterface  *hpi_socket        = NULL;
72 HPI_LibraryInterface *hpi_library       = NULL;
73 HPI_SystemInterface  *hpi_system        = NULL;
74
75
76 /* hpi_initialize **************************************************************
77
78    Initialize the Host Porting Interface (HPI).
79
80 *******************************************************************************/
81
82 void hpi_initialize(void)
83 {
84         const char* boot_library_path;
85         int   len;
86         char* p;
87         utf*  u;
88         void* handle;
89         void* dll_initialize;
90         int   result;
91
92     jint (JNICALL * DLL_Initialize)(GetInterfaceFunc *, void *);
93
94         TRACESUBSYSTEMINITIALIZATION("hpi_init");
95
96         /* Load libhpi.so */
97
98         boot_library_path = Properties_get("sun.boot.library.path");
99
100         len =
101                 os_strlen(boot_library_path) +
102                 os_strlen("/native_threads/libhpi.so") +
103                 os_strlen("0");
104
105         p = MNEW(char, len);
106
107         os_strcpy(p, boot_library_path);
108         os_strcat(p, "/native_threads/libhpi.so");
109
110         u = utf_new_char(p);
111
112     if (opt_TraceHPI)
113                 log_println("hpi_init: Loading HPI %s ", p);
114
115         MFREE(p, char, len);
116
117         handle = native_library_open(u);
118
119         if (handle == NULL)
120                 if (opt_TraceHPI)
121                         vm_abort("hpi_init: HPI open failed");
122
123         /* Resolve the DLL_Initialize function from the library. */
124
125         dll_initialize = os_dlsym(handle, "DLL_Initialize");
126
127     DLL_Initialize = (jint (JNICALL *)(GetInterfaceFunc *, void *)) (intptr_t) dll_initialize;
128
129     if (opt_TraceHPI && DLL_Initialize == NULL)
130                 log_println("hpi_init: HPI dlsym of DLL_Initialize failed: %s", os_dlerror());
131
132     if (DLL_Initialize == NULL ||
133         (*DLL_Initialize)(&hpi_get_interface, &callbacks) < 0) {
134
135         if (opt_TraceHPI)
136                         vm_abort("hpi_init: HPI DLL_Initialize failed");
137     }
138
139         native_library_add(u, NULL, handle);
140
141     if (opt_TraceHPI)
142                 log_println("hpi_init: HPI loaded successfully");
143
144         /* Resolve the interfaces. */
145         /* NOTE: The intptr_t-case is only to prevent the a compiler
146            warning with -O2: warning: dereferencing type-punned pointer
147            will break strict-aliasing rules */
148
149         result = (*hpi_get_interface)((void **) (intptr_t) &hpi_file, "File", 1);
150
151         if (result != 0)
152                 vm_abort("hpi_init: Can't find HPI_FileInterface");
153
154         result = (*hpi_get_interface)((void **) (intptr_t) &hpi_library, "Library", 1);
155
156         if (result != 0)
157                 vm_abort("hpi_init: Can't find HPI_LibraryInterface");
158
159         result = (*hpi_get_interface)((void **) (intptr_t) &hpi_system, "System", 1);
160
161         if (result != 0)
162                 vm_abort("hpi_init: Can't find HPI_SystemInterface");
163 }
164
165
166 /* hpi_initialize_socket_library ***********************************************
167
168    Initialize the library Host Porting Interface (HPI).
169
170 *******************************************************************************/
171
172 int hpi_initialize_socket_library(void)
173 {
174         int result;
175
176         /* Resolve the socket library interface. */
177
178         result = (*hpi_get_interface)((void **) (intptr_t) &hpi_socket, "Socket", 1);
179
180         if (result != 0) {
181                 if (opt_TraceHPI)
182                         log_println("hpi_initialize_socket_library: Can't find HPI_SocketInterface");
183
184                 return JNI_ERR;
185         }
186
187         return JNI_OK;
188 }
189
190
191 /*
192  * These are local overrides for various environment variables in Emacs.
193  * Please do not remove this and leave it at the end of the file, where
194  * Emacs will automagically detect them.
195  * ---------------------------------------------------------------------
196  * Local variables:
197  * mode: c
198  * indent-tabs-mode: t
199  * c-basic-offset: 4
200  * tab-width: 4
201  * End:
202  * vim:noexpandtab:sw=4:ts=4:
203  */