The open source OpenXR runtime

util: Add function for u_hashset to allocate a item

+67 -2
+2
doc/changes/auxiliary/mr.359.2.md
··· 1 + util: Expand `u_hashset` to be able to automatically allocate a `u_hashet_item` 2 + and insert it.
+53 -1
src/xrt/auxiliary/util/u_hashset.cpp
··· 1 - // Copyright 2019, Collabora, Ltd. 1 + // Copyright 2019-2020, Collabora, Ltd. 2 2 // SPDX-License-Identifier: BSL-1.0 3 3 /*! 4 4 * @file ··· 7 7 * @ingroup aux_util 8 8 */ 9 9 10 + #include "util/u_misc.h" 10 11 #include "util/u_hashset.h" 11 12 12 13 #include <cstring> ··· 80 81 std::string key = std::string(item->c_str(), item->length); 81 82 hs->map[key] = item; 82 83 return 0; 84 + } 85 + 86 + extern "C" int 87 + u_hashset_create_and_insert_str(struct u_hashset *hs, 88 + const char *str, 89 + size_t length, 90 + struct u_hashset_item **out_item) 91 + { 92 + struct u_hashset_item *dummy = NULL; 93 + struct u_hashset_item *item = NULL; 94 + size_t size = 0; 95 + int ret; 96 + 97 + ret = u_hashset_find_str(hs, str, length, &dummy); 98 + if (ret >= 0) { 99 + return -1; 100 + } 101 + 102 + size += sizeof(struct u_hashset_item); // Hashset item. 103 + size += length; // String. 104 + size += 1; // Null terminate it. 105 + 106 + // Now allocate and setup the path. 107 + item = U_CALLOC_WITH_CAST(struct u_hashset_item, size); 108 + if (item == NULL) { 109 + return -1; 110 + } 111 + 112 + item->length = length; 113 + // Yes a const cast! D: 114 + char *store = const_cast<char *>(item->c_str()); 115 + for (size_t i = 0; i < length; i++) { 116 + store[i] = str[i]; 117 + } 118 + store[length] = '\0'; 119 + 120 + std::string key = std::string(item->c_str(), item->length); 121 + hs->map[key] = item; 122 + 123 + *out_item = item; 124 + 125 + return 0; 126 + } 127 + 128 + extern "C" int 129 + u_hashset_create_and_insert_str_c(struct u_hashset *hs, 130 + const char *c_str, 131 + struct u_hashset_item **out_item) 132 + { 133 + size_t length = strlen(c_str); 134 + return u_hashset_create_and_insert_str(hs, c_str, length, out_item); 83 135 } 84 136 85 137 extern "C" int
+12 -1
src/xrt/auxiliary/util/u_hashset.h
··· 1 - // Copyright 2019, Collabora, Ltd. 1 + // Copyright 2019-2020, Collabora, Ltd. 2 2 // SPDX-License-Identifier: BSL-1.0 3 3 /*! 4 4 * @file ··· 67 67 u_hashset_find_c_str(struct u_hashset *hs, 68 68 const char *c_str, 69 69 struct u_hashset_item **out_item); 70 + 71 + int 72 + u_hashset_create_and_insert_str(struct u_hashset *hs, 73 + const char *str, 74 + size_t length, 75 + struct u_hashset_item **out_item); 76 + 77 + int 78 + u_hashset_create_and_insert_str_c(struct u_hashset *hs, 79 + const char *c_str, 80 + struct u_hashset_item **out_item); 70 81 71 82 int 72 83 u_hashset_insert_item(struct u_hashset *hs, struct u_hashset_item *item);