17#ifndef ALICEOW_GPUCOMMON_TPCFASTTRANSFORMATION_FLATOBJECT_H
18#define ALICEOW_GPUCOMMON_TPCFASTTRANSFORMATION_FLATOBJECT_H
20#if !defined(GPUCA_GPUCODE_DEVICE)
134 template <
typename T>
146 newPtr =
new T[newSize];
148 int32_t mcp = std::min(newSize, oldSize);
151 std::memmove(newPtr,
ptr, mcp *
sizeof(T));
153 if (newSize > oldSize) {
154 std::memset(newPtr + mcp, 0, (newSize - oldSize) *
sizeof(T));
162 template <
typename T>
163 T**
resizeArray(T**&
ptr, int32_t oldSize, int32_t newSize, T** newPtr =
nullptr)
174 newPtr =
new T*[newSize];
176 int32_t mcp = std::min(newSize, oldSize);
177 std::memmove(newPtr,
ptr, mcp *
sizeof(T*));
178 if (newSize > oldSize) {
179 std::memset(newPtr + mcp, 0, (newSize - oldSize) *
sizeof(T*));
275 static constexpr size_t alignSize(
size_t sizeBytes,
size_t alignmentBytes)
277 auto res = sizeBytes % alignmentBytes;
278 return res ? sizeBytes + (alignmentBytes -
res) : sizeBytes;
285 return (
ptr !=
nullptr) ?
reinterpret_cast<T*
>(newBase + (
reinterpret_cast<const char*
>(
ptr) - oldBase)) :
nullptr;
288#if !defined(GPUCA_GPUCODE) && !defined(GPUCA_STANDALONE)
291 template <
class T,
class TFile>
295 template <
class T,
class TFile>
299#if !defined(GPUCA_GPUCODE)
417 if (!newFlatBufferPtr) {
430 if (!obj.isConstructed()) {
431 return "tested object is not constructed!";
435 tst.cloneFromObject(obj,
nullptr);
436 if (!tst.isConstructed() || !tst.isBufferInternal()) {
437 return "error at cloneFromObject()!";
442 char* buf0 = tst.releaseInternalBuffer();
443 char* buf1 =
new char[tst.getFlatBufferSize()];
444 char* buf2 =
new char[tst.getFlatBufferSize()];
445 std::memcpy(buf1, tst.getFlatBufferPtr(), tst.getFlatBufferSize());
446 tst.setActualBufferAddress(buf1);
449 tst.setFutureBufferAddress(buf2);
450 std::memcpy(buf2, buf1, tst.getFlatBufferSize());
453 if (tst.isBufferInternal()) {
454 return err =
"error, buffer should be external!";
457 tst.adoptInternalBuffer(buf2);
458 if (!tst.isBufferInternal()) {
459 return err =
"error, buffer should be internal!";
462 obj.cloneFromObject(tst,
nullptr);
463 if (!obj.isBufferInternal()) {
464 return err =
"error, buffer should be internal!";
477 printf(
"0x%02x ",
v);
478 if (
i && ((
i + 1) % 20) == 0) {
490#if !defined(GPUCA_GPUCODE) && !defined(GPUCA_STANDALONE)
491template <
class T,
class TFile>
495 assert(obj.isConstructed());
497 if (outf.IsZombie()) {
498 LOG(error) <<
"Failed to write to file " << outf.GetName();
502 bool isBufferExternal = !obj.isBufferInternal();
503 if (isBufferExternal) {
504 obj.adoptInternalBuffer(obj.mFlatBufferPtr);
506 outf.WriteObjectAny(&obj, T::Class(),
name);
507 if (isBufferExternal) {
508 obj.clearInternalBufferPtr();
513template <
class T,
class TFile>
518 if (inpf.IsZombie()) {
519 LOG(error) <<
"Failed to read from file " << inpf.GetName();
522 T* pobj =
reinterpret_cast<T*
>(inpf.GetObjectChecked(
name, T::Class()));
524 LOG(error) <<
"Failed to load " <<
name <<
" from " << inpf.GetName();
527 if (pobj->mFlatBufferSize > 0 && pobj->mFlatBufferContainer ==
nullptr) {
528 LOG(error) <<
"Failed to load " <<
name <<
" from " << inpf.GetName() <<
": empty flat buffer container";
531 pobj->setActualBufferAddress(pobj->mFlatBufferContainer);
536#ifndef GPUCA_GPUCODE_DEVICE
char * releaseInternalBuffer()
_____________ Methods for making the data buffer external __________________________
void setFutureBufferAddress(char *futureFlatBufferPtr)
uint32_t mConstructionMask
mask for constructed object members, first two bytes are used by this class
int32_t mFlatBufferSize
size of the flat buffer
FlatObject & operator=(const FlatObject &)=delete
void printC() const
Print the content of the flat buffer.
void adoptInternalBuffer(char *buf)
char * mFlatBufferContainer
void destroy()
_______________ Utilities _______________________________________________
bool isConstructed() const
Tells if the object is constructed.
static constexpr size_t getBufferAlignmentBytes()
Gives minimal alignment in bytes required for the flat buffer.
static T * relocatePointer(const char *oldBase, char *newBase, const T *ptr)
Relocates a pointer inside a buffer to the new buffer address.
ClassDefNV(FlatObject, 1)
Pointer to the flat buffer.
void setActualBufferAddress(char *actualFlatBufferPtr)
_____________ Methods for moving the class with its external buffer to another location _____________...
static std::string stressTest(T &obj)
Test the flat object functionality for a child class T.
static int32_t writeToFile(T &obj, TFile &outf, const char *name)
write a child class object to the file
FlatObject()=default
_____________ Constructors / destructors __________________________
void startConstruction()
_____________ Construction _________
void moveBufferTo(char *newBufferPtr)
void finishConstruction(int32_t flatBufferSize)
bool isBufferInternal() const
Tells if the buffer is internal.
static constexpr size_t alignSize(size_t sizeBytes, size_t alignmentBytes)
_______________ Generic utilities _______________________________________________
FlatObject(const FlatObject &)=delete
T * resizeArray(T *&ptr, int32_t oldSize, int32_t newSize, T *newPtr=nullptr)
void cloneFromObject(const FlatObject &obj, char *newFlatBufferPtr)
static constexpr size_t getClassAlignmentBytes()
GPUCA_GPUCODE.
void clearInternalBufferPtr()
T ** resizeArray(T **&ptr, int32_t oldSize, int32_t newSize, T **newPtr=nullptr)
ConstructionState
GPUCA_GPUCODE.
@ InProgress
construction started: temporary memory is reserved
@ NotConstructed
the object is not constructed
@ Constructed
the object is constructed, temporary memory is released
static T * readFromFile(TFile &inpf, const char *name)
read a child class object from the file
GPUdi() size_t getFlatBufferSize() const
Gives size of the flat buffer.
GPUdi() const char *getFlatBufferPtr() const
Gives pointer to the flat buffer.
static constexpr size_t getClassAlignmentBytes()
GPUdi() size_t getFlatBufferSize() const
static constexpr size_t getBufferAlignmentBytes()
GLuint const GLchar * name
GLenum GLuint GLenum GLsizei const GLchar * buf
a couple of static helper functions to create timestamp values for CCDB queries or override obsolete ...
LOG(info)<< "Compressed in "<< sw.CpuTime()<< " s"