platform.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. * Copyright 2005 - 2016 Zarafa and its licensors
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU Affero General Public License, version 3,
  6. * as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU Affero General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU Affero General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. *
  16. */
  17. #ifndef PLATFORM_H
  18. #define PLATFORM_H
  19. #include <kopano/zcdefs.h>
  20. enum {
  21. KC_DESIRED_FILEDES = 8192,
  22. };
  23. // Log the pthreads locks
  24. #define DEBUG_PTHREADS 0
  25. #ifdef HAVE_CONFIG_H
  26. #include "config.h"
  27. #endif
  28. #include <kopano/platform.linux.h>
  29. #include <string>
  30. #include <cstddef>
  31. #include <pthread.h>
  32. namespace KC {
  33. #define KOPANO_SYSTEM_USER "SYSTEM"
  34. #define KOPANO_SYSTEM_USER_W L"SYSTEM"
  35. /* This should match what is used in proto.h for __size */
  36. typedef int gsoap_size_t;
  37. /*
  38. * Platform independent functions
  39. */
  40. extern _kc_export HRESULT UnixTimeToFileTime(time_t, FILETIME *);
  41. extern _kc_export HRESULT FileTimeToUnixTime(const FILETIME &, time_t *);
  42. extern _kc_export void UnixTimeToFileTime(time_t, int *hi, unsigned int *lo);
  43. extern _kc_export time_t FileTimeToUnixTime(unsigned int hi, unsigned int lo);
  44. void RTimeToFileTime(LONG rtime, FILETIME *pft);
  45. extern _kc_export void FileTimeToRTime(const FILETIME *, LONG *rtime);
  46. extern _kc_export HRESULT UnixTimeToRTime(time_t unixtime, LONG *rtime);
  47. extern _kc_export HRESULT RTimeToUnixTime(LONG rtime, time_t *unixtime);
  48. extern _kc_export double GetTimeOfDay(void);
  49. inline double difftimeval(struct timeval *ptstart, struct timeval *ptend) {
  50. return 1000000 * (ptend->tv_sec - ptstart->tv_sec) + (ptend->tv_usec - ptstart->tv_usec);
  51. }
  52. extern _kc_export struct tm *gmtime_safe(const time_t *timer, struct tm *result);
  53. /**
  54. * Creates the deadline timespec, which is the current time plus the specified
  55. * amount of milliseconds.
  56. *
  57. * @param[in] ulTimeoutMs The timeout in ms.
  58. * @return The required timespec.
  59. */
  60. struct timespec GetDeadline(unsigned int ulTimeoutMs);
  61. extern _kc_export double timespec2dbl(const struct timespec &);
  62. bool operator ==(const FILETIME &, const FILETIME &);
  63. extern _kc_export bool operator >(const FILETIME &, const FILETIME &);
  64. bool operator >=(const FILETIME &, const FILETIME &);
  65. extern _kc_export bool operator <(const FILETIME &, const FILETIME &);
  66. bool operator <=(const FILETIME &, const FILETIME &);
  67. extern _kc_export time_t operator -(const FILETIME &, const FILETIME &);
  68. /* convert struct tm to time_t in timezone UTC0 (GM time) */
  69. #ifndef HAVE_TIMEGM
  70. time_t timegm(struct tm *t);
  71. #endif
  72. // mkdir -p
  73. extern _kc_export int CreatePath(const char *);
  74. // Random-number generators
  75. extern _kc_export void rand_init(void);
  76. extern _kc_export int rand_mt(void);
  77. extern _kc_export void rand_free(void);
  78. extern _kc_export void rand_get(char *p, int n);
  79. extern _kc_export char *get_password(const char *prompt);
  80. /**
  81. * Memory usage calculation macros
  82. */
  83. #define MEMALIGN(x) (((x) + alignof(void *) - 1) & ~(alignof(void *) - 1))
  84. #define MEMORY_USAGE_MAP(items, map) (items * (sizeof(map) + sizeof(map::value_type)))
  85. #define MEMORY_USAGE_LIST(items, list) (items * (MEMALIGN(sizeof(list) + sizeof(list::value_type))))
  86. #define MEMORY_USAGE_HASHMAP(items, map) MEMORY_USAGE_MAP(items, map)
  87. #define MEMORY_USAGE_STRING(str) (str.capacity() + 1)
  88. #define MEMORY_USAGE_MULTIMAP(items, map) MEMORY_USAGE_MAP(items, map)
  89. extern _kc_export ssize_t read_retry(int, void *, size_t);
  90. extern _kc_export ssize_t write_retry(int, const void *, size_t);
  91. extern _kc_export void set_thread_name(pthread_t, const std::string &);
  92. extern _kc_export void my_readahead(int fd);
  93. extern _kc_export void give_filesize_hint(int fd, off_t len);
  94. extern _kc_export bool force_buffers_to_disk(int fd);
  95. extern _kc_export int ec_relocate_fd(int);
  96. /* Determine the size of an array */
  97. template<typename T, size_t N> constexpr inline size_t ARRAY_SIZE(T (&)[N]) { return N; }
  98. /* Get the one-past-end item of an array */
  99. template<typename T, size_t N> constexpr inline T *ARRAY_END(T (&a)[N]) { return a + N; }
  100. } /* namespace */
  101. #endif // PLATFORM_H