summaryrefslogtreecommitdiffstats
path: root/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/shared/utils/bh_list.c
blob: 7102d42a14195804a1be06c52524d9ce9ebeeb29 (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
/*
 * Copyright (C) 2019 Intel Corporation.  All rights reserved.
 * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
 */

#include "bh_list.h"

#if BH_DEBUG != 0
/**
 * Test whehter a pointer value has exist in given list.
 *
 * @param list    pointer to list.
 * @param elem    pointer to elem that will be inserted into list.
 * @return        <code>true</code> if the pointer has been in the list;
 *                <code>false</code> otherwise.
 */
static bool
bh_list_is_elem_exist(bh_list *list, void *elem);
#endif

bh_list_status
bh_list_init(bh_list *list)
{
    if (!list)
        return BH_LIST_ERROR;

    (list->head).next = NULL;
    list->len = 0;
    return BH_LIST_SUCCESS;
}

bh_list_status
bh_list_insert(bh_list *list, void *elem)
{
    bh_list_link *p = NULL;

    if (!list || !elem)
        return BH_LIST_ERROR;
#if BH_DEBUG != 0
    bh_assert(!bh_list_is_elem_exist(list, elem));
#endif
    p = (bh_list_link *)elem;
    p->next = (list->head).next;
    (list->head).next = p;
    list->len++;
    return BH_LIST_SUCCESS;
}

bh_list_status
bh_list_remove(bh_list *list, void *elem)
{
    bh_list_link *cur = NULL;
    bh_list_link *prev = NULL;

    if (!list || !elem)
        return BH_LIST_ERROR;

    cur = (list->head).next;

    while (cur) {
        if (cur == elem) {
            if (prev)
                prev->next = cur->next;
            else
                (list->head).next = cur->next;

            list->len--;
            return BH_LIST_SUCCESS;
        }

        prev = cur;
        cur = cur->next;
    }

    return BH_LIST_ERROR;
}

uint32
bh_list_length(bh_list *list)
{
    return (list ? list->len : 0);
}

void *
bh_list_first_elem(bh_list *list)
{
    return (list ? (list->head).next : NULL);
}

void *
bh_list_elem_next(void *node)
{
    return (node ? ((bh_list_link *)node)->next : NULL);
}

#if BH_DEBUG != 0
static bool
bh_list_is_elem_exist(bh_list *list, void *elem)
{
    bh_list_link *p = NULL;

    if (!list || !elem)
        return false;

    p = (list->head).next;
    while (p && p != elem)
        p = p->next;

    return (p != NULL);
}
#endif