0b7b20916977f511a78da9c9d6a16bc370f82aee
[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 /* We include hpi_md.h before hpi.h as the latter includes the
29    former. */
30
31 #include INCLUDE_HPI_MD_H
32 #include INCLUDE_HPI_H
33
34 #include "mm/memory.h"
35
36 #include "native/jni.h"
37 #include "native/native.h"
38
39 #include "native/vm/openjdk/hpi.h"
40
41 #include "vm/properties.h"
42 #include "vm/vm.hpp"
43
44 #include "vmcore/options.h"
45 #include "vmcore/os.hpp"
46 #include "vmcore/utf8.h"
47
48
49 /* VM callback functions ******************************************************/
50
51 static vm_calls_t callbacks = {
52         /* TODO What should we use here? */
53 /*   jio_fprintf, */
54 /*   unimplemented_panic, */
55 /*   unimplemented_monitorRegister, */
56         NULL,
57         NULL,
58         NULL,
59         
60         NULL, /* unused */
61         NULL, /* unused */
62         NULL  /* unused */
63 };
64
65
66 /* HPI interfaces *************************************************************/
67
68 GetInterfaceFunc      hpi_get_interface = NULL;
69 HPI_FileInterface    *hpi_file          = NULL;
70 HPI_SocketInterface  *hpi_socket        = NULL;
71 HPI_LibraryInterface *hpi_library       = NULL;
72 HPI_SystemInterface  *hpi_system        = NULL;
73
74
75 /* hpi_initialize **************************************************************
76
77    Initialize the Host Porting Interface (HPI).
78
79 *******************************************************************************/
80
81 void hpi_initialize(void)
82 {
83         const char* boot_library_path;
84         int   len;
85         char* p;
86         utf*  u;
87         void* handle;
88         void* dll_initialize;
89         int   result;
90
91     jint (JNICALL * DLL_Initialize)(GetInterfaceFunc *, void *);
92
93         TRACESUBSYSTEMINITIALIZATION("hpi_init");
94
95         /* Load libhpi.so */
96
97         boot_library_path = properties_get("sun.boot.library.path");
98
99         len =
100                 os_strlen(boot_library_path) +
101                 os_strlen("/native_threads/libhpi.so") +
102                 os_strlen("0");
103
104         p = MNEW(char, len);
105
106         os_strcpy(p, boot_library_path);
107         os_strcat(p, "/native_threads/libhpi.so");
108
109         u = utf_new_char(p);
110
111     if (opt_TraceHPI)
112                 log_println("hpi_init: Loading HPI %s ", p);
113
114         MFREE(p, char, len);
115
116         handle = native_library_open(u);
117
118         if (handle == NULL)
119                 if (opt_TraceHPI)
120                         vm_abort("hpi_init: HPI open failed");
121
122         /* Resolve the DLL_Initialize function from the library. */
123
124         dll_initialize = os_dlsym(handle, "DLL_Initialize");
125
126     DLL_Initialize = (jint (JNICALL *)(GetInterfaceFunc *, void *)) (intptr_t) dll_initialize;
127
128     if (opt_TraceHPI && DLL_Initialize == NULL)
129                 log_println("hpi_init: HPI dlsym of DLL_Initialize failed: %s", os_dlerror());
130
131     if (DLL_Initialize == NULL ||
132         (*DLL_Initialize)(&hpi_get_interface, &callbacks) < 0) {
133
134         if (opt_TraceHPI)
135                         vm_abort("hpi_init: HPI DLL_Initialize failed");
136     }
137
138         native_library_add(u, NULL, handle);
139
140     if (opt_TraceHPI)
141                 log_println("hpi_init: HPI loaded successfully");
142
143         /* Resolve the interfaces. */
144         /* NOTE: The intptr_t-case is only to prevent the a compiler
145            warning with -O2: warning: dereferencing type-punned pointer
146            will break strict-aliasing rules */
147
148         result = (*hpi_get_interface)((void **) (intptr_t) &hpi_file, "File", 1);
149
150         if (result != 0)
151                 vm_abort("hpi_init: Can't find HPI_FileInterface");
152
153         result = (*hpi_get_interface)((void **) (intptr_t) &hpi_library, "Library", 1);
154
155         if (result != 0)
156                 vm_abort("hpi_init: Can't find HPI_LibraryInterface");
157
158         result = (*hpi_get_interface)((void **) (intptr_t) &hpi_system, "System", 1);
159
160         if (result != 0)
161                 vm_abort("hpi_init: Can't find HPI_SystemInterface");
162 }
163
164
165 /* hpi_initialize_socket_library ***********************************************
166
167    Initialize the library Host Porting Interface (HPI).
168
169 *******************************************************************************/
170
171 int hpi_initialize_socket_library(void)
172 {
173         int result;
174
175         /* Resolve the socket library interface. */
176
177         result = (*hpi_get_interface)((void **) (intptr_t) &hpi_socket, "Socket", 1);
178
179         if (result != 0) {
180                 if (opt_TraceHPI)
181                         log_println("hpi_initialize_socket_library: Can't find HPI_SocketInterface");
182
183                 return JNI_ERR;
184         }
185
186         return JNI_OK;
187 }
188
189
190 /*
191  * These are local overrides for various environment variables in Emacs.
192  * Please do not remove this and leave it at the end of the file, where
193  * Emacs will automagically detect them.
194  * ---------------------------------------------------------------------
195  * Local variables:
196  * mode: c
197  * indent-tabs-mode: t
198  * c-basic-offset: 4
199  * tab-width: 4
200  * End:
201  * vim:noexpandtab:sw=4:ts=4:
202  */