When I started to port lvgl I took the naive approach to subclass its C structures in C++ so that my display driver class doesn’t have to replicate information such as resolutions etc, and callbacks from lvgl could directly be remapped into member functions.
But it turns out that lvgl copies the driver structure to its own dynamically allocated owner in lv_disp_drv_register(). The same is true for other driver types. So my question is, what’s the benefit of copying the structure instead of only saving a reference to it? Because there’s plenty of drawbacks:
- Waste of RAM: the driver structure contents should be constant, which would allow the whole structure to be allocated to flash. Instead heap memory is wasted to copy this information to the driver owner object.
- No possibility to append the structures with extra fields: as the callbacks pass the driver structure pointer, it would be needed that additional fields can be added at the end of the structure, in case the same driver is used with multiple instances. With the copy approach this is impossible. Instead the only available option is to enable LV_USE_USER_DATA, and point the user_data field to any additional data the driver needs. Enabling this however magnifies the first problem, as now all lv_obj_t objects also have an additional user_data field, wasting even more RAM.
Minimizing the RAM usage should be of upmost priority for a project that refers to itself as lightweight, so I hope this request is picked up soon.