duke@435: /* duke@435: * Copyright 1998-2006 Sun Microsystems, Inc. All Rights Reserved. duke@435: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. duke@435: * duke@435: * This code is free software; you can redistribute it and/or modify it duke@435: * under the terms of the GNU General Public License version 2 only, as duke@435: * published by the Free Software Foundation. duke@435: * duke@435: * This code is distributed in the hope that it will be useful, but WITHOUT duke@435: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or duke@435: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License duke@435: * version 2 for more details (a copy is included in the LICENSE file that duke@435: * accompanied this code). duke@435: * duke@435: * You should have received a copy of the GNU General Public License version duke@435: * 2 along with this work; if not, write to the Free Software Foundation, duke@435: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. duke@435: * duke@435: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, duke@435: * CA 95054 USA or visit www.sun.com if you need additional information or duke@435: * have any questions. duke@435: * duke@435: */ duke@435: duke@435: // duke@435: // C++ wrapper to HPI. duke@435: // duke@435: duke@435: class hpi : AllStatic { duke@435: duke@435: private: duke@435: static GetInterfaceFunc _get_interface; duke@435: static HPI_FileInterface* _file; duke@435: static HPI_SocketInterface* _socket; duke@435: static HPI_LibraryInterface* _library; duke@435: static HPI_SystemInterface* _system; duke@435: duke@435: private: duke@435: static void initialize_get_interface(vm_calls_t *callbacks); duke@435: duke@435: public: duke@435: // Load and initialize everything except sockets. duke@435: static jint initialize(); duke@435: duke@435: // Socket library needs to be lazy intialized because eagerly duke@435: // loading Winsock is known to cause "connect to your ISP" duke@435: // dialog to show up. Or so goes the legend. duke@435: static jint initialize_socket_library(); duke@435: duke@435: // HPI_FileInterface duke@435: static inline char* native_path(char *path); duke@435: static inline int file_type(const char *path); duke@435: static inline int open(const char *name, int mode, int perm); duke@435: static inline int close(int fd); duke@435: static inline jlong lseek(int fd, jlong off, int whence); duke@435: static inline int ftruncate(int fd, jlong length); duke@435: static inline int fsync(int fd); duke@435: static inline int available(int fd, jlong *bytes); duke@435: static inline size_t read(int fd, void *buf, unsigned int nBytes); duke@435: static inline size_t write(int fd, const void *buf, unsigned int nBytes); duke@435: static inline int fsize(int fd, jlong *size); duke@435: duke@435: // HPI_SocketInterface duke@435: static inline int socket(int domain, int type, int protocol); duke@435: static inline int socket_close(int fd); duke@435: static inline int socket_shutdown(int fd, int howto); duke@435: static inline int recv(int fd, char *buf, int nBytes, int flags); duke@435: static inline int send(int fd, char *buf, int nBytes, int flags); duke@435: static inline int timeout(int fd, long timeout); duke@435: static inline int listen(int fd, int count); duke@435: static inline int connect(int fd, struct sockaddr *him, int len); duke@435: static inline int bind(int fd, struct sockaddr *him, int len); duke@435: static inline int accept(int fd, struct sockaddr *him, int *len); duke@435: static inline int recvfrom(int fd, char *buf, int nbytes, int flags, duke@435: struct sockaddr *from, int *fromlen); duke@435: static inline int get_sock_name(int fd, struct sockaddr *him, int *len); duke@435: static inline int sendto(int fd, char *buf, int len, int flags, duke@435: struct sockaddr *to, int tolen); duke@435: static inline int socket_available(int fd, jint *pbytes); duke@435: duke@435: static inline int get_sock_opt(int fd, int level, int optname, duke@435: char *optval, int* optlen); duke@435: static inline int set_sock_opt(int fd, int level, int optname, duke@435: const char *optval, int optlen); duke@435: static inline int get_host_name(char* name, int namelen); duke@435: static inline struct hostent* get_host_by_addr(const char* name, int len, int type); duke@435: static inline struct hostent* get_host_by_name(char* name); duke@435: static inline struct protoent* get_proto_by_name(char* name); duke@435: duke@435: // HPI_LibraryInterface duke@435: static inline void dll_build_name(char *buf, int buf_len, char* path, duke@435: const char *name); duke@435: static inline void* dll_load(const char *name, char *ebuf, int ebuflen); duke@435: static inline void dll_unload(void *lib); duke@435: static inline void* dll_lookup(void *lib, const char *name); duke@435: duke@435: // HPI_SystemInterface duke@435: static inline int lasterror(char *buf, int len); duke@435: }; duke@435: duke@435: // duke@435: // Macros that provide inline bodies for the functions. duke@435: // duke@435: duke@435: #define HPIDECL(name, names, intf, func, ret_type, ret_fmt, arg_type, arg_print, arg) \ duke@435: inline ret_type hpi::name arg_type { \ duke@435: if (TraceHPI) { \ duke@435: tty->print("hpi::" names "("); \ duke@435: tty->print arg_print ; \ duke@435: tty->print(") = "); \ duke@435: } \ duke@435: ret_type result = (*intf->func) arg ; \ duke@435: if (TraceHPI) { \ duke@435: tty->print_cr(ret_fmt, result); \ duke@435: } \ duke@435: return result; \ duke@435: } duke@435: duke@435: // Macro to facilitate moving HPI functionality into the vm. duke@435: // See bug 6348631. The only difference between this macro and duke@435: // HPIDECL is that we call a vm method rather than use the HPI duke@435: // transfer vector. Ultimately, we'll replace HPIDECL with duke@435: // VM_HPIDECL for all hpi methods. duke@435: #define VM_HPIDECL(name, names, func, ret_type, ret_fmt, arg_type,arg_print, arg) \ duke@435: inline ret_type hpi::name arg_type { \ duke@435: if (TraceHPI) { \ duke@435: tty->print("hpi::" names "("); \ duke@435: tty->print arg_print ; \ duke@435: tty->print(") = "); \ duke@435: } \ duke@435: ret_type result = func arg ; \ duke@435: if (TraceHPI) { \ duke@435: tty->print_cr(ret_fmt, result); \ duke@435: } \ duke@435: return result; \ duke@435: } duke@435: duke@435: duke@435: duke@435: #define HPIDECL_VOID(name, names, intf, func, arg_type, arg_print, arg) \ duke@435: inline void hpi::name arg_type { \ duke@435: if (TraceHPI) { \ duke@435: tty->print("hpi::" names "("); \ duke@435: tty->print arg_print ; \ duke@435: tty->print_cr(") = void"); \ duke@435: } \ duke@435: (*intf->func) arg ; \ duke@435: } duke@435: duke@435: duke@435: // The macro calls below realize into duke@435: // inline char * hpi::native_path(...) { inlined_body; } duke@435: // etc. duke@435: duke@435: // HPI_FileInterface duke@435: duke@435: HPIDECL(native_path, "native_path", _file, NativePath, char *, "%s", duke@435: (char *path), duke@435: ("path = %s", path), duke@435: (path)); duke@435: duke@435: HPIDECL(file_type, "file_type", _file, FileType, int, "%d", duke@435: (const char *path), duke@435: ("path = %s", path), duke@435: (path)); duke@435: duke@435: HPIDECL(open, "open", _file, Open, int, "%d", duke@435: (const char *name, int mode, int perm), duke@435: ("name = %s, mode = %d, perm = %d", name, mode, perm), duke@435: (name, mode, perm)); duke@435: duke@435: HPIDECL(lseek, "seek", _file, Seek, jlong, "(a jlong)", duke@435: (int fd, jlong off, int whence), duke@435: ("fd = %d, off = (a jlong), whence = %d", fd, /* off, */ whence), duke@435: (fd, off, whence)); duke@435: duke@435: HPIDECL(ftruncate, "ftruncate", _file, SetLength, int, "%d", duke@435: (int fd, jlong length), duke@435: ("fd = %d, length = (a jlong)", fd /*, length */), duke@435: (fd, length)); duke@435: duke@435: HPIDECL(fsync, "fsync", _file, Sync, int, "%d", duke@435: (int fd), duke@435: ("fd = %d", fd), duke@435: (fd)); duke@435: duke@435: HPIDECL(available, "available", _file, Available, int, "%d", duke@435: (int fd, jlong *bytes), duke@435: ("fd = %d, bytes = %p", fd, bytes), duke@435: (fd, bytes)); duke@435: duke@435: HPIDECL(fsize, "fsize", _file, FileSizeFD, int, "%d", duke@435: (int fd, jlong *size), duke@435: ("fd = %d, size = %p", fd, size), duke@435: (fd, size)); duke@435: duke@435: // HPI_LibraryInterface duke@435: HPIDECL_VOID(dll_build_name, "dll_build_name", _library, BuildLibName, duke@435: (char *buf, int buf_len, char *path, const char *name), duke@435: ("buf = %p, buflen = %d, path = %s, name = %s", duke@435: buf, buf_len, path, name), duke@435: (buf, buf_len, path, name)); duke@435: duke@435: VM_HPIDECL(dll_load, "dll_load", os::dll_load, duke@435: void *, "(void *)%p", duke@435: (const char *name, char *ebuf, int ebuflen), duke@435: ("name = %s, ebuf = %p, ebuflen = %d", name, ebuf, ebuflen), duke@435: (name, ebuf, ebuflen)); duke@435: duke@435: HPIDECL_VOID(dll_unload, "dll_unload", _library, UnloadLibrary, duke@435: (void *lib), duke@435: ("lib = %p", lib), duke@435: (lib)); duke@435: duke@435: HPIDECL(dll_lookup, "dll_lookup", _library, FindLibraryEntry, void *, "%p", duke@435: (void *lib, const char *name), duke@435: ("lib = %p, name = %s", lib, name), duke@435: (lib, name)); duke@435: duke@435: // HPI_SystemInterface duke@435: HPIDECL(lasterror, "lasterror", _system, GetLastErrorString, int, "%d", duke@435: (char *buf, int len), duke@435: ("buf = %p, len = %d", buf, len), duke@435: (buf, len));