Skip to content

Commit

Permalink
Removing TaskId and reworking handle storage
Browse files Browse the repository at this point in the history
  • Loading branch information
LeStarch committed Apr 10, 2024
1 parent 7ca0c60 commit d74d09e
Show file tree
Hide file tree
Showing 14 changed files with 29 additions and 111 deletions.
1 change: 0 additions & 1 deletion Os/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ if (FPRIME_USE_POSIX)
"${CMAKE_CURRENT_LIST_DIR}/Posix/IntervalTimer.cpp"
"${CMAKE_CURRENT_LIST_DIR}/Posix/Mutex.cpp"
"${CMAKE_CURRENT_LIST_DIR}/Linux/FileSystem.cpp"
"${CMAKE_CURRENT_LIST_DIR}/Posix/TaskId.cpp"
"${CMAKE_CURRENT_LIST_DIR}/Linux/Directory.cpp"
)
endif()
Expand Down
7 changes: 4 additions & 3 deletions Os/Delegate.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <new>
#include <type_traits>
#include "Fw/Types/Assert.hpp"
#include "Os/Os.hpp"
#ifndef OS_DELEGATE_HPP_
#define OS_DELEGATE_HPP_
namespace Os {
Expand Down Expand Up @@ -42,7 +43,7 @@ namespace Delegate {
//! \param aligned_new_memory: memory to be filled via placement new call
//! \return pointer to implementation result of placement new
template<class Interface, class Implementation>
inline Interface *makeDelegate(U8 *aligned_new_memory) {
inline Interface *makeDelegate(HandleStorage& aligned_new_memory) {
FW_ASSERT(aligned_new_memory != nullptr);
// Ensure prerequisites before performing placement new
static_assert(std::is_base_of<Interface, Implementation>::value, "Implementation must derive from Interface");
Expand Down Expand Up @@ -86,12 +87,12 @@ inline Interface *makeDelegate(U8 *aligned_new_memory) {
//! \param to_copy: pointer to Interface to be copied by copy constructor
//! \return pointer to implementation result of placement new
template<class Interface, class Implementation>
inline Interface *makeDelegate(U8 *aligned_new_memory, const Interface *to_copy) {
inline Interface *makeDelegate(HandleStorage& aligned_new_memory, const Interface *to_copy) {
FW_ASSERT(aligned_new_memory != nullptr);
const Implementation *copy_me = reinterpret_cast<const Implementation *>(to_copy);
// Ensure prerequisites before performing placement new
static_assert(std::is_base_of<Interface, Implementation>::value, "Implementation must derive from Interface");
static_assert(sizeof(Implementation) <= FW_HANDLE_MAX_SIZE, "Handle size not large enough");
static_assert(sizeof(Implementation) <= sizeof(aligned_new_memory), "Handle size not large enough");
static_assert((FW_HANDLE_ALIGNMENT % alignof(Implementation)) == 0, "Handle alignment invalid");
// Placement new the object and ensure non-null result
Implementation *interface = nullptr;
Expand Down
6 changes: 3 additions & 3 deletions Os/File.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ extern "C" {
}
namespace Os {

File::File() : m_crc_buffer(), m_handle_storage(), m_delegate(*FileInterface::getDelegate(&m_handle_storage[0])) {
File::File() : m_crc_buffer(), m_handle_storage(), m_delegate(*FileInterface::getDelegate(m_handle_storage)) {
FW_ASSERT(&this->m_delegate == reinterpret_cast<FileInterface*>(&this->m_handle_storage[0]));
}

Expand All @@ -28,7 +28,7 @@ File::File(const File& other) :
m_crc(other.m_crc),
m_crc_buffer(),
m_handle_storage(),
m_delegate(*FileInterface::getDelegate(&m_handle_storage[0], &other.m_delegate)) {
m_delegate(*FileInterface::getDelegate(m_handle_storage, &other.m_delegate)) {
FW_ASSERT(&this->m_delegate == reinterpret_cast<FileInterface*>(&this->m_handle_storage[0]));
}

Expand All @@ -37,7 +37,7 @@ File& File::operator=(const File& other) {
this->m_mode = other.m_mode;
this->m_path = other.m_path;
this->m_crc = other.m_crc;
this->m_delegate = *FileInterface::getDelegate(&m_handle_storage[0], &other.m_delegate);
this->m_delegate = *FileInterface::getDelegate(m_handle_storage, &other.m_delegate);
}
return *this;
}
Expand Down
7 changes: 3 additions & 4 deletions Os/File.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#define Os_File_hpp_

#include <FpConfig.hpp>
#include <Os/Os.hpp>

namespace Os {
//! \brief base implementation of FileHandle
Expand Down Expand Up @@ -206,14 +207,12 @@ namespace Os {
//!
//! \return result of placement new, must be equivalent to `aligned_placement_new_memory`
//!
static FileInterface* getDelegate(U8* aligned_placement_new_memory, const FileInterface* to_copy=nullptr);
static FileInterface* getDelegate(HandleStorage& aligned_placement_new_memory, const FileInterface* to_copy=nullptr);
};


class File final : public FileInterface {
public:
// Required for access to m_handle_storage for static assertions against actual storage
friend FileInterface* FileInterface::getDelegate(U8*, const FileInterface*);
//! \brief constructor
//!
File();
Expand Down Expand Up @@ -485,7 +484,7 @@ namespace Os {
// opaque and thus normal allocation cannot be done. Instead, we allow the implementor to store then handle in
// the byte-array here and set `handle` to that address for storage.
//
alignas(FW_HANDLE_ALIGNMENT) U8 m_handle_storage[FW_HANDLE_MAX_SIZE]; //!< Storage for aligned FileHandle data
alignas(FW_HANDLE_ALIGNMENT) HandleStorage m_handle_storage; //!< Storage for aligned FileHandle data
FileInterface& m_delegate; //!< Delegate for the real implementation
};
}
Expand Down
11 changes: 11 additions & 0 deletions Os/Os.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// ======================================================================
// \title Os/Os.hpp
// \brief common definitions for the OSAL layer
// ======================================================================
#ifndef OS_OS_HPP_
#define OS_OS_HPP_
#include "FpConfig.h"

//! Storage type for OSAL handles
typedef U8 HandleStorage[FW_HANDLE_MAX_SIZE];
#endif
2 changes: 1 addition & 1 deletion Os/Posix/DefaultFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include "Os/Delegate.hpp"

namespace Os {
FileInterface* FileInterface::getDelegate(U8* aligned_new_memory, const FileInterface* to_copy) {
FileInterface* FileInterface::getDelegate(HandleStorage& aligned_new_memory, const FileInterface* to_copy) {
return Os::Delegate::makeDelegate<FileInterface, Os::Posix::File::PosixFile>(aligned_new_memory, to_copy);
}
}
2 changes: 1 addition & 1 deletion Os/Posix/DefaultTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ namespace Os {
return task_status;
}

TaskInterface* TaskInterface::getDelegate(U8* aligned_new_memory) {
TaskInterface* TaskInterface::getDelegate(HandleStorage& aligned_new_memory) {
return Os::Delegate::makeDelegate<TaskInterface, Os::Posix::Task::PosixTask>(aligned_new_memory);
}

Expand Down
3 changes: 1 addition & 2 deletions Os/Posix/Task.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@
#ifndef Os_Posix_Task_hpp_
#define Os_Posix_Task_hpp_

#include <pthread.h>
#include <Os/Task.hpp>

#include <FpConfig.hpp>
#include <Fw/Types/Serializable.hpp>
#include <Os/TaskString.hpp>
#include <Os/Mutex.hpp>

#include <Os/TaskId.hpp>
#include <Fw/Deprecate.hpp>

namespace Os {
Expand Down
34 changes: 0 additions & 34 deletions Os/Posix/TaskId.cpp

This file was deleted.

2 changes: 1 addition & 1 deletion Os/Stub/DefaultFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace Os {
//! \param aligned_new_memory: aligned memory to fill
//! \param to_copy: pointer to copy-constructor input
//! \return: pointer to delegate
FileInterface *FileInterface::getDelegate(U8 *aligned_placement_new_memory, const FileInterface* to_copy) {
FileInterface *FileInterface::getDelegate(HandleStorage& aligned_placement_new_memory, const FileInterface* to_copy) {
return Os::Delegate::makeDelegate<FileInterface, Os::Stub::File::StubFile>(
aligned_placement_new_memory, to_copy
);
Expand Down
2 changes: 1 addition & 1 deletion Os/Stub/test/DefaultFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace Os {
//! \param aligned_new_memory: aligned memory to fill
//! \param to_copy: pointer to copy-constructor input
//! \return: pointer to delegate
FileInterface *FileInterface::getDelegate(U8 *aligned_placement_new_memory, const FileInterface* to_copy) {
FileInterface *FileInterface::getDelegate(HandleStorage& aligned_placement_new_memory, const FileInterface* to_copy) {
return Os::Delegate::makeDelegate<FileInterface, Os::Stub::File::Test::TestFile>(
aligned_placement_new_memory, to_copy
);
Expand Down
8 changes: 3 additions & 5 deletions Os/Task.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
#include <FpConfig.hpp>
#include <Fw/Time/Time.hpp>
#include <Fw/Types/Serializable.hpp>
#include <Os/Os.hpp>
#include <Os/TaskString.hpp>
#include <Os/Mutex.hpp>

#include <Os/TaskId.hpp>
#include <Fw/Deprecate.hpp>
#include <limits>

Expand Down Expand Up @@ -143,7 +143,7 @@ namespace Os {
//!
//! \return result of placement new, must be equivalent to `aligned_placement_new_memory`
//!
static TaskInterface* getDelegate(U8* aligned_placement_new_memory);
static TaskInterface* getDelegate(HandleStorage& aligned_placement_new_memory);

// =================
// Implementation functions (instance) to be supplied by the Os::TaskInterface children
Expand Down Expand Up @@ -202,8 +202,6 @@ namespace Os {
//! parent class. Instead it wraps a delegate provided by `TaskInterface::getDelegate()` to provide system specific
//! behaviour.
class Task final : public TaskInterface {
// Required for access to m_handle_storage for static assertions against actual storage
friend TaskInterface* TaskInterface::getDelegate(U8*);
public:
static constexpr FwSizeType TASK_DEFAULT = std::numeric_limits<FwSizeType>::max();
//! Wrapper for task routine that ensures `onStart()` is called once the task actually begins
Expand Down Expand Up @@ -343,7 +341,7 @@ namespace Os {
// opaque and thus normal allocation cannot be done. Instead, we allow the implementor to store then handle in
// the byte-array here and set `handle` to that address for storage.
//
alignas(FW_HANDLE_ALIGNMENT) U8 m_handle_storage[FW_HANDLE_MAX_SIZE]; //!< Storage for aligned FileHandle data
alignas(FW_HANDLE_ALIGNMENT) HandleStorage m_handle_storage; //!< Storage for aligned FileHandle data
TaskInterface& m_delegate; //!< Delegate for the real implementation
};

Expand Down
30 changes: 0 additions & 30 deletions Os/TaskId.hpp

This file was deleted.

25 changes: 0 additions & 25 deletions Os/TaskIdRepr.hpp

This file was deleted.

0 comments on commit d74d09e

Please sign in to comment.