1 //===-- Implementation for freelist ---------------------------------------===//
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 //===----------------------------------------------------------------------===//
11 namespace LIBC_NAMESPACE_DECL
{
13 void FreeList::push(Node
*node
) {
15 LIBC_ASSERT(Block
<>::from_usable_space(node
)->outer_size() ==
16 begin_
->block()->outer_size() &&
17 "freelist entries must have the same size");
18 // Since the list is circular, insert the node immediately before begin_.
19 node
->prev
= begin_
->prev
;
21 begin_
->prev
->next
= node
;
24 begin_
= node
->prev
= node
->next
= node
;
28 void FreeList::remove(Node
*node
) {
29 LIBC_ASSERT(begin_
&& "cannot remove from empty list");
30 if (node
== node
->next
) {
31 LIBC_ASSERT(node
== begin_
&&
32 "a self-referential node must be the only element");
35 node
->prev
->next
= node
->next
;
36 node
->next
->prev
= node
->prev
;
42 } // namespace LIBC_NAMESPACE_DECL