libdrm_macros.h 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /*
  2. * SPDX-FileCopyrightText: 2014 NVIDIA Corporation
  3. * SPDX-License-Identifier: MIT
  4. */
  5. #ifndef LIBDRM_LIBDRM_H
  6. #define LIBDRM_LIBDRM_H
  7. #if HAVE_VISIBILITY
  8. # define drm_private __attribute__((visibility("hidden")))
  9. # define drm_public __attribute__((visibility("default")))
  10. #else
  11. # define drm_private
  12. # define drm_public
  13. #endif
  14. /**
  15. * Static (compile-time) assertion.
  16. * Basically, use COND to dimension an array. If COND is false/zero the
  17. * array size will be -1 and we'll get a compilation error.
  18. */
  19. #define STATIC_ASSERT(COND) \
  20. do { \
  21. (void) sizeof(char [1 - 2*!(COND)]); \
  22. } while (0)
  23. #include <sys/mman.h>
  24. #if defined(ANDROID) && !defined(__LP64__)
  25. #include <errno.h> /* for EINVAL */
  26. static inline void *drm_mmap(void *addr, size_t length, int prot, int flags,
  27. int fd, loff_t offset)
  28. {
  29. /* offset must be aligned to 4096 (not necessarily the page size) */
  30. if (offset & 4095) {
  31. errno = EINVAL;
  32. return MAP_FAILED;
  33. }
  34. return mmap64(addr, length, prot, flags, fd, offset);
  35. }
  36. # define drm_munmap(addr, length) \
  37. munmap(addr, length)
  38. #else
  39. /* assume large file support exists */
  40. # define drm_mmap(addr, length, prot, flags, fd, offset) \
  41. mmap(addr, length, prot, flags, fd, offset)
  42. static inline int drm_munmap(void *addr, size_t length)
  43. {
  44. /* Copied from configure code generated by AC_SYS_LARGEFILE */
  45. #define LARGE_OFF_T ((((off_t) 1 << 31) << 31) - 1 + \
  46. (((off_t) 1 << 31) << 31))
  47. STATIC_ASSERT(LARGE_OFF_T % 2147483629 == 721 &&
  48. LARGE_OFF_T % 2147483647 == 1);
  49. #undef LARGE_OFF_T
  50. return munmap(addr, length);
  51. }
  52. #endif
  53. #endif