/** * @file include/ActivMiddleware/ActivFramework/MessageImpl.h * * @brief Header file for message implementation class. */ #if (!defined ACTIV_FRAMEWORK_MESSAGE_IMPL_H) #define ACTIV_FRAMEWORK_MESSAGE_IMPL_H #include "ActivMiddleware/ActivFramework/MessageKey.h" #include "ActivMiddleware/ActivFramework/Address.h" #include "ActivMiddleware/ActivFramework/Subject.h" #include "ActivMiddleware/ActivFramework/RequestId.h" #include "ActivMiddleware/ActivBase/Ip/IpAddress.h" //! Activ namespace. namespace Activ { class EndPoint; class Heap; class HeapMessage; class HeapMessageQueue; class ManagedEndPointImpl; class PrivateHeap; class SharedHeap; class SystemImpl; class EndPointWalker; class HeapWalker; // --------------------------------------------------------------------------------------------------------------------------------- /** * @brief Message implementation class. */ class MessageImpl : private Boost::noncopyable { private: // To avoid compiler issues with additional size_t parameter to placement new. class BlockSizeWrapper { public: BlockSizeWrapper(const size_t value) : m_value(value) { } size_t m_value; }; /** * @brief Allocate a message from local memory. * * @param basicSize size of the message class. * @param blockSize the size of the data portion of this message. * * @return Pointer to allocated memory. */ static void *operator new(size_t basicSize, const BlockSizeWrapper &blockSize); /** * @brief Free a local memory message. * * @param p the allocated memory. */ static void operator delete(void *p); /** * @brief Free a local memory message. * * @param p the allocated memory. * @param blockSize the size of the data portion of this message. */ static void operator delete(void *p, const BlockSizeWrapper &blockSize); /** * @brief Construct a shared memory message. * * @param heapId the id of the heap containing this message. * @param bucketId the bucket within the heap containing this message. * @param thisMessageId message id of this message. * @param continuationMessageId message id of the next message in the heap. * @param blockSize the size of the data portion of this message. */ MessageImpl(HeapId heapId, BucketId bucketId, MessageId thisMessageId, MessageId continuationMessageId, uint32_t blockSize); /** * @brief Construct a local memory message. * * @param heapId the id of the heap containing this message. * @param bucketId the bucket within the heap containing this message. * @param blockSize the size of the data portion of this message. */ MessageImpl(HeapId heapId, BucketId bucketId, uint32_t blockSize); /** * @brief Attach a continuation message. * * @param messageId the continuation message id. */ void AttachContinuationMessageId(const MessageId messageId); /** * @brief Attach a continuation message and return the old. * * @param messageId the continuation message id. * * @return The old continuation message id of this message. */ MessageId AttachNewContinuationMessageId(const MessageId messageId); /** * @brief Initialize a message after removing from a heap. * * @param totalSpace the total number of bytes available in the message (chain). */ void Initialize(uint32_t totalSpace); /** * @brief Get a pointer to the data portion of the message. * * @return A pointer to the data portion. */ void *GetDataPointer() const; const MessageKey m_thisMessage; ///< Reference to ourself. const uint32_t m_size; ///< Size of the single message block. const BucketId m_bucketId; ///< Bucket this message belongs to in its heap. StatusCode m_statusCode; ///< Message status. MessageType m_type; ///< Message type. MessageId m_continuationMessageId; ///< Next message in (free) chain. Always from the same heap as this message. MessageKey m_nextMessage; ///< Next message in queue. uint32_t m_totalSpace; ///< Total size of all blocks in the chain. uint32_t m_freeSpace; ///< Total amount of free space in the message chain. Address m_sourceAddress; ///< Source address of the message. Address m_destinationAddress; ///< Destination address of the message. Address m_returnAddress; ///< Address to implicitly route message when it is "returned" to its heap. // The following are less likely to be needed. Possibly we should move them to the end of the shared memory region // to get better cache usage. #define ACTIV_MESSAGE_IMPL_END_OF_INTERESTING_DATA_MARKER m_requestId RequestId m_requestId; Subject m_subject; IpAddress m_ipAddress; friend class EndPoint; friend class Heap; friend class HeapMessage; friend class HeapMessageQueue; friend class ManagedEndPointImpl; friend class PrivateHeap; friend class SharedHeap; friend class SystemImpl; friend class EndPointWalker; friend class HeapWalker; }; // --------------------------------------------------------------------------------------------------------------------------------- inline void *MessageImpl::operator new(size_t basicSize, const BlockSizeWrapper &blockSize) { return std::malloc(AlignValueToProcessorWordBoundary(basicSize) + blockSize.m_value); } // --------------------------------------------------------------------------------------------------------------------------------- inline void MessageImpl::operator delete(void *p) { if (NULL != p) std::free(p); } // --------------------------------------------------------------------------------------------------------------------------------- inline void MessageImpl::operator delete(void *p, const BlockSizeWrapper &blockSize) { if (NULL != p) std::free(p); } // --------------------------------------------------------------------------------------------------------------------------------- inline MessageImpl::MessageImpl(HeapId heapId, BucketId bucketId, MessageId thisMessageId, MessageId continuationMessageId, uint32_t blockSize) : m_thisMessage(thisMessageId, heapId, false), m_size(blockSize), m_bucketId(bucketId), m_continuationMessageId(continuationMessageId) { // Shared heap MessageImpl constructor. } // --------------------------------------------------------------------------------------------------------------------------------- inline MessageImpl::MessageImpl(HeapId heapId, BucketId bucketId, uint32_t blockSize) : // Two stage cast because on 32-bit, if the top bit is set of "this" there's some crazy sign extension stuff going on. m_thisMessage(static_cast(reinterpret_cast(this)), heapId, true), m_size(blockSize), m_bucketId(bucketId), m_continuationMessageId(MessageKey::INVALID_MESSAGE_ID) { // Private heap MessageImpl constructor. } // --------------------------------------------------------------------------------------------------------------------------------- inline void MessageImpl::AttachContinuationMessageId(const MessageId messageId) { m_continuationMessageId = messageId; } // --------------------------------------------------------------------------------------------------------------------------------- inline MessageId MessageImpl::AttachNewContinuationMessageId(const MessageId messageId) { const MessageId previousContinuationMessageId = m_continuationMessageId; AttachContinuationMessageId(messageId); return previousContinuationMessageId; } // --------------------------------------------------------------------------------------------------------------------------------- ACTIV_FORCE_INLINE void MessageImpl::Initialize(uint32_t totalSpace) { // Message has just been removed from a heap. Clear it down. m_totalSpace = totalSpace; m_freeSpace = totalSpace; // NB a hack with memset() was tried here; performance was worse. m_sourceAddress.Clear(); m_destinationAddress.Clear(); m_returnAddress.Clear(); m_statusCode = STATUS_CODE_SUCCESS; m_requestId.Clear(); m_subject.Clear(); m_ipAddress.SetLength(0); } // --------------------------------------------------------------------------------------------------------------------------------- inline void *MessageImpl::GetDataPointer() const { // Data portion now just follows this object in memory. return reinterpret_cast(const_cast(this)) + StaticAlignValueToProcessorWordBoundary::value; } } // namespace Activ #endif // (!defined ACTIV_FRAMEWORK_MESSAGE_IMPL_H)