Skip to content

Commit

Permalink
can only push nodes that have nullptr links, make removed node safe f…
Browse files Browse the repository at this point in the history
…or reinsertion.

lock-free in fixed preallocated buffer allocator class.
  • Loading branch information
dok-net committed Sep 15, 2023
1 parent 7719e16 commit 313dd42
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/circular_queue/lfllist.h
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,8 @@ namespace ghostl
}
next->remove_lock.store(false);
node->remove_lock.store(false);
node->pred.store(nullptr);
node->next.store(nullptr);
return true;
};

Expand Down
86 changes: 86 additions & 0 deletions src/circular_queue/lfllist_allocator.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
lfllist_allocator.h
A lock free double-linked list based allocator.
Copyright (c) 2023 Dirk O. Kaar
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/

#pragma once

#ifndef __LFLLIST_ALLOCATOR_H
#define __LFLLIST_ALLOCATOR_H

#include "lfllist.h"

namespace ghostl
{
template<typename T, std::size_t CAPACITY>
struct lfllist_allocator : private lfllist<T>
{
// type definitions
using lfllist = lfllist<T>;
using node_type = lfllist::node_type;
using value_type = T;
using pointer = T*;
using const_pointer = const T*;
using reference = T&;
using const_reference = const T&;
using size_type = std::size_t;
using difference_type = std::ptrdiff_t;

lfllist_allocator()
{
for (size_type i = 0; i < CAPACITY; ++i)
{
auto node = reinterpret_cast<node_type*>(&span[i * sizeof(node_type)]);
node = new (node) node_type();
lfllist::push(node);
}
}
lfllist_allocator(const lfllist_allocator&) = delete;
lfllist_allocator(lfllist_allocator&&) = delete;
~lfllist_allocator()
{
node_type* node;
while (nullptr != (node = lfllist::back()))
{
lfllist::remove(node);
}
}
auto operator =(const lfllist_allocator&)->lfllist_allocator & = delete;
auto operator =(lfllist_allocator&&)->lfllist_allocator & = delete;
[[nodiscard]] pointer allocate(size_type n, const void* = nullptr)
{
if (n != 1)
{
return nullptr;
}
node_type* node;
if (!lfllist::try_pop(node))
{
return nullptr;
}
return reinterpret_cast<pointer>(node);
}
constexpr void deallocate(pointer const p, size_t n)
{
auto node = reinterpret_cast<node_type*>(p);
lfllist::push(node);
}
private:
char span[CAPACITY * sizeof(node_type)] = { 0 };
};
}

#endif // __LFLLIST_ALLOCATOR_H

0 comments on commit 313dd42

Please sign in to comment.