Skip to content

Commit 796aa99

Browse files
committed
Remove unnecessary refcounting and allocation for symbol, occurrence, relation
These objects are now valid only during the callbacks that provide them from the index. Since we already converted them immediately, this didn't impact the Swift code except to remove the indexstoredb_release calls for these. I updated the documentation to indicate the new lifetime. Incidentally, relation was using the make_object machinery even though it was documented to only live for the callback, so this fixes a leak of relations.
1 parent 7cf04b8 commit 796aa99

File tree

4 files changed

+96
-61
lines changed

4 files changed

+96
-61
lines changed

Sources/IndexStoreDB/Symbol.swift

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,6 @@ extension Symbol {
9595
usr: String(cString: indexstoredb_symbol_usr(value)),
9696
name: String(cString: indexstoredb_symbol_name(value)),
9797
kind: IndexSymbolKind(indexstoredb_symbol_kind(value)))
98-
99-
// FIXME: remove unnecessary refcounting of symbols.
100-
indexstoredb_release(value)
10198
}
10299
}
103100

Sources/IndexStoreDB/SymbolOccurrence.swift

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,6 @@ extension SymbolOccurrence {
7272
location: SymbolLocation(indexstoredb_symbol_occurrence_location(value)),
7373
roles: SymbolRole(rawValue: indexstoredb_symbol_occurrence_roles(value)),
7474
relations: relations)
75-
76-
// FIXME: remove unnecessary refcounting of occurrences.
77-
indexstoredb_release(value)
7875
}
7976
}
8077

include/CIndexStoreDB/CIndexStoreDB.h

Lines changed: 72 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,9 @@ INDEXSTOREDB_BEGIN_DECLS
4747
typedef void *indexstoredb_object_t;
4848
typedef indexstoredb_object_t indexstoredb_index_t;
4949
typedef indexstoredb_object_t indexstoredb_indexstore_library_t;
50-
typedef indexstoredb_object_t indexstoredb_symbol_t;
51-
typedef indexstoredb_object_t indexstoredb_symbol_occurrence_t;
5250

51+
typedef void *indexstoredb_symbol_t;
52+
typedef void *indexstoredb_symbol_occurrence_t;
5353
typedef void *indexstoredb_error_t;
5454
typedef void *indexstoredb_symbol_location_t;
5555
typedef void *indexstoredb_symbol_relation_t;
@@ -121,6 +121,9 @@ typedef bool(^indexstoredb_symbol_occurrence_receiver_t)(_Nonnull indexstoredb_s
121121
/// Returns true to continue.
122122
typedef bool(^indexstoredb_symbol_name_receiver)(const char *_Nonnull);
123123

124+
/// Creates an index for the given raw index data in \p storePath.
125+
///
126+
/// The resulting index must be released using \c indexstoredb_release.
124127
INDEXSTOREDB_PUBLIC _Nullable
125128
indexstoredb_index_t
126129
indexstoredb_index_create(const char * _Nonnull storePath,
@@ -130,6 +133,9 @@ indexstoredb_index_create(const char * _Nonnull storePath,
130133
bool listenToUnitEvents,
131134
indexstoredb_error_t _Nullable * _Nullable);
132135

136+
/// Creates an indexstore library for the given library.
137+
///
138+
/// The resulting object must be released using \c indexstoredb_release.
133139
INDEXSTOREDB_PUBLIC _Nullable
134140
indexstoredb_indexstore_library_t
135141
indexstoredb_load_indexstore_library(const char * _Nonnull dylibPath,
@@ -139,95 +145,129 @@ indexstoredb_load_indexstore_library(const char * _Nonnull dylibPath,
139145
INDEXSTOREDB_PUBLIC void
140146
indexstoredb_index_poll_for_unit_changes_and_wait(_Nonnull indexstoredb_index_t index);
141147

148+
/// Iterates over each symbol occurrence matching the given \p usr and \p roles.
149+
///
150+
/// The occurrence passed to the receiver is only valid for the duration of the
151+
/// receiver call.
142152
INDEXSTOREDB_PUBLIC bool
143153
indexstoredb_index_symbol_occurrences_by_usr(
144154
_Nonnull indexstoredb_index_t index,
145155
const char *_Nonnull usr,
146156
uint64_t roles,
147157
_Nonnull indexstoredb_symbol_occurrence_receiver_t);
148158

159+
/// Iterates over each symbol occurrence related to the \p usr with \p roles.
160+
///
161+
/// The occurrence passed to the receiver is only valid for the duration of the
162+
/// receiver call.
149163
INDEXSTOREDB_PUBLIC bool
150164
indexstoredb_index_related_symbol_occurrences_by_usr(
151165
_Nonnull indexstoredb_index_t index,
152166
const char *_Nonnull usr,
153167
uint64_t roles,
154168
_Nonnull indexstoredb_symbol_occurrence_receiver_t);
155169

170+
/// Returns the USR of the given symbol.
171+
///
172+
/// The string has the same lifetime as the \c indexstoredb_symbol_t.
156173
INDEXSTOREDB_PUBLIC
157174
const char * _Nonnull
158175
indexstoredb_symbol_usr(_Nonnull indexstoredb_symbol_t);
159176

177+
/// Returns the name of the given symbol.
178+
///
179+
/// The string has the same lifetime as the \c indexstoredb_symbol_t.
160180
INDEXSTOREDB_PUBLIC
161181
const char * _Nonnull
162182
indexstoredb_symbol_name(_Nonnull indexstoredb_symbol_t);
163183

184+
/// Returns the symbol of the given symbol occurrence.
185+
///
186+
/// The symbol has the same lifetime as the \c indexstoredb_symbol_occurrence_t.
164187
INDEXSTOREDB_PUBLIC
165188
_Nonnull indexstoredb_symbol_t
166189
indexstoredb_symbol_occurrence_symbol(_Nonnull indexstoredb_symbol_occurrence_t);
167190

191+
/// Returns the roles of the given symbol occurrence.
168192
INDEXSTOREDB_PUBLIC uint64_t
169193
indexstoredb_symbol_occurrence_roles(_Nonnull indexstoredb_symbol_occurrence_t);
170194

171-
/// The location is owned by the occurrence and shall not be used after the occurrence is freed.
195+
/// Returns the location of the given symbol occurrence.
196+
///
197+
/// The location has the same lifetime as the \c indexstoredb_symbol_occurrence_t.
172198
INDEXSTOREDB_PUBLIC _Nonnull
173199
indexstoredb_symbol_location_t
174200
indexstoredb_symbol_occurrence_location(_Nonnull indexstoredb_symbol_occurrence_t);
175201

202+
/// Returns the path of the given symbol location.
203+
///
204+
/// The string has the same lifetime as the \c indexstoredb_symbol_location_t.
176205
INDEXSTOREDB_PUBLIC
177206
const char * _Nonnull
178207
indexstoredb_symbol_location_path(_Nonnull indexstoredb_symbol_location_t);
179208

209+
/// Returns whether the given symbol location is a system location.
180210
INDEXSTOREDB_PUBLIC bool
181211
indexstoredb_symbol_location_is_system(_Nonnull indexstoredb_symbol_location_t);
182212

213+
/// Returns the one-based line number of the given symbol location.
183214
INDEXSTOREDB_PUBLIC int
184215
indexstoredb_symbol_location_line(_Nonnull indexstoredb_symbol_location_t);
185216

217+
/// Returns the one-based UTF-8 column index of the given symbol location.
186218
INDEXSTOREDB_PUBLIC int
187219
indexstoredb_symbol_location_column_utf8(_Nonnull indexstoredb_symbol_location_t);
188220

221+
/// Retains the given \c indexstoredb_object_t and returns it.
189222
INDEXSTOREDB_PUBLIC _Nonnull
190223
indexstoredb_object_t
191224
indexstoredb_retain(_Nonnull indexstoredb_object_t);
192225

226+
/// Releases the given \c indexstoredb_object_t.
193227
INDEXSTOREDB_PUBLIC void
194228
indexstoredb_release(_Nonnull indexstoredb_object_t);
195229

230+
/// Returns the string describing the given error.
231+
///
232+
/// The string has the same lifetime as the \c indexstoredb_error_t.
196233
INDEXSTOREDB_PUBLIC const char * _Nonnull
197234
indexstoredb_error_get_description(_Nonnull indexstoredb_error_t);
198235

236+
/// Destroys the given error.
199237
INDEXSTOREDB_PUBLIC void
200238
indexstoredb_error_dispose(_Nullable indexstoredb_error_t);
201239

202-
/// Loops through each symbol in the index and calls the receiver function with each symbol.
203-
/// @param index An IndexStoreDB object which contains the symbols.
204-
/// @param receiver A function to be called for each symbol, the CString of the symbol will be passed in to this function.
205-
/// The function should return a boolean indicating whether the looping should continue.
240+
/// Iterates over the name of every symbol in the index.
241+
///
242+
/// \param index An IndexStoreDB object which contains the symbols.
243+
/// \param receiver A function to be called for each symbol. The string pointer is only valid for
244+
/// the duration of the call. The function should return a true to continue iterating.
206245
INDEXSTOREDB_PUBLIC bool
207246
indexstoredb_index_symbol_names(_Nonnull indexstoredb_index_t index, _Nonnull indexstoredb_symbol_name_receiver);
208247

209-
/// Loops through each canonical symbol that matches the string and performs the passed in function.
210-
/// @param index An IndexStoreDB object which contains the symbols.
211-
/// @param symbolName The name of the symbol whose canonical occurence should be found.
212-
/// @param receiver A function to be called for each canonical occurence.
213-
/// The SymbolOccurenceRef of the symbol will be passed in to this function.
214-
/// The function should return a boolean indicating whether the looping should continue.
248+
/// Iterates over every canonical symbol that matches the string.
249+
///
250+
/// \param index An IndexStoreDB object which contains the symbols.
251+
/// \param symbolName The name of the symbol whose canonical occurence should be found.
252+
/// \param receiver A function to be called for each canonical occurence.
253+
/// The canonical symbol occurrence will be passed in to this function. It is valid only for the
254+
/// duration of the call. The function should return true to continue iterating.
215255
INDEXSTOREDB_PUBLIC bool
216256
indexstoredb_index_canonical_symbol_occurences_by_name(
217257
indexstoredb_index_t _Nonnull index,
218258
const char *_Nonnull symbolName,
219259
indexstoredb_symbol_occurrence_receiver_t _Nonnull receiver
220260
);
221261

222-
/// Loops through each canonical symbol that matches the pattern and performs the passed in function.
223-
/// @param index An IndexStoreDB object which contains the symbols.
224-
/// @param anchorStart When true, symbol names should only be considered matching when the first characters of the symbol name match the pattern.
225-
/// @param anchorEnd When true, symbol names should only be considered matching when the first characters of the symbol name match the pattern.
226-
/// @param subsequence When true, symbols will be matched even if the pattern is not matched contiguously.
227-
/// @param ignoreCase When true, symbols may be returned even if the case of letters does not match the pattern.
228-
/// @param receiver A function to be called for each canonical occurence that matches the pattern.
229-
/// The SymbolOccurenceRef of the symbol will be passed in to this function.
230-
/// The function should return a boolean indicating whether the looping should continue.
262+
/// Iterates over every canonical symbol that matches the pattern.
263+
///
264+
/// \param index An IndexStoreDB object which contains the symbols.
265+
/// \param anchorStart When true, symbol names should only be considered matching when the first characters of the symbol name match the pattern.
266+
/// \param anchorEnd When true, symbol names should only be considered matching when the first characters of the symbol name match the pattern.
267+
/// \param subsequence When true, symbols will be matched even if the pattern is not matched contiguously.
268+
/// \param ignoreCase When true, symbols may be returned even if the case of letters does not match the pattern.
269+
/// \param receiver A function to be called for each canonical occurence that matches the pattern.
270+
/// It is valid only for the duration of the call. The function should return true to continue iterating.
231271
INDEXSTOREDB_PUBLIC bool
232272
indexstoredb_index_canonical_symbol_occurences_containing_pattern(
233273
_Nonnull indexstoredb_index_t index,
@@ -238,27 +278,28 @@ indexstoredb_index_canonical_symbol_occurences_containing_pattern(
238278
bool ignoreCase,
239279
_Nonnull indexstoredb_symbol_occurrence_receiver_t receiver);
240280

241-
/// Gets the set of roles of the passed in symbol relation
242-
/// @param relation A symbol relation
281+
/// Returns the set of roles of the given symbol relation.
243282
INDEXSTOREDB_PUBLIC uint64_t
244283
indexstoredb_symbol_relation_get_roles(_Nonnull indexstoredb_symbol_relation_t);
245284

246-
/// Gets the symbol associated with the passed in relation
247-
/// @param relation A symbol relation
285+
/// Returns the symbol of the given symbol relation.
286+
///
287+
/// The symbol has the same lifetime as the \c indexstoredb_symbol_relation_t.
248288
INDEXSTOREDB_PUBLIC _Nonnull indexstoredb_symbol_t
249289
indexstoredb_symbol_relation_get_symbol(_Nonnull indexstoredb_symbol_relation_t);
250290

251-
/// Loops through each relation that a passed in symbol has, and performs the passed in function.
291+
/// Iterates over the relations of the given symbol occurrence.
292+
///
252293
/// The relations are owned by the occurrence and shall not be used after the occurrence is freed.
253-
/// @param occurrence The symbol occurrence that whose relations should be found.
254-
/// @param applier The function that should be performed on each symbol relation.
294+
///
295+
/// \param occurrence The symbol occurrence that whose relations should be found.
296+
/// \param applier The function that should be performed on each symbol relation.
255297
/// The function should return a boolean indicating whether the looping should continue.
256298
INDEXSTOREDB_PUBLIC bool
257299
indexstoredb_symbol_occurrence_relations(_Nonnull indexstoredb_symbol_occurrence_t,
258300
bool(^ _Nonnull applier)(indexstoredb_symbol_relation_t _Nonnull ));
259301

260-
/// Get the SymbolKind of a Symbol
261-
/// @param symbol The symbol whose kind should be found.
302+
/// Returns the kind of the given symbol.
262303
INDEXSTOREDB_PUBLIC indexstoredb_symbol_kind_t
263304
indexstoredb_symbol_kind(_Nonnull indexstoredb_symbol_t);
264305

lib/CIndexStoreDB/CIndexStoreDB.cpp

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ indexstoredb_index_symbol_occurrences_by_usr(
120120
auto obj = (IndexStoreDBObject<std::shared_ptr<IndexSystem>> *)index;
121121
return obj->value->foreachSymbolOccurrenceByUSR(usr, (SymbolRoleSet)roles,
122122
[&](SymbolOccurrenceRef Occur) -> bool {
123-
return receiver(make_object(Occur));
123+
return receiver((indexstoredb_symbol_occurrence_t)Occur.get());
124124
});
125125
}
126126

@@ -134,26 +134,26 @@ indexstoredb_index_related_symbol_occurrences_by_usr(
134134
auto obj = (IndexStoreDBObject<std::shared_ptr<IndexSystem>> *)index;
135135
return obj->value->foreachRelatedSymbolOccurrenceByUSR(usr, (SymbolRoleSet)roles,
136136
[&](SymbolOccurrenceRef Occur) -> bool {
137-
return receiver(make_object(Occur));
137+
return receiver((indexstoredb_symbol_occurrence_t)Occur.get());
138138
});
139139
}
140140

141141
const char *
142142
indexstoredb_symbol_usr(indexstoredb_symbol_t symbol) {
143-
auto obj = (IndexStoreDBObject<std::shared_ptr<Symbol>> *)symbol;
144-
return obj->value->getUSR().c_str();
143+
auto value = (Symbol *)symbol;
144+
return value->getUSR().c_str();
145145
}
146146

147147
const char *
148148
indexstoredb_symbol_name(indexstoredb_symbol_t symbol) {
149-
auto obj = (IndexStoreDBObject<std::shared_ptr<Symbol>> *)symbol;
150-
return obj->value->getName().c_str();
149+
auto value = (Symbol *)symbol;
150+
return value->getName().c_str();
151151
}
152152

153153
indexstoredb_symbol_kind_t
154154
indexstoredb_symbol_kind(indexstoredb_symbol_t symbol) {
155-
auto symbolObj = (IndexStoreDBObject<std::shared_ptr<Symbol>> *)symbol;
156-
return toCSymbolKind(symbolObj->value->getSymbolKind());
155+
auto value = (Symbol *)symbol;
156+
return toCSymbolKind(value->getSymbolKind());
157157
}
158158

159159
bool
@@ -172,7 +172,7 @@ indexstoredb_index_canonical_symbol_occurences_by_name(
172172
{
173173
auto obj = (IndexStoreDBObject<std::shared_ptr<IndexSystem>> *)index;
174174
return obj->value->foreachCanonicalSymbolOccurrenceByName(symbolName, [&](SymbolOccurrenceRef occur) -> bool {
175-
return receiver(make_object(occur));
175+
return receiver((indexstoredb_symbol_occurrence_t)occur.get());
176176
});
177177
}
178178

@@ -195,35 +195,35 @@ indexstoredb_index_canonical_symbol_occurences_containing_pattern(
195195
ignoreCase,
196196
[&](SymbolOccurrenceRef occur
197197
) -> bool {
198-
return receiver(make_object(occur));
198+
return receiver((indexstoredb_symbol_occurrence_t)occur.get());
199199
});
200200
}
201201

202202
indexstoredb_symbol_t
203203
indexstoredb_symbol_occurrence_symbol(indexstoredb_symbol_occurrence_t occur) {
204-
auto obj = (IndexStoreDBObject<SymbolOccurrenceRef> *)occur;
205-
return make_object(obj->value->getSymbol());
204+
auto value = (SymbolOccurrence *)occur;
205+
return (indexstoredb_symbol_t)value->getSymbol().get();
206206
}
207207

208208
uint64_t
209209
indexstoredb_symbol_relation_get_roles(indexstoredb_symbol_relation_t relation) {
210-
auto relationObj = (IndexStoreDBObject<SymbolRelation> *)relation;
211-
return relationObj->value.getRoles().toRaw();
210+
auto value = (SymbolRelation *)relation;
211+
return value->getRoles().toRaw();
212212
}
213213

214214
indexstoredb_symbol_t
215215
indexstoredb_symbol_relation_get_symbol(indexstoredb_symbol_relation_t relation) {
216-
auto relationObj = (IndexStoreDBObject<SymbolRelation> *)relation;
217-
return make_object(relationObj->value.getSymbol());
216+
auto value = (SymbolRelation *)relation;
217+
return (indexstoredb_symbol_t)(value->getSymbol().get());
218218
}
219219

220220
bool
221221
indexstoredb_symbol_occurrence_relations(indexstoredb_symbol_occurrence_t occurrence,
222222
bool(^applier)(indexstoredb_symbol_relation_t)) {
223-
auto occurrenceObj = (IndexStoreDBObject<SymbolOccurrenceRef> *)occurrence;
224-
ArrayRef<SymbolRelation> relations = occurrenceObj->value->getRelations();
225-
for (SymbolRelation rel : relations) {
226-
if(!applier(make_object(rel))) {
223+
auto value = (SymbolOccurrence *)occurrence;
224+
ArrayRef<SymbolRelation> relations = value->getRelations();
225+
for (auto &rel : relations) {
226+
if(!applier((indexstoredb_symbol_relation_t)&rel)) {
227227
return false;
228228
}
229229
}
@@ -232,14 +232,14 @@ indexstoredb_symbol_occurrence_relations(indexstoredb_symbol_occurrence_t occurr
232232

233233
uint64_t
234234
indexstoredb_symbol_occurrence_roles(indexstoredb_symbol_occurrence_t occur) {
235-
auto obj = (IndexStoreDBObject<SymbolOccurrenceRef> *)occur;
236-
return (uint64_t)obj->value->getRoles();
235+
auto value = (SymbolOccurrence *)occur;
236+
return (uint64_t)value->getRoles();
237237
}
238238

239239
indexstoredb_symbol_location_t indexstoredb_symbol_occurrence_location(
240240
indexstoredb_symbol_occurrence_t occur) {
241-
auto obj = (IndexStoreDBObject<SymbolOccurrenceRef> *)occur;
242-
return (indexstoredb_symbol_location_t)&obj->value->getLocation();
241+
auto value = (SymbolOccurrence *)occur;
242+
return (indexstoredb_symbol_location_t)&value->getLocation();
243243
}
244244

245245
const char *

0 commit comments

Comments
 (0)