util.h 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /* Copyright (c) 2003-2004, Roger Dingledine
  2. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  3. * Copyright (c) 2007-2018, The Tor Project, Inc. */
  4. /* See LICENSE for licensing information */
  5. /**
  6. * \file util.h
  7. * \brief Headers for util.c
  8. **/
  9. #ifndef TOR_UTIL_H
  10. #define TOR_UTIL_H
  11. #include "orconfig.h"
  12. #include "lib/cc/torint.h"
  13. #include "common/compat.h"
  14. #include "lib/ctime/di_ops.h"
  15. #include "lib/testsupport/testsupport.h"
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #ifdef _WIN32
  19. /* for the correct alias to struct stat */
  20. #include <sys/stat.h>
  21. #endif
  22. #include "lib/err/torerr.h"
  23. #include "lib/malloc/util_malloc.h"
  24. #include "lib/wallclock/approx_time.h"
  25. #include "lib/string/util_string.h"
  26. #include "lib/string/parse_int.h"
  27. #include "lib/string/scanf.h"
  28. #include "lib/intmath/bits.h"
  29. #include "lib/intmath/addsub.h"
  30. #include "lib/intmath/muldiv.h"
  31. #include "lib/intmath/cmp.h"
  32. #include "lib/log/ratelim.h"
  33. #include "lib/log/util_bug.h"
  34. #include "lib/log/escape.h"
  35. #include "lib/fs/dir.h"
  36. #include "lib/fs/files.h"
  37. #include "lib/fs/path.h"
  38. #include "lib/encoding/time_fmt.h"
  39. #include "lib/encoding/cstring.h"
  40. void tor_log_mallinfo(int severity);
  41. /** Macro: yield a pointer to an enclosing structure given a pointer to
  42. * a substructure at offset <b>off</b>. Example:
  43. * <pre>
  44. * struct base { ... };
  45. * struct subtype { int x; struct base b; } x;
  46. * struct base *bp = &x.base;
  47. * struct *sp = SUBTYPE_P(bp, struct subtype, b);
  48. * </pre>
  49. */
  50. #define SUBTYPE_P(p, subtype, basemember) \
  51. ((void*) ( ((char*)(p)) - offsetof(subtype, basemember) ))
  52. /* Logic */
  53. /** Macro: true if two values have the same boolean value. */
  54. #define bool_eq(a,b) (!(a)==!(b))
  55. /** Macro: true if two values have different boolean values. */
  56. #define bool_neq(a,b) (!(a)!=!(b))
  57. /* Math functions */
  58. double tor_mathlog(double d) ATTR_CONST;
  59. long tor_lround(double d) ATTR_CONST;
  60. int64_t tor_llround(double d) ATTR_CONST;
  61. int64_t sample_laplace_distribution(double mu, double b, double p);
  62. int64_t add_laplace_noise(int64_t signal, double random, double delta_f,
  63. double epsilon);
  64. int64_t clamp_double_to_int64(double number);
  65. /* String manipulation */
  66. /* Time helpers */
  67. long tv_udiff(const struct timeval *start, const struct timeval *end);
  68. long tv_mdiff(const struct timeval *start, const struct timeval *end);
  69. int64_t tv_to_msec(const struct timeval *tv);
  70. /* File helpers */
  71. #define write_all(fd, buf, count, isSock) \
  72. ((isSock) ? write_all_to_socket((fd), (buf), (count)) \
  73. : write_all_to_fd((int)(fd), (buf), (count)))
  74. #define read_all(fd, buf, count, isSock) \
  75. ((isSock) ? read_all_from_socket((fd), (buf), (count)) \
  76. : read_all_from_fd((int)(fd), (buf), (count)))
  77. #ifdef _WIN32
  78. HANDLE load_windows_system_library(const TCHAR *library_name);
  79. #endif
  80. #endif /* !defined(TOR_UTIL_H) */