bh_bitmap.h 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*
  2. * Copyright (C) 2021 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #ifndef _BH_BITMAP_H
  6. #define _BH_BITMAP_H
  7. #include "bh_platform.h"
  8. #ifdef __cplusplus
  9. extern "C" {
  10. #endif
  11. /**
  12. * A simple fixed size bitmap.
  13. */
  14. typedef struct bh_bitmap {
  15. /* The first valid bit index. */
  16. uintptr_t begin_index;
  17. /* The last valid bit index plus one. */
  18. uintptr_t end_index;
  19. /* The bitmap. */
  20. uint8 map[1];
  21. } bh_bitmap;
  22. /**
  23. * Create a new bitmap.
  24. *
  25. * @param begin_index the first valid bit index
  26. * @param bitnum maximal bit number of the bitmap.
  27. *
  28. * @return the new bitmap if succeeds, NULL otherwise.
  29. */
  30. bh_bitmap *
  31. bh_bitmap_new(uintptr_t begin_index, unsigned bitnum);
  32. /**
  33. * Delete a bitmap.
  34. *
  35. * @param bitmap the bitmap to be deleted
  36. */
  37. static inline void
  38. bh_bitmap_delete(bh_bitmap *bitmap)
  39. {
  40. if (bitmap != NULL)
  41. BH_FREE(bitmap);
  42. }
  43. /**
  44. * Check whether the given index is in the range of the bitmap.
  45. *
  46. * @param bitmap the bitmap
  47. * @param n the bit index
  48. *
  49. * @return true if the index is in range, false otherwise
  50. */
  51. static inline bool
  52. bh_bitmap_is_in_range(bh_bitmap *bitmap, uintptr_t n)
  53. {
  54. return n >= bitmap->begin_index && n < bitmap->end_index;
  55. }
  56. /**
  57. * Get a bit in the bitmap
  58. *
  59. * @param bitmap the bitmap
  60. * @param n the n-th bit to be get
  61. *
  62. * @return value of the bit
  63. */
  64. static inline int
  65. bh_bitmap_get_bit(bh_bitmap *bitmap, uintptr_t n)
  66. {
  67. uintptr_t idx = n - bitmap->begin_index;
  68. bh_assert(n >= bitmap->begin_index && n < bitmap->end_index);
  69. return (bitmap->map[idx / 8] >> (idx % 8)) & 1;
  70. }
  71. /**
  72. * Set a bit in the bitmap.
  73. *
  74. * @param bitmap the bitmap
  75. * @param n the n-th bit to be set
  76. */
  77. static inline void
  78. bh_bitmap_set_bit(bh_bitmap *bitmap, uintptr_t n)
  79. {
  80. uintptr_t idx = n - bitmap->begin_index;
  81. bh_assert(n >= bitmap->begin_index && n < bitmap->end_index);
  82. bitmap->map[idx / 8] |= 1 << (idx % 8);
  83. }
  84. /**
  85. * Clear a bit in the bitmap.
  86. *
  87. * @param bitmap the bitmap
  88. * @param n the n-th bit to be cleared
  89. */
  90. static inline void
  91. bh_bitmap_clear_bit(bh_bitmap *bitmap, uintptr_t n)
  92. {
  93. uintptr_t idx = n - bitmap->begin_index;
  94. bh_assert(n >= bitmap->begin_index && n < bitmap->end_index);
  95. bitmap->map[idx / 8] &= ~(1 << (idx % 8));
  96. }
  97. #ifdef __cplusplus
  98. }
  99. #endif
  100. #endif