1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
|
/*
* Copyright (c) 1998, 2009, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*
*/
//
// C++ wrapper to HPI.
//
class hpi : AllStatic {
private:
static GetInterfaceFunc _get_interface;
static HPI_FileInterface* _file;
static HPI_SocketInterface* _socket;
static HPI_LibraryInterface* _library;
static HPI_SystemInterface* _system;
private:
static void initialize_get_interface(vm_calls_t *callbacks);
public:
// Load and initialize everything except sockets.
static jint initialize();
// Socket library needs to be lazy intialized because eagerly
// loading Winsock is known to cause "connect to your ISP"
// dialog to show up. Or so goes the legend.
static jint initialize_socket_library();
// HPI_FileInterface
static inline char* native_path(char *path);
static inline int file_type(const char *path);
static inline int open(const char *name, int mode, int perm);
static inline int close(int fd);
static inline jlong lseek(int fd, jlong off, int whence);
static inline int ftruncate(int fd, jlong length);
static inline int fsync(int fd);
static inline int available(int fd, jlong *bytes);
static inline size_t read(int fd, void *buf, unsigned int nBytes);
static inline size_t write(int fd, const void *buf, unsigned int nBytes);
static inline int fsize(int fd, jlong *size);
// HPI_SocketInterface
static inline int socket(int domain, int type, int protocol);
static inline int socket_close(int fd);
static inline int socket_shutdown(int fd, int howto);
static inline int recv(int fd, char *buf, int nBytes, int flags);
static inline int send(int fd, char *buf, int nBytes, int flags);
// Variant of send that doesn't support interruptible I/O
static inline int raw_send(int fd, char *buf, int nBytes, int flags);
static inline int timeout(int fd, long timeout);
static inline int listen(int fd, int count);
static inline int connect(int fd, struct sockaddr *him, int len);
static inline int bind(int fd, struct sockaddr *him, int len);
static inline int accept(int fd, struct sockaddr *him, int *len);
static inline int recvfrom(int fd, char *buf, int nbytes, int flags,
struct sockaddr *from, int *fromlen);
static inline int get_sock_name(int fd, struct sockaddr *him, int *len);
static inline int sendto(int fd, char *buf, int len, int flags,
struct sockaddr *to, int tolen);
static inline int socket_available(int fd, jint *pbytes);
static inline int get_sock_opt(int fd, int level, int optname,
char *optval, int* optlen);
static inline int set_sock_opt(int fd, int level, int optname,
const char *optval, int optlen);
static inline int get_host_name(char* name, int namelen);
static inline struct hostent* get_host_by_addr(const char* name, int len, int type);
static inline struct hostent* get_host_by_name(char* name);
static inline struct protoent* get_proto_by_name(char* name);
// HPI_LibraryInterface
static inline void dll_build_name(char *buf, int buf_len, const char* path,
const char *name);
static inline void* dll_load(const char *name, char *ebuf, int ebuflen);
static inline void dll_unload(void *lib);
static inline void* dll_lookup(void *lib, const char *name);
// HPI_SystemInterface
static inline int lasterror(char *buf, int len);
};
//
// Macros that provide inline bodies for the functions.
//
#define HPIDECL(name, names, intf, func, ret_type, ret_fmt, arg_type, arg_print, arg) \
inline ret_type hpi::name arg_type { \
if (TraceHPI) { \
tty->print("hpi::" names "("); \
tty->print arg_print ; \
tty->print(") = "); \
} \
ret_type result = (*intf->func) arg ; \
if (TraceHPI) { \
tty->print_cr(ret_fmt, result); \
} \
return result; \
}
// Macro to facilitate moving HPI functionality into the vm.
// See bug 6348631. The only difference between this macro and
// HPIDECL is that we call a vm method rather than use the HPI
// transfer vector. Ultimately, we'll replace HPIDECL with
// VM_HPIDECL for all hpi methods.
#define VM_HPIDECL(name, names, func, ret_type, ret_fmt, arg_type,arg_print, arg) \
inline ret_type hpi::name arg_type { \
if (TraceHPI) { \
tty->print("hpi::" names "("); \
tty->print arg_print ; \
tty->print(") = "); \
} \
ret_type result = func arg ; \
if (TraceHPI) { \
tty->print_cr(ret_fmt, result); \
} \
return result; \
}
#define VM_HPIDECL_VOID(name, names, func, arg_type, arg_print, arg) \
inline void hpi::name arg_type { \
if (TraceHPI) { \
tty->print("hpi::" names "("); \
tty->print arg_print; \
tty->print(") = "); \
} \
func arg; \
}
#define HPIDECL_VOID(name, names, intf, func, arg_type, arg_print, arg) \
inline void hpi::name arg_type { \
if (TraceHPI) { \
tty->print("hpi::" names "("); \
tty->print arg_print ; \
tty->print_cr(") = void"); \
} \
(*intf->func) arg ; \
}
// The macro calls below realize into
// inline char * hpi::native_path(...) { inlined_body; }
// etc.
// HPI_FileInterface
HPIDECL(native_path, "native_path", _file, NativePath, char *, "%s",
(char *path),
("path = %s", path),
(path));
HPIDECL(file_type, "file_type", _file, FileType, int, "%d",
(const char *path),
("path = %s", path),
(path));
HPIDECL(open, "open", _file, Open, int, "%d",
(const char *name, int mode, int perm),
("name = %s, mode = %d, perm = %d", name, mode, perm),
(name, mode, perm));
HPIDECL(lseek, "seek", _file, Seek, jlong, "(a jlong)",
(int fd, jlong off, int whence),
("fd = %d, off = (a jlong), whence = %d", fd, /* off, */ whence),
(fd, off, whence));
HPIDECL(ftruncate, "ftruncate", _file, SetLength, int, "%d",
(int fd, jlong length),
("fd = %d, length = (a jlong)", fd /*, length */),
(fd, length));
HPIDECL(fsync, "fsync", _file, Sync, int, "%d",
(int fd),
("fd = %d", fd),
(fd));
HPIDECL(available, "available", _file, Available, int, "%d",
(int fd, jlong *bytes),
("fd = %d, bytes = %p", fd, bytes),
(fd, bytes));
HPIDECL(fsize, "fsize", _file, FileSizeFD, int, "%d",
(int fd, jlong *size),
("fd = %d, size = %p", fd, size),
(fd, size));
// HPI_LibraryInterface
VM_HPIDECL_VOID(dll_build_name, "dll_build_name", os::dll_build_name,
(char *buf, int buf_len, const char *path, const char *name),
("buf = %p, buflen = %d, path = %s, name = %s",
buf, buf_len, path, name),
(buf, buf_len, path, name));
VM_HPIDECL(dll_load, "dll_load", os::dll_load,
void *, "(void *)%p",
(const char *name, char *ebuf, int ebuflen),
("name = %s, ebuf = %p, ebuflen = %d", name, ebuf, ebuflen),
(name, ebuf, ebuflen));
HPIDECL_VOID(dll_unload, "dll_unload", _library, UnloadLibrary,
(void *lib),
("lib = %p", lib),
(lib));
HPIDECL(dll_lookup, "dll_lookup", _library, FindLibraryEntry, void *, "%p",
(void *lib, const char *name),
("lib = %p, name = %s", lib, name),
(lib, name));
// HPI_SystemInterface
HPIDECL(lasterror, "lasterror", _system, GetLastErrorString, int, "%d",
(char *buf, int len),
("buf = %p, len = %d", buf, len),
(buf, len));
|