summaryrefslogtreecommitdiffstats
path: root/src/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/iwasm/fast-jit/jit_utils.h
blob: c165b7a3cf1c8ea143f0d81d521f0f94281df2e0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*
 * Copyright (C) 2021 Intel Corporation.  All rights reserved.
 * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
 */

#ifndef _JIT_UTILS_H_
#define _JIT_UTILS_H_

#include "bh_platform.h"

#ifdef __cplusplus
extern "C" {
#endif

/**
 * A simple fixed size bitmap.
 */
typedef struct JitBitmap {
    /* The first valid bit index.  */
    uintptr_t begin_index;

    /* The last valid bit index plus one.  */
    uintptr_t end_index;

    /* The bitmap.  */
    uint8 map[1];
} JitBitmap;

static inline void *
jit_malloc(unsigned int size)
{
    return wasm_runtime_malloc(size);
}

static inline void *
jit_calloc(unsigned int size)
{
    void *ret = wasm_runtime_malloc(size);
    if (ret) {
        memset(ret, 0, size);
    }
    return ret;
}

static inline void
jit_free(void *ptr)
{
    if (ptr)
        wasm_runtime_free(ptr);
}

/**
 * Create a new bitmap.
 *
 * @param begin_index the first valid bit index
 * @param bitnum maximal bit number of the bitmap.
 *
 * @return the new bitmap if succeeds, NULL otherwise.
 */
JitBitmap *
jit_bitmap_new(uintptr_t begin_index, unsigned bitnum);

/**
 * Delete a bitmap.
 *
 * @param bitmap the bitmap to be deleted
 */
static inline void
jit_bitmap_delete(JitBitmap *bitmap)
{
    jit_free(bitmap);
}

/**
 * Check whether the given index is in the range of the bitmap.
 *
 * @param bitmap the bitmap
 * @param n the bit index
 *
 * @return true if the index is in range, false otherwise
 */
static inline bool
jit_bitmap_is_in_range(JitBitmap *bitmap, unsigned n)
{
    return n >= bitmap->begin_index && n < bitmap->end_index;
}

/**
 * Get a bit in the bitmap
 *
 * @param bitmap the bitmap
 * @param n the n-th bit to be get
 *
 * @return value of the bit
 */
static inline int
jit_bitmap_get_bit(JitBitmap *bitmap, unsigned n)
{
    unsigned idx = n - bitmap->begin_index;
    bh_assert(n >= bitmap->begin_index && n < bitmap->end_index);
    return (bitmap->map[idx / 8] >> (idx % 8)) & 1;
}

/**
 * Set a bit in the bitmap.
 *
 * @param bitmap the bitmap
 * @param n the n-th bit to be set
 */
static inline void
jit_bitmap_set_bit(JitBitmap *bitmap, unsigned n)
{
    unsigned idx = n - bitmap->begin_index;
    bh_assert(n >= bitmap->begin_index && n < bitmap->end_index);
    bitmap->map[idx / 8] |= 1 << (idx % 8);
}

/**
 * Clear a bit in the bitmap.
 *
 * @param bitmap the bitmap
 * @param n the n-th bit to be cleared
 */
static inline void
jit_bitmap_clear_bit(JitBitmap *bitmap, unsigned n)
{
    unsigned idx = n - bitmap->begin_index;
    bh_assert(n >= bitmap->begin_index && n < bitmap->end_index);
    bitmap->map[idx / 8] &= ~(1 << (idx % 8));
}

#ifdef __cplusplus
}
#endif

#endif