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