avb_sysdeps.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*
  2. * Copyright (C) 2016 The Android Open Source Project
  3. *
  4. * Permission is hereby granted, free of charge, to any person
  5. * obtaining a copy of this software and associated documentation
  6. * files (the "Software"), to deal in the Software without
  7. * restriction, including without limitation the rights to use, copy,
  8. * modify, merge, publish, distribute, sublicense, and/or sell copies
  9. * of the Software, and to permit persons to whom the Software is
  10. * furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be
  13. * included in all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  16. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  18. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  19. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  20. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  21. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  22. * SOFTWARE.
  23. */
  24. #if !defined(AVB_INSIDE_LIBAVB_H) && !defined(AVB_COMPILATION)
  25. #error "Never include this file directly, include libavb.h instead."
  26. #endif
  27. #ifndef AVB_SYSDEPS_H_
  28. #define AVB_SYSDEPS_H_
  29. #ifdef __cplusplus
  30. extern "C" {
  31. #endif
  32. /* Change these includes to match your platform to bring in the
  33. * equivalent types available in a normal C runtime. At least things
  34. * like uint8_t, uint64_t, and bool (with |false|, |true| keywords)
  35. * must be present.
  36. */
  37. #include <inttypes.h>
  38. #include <stddef.h>
  39. #include <stdint.h>
  40. #include <pal_typedefs.h>
  41. /* If you don't have gcc or clang, these attribute macros may need to
  42. * be adjusted.
  43. */
  44. #define AVB_ATTR_WARN_UNUSED_RESULT __attribute__((warn_unused_result))
  45. #define AVB_ATTR_PACKED __attribute__((packed))
  46. #define AVB_ATTR_NO_RETURN __attribute__((noreturn))
  47. #define AVB_ATTR_SENTINEL __attribute__((__sentinel__))
  48. /* Size in bytes used for alignment. */
  49. #ifdef __LP64__
  50. #define AVB_ALIGNMENT_SIZE 8
  51. #else
  52. #define AVB_ALIGNMENT_SIZE 4
  53. #endif
  54. /* Compare |n| bytes in |src1| and |src2|.
  55. *
  56. * Returns an integer less than, equal to, or greater than zero if the
  57. * first |n| bytes of |src1| is found, respectively, to be less than,
  58. * to match, or be greater than the first |n| bytes of |src2|. */
  59. int avb_memcmp(const void* src1,
  60. const void* src2,
  61. size_t n) AVB_ATTR_WARN_UNUSED_RESULT;
  62. /* Compare two strings.
  63. *
  64. * Return an integer less than, equal to, or greater than zero if |s1|
  65. * is found, respectively, to be less than, to match, or be greater
  66. * than |s2|.
  67. */
  68. int avb_strcmp(const char* s1, const char* s2);
  69. /* Compare |n| bytes in two strings.
  70. *
  71. * Return an integer less than, equal to, or greater than zero if the
  72. * first |n| bytes of |s1| is found, respectively, to be less than,
  73. * to match, or be greater than the first |n| bytes of |s2|.
  74. */
  75. int avb_strncmp(const char* s1, const char* s2, size_t n);
  76. /* Copy |n| bytes from |src| to |dest|. */
  77. void* avb_memcpy(void* dest, const void* src, size_t n);
  78. /* Set |n| bytes starting at |s| to |c|. Returns |dest|. */
  79. void* avb_memset(void* dest, const int c, size_t n);
  80. /* Prints out a message. The string passed must be a NUL-terminated
  81. * UTF-8 string.
  82. */
  83. void avb_print(const char* message);
  84. /* Prints out a vector of strings. Each argument must point to a
  85. * NUL-terminated UTF-8 string and NULL should be the last argument.
  86. */
  87. void avb_printv(const char* message, ...) AVB_ATTR_SENTINEL;
  88. /* Aborts the program or reboots the device. */
  89. void avb_abort(void) AVB_ATTR_NO_RETURN;
  90. /* Allocates |size| bytes. Returns NULL if no memory is available,
  91. * otherwise a pointer to the allocated memory.
  92. *
  93. * The memory is not initialized.
  94. *
  95. * The pointer returned is guaranteed to be word-aligned.
  96. *
  97. * The memory should be freed with avb_free() when you are done with it.
  98. */
  99. void* avb_malloc_(size_t size) AVB_ATTR_WARN_UNUSED_RESULT;
  100. /* Frees memory previously allocated with avb_malloc(). */
  101. void avb_free(void* ptr);
  102. /* Returns the lenght of |str|, excluding the terminating NUL-byte. */
  103. size_t avb_strlen(const char* str) AVB_ATTR_WARN_UNUSED_RESULT;
  104. /* Divide the |dividend| by 10 and saves back to the pointer. Return the
  105. * remainder. */
  106. uint32_t avb_div_by_10(uint64_t* dividend);
  107. #ifdef __cplusplus
  108. }
  109. #endif
  110. #endif /* AVB_SYSDEPS_H_ */