Monado OpenXR Runtime
Loading...
Searching...
No Matches
oxr_handle_base.h
Go to the documentation of this file.
1// Copyright 2019, Collabora, Ltd.
2// Copyright 2026, Beyley Cardellio
3// SPDX-License-Identifier: BSL-1.0
4/*!
5 * @file
6 * @brief Contains handle-related functions and defines only required in a few
7 * locations.
8 * @author Rylie Pavlik <rylie.pavlik@collabora.com>
9 * @author Beyley Cardellio <ep1cm1n10n123@gmail.com>
10 * @ingroup oxr_main
11 */
12
13#pragma once
14
15#include "../oxr_logger.h"
16
17#include "oxr_handle_array.h"
18
19#include <stdlib.h>
20
21
22#ifdef __cplusplus
23extern "C" {
24#endif
25
26// Forward declare
27struct oxr_handle_base;
28
29/*!
30 * Function pointer type for a handle destruction function.
31 *
32 * @relates oxr_handle_base
33 */
34typedef XrResult (*oxr_handle_destroyer)(struct oxr_logger *log, struct oxr_handle_base *hb);
35
36/*!
37 * State of a handle base, to reduce likelihood of going "boom" on
38 * out-of-order destruction or other unsavory behavior.
39 *
40 * @ingroup oxr_main
41 */
43{
44 /*! State during/before oxr_handle_init, or after failure */
46
47 /*! State after successful oxr_handle_init */
49
50 /*! State after successful oxr_handle_destroy */
52};
53
54/*!
55 * Used to hold diverse child handles and ensure orderly destruction.
56 *
57 * Each object referenced by an OpenXR handle should have one of these as its
58 * first element, thus "extending" this class.
59 */
61{
62 //! Magic (per-handle-type) value for debugging.
63 uint64_t debug;
64
65 /*!
66 * Pointer to this object's parent handle holder, if any.
67 */
69
70 /*!
71 * Array of children, if any.
72 */
74
75 /*!
76 * Current handle state.
77 */
79
80 /*!
81 * Destroy the object this handle refers to.
82 */
84};
85
86/*
87 *
88 * oxr_handle_base.c
89 *
90 */
91
92/*!
93 * Initialize a handle holder, and if a parent is specified, update its child
94 * list to include this handle.
95 *
96 * @protected @memberof oxr_handle_base
97 */
98XrResult
100 struct oxr_handle_base *hb,
101 uint64_t debug,
102 oxr_handle_destroyer destroy,
103 struct oxr_handle_base *parent);
104
105/*!
106 * Recursively destroys a handle and all of its children, removing it from its parent if it has one.
107 *
108 * oxr_handle_destroy wraps this to provide some extra output and start `level`
109 * at 0. `level`, which is reported in debug output, is the current depth of
110 * recursion.
111 *
112 * @protected @memberof oxr_handle_base
113 */
114XrResult
115oxr_handle_destroy_internal(struct oxr_logger *log, struct oxr_handle_base *hb, int level);
116
117/*!
118 * Allocate some memory for use as a handle, and initialize it as a handle.
119 *
120 * Mainly for internal use - use OXR_ALLOCATE_HANDLE instead which wraps this.
121 *
122 * @relates oxr_handle_base
123 */
124XrResult
126 size_t size,
127 uint64_t debug,
128 oxr_handle_destroyer destroy,
129 struct oxr_handle_base *parent,
130 void **out);
131/*!
132 * Allocates memory for a handle and evaluates to an XrResult.
133 *
134 * @param LOG pointer to struct oxr_logger
135 * @param OUT the pointer to handle struct type you already created.
136 * @param DEBUG Magic per-type debugging constant
137 * @param DESTROY Handle destructor function
138 * @param PARENT a parent handle, if any
139 *
140 * Use when you want to do something other than immediately returning in case of
141 * failure. If returning immediately is OK, see OXR_ALLOCATE_HANDLE_OR_RETURN().
142 *
143 * @relates oxr_handle_base
144 */
145#define OXR_ALLOCATE_HANDLE(LOG, OUT, DEBUG, DESTROY, PARENT) \
146 oxr_handle_allocate_and_init(LOG, sizeof(*OUT), DEBUG, DESTROY, PARENT, (void **)&OUT)
147
148/*!
149 * Allocate memory for a handle, returning in case of failure.
150 *
151 * @param LOG pointer to struct oxr_logger
152 * @param OUT the pointer to handle struct type you already created.
153 * @param DEBUG Magic per-type debugging constant
154 * @param DESTROY Handle destructor function
155 * @param PARENT a parent handle, if any
156 *
157 * Will return an XrResult from the current function if something fails.
158 * If that's not OK, see OXR_ALLOCATE_HANDLE().
159 *
160 * @relates oxr_handle_base
161 */
162#define OXR_ALLOCATE_HANDLE_OR_RETURN(LOG, OUT, DEBUG, DESTROY, PARENT) \
163 do { \
164 XrResult allocResult = OXR_ALLOCATE_HANDLE(LOG, OUT, DEBUG, DESTROY, PARENT); \
165 if (allocResult != XR_SUCCESS) { \
166 return allocResult; \
167 } \
168 } while (0)
169
170#ifdef __cplusplus
171}
172#endif
oxr_handle_state
State of a handle base, to reduce likelihood of going "boom" on out-of-order destruction or other uns...
Definition oxr_handle_base.h:43
@ OXR_HANDLE_STATE_DESTROYED
State after successful oxr_handle_destroy.
Definition oxr_handle_base.h:51
@ OXR_HANDLE_STATE_LIVE
State after successful oxr_handle_init.
Definition oxr_handle_base.h:48
@ OXR_HANDLE_STATE_UNINITIALIZED
State during/before oxr_handle_init, or after failure.
Definition oxr_handle_base.h:45
A dynamic array of handles.
Manages an array of handles, does not have a init function but must be zero initialized where it is d...
Definition oxr_handle_array.h:19
Used to hold diverse child handles and ensure orderly destruction.
Definition oxr_handle_base.h:61
struct oxr_handle_base * parent
Pointer to this object's parent handle holder, if any.
Definition oxr_handle_base.h:68
uint64_t debug
Magic (per-handle-type) value for debugging.
Definition oxr_handle_base.h:63
enum oxr_handle_state state
Current handle state.
Definition oxr_handle_base.h:78
XrResult oxr_handle_init(struct oxr_logger *log, struct oxr_handle_base *hb, uint64_t debug, oxr_handle_destroyer destroy, struct oxr_handle_base *parent)
Initialize a handle holder, and if a parent is specified, update its child list to include this handl...
XrResult(* oxr_handle_destroyer)(struct oxr_logger *log, struct oxr_handle_base *hb)
Function pointer type for a handle destruction function.
Definition oxr_handle_base.h:34
oxr_handle_destroyer destroy
Destroy the object this handle refers to.
Definition oxr_handle_base.h:83
XrResult oxr_handle_allocate_and_init(struct oxr_logger *log, size_t size, uint64_t debug, oxr_handle_destroyer destroy, struct oxr_handle_base *parent, void **out)
Allocate some memory for use as a handle, and initialize it as a handle.
struct oxr_handle_array children
Array of children, if any.
Definition oxr_handle_base.h:73
Logger struct that lives on the stack, one for each call client call.
Definition oxr_logger.h:44