3

我试图找到 QByteArray 的内存布局(因为我必须与一个以 QByteArray 作为参数的 DLL 交互)。从原始 QT 源中,我提取了以下内容:

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;
};

(在这段代码中,我删除了所有函数,因为我只对数据布局感兴趣。)

所以我假设,内存布局如下:

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

这个对吗?我特别想知道“偏移”;通过查看代码,我的印象是,真实数据直接在偏移之后开始,并且是结构的一部分。真实数据似乎也可能位于“ArrayData”标头之前。

所以 'd' 可以指向以下布局之一:

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

这个对吗?

卡尔

4

0 回答 0