Commit Graph

13 Commits

Author SHA1 Message Date
Barnabás Pőcze 15e14946fb spa: expose utils/cleanup.h for external users
Up until now, `spa/utils/cleanup.h` was not installed,
but 779f06865c ("pod: add spa_auto support for dynamic builder")
included it in a public header. So now `cleanup.h` also needs to
be installed like any other public header so as to not break
3rd party users.

For example, `xdg-desktop-portal-wlr` would break:
https://codesearch.debian.net/search?q=spa_pod_dynamic_builder_init&literal=1
2023-10-11 19:56:11 +02:00
Barnabás Pőcze 65d949558b spa: utils: add scope based resource cleanup
systemd, dbus-broker, and many glib applications heavily
utilize the GNU C attribute "cleanup" to achieve C++ RAII-like
semantics for local resource management. This commit introduces
essentialy same mechanism into pipewire.

At the moment, this is inteded to be a strictly private API.

free() and close() as cleanup targets are sufficiently common
to warrant their own special macros:

  spa_autofree char *s = strdup(p);
  // will call `free(s)` at the end of the lifetime of `s`

  spa_autoclose int fd = openat(...);
  // will call `close(fd)` if `fd >= 0` at the end of the lifetime of `fd`

However, with `spa_auto()` or `spa_autoptr()` it is possible to define
other variables that will be cleaned up properly. Currently four are supported:

  spa_autoptr(FILE) f = fopen(...);
  // `f` has type `FILE *`
  // will call `fclose(f)` if `f != NULL`

  spa_autoptr(DIR) d = opendir(...);
  // `d` has type `DIR *`
  // will call `closedir(d)` if `d != NULL`

  spa_autoptr(pw_properties) p = pw_properties_new(NULL, NULL);
  // `p` has type `struct pw_properties *`
  // will call `pw_properties_free(p)`

  spa_auto(pw_strv) v = pw_split_strv(...);
  // `v` has type `char **`
  // will call `pw_strv_free(v)`

It is possible to add support for other types, e.g.

  SPA_DEFINE_AUTOPTR_CLEANUP(pw_main_loop, struct pw_main_loop, {
    // the pointer can be accessed using `*thing`
    // `thing` has type `struct pw_main_loop **`
    spa_clear_ptr(*thing, pw_main_loop_destroy);
  })

  spa_autoptr(pw_main_loop) l = ...;
  // `l` has type `struct pw_main_loop *`
  // will call `pw_main_loop_destroy(l)`

or

  SPA_DEFINE_AUTO_CLEANUP(spa_pod_dynamic_builder, struct spa_pod_dynamic_builder, {
    // `thing` has type `struct spa_pod_dynamic_builder *`
    spa_pod_dynamic_builder_clean(thing);
  })

  spa_auto(spa_pod_dynamic_builder) builder = ...
  // `builder` has type `struct spa_pod_dynamic_builder`
  // will call `spa_pod_dynamic_builder_clean(&builder)`

The first argument is always an arbitrary name. This name must be passed to
`spa_auto()` and `spa_autoptr()` as it is what determines the actual type
and destructor used. The second parameter is the concrete type. For
`SPA_DEFINE_AUTO_CLEANUP()` this is the concrete type to be used, while for
`SPA_DEFINE_AUTOPTR_CLEANUP()` it is the concrete type without the
outermost pointer. That is,

  SPA_DEFINE_AUTOPTR_CLEANUP(A, foo, ...)
  SPA_DEFINE_AUTO_CLEANUP(B, foo, ...)

  spa_autoptr(A) x; // `x` has type `foo *`
  spa_auto(B) y; // `y` has type `foo`

A couple other macros are also added:

  spa_clear_ptr(ptr, destructor)
  // calls `destructor(ptr)` if `ptr != NULL` and sets `ptr` to `NULL`

  spa_clear_fd(fd)
  // calls `close(fd)` if `fd >= 0` and sets `fd` to -1

  spa_steal_ptr(ptr)
  // sets `ptr` to `NULL` and returns the old value,
  // useful for preventing the auto cleanup mechanism from kicking in
  // when returning the pointer from a function

  spa_steal_fd(fd)
  // sets `fd` to -1 and returns the old value
2023-07-11 14:23:53 +02:00
George Kiagiadakis 8a3c1bedde gitlab-ci: move check_missing_headers into its own job and hide the script 2022-10-18 17:19:28 +03:00
Peter Hutterer c2a273b57d gitlab: structure the issue templates 2021-10-20 08:56:43 +00:00
Pauli Virtanen 652c1423f8 gitlab: fix bluetooth issue template
Should use lsusb for determining adapter.
2021-09-13 21:11:14 +03:00
Pauli Virtanen 535aca1b30 gitlab: update Bluetooth issue template
Add commands that output basic info, and add link to instructions for
debug log.
2021-09-12 11:51:01 +03:00
jasker5183 5c92928eab Add No Color #2 2021-04-12 13:05:57 +00:00
jasker5183 1e3bbfbbc6 Add No Color 2021-04-12 12:52:12 +00:00
jasker5183 04a13471fb Update issue.md 2021-03-02 19:37:02 +00:00
jasker5183 c32c532e2a Update bluetooth issue.md 2021-03-02 19:33:59 +00:00
jasker5183 b44cb0a897 Add bluetooth issue.md 2021-01-23 12:49:06 +00:00
jasker5183 c3d4eea6ca Add new issue template 2021-01-20 12:43:09 +01:00
jasker5183 419d0ad7b6 Add directory for issue templates 2021-01-18 16:28:06 +00:00