util.h 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef GIT_COMPAT_UTIL_H
  3. #define GIT_COMPAT_UTIL_H
  4. #define _BSD_SOURCE 1
  5. /* glibc 2.20 deprecates _BSD_SOURCE in favour of _DEFAULT_SOURCE */
  6. #define _DEFAULT_SOURCE 1
  7. #include <stdbool.h>
  8. #include <stddef.h>
  9. #include <stdlib.h>
  10. #include <stdarg.h>
  11. #include <linux/compiler.h>
  12. #include <sys/types.h>
  13. /* General helper functions */
  14. void usage(const char *err) __noreturn;
  15. void die(const char *err, ...) __noreturn __printf(1, 2);
  16. static inline void *zalloc(size_t size)
  17. {
  18. return calloc(1, size);
  19. }
  20. #define zfree(ptr) ({ free((void *)*ptr); *ptr = NULL; })
  21. struct dirent;
  22. struct nsinfo;
  23. struct strlist;
  24. int mkdir_p(char *path, mode_t mode);
  25. int rm_rf(const char *path);
  26. struct strlist *lsdir(const char *name, bool (*filter)(const char *, struct dirent *));
  27. bool lsdir_no_dot_filter(const char *name, struct dirent *d);
  28. int copyfile(const char *from, const char *to);
  29. int copyfile_mode(const char *from, const char *to, mode_t mode);
  30. int copyfile_ns(const char *from, const char *to, struct nsinfo *nsi);
  31. ssize_t readn(int fd, void *buf, size_t n);
  32. ssize_t writen(int fd, const void *buf, size_t n);
  33. size_t hex_width(u64 v);
  34. int hex2u64(const char *ptr, u64 *val);
  35. extern unsigned int page_size;
  36. int __pure cacheline_size(void);
  37. int sysctl__max_stack(void);
  38. int fetch_kernel_version(unsigned int *puint,
  39. char *str, size_t str_sz);
  40. #define KVER_VERSION(x) (((x) >> 16) & 0xff)
  41. #define KVER_PATCHLEVEL(x) (((x) >> 8) & 0xff)
  42. #define KVER_SUBLEVEL(x) ((x) & 0xff)
  43. #define KVER_FMT "%d.%d.%d"
  44. #define KVER_PARAM(x) KVER_VERSION(x), KVER_PATCHLEVEL(x), KVER_SUBLEVEL(x)
  45. const char *perf_tip(const char *dirpath);
  46. #ifndef HAVE_GET_CURRENT_DIR_NAME
  47. char *get_current_dir_name(void);
  48. #endif
  49. #ifndef HAVE_SCHED_GETCPU_SUPPORT
  50. int sched_getcpu(void);
  51. #endif
  52. #ifndef HAVE_SETNS_SUPPORT
  53. int setns(int fd, int nstype);
  54. #endif
  55. extern bool perf_singlethreaded;
  56. void perf_set_singlethreaded(void);
  57. void perf_set_multithreaded(void);
  58. #ifndef O_CLOEXEC
  59. #ifdef __sparc__
  60. #define O_CLOEXEC 0x400000
  61. #elif defined(__alpha__) || defined(__hppa__)
  62. #define O_CLOEXEC 010000000
  63. #else
  64. #define O_CLOEXEC 02000000
  65. #endif
  66. #endif
  67. #endif /* GIT_COMPAT_UTIL_H */