Unix.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file defines things specific to Unix implementations.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #ifndef LLVM_LIB_SUPPORT_UNIX_UNIX_H
  13. #define LLVM_LIB_SUPPORT_UNIX_UNIX_H
  14. //===----------------------------------------------------------------------===//
  15. //=== WARNING: Implementation here must contain only generic UNIX code that
  16. //=== is guaranteed to work on all UNIX variants.
  17. //===----------------------------------------------------------------------===//
  18. #include "llvm/Config/config.h"
  19. #include "llvm/Support/Chrono.h"
  20. #include "llvm/Support/Errno.h"
  21. #include <algorithm>
  22. #include <assert.h>
  23. #include <cerrno>
  24. #include <cstdio>
  25. #include <cstdlib>
  26. #include <cstring>
  27. #include <string>
  28. #include <sys/types.h>
  29. #include <sys/wait.h>
  30. #ifdef HAVE_UNISTD_H
  31. #include <unistd.h>
  32. #endif
  33. #ifdef HAVE_SYS_PARAM_H
  34. #include <sys/param.h>
  35. #endif
  36. #ifdef HAVE_SYS_TIME_H
  37. # include <sys/time.h>
  38. #endif
  39. #include <time.h>
  40. #ifdef HAVE_DLFCN_H
  41. # include <dlfcn.h>
  42. #endif
  43. #ifdef HAVE_FCNTL_H
  44. # include <fcntl.h>
  45. #endif
  46. /// This function builds an error message into \p ErrMsg using the \p prefix
  47. /// string and the Unix error number given by \p errnum. If errnum is -1, the
  48. /// default then the value of errno is used.
  49. /// Make an error message
  50. ///
  51. /// If the error number can be converted to a string, it will be
  52. /// separated from prefix by ": ".
  53. static inline bool MakeErrMsg(
  54. std::string* ErrMsg, const std::string& prefix, int errnum = -1) {
  55. if (!ErrMsg)
  56. return true;
  57. if (errnum == -1)
  58. errnum = errno;
  59. *ErrMsg = prefix + ": " + llvm::sys::StrError(errnum);
  60. return true;
  61. }
  62. namespace llvm {
  63. namespace sys {
  64. /// Convert a struct timeval to a duration. Note that timeval can be used both
  65. /// as a time point and a duration. Be sure to check what the input represents.
  66. inline std::chrono::microseconds toDuration(const struct timeval &TV) {
  67. return std::chrono::seconds(TV.tv_sec) +
  68. std::chrono::microseconds(TV.tv_usec);
  69. }
  70. /// Convert a time point to struct timespec.
  71. inline struct timespec toTimeSpec(TimePoint<> TP) {
  72. using namespace std::chrono;
  73. struct timespec RetVal;
  74. RetVal.tv_sec = toTimeT(TP);
  75. RetVal.tv_nsec = (TP.time_since_epoch() % seconds(1)).count();
  76. return RetVal;
  77. }
  78. /// Convert a time point to struct timeval.
  79. inline struct timeval toTimeVal(TimePoint<std::chrono::microseconds> TP) {
  80. using namespace std::chrono;
  81. struct timeval RetVal;
  82. RetVal.tv_sec = toTimeT(TP);
  83. RetVal.tv_usec = (TP.time_since_epoch() % seconds(1)).count();
  84. return RetVal;
  85. }
  86. } // namespace sys
  87. } // namespace llvm
  88. #endif