···11+u/system: Make the system re-usable by other code.
+33-8
src/xrt/auxiliary/util/u_system.c
···11// Copyright 2023, Collabora, Ltd.
22+// Copyright 2024-2025, NVIDIA CORPORATION.
23// SPDX-License-Identifier: BSL-1.0
34/*!
45 * @file
···9192{
9293 struct u_system *usys = u_system(xsys);
93949494- if (usys->sessions.count > 0) {
9595- U_LOG_E("Number of sessions not zero, things will crash!");
9696- }
9595+ // Use shared fini function.
9696+ u_system_fini(usys);
97979898- free(usys->sessions.pairs);
9998 free(usys);
10099}
101100···111110{
112111 struct u_system *usys = U_TYPED_CALLOC(struct u_system);
113112113113+ // Use init function, then add the common destroy function.
114114+ if (!u_system_init(usys, destroy)) {
115115+ free(usys);
116116+ return NULL;
117117+ }
118118+119119+ return usys;
120120+}
121121+122122+bool
123123+u_system_init(struct u_system *usys, void (*destroy_fn)(struct xrt_system *))
124124+{
114125 // xrt_system fields.
115126 usys->base.create_session = create_session;
116116- usys->base.destroy = destroy;
127127+ usys->base.destroy = destroy_fn;
117128118129 // xrt_session_event_sink fields.
119130 usys->broadcast.push_event = push_event;
···122133 usys->sessions.pairs = U_TYPED_ARRAY_CALLOC(struct u_system_session_pair, usys->sessions.capacity);
123134 if (usys->sessions.pairs == NULL) {
124135 U_LOG_E("Failed to allocate session array");
125125- free(usys);
126126- return NULL;
136136+ return false;
127137 }
128138129139 // u_system fields.
130140 XRT_MAYBE_UNUSED int ret = os_mutex_init(&usys->sessions.mutex);
131141 assert(ret == 0);
132142133133- return usys;
143143+ return true;
144144+}
145145+146146+void
147147+u_system_fini(struct u_system *usys)
148148+{
149149+ // Just in case, should never happen.
150150+ if (usys->sessions.count > 0) {
151151+ U_LOG_E("Number of sessions not zero, things will crash!");
152152+ }
153153+154154+ free(usys->sessions.pairs);
155155+ usys->sessions.count = 0;
156156+157157+ // Mutex needs to be destroyed.
158158+ os_mutex_destroy(&usys->sessions.mutex);
134159}
135160136161void
+27-1
src/xrt/auxiliary/util/u_system.h
···11// Copyright 2023, Collabora, Ltd.
22+// Copyright 2024-2025, NVIDIA CORPORATION.
23// SPDX-License-Identifier: BSL-1.0
34/*!
45 * @file
···6869};
69707071/*!
7171- * Create a @ref u_system.
7272+ * Create a @ref u_system, creates a fully working system. Objects wishing to
7373+ * use @ref u_system as a parent class should use @ref u_system_init.
7274 *
7375 * @public @memberof u_system
7476 * @ingroup aux_util
7777+ * @see u_system_init
7578 */
7679struct u_system *
7780u_system_create(void);
8181+8282+/*!
8383+ * Inits a @ref u_system struct when used as a parent class, only to be used
8484+ * by base class. Not needed to be called if created by @ref u_system_create.
8585+ *
8686+ * @protected @memberof u_system
8787+ * @ingroup aux_util
8888+ */
8989+bool
9090+u_system_init(struct u_system *usys, void (*destroy_fn)(struct xrt_system *));
9191+9292+/*!
9393+ * Finalizes a @ref u_system struct when used as a parent class, only to be used
9494+ * by base class. This will not free the @ref u_system pointer itself but will
9595+ * free any resources created by the default implementation functions. Not
9696+ * needed to be called if created by @ref u_system_create, instead use
9797+ * xrt_system::destroy.
9898+ *
9999+ * @protected @memberof u_system
100100+ * @ingroup aux_util
101101+ */
102102+void
103103+u_system_fini(struct u_system *usys);
7810479105/*!
80106 * Add a @ref xrt_session to be tracked and to receive multiplexed events.