avb_sysdeps.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /* SPDX-License-Identifier: MIT */
  2. /*
  3. * Copyright (C) 2016 The Android Open Source Project
  4. */
  5. #if !defined(AVB_INSIDE_LIBAVB_H) && !defined(AVB_COMPILATION)
  6. #error "Never include this file directly, include libavb.h instead."
  7. #endif
  8. #ifndef AVB_SYSDEPS_H_
  9. #define AVB_SYSDEPS_H_
  10. #ifdef __cplusplus
  11. extern "C" {
  12. #endif
  13. /* Change these includes to match your platform to bring in the
  14. * equivalent types available in a normal C runtime. At least things
  15. * like uint8_t, uint64_t, and bool (with |false|, |true| keywords)
  16. * must be present.
  17. */
  18. #include <common.h>
  19. /* If you don't have gcc or clang, these attribute macros may need to
  20. * be adjusted.
  21. */
  22. #define AVB_ATTR_WARN_UNUSED_RESULT __attribute__((warn_unused_result))
  23. #define AVB_ATTR_PACKED __attribute__((packed))
  24. #define AVB_ATTR_NO_RETURN __attribute__((noreturn))
  25. #define AVB_ATTR_SENTINEL __attribute__((__sentinel__))
  26. /* Size in bytes used for alignment. */
  27. #ifdef __LP64__
  28. #define AVB_ALIGNMENT_SIZE 8
  29. #else
  30. #define AVB_ALIGNMENT_SIZE 4
  31. #endif
  32. /* Compare |n| bytes in |src1| and |src2|.
  33. *
  34. * Returns an integer less than, equal to, or greater than zero if the
  35. * first |n| bytes of |src1| is found, respectively, to be less than,
  36. * to match, or be greater than the first |n| bytes of |src2|. */
  37. int avb_memcmp(const void* src1,
  38. const void* src2,
  39. size_t n) AVB_ATTR_WARN_UNUSED_RESULT;
  40. /* Compare two strings.
  41. *
  42. * Return an integer less than, equal to, or greater than zero if |s1|
  43. * is found, respectively, to be less than, to match, or be greater
  44. * than |s2|.
  45. */
  46. int avb_strcmp(const char* s1, const char* s2);
  47. /* Compare |n| bytes in two strings.
  48. *
  49. * Return an integer less than, equal to, or greater than zero if the
  50. * first |n| bytes of |s1| is found, respectively, to be less than,
  51. * to match, or be greater than the first |n| bytes of |s2|.
  52. */
  53. int avb_strncmp(const char* s1, const char* s2, size_t n);
  54. /* Copy |n| bytes from |src| to |dest|. */
  55. void* avb_memcpy(void* dest, const void* src, size_t n);
  56. /* Set |n| bytes starting at |s| to |c|. Returns |dest|. */
  57. void* avb_memset(void* dest, const int c, size_t n);
  58. /* Prints out a message. The string passed must be a NUL-terminated
  59. * UTF-8 string.
  60. */
  61. void avb_print(const char* message);
  62. /* Prints out a vector of strings. Each argument must point to a
  63. * NUL-terminated UTF-8 string and NULL should be the last argument.
  64. */
  65. void avb_printv(const char* message, ...) AVB_ATTR_SENTINEL;
  66. /* Aborts the program or reboots the device. */
  67. void avb_abort(void) AVB_ATTR_NO_RETURN;
  68. /* Allocates |size| bytes. Returns NULL if no memory is available,
  69. * otherwise a pointer to the allocated memory.
  70. *
  71. * The memory is not initialized.
  72. *
  73. * The pointer returned is guaranteed to be word-aligned.
  74. *
  75. * The memory should be freed with avb_free() when you are done with it.
  76. */
  77. void* avb_malloc_(size_t size) AVB_ATTR_WARN_UNUSED_RESULT;
  78. /* Frees memory previously allocated with avb_malloc(). */
  79. void avb_free(void* ptr);
  80. /* Returns the lenght of |str|, excluding the terminating NUL-byte. */
  81. size_t avb_strlen(const char* str) AVB_ATTR_WARN_UNUSED_RESULT;
  82. /* Divide the |dividend| by 10 and saves back to the pointer. Return the
  83. * remainder. */
  84. uint32_t avb_div_by_10(uint64_t* dividend);
  85. #ifdef __cplusplus
  86. }
  87. #endif
  88. #endif /* AVB_SYSDEPS_H_ */