1 //===-- Intrusive queue implementation. -------------------------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // An intrusive list that implements the insque and remque semantics.
11 //===----------------------------------------------------------------------===//
13 #ifndef LLVM_LIBC_SRC___SUPPORT_INTRUSIVE_LIST_H
14 #define LLVM_LIBC_SRC___SUPPORT_INTRUSIVE_LIST_H
17 #include "src/__support/macros/config.h"
19 namespace LIBC_NAMESPACE_DECL
{
23 struct IntrusiveNodeHeader
{
24 IntrusiveNodeHeader
*next
;
25 IntrusiveNodeHeader
*prev
;
29 LIBC_INLINE
static void insert(void *elem
, void *prev
) {
30 auto elem_header
= static_cast<IntrusiveNodeHeader
*>(elem
);
31 auto prev_header
= static_cast<IntrusiveNodeHeader
*>(prev
);
34 // The list is linear and elem will be the only element.
35 elem_header
->next
= nullptr;
36 elem_header
->prev
= nullptr;
40 auto next
= prev_header
->next
;
42 elem_header
->next
= next
;
43 elem_header
->prev
= prev_header
;
45 prev_header
->next
= elem_header
;
47 next
->prev
= elem_header
;
50 LIBC_INLINE
static void remove(void *elem
) {
51 auto elem_header
= static_cast<IntrusiveNodeHeader
*>(elem
);
53 auto prev
= elem_header
->prev
;
54 auto next
= elem_header
->next
;
63 } // namespace internal
64 } // namespace LIBC_NAMESPACE_DECL
66 #endif // LLVM_LIBC_SRC___SUPPORT_INTRUSIVE_LIST_H