jit_utils.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*
  2. * Copyright (C) 2021 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #ifndef _JIT_UTILS_H_
  6. #define _JIT_UTILS_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 JitBitmap {
  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. } JitBitmap;
  22. static inline void *
  23. jit_malloc(unsigned int size)
  24. {
  25. return wasm_runtime_malloc(size);
  26. }
  27. static inline void *
  28. jit_calloc(unsigned int size)
  29. {
  30. void *ret = wasm_runtime_malloc(size);
  31. if (ret) {
  32. memset(ret, 0, size);
  33. }
  34. return ret;
  35. }
  36. static inline void
  37. jit_free(void *ptr)
  38. {
  39. if (ptr)
  40. wasm_runtime_free(ptr);
  41. }
  42. /**
  43. * Create a new bitmap.
  44. *
  45. * @param begin_index the first valid bit index
  46. * @param bitnum maximal bit number of the bitmap.
  47. *
  48. * @return the new bitmap if succeeds, NULL otherwise.
  49. */
  50. JitBitmap *
  51. jit_bitmap_new(uintptr_t begin_index, unsigned bitnum);
  52. /**
  53. * Delete a bitmap.
  54. *
  55. * @param bitmap the bitmap to be deleted
  56. */
  57. static inline void
  58. jit_bitmap_delete(JitBitmap *bitmap)
  59. {
  60. jit_free(bitmap);
  61. }
  62. /**
  63. * Check whether the given index is in the range of the bitmap.
  64. *
  65. * @param bitmap the bitmap
  66. * @param n the bit index
  67. *
  68. * @return true if the index is in range, false otherwise
  69. */
  70. static inline bool
  71. jit_bitmap_is_in_range(JitBitmap *bitmap, unsigned n)
  72. {
  73. return n >= bitmap->begin_index && n < bitmap->end_index;
  74. }
  75. /**
  76. * Get a bit in the bitmap
  77. *
  78. * @param bitmap the bitmap
  79. * @param n the n-th bit to be get
  80. *
  81. * @return value of the bit
  82. */
  83. static inline int
  84. jit_bitmap_get_bit(JitBitmap *bitmap, unsigned n)
  85. {
  86. unsigned idx = n - bitmap->begin_index;
  87. bh_assert(n >= bitmap->begin_index && n < bitmap->end_index);
  88. return (bitmap->map[idx / 8] >> (idx % 8)) & 1;
  89. }
  90. /**
  91. * Set a bit in the bitmap.
  92. *
  93. * @param bitmap the bitmap
  94. * @param n the n-th bit to be set
  95. */
  96. static inline void
  97. jit_bitmap_set_bit(JitBitmap *bitmap, unsigned n)
  98. {
  99. unsigned idx = n - bitmap->begin_index;
  100. bh_assert(n >= bitmap->begin_index && n < bitmap->end_index);
  101. bitmap->map[idx / 8] |= 1 << (idx % 8);
  102. }
  103. /**
  104. * Clear a bit in the bitmap.
  105. *
  106. * @param bitmap the bitmap
  107. * @param n the n-th bit to be cleared
  108. */
  109. static inline void
  110. jit_bitmap_clear_bit(JitBitmap *bitmap, unsigned n)
  111. {
  112. unsigned idx = n - bitmap->begin_index;
  113. bh_assert(n >= bitmap->begin_index && n < bitmap->end_index);
  114. bitmap->map[idx / 8] &= ~(1 << (idx % 8));
  115. }
  116. #ifdef __cplusplus
  117. }
  118. #endif
  119. #endif