The open source OpenXR runtime
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

a/util: rework sessions handling in u_system

Part-of: <https://gitlab.freedesktop.org/monado/monado/-/merge_requests/2440>

authored by

Simon Zeni and committed by
Rylie Pavlik
e926230f b51ddbac

+29 -10
+27 -10
src/xrt/auxiliary/util/u_system.c
··· 93 93 94 94 if (usys->sessions.count > 0) { 95 95 U_LOG_E("Number of sessions not zero, things will crash!"); 96 - free(usys->sessions.pairs); 97 - usys->sessions.pairs = NULL; 98 - usys->sessions.count = 0; 99 96 } 100 97 98 + free(usys->sessions.pairs); 101 99 free(usys); 102 100 } 103 101 ··· 120 118 // xrt_session_event_sink fields. 121 119 usys->broadcast.push_event = push_event; 122 120 121 + usys->sessions.capacity = 2; 122 + usys->sessions.pairs = U_TYPED_ARRAY_CALLOC(struct u_system_session_pair, usys->sessions.capacity); 123 + if (usys->sessions.pairs == NULL) { 124 + U_LOG_E("Failed to allocate session array"); 125 + free(usys); 126 + return NULL; 127 + } 128 + 123 129 // u_system fields. 124 130 XRT_MAYBE_UNUSED int ret = os_mutex_init(&usys->sessions.mutex); 125 131 assert(ret == 0); ··· 135 141 136 142 os_mutex_lock(&usys->sessions.mutex); 137 143 138 - uint32_t count = usys->sessions.count; 144 + const uint32_t new_count = usys->sessions.count + 1; 139 145 140 - U_ARRAY_REALLOC_OR_FREE(usys->sessions.pairs, struct u_system_session_pair, count + 1); 146 + if (new_count > usys->sessions.capacity) { 147 + usys->sessions.capacity *= 2; 148 + const size_t size = usys->sessions.capacity * sizeof(*usys->sessions.pairs); 149 + struct u_system_session_pair *tmp = realloc(usys->sessions.pairs, size); 150 + if (tmp == NULL) { 151 + U_LOG_E("Failed to reallocate session array"); 152 + goto add_unlock; 153 + } 154 + usys->sessions.pairs = tmp; 155 + } 141 156 142 - usys->sessions.pairs[count] = (struct u_system_session_pair){xs, xses}; 143 - usys->sessions.count++; 157 + usys->sessions.pairs[usys->sessions.count++] = (struct u_system_session_pair){xs, xses}; 144 158 159 + add_unlock: 145 160 os_mutex_unlock(&usys->sessions.mutex); 146 161 } 147 162 ··· 165 180 // Guards against empty array as well as not finding the session. 166 181 if (dst >= count) { 167 182 U_LOG_E("Could not find session to remove!"); 168 - goto unlock; 183 + goto remove_unlock; 169 184 } 170 185 171 186 // Should not be true with above check. ··· 183 198 } 184 199 185 200 count--; 186 - U_ARRAY_REALLOC_OR_FREE(usys->sessions.pairs, struct u_system_session_pair, count); 201 + // This ensures that the memory returned in add is always zero initialized. 202 + U_ZERO(&usys->sessions.pairs[count]); 203 + 187 204 usys->sessions.count = count; 188 205 189 - unlock: 206 + remove_unlock: 190 207 os_mutex_unlock(&usys->sessions.mutex); 191 208 } 192 209
+2
src/xrt/auxiliary/util/u_system.h
··· 52 52 53 53 //! Number of session and event sink pairs. 54 54 uint32_t count; 55 + //! Capacity of the session array. 56 + uint32_t capacity; 55 57 //! Array of session and event sink pairs. 56 58 struct u_system_session_pair *pairs; 57 59 } sessions;