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