Skip to content

Commit

Permalink
[utils] Deprecate allocator::Dynamic for std::allocator
Browse files Browse the repository at this point in the history
  • Loading branch information
salkinium committed Jul 19, 2024
1 parent 5f15719 commit ffb2a4b
Show file tree
Hide file tree
Showing 10 changed files with 52 additions and 50 deletions.
4 changes: 2 additions & 2 deletions src/modm/communication/rpr/interface.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
#include <modm/architecture/utils.hpp>
#include <modm/container/queue.hpp>
#include <modm/processing/timer.hpp>
#include <modm/utils/allocator/dynamic.hpp>
#include <memory>

#include "constants.hpp"

Expand Down Expand Up @@ -123,7 +123,7 @@ namespace modm
static Queue messagesToSend;
static Queue receivedMessages;

static modm::allocator::Dynamic<uint8_t> bufferAllocator;
static std::allocator<uint8_t> bufferAllocator;

static Message receiveBuffer;
static uint8_t rx_buffer[N+8];
Expand Down
2 changes: 1 addition & 1 deletion src/modm/communication/rpr/interface_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -472,7 +472,7 @@ modm::rpr::Interface<Device, N>::popMessage(Queue &queue)
// deallocate the external buffer
Message message = queue.get();
MODM_RPR_LOG("deallocating");
bufferAllocator.deallocate(message.payload);
bufferAllocator.deallocate(message.payload, 1);
// then remove the rest
queue.pop();
}
Expand Down
6 changes: 3 additions & 3 deletions src/modm/container/doubly_linked_list.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
#define MODM_DOUBLY_LINKED_LIST_HPP

#include <stdint.h>
#include <modm/utils/allocator.hpp>
#include <memory>
#include <iterator>

namespace modm
Expand All @@ -30,7 +30,7 @@ namespace modm
* \author Fabian Greif
* \ingroup modm_container
*/
template <typename T, typename Allocator = allocator::Dynamic<T> >
template <typename T, typename Allocator = std::allocator<T> >
class DoublyLinkedList
{
public:
Expand Down Expand Up @@ -91,7 +91,7 @@ namespace modm
// rebind the type to Allocator<Node<T>>. Node<T> is not the same
// size as T (it's one pointer larger), and specializations on T may go
// unused because Node<T> is being bound instead.
typedef typename Allocator::template rebind< Node >::other NodeAllocator;
using NodeAllocator = typename std::allocator_traits<Allocator>::template rebind_alloc<Node>;

NodeAllocator nodeAllocator;

Expand Down
20 changes: 10 additions & 10 deletions src/modm/container/doubly_linked_list_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ modm::DoublyLinkedList<T, Allocator>::~DoublyLinkedList()
Node *node = this->front;
this->front = this->front->next;

Allocator::destroy(&node->value);
this->nodeAllocator.deallocate(node);
std::destroy_at(&node->value);
this->nodeAllocator.deallocate(node, 1);
}
}

Expand Down Expand Up @@ -63,7 +63,7 @@ modm::DoublyLinkedList<T, Allocator>::prepend(const T& value)
{
// allocate memory for the new node and copy the value into it
Node *node = this->nodeAllocator.allocate(1);
Allocator::construct(&node->value, value);
std::construct_at(&node->value, value);

// hook the node into the list
node->next = this->front;
Expand All @@ -88,7 +88,7 @@ modm::DoublyLinkedList<T, Allocator>::append(const T& value)
{
// allocate memory for the new node and copy the value into it
Node *node = this->nodeAllocator.allocate(1);
Allocator::construct(&node->value, value);
std::construct_at(&node->value, value);

// hook the node into the list
node->next = 0;
Expand Down Expand Up @@ -125,8 +125,8 @@ modm::DoublyLinkedList<T, Allocator>::removeFront()
}

// call destructor and free memory
Allocator::destroy(&node->value);
this->nodeAllocator.deallocate(node);
std::destroy_at(&node->value);
this->nodeAllocator.deallocate(node, 1);
}

template <typename T, typename Allocator>
Expand All @@ -148,8 +148,8 @@ modm::DoublyLinkedList<T, Allocator>::removeBack()
}

// call destructor and free memory
Allocator::destroy(&node->value);
this->nodeAllocator.deallocate(node);
std::destroy_at(&node->value);
this->nodeAllocator.deallocate(node, 1);
}

// ----------------------------------------------------------------------------
Expand Down Expand Up @@ -216,8 +216,8 @@ modm::DoublyLinkedList<T, Allocator>::erase(iterator position)

Node* next = position.node->next;

Allocator::destroy(&(position.node->value));
this->nodeAllocator.deallocate(position.node);
std::destroy_at(&(position.node->value));
this->nodeAllocator.deallocate(position.node, 1);

return iterator(next);

Expand Down
4 changes: 2 additions & 2 deletions src/modm/container/dynamic_array.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
#define MODM_DYNAMIC_ARRAY_HPP

#include <cstddef>
#include <modm/utils/allocator.hpp>
#include <memory>
#include <initializer_list>
#include <iterator>

Expand All @@ -44,7 +44,7 @@ namespace modm
* \author Fabian Greif <fabian.greif@rwth-aachen.de>
* \ingroup modm_container
*/
template <typename T, typename Allocator = allocator::Dynamic<T> >
template <typename T, typename Allocator = std::allocator<T> >
class DynamicArray
{
public:
Expand Down
32 changes: 16 additions & 16 deletions src/modm/container/dynamic_array_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ modm::DynamicArray<T, Allocator>::DynamicArray(SizeType n, const T& value, const
{
this->values = this->allocator.allocate(n);
for (SizeType i = 0; i < n; ++i) {
allocator.construct(&this->values[i], value);
std::construct_at(&this->values[i], value);
}
}

Expand All @@ -49,7 +49,7 @@ modm::DynamicArray<T, Allocator>::DynamicArray(std::initializer_list<T> init, co
this->values = this->allocator.allocate(init.size());
std::size_t ii = 0;
for (auto value : init) {
allocator.construct(&this->values[ii], value);
std::construct_at(&this->values[ii], value);
++ii;
}
}
Expand All @@ -61,35 +61,35 @@ modm::DynamicArray<T, Allocator>::DynamicArray(const DynamicArray& other) :
{
this->values = allocator.allocate(other.capacity);
for (SizeType i = 0; i < this->size; ++i) {
this->allocator.construct(&this->values[i], other.values[i]);
std::construct_at(&this->values[i], other.values[i]);
}
}

template <typename T, typename Allocator>
modm::DynamicArray<T, Allocator>::~DynamicArray()
{
for (SizeType i = 0; i < this->size; ++i) {
this->allocator.destroy(&this->values[i]);
std::destroy_at(&this->values[i]);
}
this->allocator.deallocate(this->values);
this->allocator.deallocate(this->values, 1);
}

template <typename T, typename Allocator>
modm::DynamicArray<T, Allocator>&
modm::DynamicArray<T, Allocator>::operator = (const DynamicArray& other)
{
for (SizeType i = 0; i < this->size; ++i) {
this->allocator.destroy(&this->values[i]);
std::destroy_at(&this->values[i]);
}
this->allocator.deallocate(this->values);
this->allocator.deallocate(this->values, 1);

this->allocator = other.allocator;
this->size = other.size;
this->capacity = other.capacity;
this->values = this->allocator.allocate(this->capacity);

for (SizeType i = 0; i < this->size; ++i) {
this->allocator.construct(&this->values[i], other.values[i]);
std::construct_at(&this->values[i], other.values[i]);
}
return *this;
}
Expand All @@ -114,9 +114,9 @@ void
modm::DynamicArray<T, Allocator>::clear()
{
for (SizeType i = 0; i < this->size; ++i) {
this->allocator.destroy(&this->values[i]);
std::destroy_at(&this->values[i]);
}
this->allocator.deallocate(this->values);
this->allocator.deallocate(this->values, 1);
this->values = 0;

this->size = 0;
Expand All @@ -129,7 +129,7 @@ void
modm::DynamicArray<T, Allocator>::removeAll()
{
for (SizeType i = 0; i < this->size; ++i) {
this->allocator.destroy(&this->values[i]);
std::destroy_at(&this->values[i]);
}
this->size = 0;
}
Expand All @@ -149,7 +149,7 @@ modm::DynamicArray<T, Allocator>::append(const T& value)
this->relocate(n);
}

this->allocator.construct(&this->values[this->size], value);
std::construct_at(&this->values[this->size], value);
++this->size;
}

Expand All @@ -161,7 +161,7 @@ modm::DynamicArray<T, Allocator>::removeBack()
--this->size;

// call destructor
this->allocator.destroy(&this->values[this->size]);
std::destroy_at(&this->values[this->size]);
}

// ----------------------------------------------------------------------------
Expand All @@ -173,10 +173,10 @@ modm::DynamicArray<T, Allocator>::relocate(SizeType n)

T* newBuffer = allocator.allocate(n);
for (SizeType i = 0; i < this->size; ++i) {
this->allocator.construct(&newBuffer[i], this->values[i]);
this->allocator.destroy(&this->values[i]);
std::construct_at(&newBuffer[i], this->values[i]);
std::destroy_at(&this->values[i]);
}
this->allocator.deallocate(this->values);
this->allocator.deallocate(this->values, 1);

this->values = newBuffer;
}
Expand Down
9 changes: 4 additions & 5 deletions src/modm/container/linked_list.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

#include <stdint.h>
#include <iterator>
#include <modm/utils/allocator.hpp>
#include <memory>

namespace modm
{
Expand All @@ -29,13 +29,12 @@ namespace modm
* \todo implementation
*
* \tparam T Type of list entries
* \tparam Allocator Allocator used for memory allocation. See
* classes from modm::allocator namespace.
* \tparam Allocator Allocator used for memory allocation.
*
* \author Fabian Greif
* \ingroup modm_container
*/
template <typename T, typename Allocator = allocator::Dynamic<T> >
template <typename T, typename Allocator = std::allocator<T> >
class LinkedList
{
public:
Expand Down Expand Up @@ -106,7 +105,7 @@ namespace modm
// rebind the type to Allocator<Node<T>>. Node<T> is not the same
// size as T (it's one pointer larger), and specializations on T may go
// unused because Node<T> is being bound instead.
typedef typename Allocator::template rebind< Node >::other NodeAllocator;
using NodeAllocator = typename std::allocator_traits<Allocator>::template rebind_alloc<Node>;

NodeAllocator nodeAllocator;

Expand Down
18 changes: 9 additions & 9 deletions src/modm/container/linked_list_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ modm::LinkedList<T, Allocator>::~LinkedList()
Node *node = this->front;
this->front = this->front->next;

Allocator::destroy(&node->value);
this->nodeAllocator.deallocate(node);
std::destroy_at(&node->value);
this->nodeAllocator.deallocate(node, 1);
}
}

Expand Down Expand Up @@ -63,7 +63,7 @@ modm::LinkedList<T, Allocator>::prepend(const T& value)
{
// allocate memory for the new node and copy the value into it
Node *node = this->nodeAllocator.allocate(1);
Allocator::construct(&node->value, value);
std::construct_at(&node->value, value);

// hook the node into the list
node->next = this->front;
Expand All @@ -83,7 +83,7 @@ modm::LinkedList<T, Allocator>::append(const T& value)
{
// allocate memory for the new node and copy the value into it
Node *node = this->nodeAllocator.allocate(1);
Allocator::construct(&node->value, value);
std::construct_at(&node->value, value);

// hook the node into the list
node->next = 0;
Expand Down Expand Up @@ -111,7 +111,7 @@ modm::LinkedList<T, Allocator>::insert(const_iterator pos, const T& value)

// allocate memory for the new node and copy the value into it
Node *node = this->nodeAllocator.allocate(1);
Allocator::construct(&node->value, value);
std::construct_at(&node->value, value);

// hook the node into the list
node->next = pos.node->next;
Expand Down Expand Up @@ -141,8 +141,8 @@ modm::LinkedList<T, Allocator>::removeFront()
}

// call destructor and free memory
Allocator::destroy(&node->value);
this->nodeAllocator.deallocate(node);
std::destroy_at(&node->value);
this->nodeAllocator.deallocate(node, 1);
}

// ----------------------------------------------------------------------------
Expand Down Expand Up @@ -209,8 +209,8 @@ modm::LinkedList<T, Allocator>::remove(const iterator& iter)
node->next = iter.node->next;

// call destructor and free memory
Allocator::destroy(&iter.node->value);
this->nodeAllocator.deallocate(iter.node);
std::destroy_at(&iter.node->value);
this->nodeAllocator.deallocate(iter.node, 1);

return iterator(node->next);
}
Expand Down
3 changes: 1 addition & 2 deletions src/modm/container/module.lb
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ def init(module):
def prepare(module, options):
module.depends(
":architecture",
":io",
":utils")
":io")
return True


Expand Down
4 changes: 4 additions & 0 deletions src/modm/utils/allocator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@
namespace modm::allocator
{

// DEPRECATED: 2025q3
/**
* Base class for all allocator types
*
* @ingroup modm_utils
* @author Fabian Greif
*/
[[deprecated("Please use std::allocator<T> instead!")]]
template <typename T>
class AllocatorBase
{
Expand Down Expand Up @@ -66,6 +68,7 @@ class AllocatorBase
}
};

// DEPRECATED: 2025q3
/**
* Dynamic memory allocator
*
Expand All @@ -75,6 +78,7 @@ class AllocatorBase
* @ingroup modm_utils
* @author Fabian Greif
*/
[[deprecated("Please use std::allocator<T> instead!")]]
template <typename T>
class Dynamic : public AllocatorBase<T>
{
Expand Down

0 comments on commit ffb2a4b

Please sign in to comment.