1 //===- llvm/Support/Unix/Unix.h - Common Unix Include File -------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines things specific to Unix implementations.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_LIB_SUPPORT_UNIX_UNIX_H
15 #define LLVM_LIB_SUPPORT_UNIX_UNIX_H
17 //===----------------------------------------------------------------------===//
18 //=== WARNING: Implementation here must contain only generic UNIX code that
19 //=== is guaranteed to work on all UNIX variants.
20 //===----------------------------------------------------------------------===//
22 #include "llvm/Config/config.h" // Get autoconf configuration settings
23 #include "llvm/Support/Chrono.h"
24 #include "llvm/Support/Errno.h"
32 #include <sys/types.h>
39 #ifdef HAVE_SYS_PARAM_H
40 #include <sys/param.h>
43 #ifdef HAVE_SYS_TIME_H
44 # include <sys/time.h>
56 /// This function builds an error message into \p ErrMsg using the \p prefix
57 /// string and the Unix error number given by \p errnum. If errnum is -1, the
58 /// default then the value of errno is used.
59 /// @brief Make an error message
61 /// If the error number can be converted to a string, it will be
62 /// separated from prefix by ": ".
63 static inline bool MakeErrMsg(
64 std::string* ErrMsg, const std::string& prefix, int errnum = -1) {
69 *ErrMsg = prefix + ": " + llvm::sys::StrError(errnum);
76 /// Convert a struct timeval to a duration. Note that timeval can be used both
77 /// as a time point and a duration. Be sure to check what the input represents.
78 inline std::chrono::microseconds toDuration(const struct timeval &TV) {
79 return std::chrono::seconds(TV.tv_sec) +
80 std::chrono::microseconds(TV.tv_usec);
83 /// Convert a time point to struct timespec.
84 inline struct timespec toTimeSpec(TimePoint<> TP) {
85 using namespace std::chrono;
87 struct timespec RetVal;
88 RetVal.tv_sec = toTimeT(TP);
89 RetVal.tv_nsec = (TP.time_since_epoch() % seconds(1)).count();
93 /// Convert a time point to struct timeval.
94 inline struct timeval toTimeVal(TimePoint<std::chrono::microseconds> TP) {
95 using namespace std::chrono;
97 struct timeval RetVal;
98 RetVal.tv_sec = toTimeT(TP);
99 RetVal.tv_usec = (TP.time_since_epoch() % seconds(1)).count();