What is the memory structure of QByteArray

I am trying to find the QByteArray memory layout (because I need to interact with a DLL that has QByteArray as parameters). From the original QT sources, I extracted the following:

template <typename BaseClass> struct QGenericAtomicOps {
};
template <int size> struct QBasicAtomicOps : QGenericAtomicOps < QBasicAtomicOps<size> > {
};
template <typename T> struct QAtomicOps : QBasicAtomicOps < sizeof( T ) > {
    typedef T Type;
};
template <typename T>
struct QBasicAtomicInteger {
    typedef QAtomicOps<T> Ops;
    typename Ops::Type _q_value;
};
typedef QBasicAtomicInteger<int> QBasicAtomicInt;
struct RefCount {
    QBasicAtomicInt atomic;
};
typedef void* qptrdiff;
struct QArrayData {
    RefCount ref;
    int size;
    UINT alloc : 31;
    UINT capacityReserved : 1;
    qptrdiff offset; // in bytes from beginning of header
};
template <class T>
struct QTypedArrayData
    : QArrayData {
};
struct QByteArray {
    typedef QTypedArrayData<char> Data;
    Data *d;
};

      

(In this code, I've removed all functions because I'm only interested in the data layout.)

So my guess is the memory layout looks like this:

QByteArray
    QArrayData * Data; // pointer to the array-data struct.

QArrayData
    int ref; // the reference counter
    int size; // the used data size
    UINT alloc:31; // the number of bytes allocated
    UINT reserve:1; // unknown
    void* offset; // pointer to real data

      

It is right? I am especially interested in "offset"; from looking at the code, I got the impression that the real data starts right after the offset and is part of the structure. It is also possible that the real data is before the "ArrayData" header.

So 'd' can point to one of these layouts:

1. [ref|size|alloc|reserve|offset|--the real data--]
2. [--the real data--|ref|size|alloc|reserve|-offset]

      

Is it correct?

Charles

+3


source to share





All Articles