103 lines · c
1//===- llvm/Support/Unix/Unix.h - Common Unix Include File -------*- C++ -*-===//2//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-exception6//7//===----------------------------------------------------------------------===//8//9// This file defines things specific to Unix implementations.10//11//===----------------------------------------------------------------------===//12 13#ifndef LLVM_LIB_SUPPORT_UNIX_UNIX_H14#define LLVM_LIB_SUPPORT_UNIX_UNIX_H15 16//===----------------------------------------------------------------------===//17//=== WARNING: Implementation here must contain only generic UNIX code that18//=== is guaranteed to work on all UNIX variants.19//===----------------------------------------------------------------------===//20 21#include "llvm/Config/config.h"22#include "llvm/Support/Chrono.h"23#include "llvm/Support/Errno.h"24#include "llvm/Support/ErrorHandling.h"25#include <assert.h>26#include <cerrno>27#include <cstdio>28#include <cstdlib>29#include <cstring>30#include <string>31#include <sys/types.h>32#include <sys/wait.h>33 34#ifdef HAVE_UNISTD_H35#include <unistd.h>36#endif37 38#include <sys/time.h>39#include <time.h>40 41#include <dlfcn.h>42 43# include <fcntl.h>44 45/// This function builds an error message into \p ErrMsg using the \p prefix46/// string and the Unix error number given by \p errnum. If errnum is -1, the47/// default then the value of errno is used.48/// Make an error message49///50/// If the error number can be converted to a string, it will be51/// separated from prefix by ": ".52static inline bool MakeErrMsg(53 std::string* ErrMsg, const std::string& prefix, int errnum = -1) {54 if (!ErrMsg)55 return true;56 if (errnum == -1)57 errnum = errno;58 *ErrMsg = prefix + ": " + llvm::sys::StrError(errnum);59 return true;60}61 62// Include StrError(errnum) in a fatal error message.63[[noreturn]] static inline void ReportErrnumFatal(const char *Msg, int errnum) {64 std::string ErrMsg;65 MakeErrMsg(&ErrMsg, Msg, errnum);66 llvm::report_fatal_error(llvm::Twine(ErrMsg));67}68 69namespace llvm {70namespace sys {71 72/// Convert a struct timeval to a duration. Note that timeval can be used both73/// as a time point and a duration. Be sure to check what the input represents.74inline std::chrono::microseconds toDuration(const struct timeval &TV) {75 return std::chrono::seconds(TV.tv_sec) +76 std::chrono::microseconds(TV.tv_usec);77}78 79/// Convert a time point to struct timespec.80inline struct timespec toTimeSpec(TimePoint<> TP) {81 using namespace std::chrono;82 83 struct timespec RetVal;84 RetVal.tv_sec = toTimeT(TP);85 RetVal.tv_nsec = (TP.time_since_epoch() % seconds(1)).count();86 return RetVal;87}88 89/// Convert a time point to struct timeval.90inline struct timeval toTimeVal(TimePoint<std::chrono::microseconds> TP) {91 using namespace std::chrono;92 93 struct timeval RetVal;94 RetVal.tv_sec = toTimeT(TP);95 RetVal.tv_usec = (TP.time_since_epoch() % seconds(1)).count();96 return RetVal;97}98 99} // namespace sys100} // namespace llvm101 102#endif103