* configure.ac: Added check for sys/loadavg.h (for OpenSolaris).
[cacao.git] / src / vm / os.hpp
index 6a18dd71ddbda4d06b10735611f669a41a170e99..c812c33f9d684eb08265f8ed0d32a328b7e77d5a 100644 (file)
@@ -1,6 +1,6 @@
 /* src/vm/os.hpp - system (OS) functions
 
-   Copyright (C) 2007, 2008
+   Copyright (C) 2007, 2008, 2009
    CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
    Copyright (C) 2008 Theobroma Systems Ltd.
 
 # include <unistd.h>
 #endif
 
+#if defined(HAVE_SYS_LOADAVG_H)
+# include <sys/loadavg.h>
+#endif
+
 #if defined(HAVE_SYS_MMAN_H)
 # include <sys/mman.h>
 #endif
@@ -135,6 +139,7 @@ public:
        static inline void    free(void* ptr);
        static inline char*   getenv(const char* name);
        static inline int     gethostname(char* name, size_t len);
+       static inline int     getloadavg(double loadavg[], int nelem);
        static inline int     getpagesize(void);
        static inline int     getsockname(int s, struct sockaddr* name, socklen_t* namelen);
        static inline int     getsockopt(int s, int level, int optname, void* optval, socklen_t* optlen);
@@ -145,6 +150,7 @@ public:
        static inline int     mprotect(void* addr, size_t len, int prot);
        static inline ssize_t readlink(const char* path, char* buf, size_t bufsiz);
        static inline int     scandir(const char* dir, struct dirent*** namelist, int(*filter)(const struct dirent*), int(*compar)(const void*, const void*));
+       static inline ssize_t send(int s, const void* buf, size_t len, int flags);
        static inline int     setsockopt(int s, int level, int optname, const void* optval, socklen_t optlen);
        static inline int     shutdown(int s, int how);
        static inline int     socket(int domain, int type, int protocol);
@@ -166,6 +172,10 @@ public:
        static void* mmap_anonymous(void *addr, size_t len, int prot, int flags);
        static void  print_backtrace();
        static int   processors_online();
+
+       // Template helper
+       template<class F1, class F2>
+       static int call_scandir(int (*scandir)(const char *, struct dirent ***, F1, F2), const char *dir, struct dirent ***namelist, int(*filter)(const struct dirent *), int(*compar)(const void *, const void *));
 };
 
 
@@ -324,7 +334,7 @@ inline int os::fprintf(FILE* stream, const char* format, ...)
 #if defined(HAVE_FPRINTF)
        va_list ap;
        va_start(ap, format);
-       int result = ::fprintf(stream, format, ap);
+       int result = ::vfprintf(stream, format, ap);
        va_end(ap);
        return result;
 #else
@@ -341,15 +351,6 @@ inline size_t os::fread(void* ptr, size_t size, size_t nmemb, FILE* stream)
 #endif
 }
 
-inline static int system_fseek(FILE *stream, off_t offset, int whence)
-{
-#if defined(HAVE_FSEEK)
-       return fseek(stream, offset, whence);
-#else
-# error fseek not available
-#endif
-}
-
 inline void os::free(void* ptr)
 {
 #if defined(HAVE_FREE)
@@ -395,6 +396,15 @@ inline int os::gethostname(char* name, size_t len)
 #endif
 }
 
+inline int os::getloadavg(double loadavg[], int nelem)
+{
+#if defined(HAVE_GETLOADAVG)
+       return ::getloadavg(loadavg, nelem);
+#else
+# error getloadavg not available
+#endif
+}
+
 inline int os::getpagesize(void)
 {
 #if defined(HAVE_GETPAGESIZE)
@@ -512,30 +522,31 @@ inline static void *system_realloc(void *ptr, size_t size)
 #endif
 }
 
+template<class F1, class F2>
+inline int os::call_scandir(int (*scandir)(const char *, struct dirent ***, F1, F2), const char *dir, struct dirent ***namelist, int(*filter)(const struct dirent *), int(*compar)(const void *, const void *))
+{
+       return scandir(dir, namelist, (F1) filter, (F2) compar);
+}
+
 inline int os::scandir(const char *dir, struct dirent ***namelist, int(*filter)(const struct dirent *), int(*compar)(const void *, const void *))
-/*
-#elif defined(__SOLARIS__)
-inline int os::scandir(const char *dir, struct dirent ***namelist, int(*filter)(const struct dirent *), int(*compar)(const struct dirent **, const struct dirent **))
-#elif defined(__IRIX__)
-inline int os::scandir(const char *dir, struct dirent ***namelist, int(*filter)(dirent_t *), int(*compar)(dirent_t **, dirent_t **))
-#else
-inline int os::scandir(const char *dir, struct dirent ***namelist, int(*filter)(struct dirent *), int(*compar)(const void *, const void *))
-#endif
-*/
 {
 #if defined(HAVE_SCANDIR)
-# if defined(__LINUX__)
-       return ::scandir(dir, namelist, filter, compar);
-#elif defined(__SOLARIS__)
-       return ::scandir(dir, namelist, filter, (int (*)(const dirent**, const dirent**)) compar);
-# else
-       return ::scandir(dir, namelist, (int (*)(struct dirent*)) filter, compar);
-# endif
+       return call_scandir(::scandir, dir, namelist, filter, compar);
 #else
 # error scandir not available
 #endif
 }
 
+inline ssize_t os::send(int s, const void* buf, size_t len, int flags)
+{
+       // TODO Should be restartable on Linux and interruptible on Solaris.
+#if defined(HAVE_SEND)
+       return ::send(s, buf, len, flags);
+#else
+# error send not available
+#endif
+}
+
 inline int os::setsockopt(int s, int level, int optname, const void* optval, socklen_t optlen)
 {
 #if defined(HAVE_SETSOCKOPT)