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
20
21void* FairOutputTBuffer::embedInItself(fair::mq::Message& msg)
22{
23 // The first bytes of the message are used to store the pointer to the message itself
24 // so that we can reallocate it if needed.
25 if (sizeof(char*) > msg.GetSize()) {
26 throw std::runtime_error("Message size too small to embed pointer");
27 }
28 char* data = reinterpret_cast<char*>(msg.GetData());
29 char* ptr = reinterpret_cast<char*>(&msg);
30 std::memcpy(data, &ptr, sizeof(char*));
31 return data + sizeof(char*);
32}
33
34// Reallocation function. Get the message pointer from the data and call Rebuild.
35char* FairOutputTBuffer::fairMQrealloc(char* oldData, size_t newSize, size_t oldSize)
36{
37 // Old data is the pointer at the beginning of the message, so the pointer
38 // to the message is **stored** in the 8 bytes before it.
39 auto* msg = *(fair::mq::Message**)(oldData - sizeof(char*));
40 if (newSize <= msg->GetSize()) {
41 // no need to reallocate, the message is already big enough
42 return oldData;
43 }
44 // Create a shallow copy of the message
45 fair::mq::MessagePtr oldMsg = msg->GetTransport()->CreateMessage();
46 oldMsg->Copy(*msg);
47 // Copy the old data while rebuilding. Reference counting should make
48 // sure the old message is not deleted until the new one is ready.
49 // We need 8 extra bytes for the pointer to the message itself (realloc does not know about it)
50 // and we need to copy 8 bytes more than the old size (again, the extra pointer).
51 msg->Rebuild(newSize + 8, fair::mq::Alignment{64});
52 memcpy(msg->GetData(), oldMsg->GetData(), oldSize + 8);
53
54 return reinterpret_cast<char*>(msg->GetData()) + sizeof(char*);
55}
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