Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve struct generation #622

Closed
13 tasks done
badcel opened this issue May 8, 2022 · 4 comments
Closed
13 tasks done

Improve struct generation #622

badcel opened this issue May 8, 2022 · 4 comments
Milestone

Comments

@badcel
Copy link
Member

badcel commented May 8, 2022

Differentiate struct types and render them as appropriate:

Todo


For non opaque records: Consider to somehow use ref struct to avoid memory allocations?


Verify if we can differentiate between different types of structs to improve the overall struct handling.

E.g.:

  • Use a class in the public API for C structs which are opaque or class structs
  • Use a struct in the public API for C structs which are only transporting data into / out of an object

Verify: If we handle structs as structs could they be passed as "ref" to / from native code? How or those structs freed? Can those structs be defined as public ref struct xxxx?

Rust is implementing struct fields manually.


Is it useful to differentiate between Gdk.ColorRef and Gdk.Color?

In this way ColorRef could have a method which converts its pointer in an actual struct.

If it is not useful it would be possible to use generated properties on the structs to access it fields without the need to introduce the actual struct to the user. The properties would hide the struct.


Verify:


As there is some kind of memory management available for boxed structs this issue should perhaps only be applied for those record types. All other records should be generated like they are now. Meaning adding new converters and so on only for records which are a boxed type.

https://docs.gtk.org/gobject/boxed.html#boxed-types

JavaScript docs: http://webreflection.github.io/gjs-documentation/GObject-2.0/GObject.TYPE_BOXED.html

The internal namespace connects the public API to the native C world. Structs are currently generated in the internal namespace. Verify if the struct can be generated in the public namespace with private fields. Public properties could be generated for all fields which should be part of the public API. The aforementioned parameter modifiers could help to avoid allocations.


Boxed types in GLib: https://gitlab.gnome.org/GNOME/glib/-/blob/main/gobject/gboxed.c

If there is no information in the gir regarding boxed types this should probably as easy as possible to add manually per type in the bindings.

Alternatively only support structs which support the new copy/free annotations. If a struct does not have those annotations there could be some fallback implementation which makes a copy for each returned struct to avoid referencing memory which could freed by c in the meantime.

Example: a struct is returned (as pointer or ref) if the free/ copy methods are available we use those to copy / increase ref count (depending on c implementation). If not available we make a copy of the struct and pass it on.

Every record which has a get type function is a boxed type. A record without a get type func is just a struct. A boxed type can use g_boxed_copy.

@cameronwhite
Copy link
Contributor

Looking at what rust's bindings did, they seemed to use a similar approach to what we're currently doing (wrapper around a handle), but with some accessors for the fields
https://docs.rs/gdk/latest/src/gdk/auto/rectangle.rs.html#7-15
https://docs.rs/gdk/latest/src/gdk/rectangle.rs.html#22-24
Although I think the BoxedInline may avoid the extra heap allocation

However the cairo bindings are different, possible from being manually written https://docs.rs/cairo-rs/latest/src/cairo/rectangle\_int.rs.html#12-17

I also noticed from this that g_boxed_free() could be used for cleaning up records without having to figure out the correct free function (https://docs.gtk.org/gobject/boxed.html), and it seems like it would even work for the foreign records from Cairo (https://github.com/freedesktop/cairo/blob/master/util/cairo-gobject/cairo-gobject-structs.c)

@PlatinumLucario
Copy link

@badcel Since #907 has been merged, does the struct generation have any new issues? Just curious

@badcel
Copy link
Member Author

badcel commented Jul 29, 2023

Nothing happened till now. The pr was just for preparation of struct generation as it only fixed #747.

@badcel
Copy link
Member Author

badcel commented Mar 16, 2024

All points collected in the initial issue are handled (see linked pull requests).

@badcel badcel closed this as completed Mar 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants