tangled
alpha
login
or
join now
matrixfurry.com
/
monado
0
fork
atom
The open source OpenXR runtime
0
fork
atom
overview
issues
pulls
pipelines
util: Add function for u_hashset to allocate a item
Jakob Bornecrantz
5 years ago
ebd5773f
0d2a24b9
+67
-2
3 changed files
expand all
collapse all
unified
split
doc
changes
auxiliary
mr.359.2.md
src
xrt
auxiliary
util
u_hashset.cpp
u_hashset.h
+2
doc/changes/auxiliary/mr.359.2.md
···
1
1
+
util: Expand `u_hashset` to be able to automatically allocate a `u_hashet_item`
2
2
+
and insert it.
+53
-1
src/xrt/auxiliary/util/u_hashset.cpp
···
1
1
-
// Copyright 2019, Collabora, Ltd.
1
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
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
84
+
}
85
85
+
86
86
+
extern "C" int
87
87
+
u_hashset_create_and_insert_str(struct u_hashset *hs,
88
88
+
const char *str,
89
89
+
size_t length,
90
90
+
struct u_hashset_item **out_item)
91
91
+
{
92
92
+
struct u_hashset_item *dummy = NULL;
93
93
+
struct u_hashset_item *item = NULL;
94
94
+
size_t size = 0;
95
95
+
int ret;
96
96
+
97
97
+
ret = u_hashset_find_str(hs, str, length, &dummy);
98
98
+
if (ret >= 0) {
99
99
+
return -1;
100
100
+
}
101
101
+
102
102
+
size += sizeof(struct u_hashset_item); // Hashset item.
103
103
+
size += length; // String.
104
104
+
size += 1; // Null terminate it.
105
105
+
106
106
+
// Now allocate and setup the path.
107
107
+
item = U_CALLOC_WITH_CAST(struct u_hashset_item, size);
108
108
+
if (item == NULL) {
109
109
+
return -1;
110
110
+
}
111
111
+
112
112
+
item->length = length;
113
113
+
// Yes a const cast! D:
114
114
+
char *store = const_cast<char *>(item->c_str());
115
115
+
for (size_t i = 0; i < length; i++) {
116
116
+
store[i] = str[i];
117
117
+
}
118
118
+
store[length] = '\0';
119
119
+
120
120
+
std::string key = std::string(item->c_str(), item->length);
121
121
+
hs->map[key] = item;
122
122
+
123
123
+
*out_item = item;
124
124
+
125
125
+
return 0;
126
126
+
}
127
127
+
128
128
+
extern "C" int
129
129
+
u_hashset_create_and_insert_str_c(struct u_hashset *hs,
130
130
+
const char *c_str,
131
131
+
struct u_hashset_item **out_item)
132
132
+
{
133
133
+
size_t length = strlen(c_str);
134
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
1
-
// Copyright 2019, Collabora, Ltd.
1
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
70
+
71
71
+
int
72
72
+
u_hashset_create_and_insert_str(struct u_hashset *hs,
73
73
+
const char *str,
74
74
+
size_t length,
75
75
+
struct u_hashset_item **out_item);
76
76
+
77
77
+
int
78
78
+
u_hashset_create_and_insert_str_c(struct u_hashset *hs,
79
79
+
const char *c_str,
80
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);