17#ifndef ALICEOW_GPUCOMMON_TPCFASTTRANSFORMATION_FLATOBJECT_H
18#define ALICEOW_GPUCOMMON_TPCFASTTRANSFORMATION_FLATOBJECT_H
20#if !defined(GPUCA_GPUCODE_DEVICE)
132 newPtr =
new T[newSize];
134 int32_t mcp = std::min(newSize, oldSize);
137 std::memmove(newPtr,
ptr, mcp *
sizeof(T));
139 if (newSize > oldSize) {
140 std::memset(newPtr + mcp, 0, (newSize - oldSize) *
sizeof(T));
160 newPtr =
new T*[newSize];
162 int32_t mcp = std::min(newSize, oldSize);
163 std::memmove(newPtr,
ptr, mcp *
sizeof(T*));
164 if (newSize > oldSize) {
165 std::memset(newPtr + mcp, 0, (newSize - oldSize) *
sizeof(T*));
277 static size_t alignSize(
size_t sizeBytes,
size_t alignmentBytes)
279 auto res = sizeBytes % alignmentBytes;
280 return res ? sizeBytes + (alignmentBytes -
res) : sizeBytes;
287 return (
ptr !=
nullptr) ?
reinterpret_cast<T*
>(newBase + (
reinterpret_cast<const char*
>(
ptr) - oldBase)) :
nullptr;
290#if !defined(GPUCA_GPUCODE) && !defined(GPUCA_STANDALONE)
293 template <
class T,
class TFile>
297 template <
class T,
class TFile>
301#if !defined(GPUCA_GPUCODE)
405 if (!newFlatBufferPtr) {
418 if (!obj.isConstructed()) {
419 return "tested object is not constructed!";
423 tst.cloneFromObject(obj,
nullptr);
424 if (!tst.isConstructed() || !tst.isBufferInternal()) {
425 return "error at cloneFromObject()!";
430 char* buf0 = tst.releaseInternalBuffer();
431 char* buf1 =
new char[tst.getFlatBufferSize()];
432 char* buf2 =
new char[tst.getFlatBufferSize()];
433 std::memcpy(buf1, tst.getFlatBufferPtr(), tst.getFlatBufferSize());
434 tst.setActualBufferAddress(buf1);
437 tst.setFutureBufferAddress(buf2);
438 std::memcpy(buf2, buf1, tst.getFlatBufferSize());
441 if (tst.isBufferInternal()) {
442 return err =
"error, buffer should be external!";
445 tst.adoptInternalBuffer(buf2);
446 if (!tst.isBufferInternal()) {
447 return err =
"error, buffer should be internal!";
450 obj.cloneFromObject(tst,
nullptr);
451 if (!obj.isBufferInternal()) {
452 return err =
"error, buffer should be internal!";
465 printf(
"0x%02x ",
v);
466 if (
i && ((
i + 1) % 20) == 0) {
478#if !defined(GPUCA_GPUCODE) && !defined(GPUCA_STANDALONE)
479template <
class T,
class TFile>
483 assert(obj.isConstructed());
485 if (outf.IsZombie()) {
486 LOG(error) <<
"Failed to write to file " << outf.GetName();
490 bool isBufferExternal = !obj.isBufferInternal();
491 if (isBufferExternal) {
492 obj.adoptInternalBuffer(obj.mFlatBufferPtr);
494 outf.WriteObjectAny(&obj, T::Class(),
name);
495 if (isBufferExternal) {
496 obj.clearInternalBufferPtr();
501template <
class T,
class TFile>
506 if (inpf.IsZombie()) {
507 LOG(error) <<
"Failed to read from file " << inpf.GetName();
510 T* pobj =
reinterpret_cast<T*
>(inpf.GetObjectChecked(
name, T::Class()));
512 LOG(error) <<
"Failed to load " <<
name <<
" from " << inpf.GetName();
515 if (pobj->mFlatBufferSize > 0 && pobj->mFlatBufferContainer ==
nullptr) {
516 LOG(error) <<
"Failed to load " <<
name <<
" from " << inpf.GetName() <<
": empty flat buffer container";
519 pobj->setActualBufferAddress(pobj->mFlatBufferContainer);
524#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 _______________________________________________
static size_t alignSize(size_t sizeBytes, size_t alignmentBytes)
_______________ Generic 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.
FlatObject(const FlatObject &)=delete
void cloneFromObject(const FlatObject &obj, char *newFlatBufferPtr)
static constexpr size_t getClassAlignmentBytes()
_____________ Memory alignment __________________________
void clearInternalBufferPtr()
size_t getFlatBufferSize() const
Gives size of the flat buffer.
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
const char * getFlatBufferPtr() const
Gives pointer to the flat buffer.
GLuint const GLchar * name
GLenum GLuint GLenum GLsizei const GLchar * buf
T * resizeArray(T *&ptr, int32_t oldSize, int32_t newSize, T *newPtr=nullptr)
a couple of static helper functions to create timestamp values for CCDB queries or override obsolete ...
LOG(info)<< "Compressed in "<< sw.CpuTime()<< " s"