1 //===- llvm/Support/Unix/Unix.h - Common Unix Include File -------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file defines things specific to Unix implementations.
11 //===----------------------------------------------------------------------===//
13 #ifndef LLVM_LIB_SUPPORT_UNIX_UNIX_H
14 #define LLVM_LIB_SUPPORT_UNIX_UNIX_H
16 //===----------------------------------------------------------------------===//
17 //=== WARNING: Implementation here must contain only generic UNIX code that
18 //=== is guaranteed to work on all UNIX variants.
19 //===----------------------------------------------------------------------===//
21 #include "llvm/Config/config.h"
22 #include "llvm/Support/Chrono.h"
23 #include "llvm/Support/Errno.h"
31 #include <sys/types.h>
38 #ifdef HAVE_SYS_PARAM_H
39 #include <sys/param.h>
42 #ifdef HAVE_SYS_TIME_H
43 # include <sys/time.h>
55 /// This function builds an error message into \p ErrMsg using the \p prefix
56 /// string and the Unix error number given by \p errnum. If errnum is -1, the
57 /// default then the value of errno is used.
58 /// Make an error message
60 /// If the error number can be converted to a string, it will be
61 /// separated from prefix by ": ".
62 static inline bool MakeErrMsg(
63 std::string
* ErrMsg
, const std::string
& prefix
, int errnum
= -1) {
68 *ErrMsg
= prefix
+ ": " + llvm::sys::StrError(errnum
);
75 /// Convert a struct timeval to a duration. Note that timeval can be used both
76 /// as a time point and a duration. Be sure to check what the input represents.
77 inline std::chrono::microseconds
toDuration(const struct timeval
&TV
) {
78 return std::chrono::seconds(TV
.tv_sec
) +
79 std::chrono::microseconds(TV
.tv_usec
);
82 /// Convert a time point to struct timespec.
83 inline struct timespec
toTimeSpec(TimePoint
<> TP
) {
84 using namespace std::chrono
;
86 struct timespec RetVal
;
87 RetVal
.tv_sec
= toTimeT(TP
);
88 RetVal
.tv_nsec
= (TP
.time_since_epoch() % seconds(1)).count();
92 /// Convert a time point to struct timeval.
93 inline struct timeval
toTimeVal(TimePoint
<std::chrono::microseconds
> TP
) {
94 using namespace std::chrono
;
96 struct timeval RetVal
;
97 RetVal
.tv_sec
= toTimeT(TP
);
98 RetVal
.tv_usec
= (TP
.time_since_epoch() % seconds(1)).count();