| 1 | // Copyright 2019-2020, Collabora, Ltd.cxx . . $ `<file>/src/xrt/state_trackers/oxr/oxr_path.c`/ |
| 2 | // SPDX-License-Identifier: BSL-1.0 |
| 3 | /*! |
| 4 | * @file |
| 5 | * @brief Holds path related functions. |
| 6 | * @author Jakob Bornecrantz <[email protected]> |
| 7 | * @ingroup oxr_main |
| 8 | */ |
| 9 | |
| 10 | #include <stdio.h> |
| 11 | #include <string.h> |
| 12 | #include <stdlib.h> |
| 13 | |
| 14 | #include "math/m_api.h"cxx . . $ `<file>/src/xrt/auxiliary/math/m_api.h`/ |
| 15 | #include "util/u_misc.h"cxx . . $ `<file>/src/xrt/auxiliary/util/u_misc.h`/ |
| 16 | |
| 17 | #include "oxr_objects.h"cxx . . $ `<file>/src/xrt/state_trackers/oxr/oxr_objects.h`/ |
| 18 | #include "oxr_logger.h"cxx . . $ `<file>/src/xrt/state_trackers/oxr/oxr_logger.h`/ |
| 19 | |
| 20 | |
| 21 | /*! |
| 22 | * Internal representation of a path, item follows this struct in memory and |
| 23 | * that in turn is followed by the string. |
| 24 | * |
| 25 | * @ingroup oxr_main |
| 26 | */ |
| 27 | struct oxr_pathcxx . . $ oxr_path# |
| 28 | { |
| 29 | uint64_tcxx . . $ uint64_t# debugcxx . . $ oxr_path#debug.; |
| 30 | XrPathcxx . . $ XrPath# idcxx . . $ oxr_path#id.; |
| 31 | void *attachedcxx . . $ oxr_path#attached.; |
| 32 | }; |
| 33 | |
| 34 | |
| 35 | /* |
| 36 | * |
| 37 | * Helpers |
| 38 | * |
| 39 | */ |
| 40 | |
| 41 | static inline XrPathcxx . . $ XrPath# |
| 42 | to_xr_pathcxx . . $ to_xr_path(b9788cfe1cf21842).(struct oxr_pathcxx . . $ oxr_path# *path) |
| 43 | { |
| 44 | return path->idcxx . . $ oxr_path#id.; |
| 45 | } |
| 46 | |
| 47 | static inline struct u_hashset_itemcxx . . $ u_hashset_item# * |
| 48 | get_itemcxx . . $ get_item(936f44567a884b3f).(struct oxr_pathcxx . . $ oxr_path# *path) |
| 49 | { |
| 50 | return (struct u_hashset_itemcxx . . $ u_hashset_item# *)&path[1]; |
| 51 | } |
| 52 | |
| 53 | static inline struct oxr_pathcxx . . $ oxr_path# * |
| 54 | from_itemcxx . . $ from_item(ae3e4176f3040890).(struct u_hashset_itemcxx . . $ u_hashset_item# *item) |
| 55 | { |
| 56 | return &((struct oxr_pathcxx . . $ oxr_path# *)item)[-1]; |
| 57 | } |
| 58 | |
| 59 | |
| 60 | /* |
| 61 | * |
| 62 | * Static functions. |
| 63 | * |
| 64 | */ |
| 65 | |
| 66 | static XrResultcxx . . $ XrResult# |
| 67 | oxr_ensure_array_lengthcxx . . $ oxr_ensure_array_length(7d4eed8456582fb6).(struct oxr_loggercxx . . $ oxr_logger# *log, struct oxr_instancecxx . . $ oxr_instance# *inst, XrPathcxx . . $ XrPath# *out_id) |
| 68 | { |
| 69 | size_t num = inst->path_numcxx . . $ oxr_instance#path_num. + 1; |
| 70 | |
| 71 | if (num < inst->path_array_lengthcxx . . $ oxr_instance#path_array_length.) { |
| 72 | *out_id = inst->path_numcxx . . $ oxr_instance#path_num.++; |
| 73 | return XR_SUCCESScxx . . $ XR_SUCCESS.; |
| 74 | } |
| 75 | |
| 76 | size_t new_size = inst->path_array_lengthcxx . . $ oxr_instance#path_array_length.; |
| 77 | while (new_size < num) { |
| 78 | new_size += 64; |
| 79 | } |
| 80 | |
| 81 | U_ARRAY_REALLOC_OR_FREEcxx . . $ `src/xrt/auxiliary/util/u_misc.h:117:9`!(inst->path_array, struct oxr_path *, new_size); |
| 82 | inst->path_array_lengthcxx . . $ oxr_instance#path_array_length. = new_size; |
| 83 | |
| 84 | *out_id = inst->path_numcxx . . $ oxr_instance#path_num.++; |
| 85 | |
| 86 | return XR_SUCCESScxx . . $ XR_SUCCESS.; |
| 87 | } |
| 88 | |
| 89 | static XrResultcxx . . $ XrResult# |
| 90 | oxr_allocate_pathcxx . . $ oxr_allocate_path(7487e21edf93b068).( |
| 91 | struct oxr_loggercxx . . $ oxr_logger# *log, struct oxr_instancecxx . . $ oxr_instance# *inst, const char *str, size_t length, struct oxr_pathcxx . . $ oxr_path# **out_path) |
| 92 | { |
| 93 | struct u_hashset_itemcxx . . $ u_hashset_item# *item = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; |
| 94 | struct oxr_pathcxx . . $ oxr_path# *path = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; |
| 95 | size_t size = 0; |
| 96 | int ret; |
| 97 | |
| 98 | size += sizeof(struct oxr_pathcxx . . $ oxr_path#); // Main path object. |
| 99 | size += sizeof(struct u_hashset_itemcxx . . $ u_hashset_item#); // Embedded hashset item. |
| 100 | size += length; // String. |
| 101 | size += 1; // Null terminate it. |
| 102 | |
| 103 | // Now allocate and setup the path. |
| 104 | path = U_CALLOC_WITH_CASTcxx . . $ `src/xrt/auxiliary/util/u_misc.h:37:9`!(struct oxr_path, size); |
| 105 | if (path == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) { |
| 106 | return oxr_errorcxx . . $ oxr_error(1140daf53870d718).(log, XR_ERROR_RUNTIME_FAILUREcxx . . $ XR_ERROR_RUNTIME_FAILURE., "Failed to allocate path"); |
| 107 | } |
| 108 | path->debugcxx . . $ oxr_path#debug. = OXR_XR_DEBUG_PATHcxx . . $ `src/xrt/state_trackers/oxr/oxr_defines.h:21:9`!; |
| 109 | |
| 110 | // Setup the item. |
| 111 | item = get_itemcxx . . $ get_item(936f44567a884b3f).(path); |
| 112 | item->hashcxx . . $ u_hashset_item#hash. = math_hash_stringcxx . . $ math_hash_string(ac5a385650b4bde5).(str, length); |
| 113 | item->lengthcxx . . $ u_hashset_item#length. = length; |
| 114 | |
| 115 | // Yes a const cast! D: |
| 116 | char *store = (char *)item->c_strcxx . . $ u_hashset_item#c_str.; |
| 117 | for (size_t i = 0; i < length; i++) { |
| 118 | store[i] = str[i]; |
| 119 | } |
| 120 | store[length] = '\0'; |
| 121 | |
| 122 | // Insert and return. |
| 123 | ret = u_hashset_insert_itemcxx . . $ u_hashset_insert_item(3bc393a7e8130036).(inst->path_storecxx . . $ oxr_instance#path_store., item); |
| 124 | if (ret) { |
| 125 | freecxx . . $ free(5d22bdacc48458e8).(path); |
| 126 | return oxr_errorcxx . . $ oxr_error(1140daf53870d718).(log, XR_ERROR_RUNTIME_FAILUREcxx . . $ XR_ERROR_RUNTIME_FAILURE., "Failed to insert item"); |
| 127 | } |
| 128 | |
| 129 | oxr_ensure_array_lengthcxx . . $ oxr_ensure_array_length(7d4eed8456582fb6).(log, inst, &path->idcxx . . $ oxr_path#id.); |
| 130 | inst->path_arraycxx . . $ oxr_instance#path_array.[path->idcxx . . $ oxr_path#id.] = path; |
| 131 | |
| 132 | *out_path = path; |
| 133 | |
| 134 | return XR_SUCCESScxx . . $ XR_SUCCESS.; |
| 135 | } |
| 136 | |
| 137 | struct oxr_pathcxx . . $ oxr_path# * |
| 138 | get_path_or_nullcxx . . $ get_path_or_null(d76a438178eb1bf3).(struct oxr_loggercxx . . $ oxr_logger# *log, const struct oxr_instancecxx . . $ oxr_instance# *inst, XrPathcxx . . $ XrPath# xr_path) |
| 139 | { |
| 140 | if (xr_path >= inst->path_array_lengthcxx . . $ oxr_instance#path_array_length.) { |
| 141 | return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; |
| 142 | } |
| 143 | |
| 144 | return inst->path_arraycxx . . $ oxr_instance#path_array.[xr_path]; |
| 145 | } |
| 146 | |
| 147 | |
| 148 | /* |
| 149 | * |
| 150 | * "Exported" functions. |
| 151 | * |
| 152 | */ |
| 153 | |
| 154 | boolcxx . . $ `<external>/14268322491693841775/stdbool.h:36:9`! |
| 155 | oxr_path_is_validcxx . . $ oxr_path_is_valid(5e066177c755985b).(struct oxr_loggercxx . . $ oxr_logger# *log, struct oxr_instancecxx . . $ oxr_instance# *inst, XrPathcxx . . $ XrPath# xr_path) |
| 156 | { |
| 157 | return get_path_or_nullcxx . . $ get_path_or_null(d76a438178eb1bf3).(log, inst, xr_path) != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; |
| 158 | } |
| 159 | |
| 160 | void * |
| 161 | oxr_path_get_attachedcxx . . $ oxr_path_get_attached(4a259facf8454e20).(struct oxr_loggercxx . . $ oxr_logger# *log, struct oxr_instancecxx . . $ oxr_instance# *inst, XrPathcxx . . $ XrPath# xr_path) |
| 162 | { |
| 163 | struct oxr_pathcxx . . $ oxr_path# *path = get_path_or_nullcxx . . $ get_path_or_null(d76a438178eb1bf3).(log, inst, xr_path); |
| 164 | if (path == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) { |
| 165 | return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; |
| 166 | } |
| 167 | |
| 168 | return path->attachedcxx . . $ oxr_path#attached.; |
| 169 | } |
| 170 | |
| 171 | XrResultcxx . . $ XrResult# |
| 172 | oxr_path_get_or_createcxx . . $ oxr_path_get_or_create(ae436ac1536ffc4f).( |
| 173 | struct oxr_loggercxx . . $ oxr_logger# *log, struct oxr_instancecxx . . $ oxr_instance# *inst, const char *str, size_t length, XrPathcxx . . $ XrPath# *out_path) |
| 174 | { |
| 175 | struct u_hashset_itemcxx . . $ u_hashset_item# *item; |
| 176 | struct oxr_pathcxx . . $ oxr_path# *path = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; |
| 177 | XrResultcxx . . $ XrResult# ret; |
| 178 | int h_ret; |
| 179 | |
| 180 | // Look it up the instance path store. |
| 181 | h_ret = u_hashset_find_strcxx . . $ u_hashset_find_str(9a92c917070d9d44).(inst->path_storecxx . . $ oxr_instance#path_store., str, length, &item); |
| 182 | if (h_ret == 0) { |
| 183 | *out_path = to_xr_pathcxx . . $ to_xr_path(b9788cfe1cf21842).(from_itemcxx . . $ from_item(ae3e4176f3040890).(item)); |
| 184 | return XR_SUCCESScxx . . $ XR_SUCCESS.; |
| 185 | } |
| 186 | |
| 187 | // Create the path since it was not found. |
| 188 | ret = oxr_allocate_pathcxx . . $ oxr_allocate_path(7487e21edf93b068).(log, inst, str, length, &path); |
| 189 | if (ret != XR_SUCCESScxx . . $ XR_SUCCESS.) { |
| 190 | return ret; |
| 191 | } |
| 192 | |
| 193 | *out_path = to_xr_pathcxx . . $ to_xr_path(b9788cfe1cf21842).(path); |
| 194 | |
| 195 | return XR_SUCCESScxx . . $ XR_SUCCESS.; |
| 196 | } |
| 197 | |
| 198 | XrResultcxx . . $ XrResult# |
| 199 | oxr_path_only_getcxx . . $ oxr_path_only_get(ae436ac1536ffc4f).(struct oxr_loggercxx . . $ oxr_logger# *log, struct oxr_instancecxx . . $ oxr_instance# *inst, const char *str, size_t length, XrPathcxx . . $ XrPath# *out_path) |
| 200 | { |
| 201 | struct u_hashset_itemcxx . . $ u_hashset_item# *item; |
| 202 | int h_ret; |
| 203 | |
| 204 | // Look it up the instance path store. |
| 205 | h_ret = u_hashset_find_strcxx . . $ u_hashset_find_str(9a92c917070d9d44).(inst->path_storecxx . . $ oxr_instance#path_store., str, length, &item); |
| 206 | if (h_ret == 0) { |
| 207 | *out_path = to_xr_pathcxx . . $ to_xr_path(b9788cfe1cf21842).(from_itemcxx . . $ from_item(ae3e4176f3040890).(item)); |
| 208 | return XR_SUCCESScxx . . $ XR_SUCCESS.; |
| 209 | } |
| 210 | |
| 211 | *out_path = XR_NULL_PATHcxx . . $ `src/external/openxr_includes/openxr/openxr.h:53:9`!; |
| 212 | return XR_SUCCESScxx . . $ XR_SUCCESS.; |
| 213 | } |
| 214 | |
| 215 | XrResultcxx . . $ XrResult# |
| 216 | oxr_path_get_stringcxx . . $ oxr_path_get_string(aa55199e18d2d48b).( |
| 217 | struct oxr_loggercxx . . $ oxr_logger# *log, const struct oxr_instancecxx . . $ oxr_instance# *inst, XrPathcxx . . $ XrPath# xr_path, const char **out_str, size_t *out_length) |
| 218 | { |
| 219 | struct oxr_pathcxx . . $ oxr_path# *path = get_path_or_nullcxx . . $ get_path_or_null(d76a438178eb1bf3).(log, inst, xr_path); |
| 220 | if (path == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) { |
| 221 | return XR_ERROR_PATH_INVALIDcxx . . $ XR_ERROR_PATH_INVALID.; |
| 222 | } |
| 223 | |
| 224 | *out_str = get_itemcxx . . $ get_item(936f44567a884b3f).(path)->c_strcxx . . $ u_hashset_item#c_str.; |
| 225 | *out_length = get_itemcxx . . $ get_item(936f44567a884b3f).(path)->lengthcxx . . $ u_hashset_item#length.; |
| 226 | |
| 227 | return XR_SUCCESScxx . . $ XR_SUCCESS.; |
| 228 | } |
| 229 | |
| 230 | void |
| 231 | destroy_callbackcxx . . $ destroy_callback(f3677c0ea1810fff).(struct u_hashset_itemcxx . . $ u_hashset_item# *item, void *priv) |
| 232 | { |
| 233 | struct oxr_pathcxx . . $ oxr_path# *path = from_itemcxx . . $ from_item(ae3e4176f3040890).(item); |
| 234 | |
| 235 | freecxx . . $ free(5d22bdacc48458e8).(path); |
| 236 | } |
| 237 | |
| 238 | XrResultcxx . . $ XrResult# |
| 239 | oxr_path_initcxx . . $ oxr_path_init(63486995f0ea0a11).(struct oxr_loggercxx . . $ oxr_logger# *log, struct oxr_instancecxx . . $ oxr_instance# *inst) |
| 240 | { |
| 241 | int h_ret = u_hashset_createcxx . . $ u_hashset_create(b74a834091d759f7).(&inst->path_storecxx . . $ oxr_instance#path_store.); |
| 242 | if (h_ret != 0) { |
| 243 | return oxr_errorcxx . . $ oxr_error(1140daf53870d718).(log, XR_ERROR_RUNTIME_FAILUREcxx . . $ XR_ERROR_RUNTIME_FAILURE., "Failed to create hashset"); |
| 244 | } |
| 245 | |
| 246 | size_t new_size = 64; |
| 247 | U_ARRAY_REALLOC_OR_FREEcxx . . $ `src/xrt/auxiliary/util/u_misc.h:117:9`!(inst->path_array, struct oxr_path *, new_size); |
| 248 | inst->path_array_lengthcxx . . $ oxr_instance#path_array_length. = new_size; |
| 249 | inst->path_numcxx . . $ oxr_instance#path_num. = 1; // Reserve space for XR_NULL_PATH |
| 250 | |
| 251 | return XR_SUCCESScxx . . $ XR_SUCCESS.; |
| 252 | } |
| 253 | |
| 254 | void |
| 255 | oxr_path_destroycxx . . $ oxr_path_destroy(5171e90bd7167858).(struct oxr_loggercxx . . $ oxr_logger# *log, struct oxr_instancecxx . . $ oxr_instance# *inst) |
| 256 | { |
| 257 | if (inst->path_arraycxx . . $ oxr_instance#path_array. != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) { |
| 258 | freecxx . . $ free(5d22bdacc48458e8).(inst->path_arraycxx . . $ oxr_instance#path_array.); |
| 259 | } |
| 260 | |
| 261 | inst->path_arraycxx . . $ oxr_instance#path_array. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; |
| 262 | inst->path_numcxx . . $ oxr_instance#path_num. = 0; |
| 263 | inst->path_array_lengthcxx . . $ oxr_instance#path_array_length. = 0; |
| 264 | |
| 265 | if (inst->path_storecxx . . $ oxr_instance#path_store. == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) { |
| 266 | return; |
| 267 | } |
| 268 | |
| 269 | u_hashset_clear_and_call_for_eachcxx . . $ u_hashset_clear_and_call_for_each(df8202dff1b21341).(inst->path_storecxx . . $ oxr_instance#path_store., destroy_callbackcxx . . $ destroy_callback(f3677c0ea1810fff)., inst); |
| 270 | u_hashset_destroycxx . . $ u_hashset_destroy(b74a834091d759f7).(&inst->path_storecxx . . $ oxr_instance#path_store.); |
| 271 | } |