Project
Loading...
Searching...
No Matches
TMessageSerializer.cxx
Go to the documentation of this file.
1// Copyright 2019-2020 CERN and copyright holders of ALICE O2.
2// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
3// All rights not expressly granted are reserved.
4//
5// This software is distributed under the terms of the GNU General Public
6// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
7//
8// In applying this license CERN does not waive the privileges and immunities
9// granted to it by virtue of its status as an Intergovernmental Organization
10// or submit itself to any jurisdiction.
12#include <FairMQTransportFactory.h>
13#include <algorithm>
14#include <memory>
15
16using namespace o2::framework;
17
18void* FairOutputTBuffer::embedInItself(fair::mq::Message& msg)
19{
20 // The first bytes of the message are used to store the pointer to the message itself
21 // so that we can reallocate it if needed.
22 if (sizeof(char*) > msg.GetSize()) {
23 throw std::runtime_error("Message size too small to embed pointer");
24 }
25 char* data = reinterpret_cast<char*>(msg.GetData());
26 char* ptr = reinterpret_cast<char*>(&msg);
27 std::memcpy(data, &ptr, sizeof(char*));
28 return data + sizeof(char*);
29}
30
31// Reallocation function. Get the message pointer from the data and call Rebuild.
32char* FairOutputTBuffer::fairMQrealloc(char* oldData, size_t newSize, size_t oldSize)
33{
34 // Old data is the pointer at the beginning of the message, so the pointer
35 // to the message is **stored** in the 8 bytes before it.
36 auto* msg = *(fair::mq::Message**)(oldData - sizeof(char*));
37 if (newSize <= msg->GetSize()) {
38 // no need to reallocate, the message is already big enough
39 return oldData;
40 }
41 // Create a shallow copy of the message
42 fair::mq::MessagePtr oldMsg = msg->GetTransport()->CreateMessage();
43 oldMsg->Copy(*msg);
44 // Copy the old data while rebuilding. Reference counting should make
45 // sure the old message is not deleted until the new one is ready.
46 // We need 8 extra bytes for the pointer to the message itself (realloc does not know about it)
47 // and we need to copy 8 bytes more than the old size (again, the extra pointer).
48 msg->Rebuild(newSize + 8, fair::mq::Alignment{64});
49 memcpy(msg->GetData(), oldMsg->GetData(), oldSize + 8);
50
51 return reinterpret_cast<char*>(msg->GetData()) + sizeof(char*);
52}
TBranch * ptr
void * embedInItself(fair::mq::Message &msg)
static char * fairMQrealloc(char *oldData, size_t newSize, size_t oldSize)
GLboolean * data
Definition glcorearb.h:298
Defining PrimaryVertex explicitly as messageable.
Definition TFIDInfo.h:20
uint64_t const void const *restrict const msg
Definition x9.h:153