[Development] Storing and managing ValueArray inside QV4::Heap objects

Lars Knoll lars.knoll at qt.io
Wed Aug 8 09:34:50 CEST 2018


Hi Valery,

> On 8 Aug 2018, at 09:10, Valery Kotov <kotov.valery at gmail.com> wrote:
> 
> Hello all,
> 
> I have a question about QV4 heap objects and what could be stored in them.
> I would like to move data structure from c++ heap to GC heap. Unfortunately, my data structure stores a pair of QLists internally.
> As far as I'm aware, QV4::Heap objects should be "trivially constructible", and thus, cannot store QList(s).
> One option would be to store QList pointer and manage it's lifecycle manually. But that does not sound like a good idea.
> I've noticed though, that some of qv4 types are using DECLARE_MARKOBJECTS macro. By using DECLARE_MARKOBJECTS macro and ObjectMember(s) define I can actually store a ValueArray member in my QV4 heap object.
> 
> #define MyObjectMembers(class, Member) \
>     Member(class, ValueArray, ValueArray, myValues)
> 
> DECLARE_HEAP_OBJECT(MyObject, Object) {
>     DECLARE_MARKOBJECTS(MyObject);
> 
> In this case I can refer to myValues ValueArray type inside of MyObject.
> Unfortunately, it is a little bit unclear to me how to manage ValueArrays.
> From what I can see from some examples in the code (qv4generatorobject, qv4arraydata), it seems that I need to manage ValueArray manually.
> What I'm missing at the moment a little bit is how the actually allocation for ValueArray.values is done.
> I suspect that it is probably not done via plain "new" call. Otherwise it is a little bit confusing why not just to store the pointer directly. 
> 
> Could you guys please give me a few hints how to go about ValueArray management?

ValueArrays are allocated with a given size together with the object through the memory manager. They are rather low level, and can’t be resized after initial allocation. That means to get a larger ValueArray you need to allocate a new object. Maybe what you need is closer to a Pointer to a SimpleArrayData. Those are separately allocated by the memory manager, and you can grow the array using SimpleArrayData::reallocate().

Cheers,
Lars



More information about the Development mailing list