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

Simon Hausmann Simon.Hausmann at qt.io
Wed Aug 8 09:32:52 CEST 2018


Hi,


If you want to use the ValueArray, then you need to make sure that it's the last member of your type. Then encapsulate the creation if the type in a factory function that works a bit like ExecutionContext::newCallContext:


    (1) Calculate how much memory you're going to need for the type _and_ the value array (minus one Value).

    (2) Allocate the memory with one call to allocManaged.

    (3) call init() manually.

    (4) Set the alloc and size members of the value array to the amount of allocated values.


While this approach is clever in the sense that it gives a compact memory representation and allocates object and array data in one shot, it also means that it's not so suitable for frequent dynamic resizing as it will require re-allocating the entire object.


If you have a fully dynamic array, then the alternative would be to store a pointer to your QVector in the heap object. Heap::QQmlSequence does that, for example. Just make sure to use V4_NEEDS_DESTROY and provide a destroy() function to delete the vector.


I'd probably go for the first approach with perhaps an indirect type (similar to how Object has MemberData) if the array contains references to the JS heap. If your array has only references to the C heap, then you're probably better off using a pointer to a regular vector.


Simon

________________________________
From: Development <development-bounces+simon.hausmann=qt.io at qt-project.org> on behalf of Valery Kotov <kotov.valery at gmail.com>
Sent: Wednesday, August 8, 2018 9:10:43 AM
To: Qt development mailing list
Subject: [Development] Storing and managing ValueArray inside QV4::Heap objects

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?

Thank you!
Sincerely yours,
Valery Kotov
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/development/attachments/20180808/f853c435/attachment.html>


More information about the Development mailing list