Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
iox-#859 Move typed allocator to iox::memory
Browse files Browse the repository at this point in the history
Signed-off-by: Matthias Killat <matthias.killat@apex.ai>
MatthiasKillat committed Jan 31, 2022
1 parent ad29966 commit 9d2a0d2
Showing 7 changed files with 37 additions and 32 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -21,6 +21,7 @@
- Extend cxx::optional constructor for in place construction so that copy/move for values inside the optional even could be deleted[\#967](https://github.com/eclipse-iceoryx/iceoryx/issues/967)
- Add templated `from`/`into` free functions to formalize conversions from enums and other types [#992](https://github.com/eclipse-iceoryx/iceoryx/issues/992)
- UniqueId class for unique IDs within a process [#1010](https://github.com/eclipse-iceoryx/iceoryx/issues/1010)
- Add PrefixTree and TypedAllocator to iceoryx hoofs [\#859](https://github.com/eclipse-iceoryx/iceoryx/issues/859)

**Bugfixes:**

Original file line number Diff line number Diff line change
@@ -14,18 +14,15 @@
//
// SPDX-License-Identifier: Apache-2.0

#ifndef IOX_HOOFS_DATA_STRUCTURES_PREFIX_TREE_HPP
#define IOX_HOOFS_DATA_STRUCTURES_PREFIX_TREE_HPP
#ifndef IOX_HOOFS_CXX_PREFIX_TREE_HPP
#define IOX_HOOFS_CXX_PREFIX_TREE_HPP

#include <iostream>
#include <stdint.h>
#include <string>
#include <vector>

#include "iceoryx_hoofs/cxx/string.hpp"
#include "iceoryx_hoofs/cxx/vector.hpp"
#include "iceoryx_hoofs/data_structures/typed_allocator.hpp"
#include "iceoryx_hoofs/internal/relocatable_pointer/relocatable_ptr.hpp"
#include "iceoryx_hoofs/memory/typed_allocator.hpp"

namespace iox
{
@@ -102,8 +99,9 @@ class PrefixTree

/// @brief Find all values corresponding to keys with a given prefix.
/// @param prefix prefix of keys whose values we want to find
/// @return pointers to values matching the key
/// @return pointers to values matching a key containing the prefix
/// @note returned pointers allow modification of corresponding values in the tree
/// @note exact matches are included if there is a key equal to prefix
cxx::vector<Value*, Capacity> findPrefix(const Key& prefix) const noexcept;

/// @brief Get all key strings currently in the tree.
@@ -224,8 +222,8 @@ class PrefixTree
// suitable external allocators.
// The allocators can allocate enough nodes to support the specified capacity.

using NodeAllocator = iox::cxx::TypedAllocator<Node, NUMBER_OF_ALLOCATABLE_NODES>;
using DataNodeAllocator = iox::cxx::TypedAllocator<DataNode, Capacity>;
using NodeAllocator = iox::memory::TypedAllocator<Node, NUMBER_OF_ALLOCATABLE_NODES>;
using DataNodeAllocator = iox::memory::TypedAllocator<DataNode, Capacity>;

DataNodeAllocator m_dataNodeAllocator;
NodeAllocator m_nodeAllocator;
@@ -287,7 +285,7 @@ class PrefixTree
} // namespace cxx
} // namespace iox

#include "iceoryx_hoofs/data_structures/prefix_tree.inl"
#include "iceoryx_hoofs/internal/cxx/prefix_tree.inl"


#endif // IOX_HOOFS_DATA_STRUCTURES_PREFIX_TREE_HPP
#endif // IOX_HOOFS_CXX_PREFIX_TREE_HPP
Original file line number Diff line number Diff line change
@@ -14,7 +14,10 @@
//
// SPDX-License-Identifier: Apache-2.0

#include "iceoryx_hoofs/data_structures/prefix_tree.hpp"
#ifndef IOX_HOOFS_CXX_PREFIX_TREE_INL
#define IOX_HOOFS_CXX_PREFIX_TREE_INL

#include "iceoryx_hoofs/cxx/prefix_tree.hpp"

namespace iox
{
@@ -717,5 +720,6 @@ void PrefixTree<Value, Capacity, MaxKeyLength>::getPairs(Node* node,
}

} // namespace cxx

} // namespace iox

#endif // IOX_HOOFS_CXX_PREFIX_TREE_INL
Original file line number Diff line number Diff line change
@@ -14,19 +14,19 @@
//
// SPDX-License-Identifier: Apache-2.0

#ifndef IOX_HOOFS_DATA_STRUCTURES_TYPED_ALLOCATOR_INL
#define IOX_HOOFS_DATA_STRUCTURES_TYPED_ALLOCATOR_INL
#ifndef IOX_HOOFS_MEMORY_TYPED_ALLOCATOR_INL
#define IOX_HOOFS_MEMORY_TYPED_ALLOCATOR_INL

#include "iceoryx_hoofs/data_structures/typed_allocator.hpp"
#include "iceoryx_hoofs/memory/typed_allocator.hpp"

#include <stdint.h>

namespace iox
{
namespace cxx
namespace memory
{
template <typename T, uint64_t Capacity>
T* iox::cxx::TypedAllocator<T, Capacity>::allocate()
T* TypedAllocator<T, Capacity>::allocate()
{
auto maybeIndex = m_freeIndices.pop();
if (maybeIndex.has_value())
@@ -37,7 +37,7 @@ T* iox::cxx::TypedAllocator<T, Capacity>::allocate()
}

template <typename T, uint64_t Capacity>
void iox::cxx::TypedAllocator<T, Capacity>::deallocate(T* element)
void TypedAllocator<T, Capacity>::deallocate(T* element)
{
// no checking whether this is a pointer allocated by this allocator (
// (not possible without serious overhead)
@@ -46,7 +46,7 @@ void iox::cxx::TypedAllocator<T, Capacity>::deallocate(T* element)

template <typename T, uint64_t Capacity>
template <typename... Args>
T* iox::cxx::TypedAllocator<T, Capacity>::create(Args&&... args)
T* TypedAllocator<T, Capacity>::create(Args&&... args)
{
auto element = allocate();
if (element)
@@ -58,7 +58,7 @@ T* iox::cxx::TypedAllocator<T, Capacity>::create(Args&&... args)
}

template <typename T, uint64_t Capacity>
void iox::cxx::TypedAllocator<T, Capacity>::destroy(T* element)
void TypedAllocator<T, Capacity>::destroy(T* element)
{
if (element)
{
@@ -67,7 +67,7 @@ void iox::cxx::TypedAllocator<T, Capacity>::destroy(T* element)
}
}

} // namespace cxx
} // namespace memory
} // namespace iox

#endif IOX_HOOFS_DATA_STRUCTURES_TYPED_ALLOCATOR_INL
#endif // IOX_HOOFS_MEMORY_TYPED_ALLOCATOR_INL
Original file line number Diff line number Diff line change
@@ -14,16 +14,16 @@
//
// SPDX-License-Identifier: Apache-2.0

#ifndef IOX_HOOFS_DATA_STRUCTURES_TYPED_ALLOCATOR_HPP
#define IOX_HOOFS_DATA_STRUCTURES_TYPED_ALLOCATOR_HPP
#ifndef IOX_HOOFS_MEMORY_TYPED_ALLOCATOR_HPP
#define IOX_HOOFS_MEMORY_TYPED_ALLOCATOR_HPP

#include <stdint.h>

#include "iceoryx_hoofs/internal/concurrent/lockfree_queue/index_queue.hpp"

namespace iox
{
namespace cxx
namespace memory
{
/// @brief An allocator for objects of a specific type T.
/// Supports allocation of initialized objects and aligned raw memory where objects
@@ -104,9 +104,9 @@ class TypedAllocator
}
};

} // namespace cxx
} // namespace memory
} // namespace iox

#include "iceoryx_hoofs/data_structures/typed_allocator.inl"
#include "iceoryx_hoofs/internal/memory/typed_allocator.inl"

#endif // IOX_HOOFS_DATA_STRUCTURES_TYPED_ALLOCATOR_HPP
#endif // IOX_HOOFS_MEMORY_TYPED_ALLOCATOR_HPP
2 changes: 1 addition & 1 deletion iceoryx_hoofs/test/moduletests/test_prefix_tree.cpp
Original file line number Diff line number Diff line change
@@ -16,7 +16,7 @@

#include "test.hpp"

#include "iceoryx_hoofs/data_structures/prefix_tree.hpp"
#include "iceoryx_hoofs/cxx/prefix_tree.hpp"

#include <set>
#include <vector>
6 changes: 4 additions & 2 deletions iceoryx_hoofs/test/moduletests/test_typed_allocator.cpp
Original file line number Diff line number Diff line change
@@ -16,7 +16,7 @@

#include "test.hpp"

#include "iceoryx_hoofs/data_structures/typed_allocator.hpp"
#include "iceoryx_hoofs/memory/typed_allocator.hpp"

#include <algorithm>
#include <vector>
@@ -25,7 +25,7 @@ namespace
{
using namespace ::testing;

using namespace iox::cxx;
using namespace iox::memory;

// count #ctor and #dtor calls
template <typename T>
@@ -302,5 +302,7 @@ TEST_F(TypedAllocator_test, CreatedElementsHaveUniqueAdresses)
}
}

// todo: relocation test


} // namespace

0 comments on commit 9d2a0d2

Please sign in to comment.