Skip to content

Commit c6a437e

Browse files
authored
Merge pull request #43 from benlangmuir/refless
Remove unnecessary refcounting and allocation for symbol, occurrence, relation
2 parents 65bbf59 + 796aa99 commit c6a437e

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
@@ -51,9 +51,9 @@ INDEXSTOREDB_BEGIN_DECLS
5151
typedef void *indexstoredb_object_t;
5252
typedef indexstoredb_object_t indexstoredb_index_t;
5353
typedef indexstoredb_object_t indexstoredb_indexstore_library_t;
54-
typedef indexstoredb_object_t indexstoredb_symbol_t;
55-
typedef indexstoredb_object_t indexstoredb_symbol_occurrence_t;
5654

55+
typedef void *indexstoredb_symbol_t;
56+
typedef void *indexstoredb_symbol_occurrence_t;
5757
typedef void *indexstoredb_error_t;
5858
typedef void *indexstoredb_symbol_location_t;
5959
typedef void *indexstoredb_symbol_relation_t;
@@ -125,6 +125,9 @@ typedef bool(^indexstoredb_symbol_occurrence_receiver_t)(_Nonnull indexstoredb_s
125125
/// Returns true to continue.
126126
typedef bool(^indexstoredb_symbol_name_receiver)(const char *_Nonnull);
127127

128+
/// Creates an index for the given raw index data in \p storePath.
129+
///
130+
/// The resulting index must be released using \c indexstoredb_release.
128131
INDEXSTOREDB_PUBLIC _Nullable
129132
indexstoredb_index_t
130133
indexstoredb_index_create(const char * _Nonnull storePath,
@@ -134,6 +137,9 @@ indexstoredb_index_create(const char * _Nonnull storePath,
134137
bool listenToUnitEvents,
135138
indexstoredb_error_t _Nullable * _Nullable);
136139

140+
/// Creates an indexstore library for the given library.
141+
///
142+
/// The resulting object must be released using \c indexstoredb_release.
137143
INDEXSTOREDB_PUBLIC _Nullable
138144
indexstoredb_indexstore_library_t
139145
indexstoredb_load_indexstore_library(const char * _Nonnull dylibPath,
@@ -143,95 +149,129 @@ indexstoredb_load_indexstore_library(const char * _Nonnull dylibPath,
143149
INDEXSTOREDB_PUBLIC void
144150
indexstoredb_index_poll_for_unit_changes_and_wait(_Nonnull indexstoredb_index_t index);
145151

152+
/// Iterates over each symbol occurrence matching the given \p usr and \p roles.
153+
///
154+
/// The occurrence passed to the receiver is only valid for the duration of the
155+
/// receiver call.
146156
INDEXSTOREDB_PUBLIC bool
147157
indexstoredb_index_symbol_occurrences_by_usr(
148158
_Nonnull indexstoredb_index_t index,
149159
const char *_Nonnull usr,
150160
uint64_t roles,
151161
_Nonnull indexstoredb_symbol_occurrence_receiver_t);
152162

163+
/// Iterates over each symbol occurrence related to the \p usr with \p roles.
164+
///
165+
/// The occurrence passed to the receiver is only valid for the duration of the
166+
/// receiver call.
153167
INDEXSTOREDB_PUBLIC bool
154168
indexstoredb_index_related_symbol_occurrences_by_usr(
155169
_Nonnull indexstoredb_index_t index,
156170
const char *_Nonnull usr,
157171
uint64_t roles,
158172
_Nonnull indexstoredb_symbol_occurrence_receiver_t);
159173

174+
/// Returns the USR of the given symbol.
175+
///
176+
/// The string has the same lifetime as the \c indexstoredb_symbol_t.
160177
INDEXSTOREDB_PUBLIC
161178
const char * _Nonnull
162179
indexstoredb_symbol_usr(_Nonnull indexstoredb_symbol_t);
163180

181+
/// Returns the name of the given symbol.
182+
///
183+
/// The string has the same lifetime as the \c indexstoredb_symbol_t.
164184
INDEXSTOREDB_PUBLIC
165185
const char * _Nonnull
166186
indexstoredb_symbol_name(_Nonnull indexstoredb_symbol_t);
167187

188+
/// Returns the symbol of the given symbol occurrence.
189+
///
190+
/// The symbol has the same lifetime as the \c indexstoredb_symbol_occurrence_t.
168191
INDEXSTOREDB_PUBLIC
169192
_Nonnull indexstoredb_symbol_t
170193
indexstoredb_symbol_occurrence_symbol(_Nonnull indexstoredb_symbol_occurrence_t);
171194

195+
/// Returns the roles of the given symbol occurrence.
172196
INDEXSTOREDB_PUBLIC uint64_t
173197
indexstoredb_symbol_occurrence_roles(_Nonnull indexstoredb_symbol_occurrence_t);
174198

175-
/// The location is owned by the occurrence and shall not be used after the occurrence is freed.
199+
/// Returns the location of the given symbol occurrence.
200+
///
201+
/// The location has the same lifetime as the \c indexstoredb_symbol_occurrence_t.
176202
INDEXSTOREDB_PUBLIC _Nonnull
177203
indexstoredb_symbol_location_t
178204
indexstoredb_symbol_occurrence_location(_Nonnull indexstoredb_symbol_occurrence_t);
179205

206+
/// Returns the path of the given symbol location.
207+
///
208+
/// The string has the same lifetime as the \c indexstoredb_symbol_location_t.
180209
INDEXSTOREDB_PUBLIC
181210
const char * _Nonnull
182211
indexstoredb_symbol_location_path(_Nonnull indexstoredb_symbol_location_t);
183212

213+
/// Returns whether the given symbol location is a system location.
184214
INDEXSTOREDB_PUBLIC bool
185215
indexstoredb_symbol_location_is_system(_Nonnull indexstoredb_symbol_location_t);
186216

217+
/// Returns the one-based line number of the given symbol location.
187218
INDEXSTOREDB_PUBLIC int
188219
indexstoredb_symbol_location_line(_Nonnull indexstoredb_symbol_location_t);
189220

221+
/// Returns the one-based UTF-8 column index of the given symbol location.
190222
INDEXSTOREDB_PUBLIC int
191223
indexstoredb_symbol_location_column_utf8(_Nonnull indexstoredb_symbol_location_t);
192224

225+
/// Retains the given \c indexstoredb_object_t and returns it.
193226
INDEXSTOREDB_PUBLIC _Nonnull
194227
indexstoredb_object_t
195228
indexstoredb_retain(_Nonnull indexstoredb_object_t);
196229

230+
/// Releases the given \c indexstoredb_object_t.
197231
INDEXSTOREDB_PUBLIC void
198232
indexstoredb_release(_Nonnull indexstoredb_object_t);
199233

234+
/// Returns the string describing the given error.
235+
///
236+
/// The string has the same lifetime as the \c indexstoredb_error_t.
200237
INDEXSTOREDB_PUBLIC const char * _Nonnull
201238
indexstoredb_error_get_description(_Nonnull indexstoredb_error_t);
202239

240+
/// Destroys the given error.
203241
INDEXSTOREDB_PUBLIC void
204242
indexstoredb_error_dispose(_Nullable indexstoredb_error_t);
205243

206-
/// Loops through each symbol in the index and calls the receiver function with each symbol.
207-
/// @param index An IndexStoreDB object which contains the symbols.
208-
/// @param receiver A function to be called for each symbol, the CString of the symbol will be passed in to this function.
209-
/// The function should return a boolean indicating whether the looping should continue.
244+
/// Iterates over the name of every symbol in the index.
245+
///
246+
/// \param index An IndexStoreDB object which contains the symbols.
247+
/// \param receiver A function to be called for each symbol. The string pointer is only valid for
248+
/// the duration of the call. The function should return a true to continue iterating.
210249
INDEXSTOREDB_PUBLIC bool
211250
indexstoredb_index_symbol_names(_Nonnull indexstoredb_index_t index, _Nonnull indexstoredb_symbol_name_receiver);
212251

213-
/// Loops through each canonical symbol that matches the string and performs the passed in function.
214-
/// @param index An IndexStoreDB object which contains the symbols.
215-
/// @param symbolName The name of the symbol whose canonical occurence should be found.
216-
/// @param receiver A function to be called for each canonical occurence.
217-
/// The SymbolOccurenceRef of the symbol will be passed in to this function.
218-
/// The function should return a boolean indicating whether the looping should continue.
252+
/// Iterates over every canonical symbol that matches the string.
253+
///
254+
/// \param index An IndexStoreDB object which contains the symbols.
255+
/// \param symbolName The name of the symbol whose canonical occurence should be found.
256+
/// \param receiver A function to be called for each canonical occurence.
257+
/// The canonical symbol occurrence will be passed in to this function. It is valid only for the
258+
/// duration of the call. The function should return true to continue iterating.
219259
INDEXSTOREDB_PUBLIC bool
220260
indexstoredb_index_canonical_symbol_occurences_by_name(
221261
indexstoredb_index_t _Nonnull index,
222262
const char *_Nonnull symbolName,
223263
indexstoredb_symbol_occurrence_receiver_t _Nonnull receiver
224264
);
225265

226-
/// Loops through each canonical symbol that matches the pattern and performs the passed in function.
227-
/// @param index An IndexStoreDB object which contains the symbols.
228-
/// @param anchorStart When true, symbol names should only be considered matching when the first characters of the symbol name match the pattern.
229-
/// @param anchorEnd When true, symbol names should only be considered matching when the first characters of the symbol name match the pattern.
230-
/// @param subsequence When true, symbols will be matched even if the pattern is not matched contiguously.
231-
/// @param ignoreCase When true, symbols may be returned even if the case of letters does not match the pattern.
232-
/// @param receiver A function to be called for each canonical occurence that matches the pattern.
233-
/// The SymbolOccurenceRef of the symbol will be passed in to this function.
234-
/// The function should return a boolean indicating whether the looping should continue.
266+
/// Iterates over every canonical symbol that matches the pattern.
267+
///
268+
/// \param index An IndexStoreDB object which contains the symbols.
269+
/// \param anchorStart When true, symbol names should only be considered matching when the first characters of the symbol name match the pattern.
270+
/// \param anchorEnd When true, symbol names should only be considered matching when the first characters of the symbol name match the pattern.
271+
/// \param subsequence When true, symbols will be matched even if the pattern is not matched contiguously.
272+
/// \param ignoreCase When true, symbols may be returned even if the case of letters does not match the pattern.
273+
/// \param receiver A function to be called for each canonical occurence that matches the pattern.
274+
/// It is valid only for the duration of the call. The function should return true to continue iterating.
235275
INDEXSTOREDB_PUBLIC bool
236276
indexstoredb_index_canonical_symbol_occurences_containing_pattern(
237277
_Nonnull indexstoredb_index_t index,
@@ -242,27 +282,28 @@ indexstoredb_index_canonical_symbol_occurences_containing_pattern(
242282
bool ignoreCase,
243283
_Nonnull indexstoredb_symbol_occurrence_receiver_t receiver);
244284

245-
/// Gets the set of roles of the passed in symbol relation
246-
/// @param relation A symbol relation
285+
/// Returns the set of roles of the given symbol relation.
247286
INDEXSTOREDB_PUBLIC uint64_t
248287
indexstoredb_symbol_relation_get_roles(_Nonnull indexstoredb_symbol_relation_t);
249288

250-
/// Gets the symbol associated with the passed in relation
251-
/// @param relation A symbol relation
289+
/// Returns the symbol of the given symbol relation.
290+
///
291+
/// The symbol has the same lifetime as the \c indexstoredb_symbol_relation_t.
252292
INDEXSTOREDB_PUBLIC _Nonnull indexstoredb_symbol_t
253293
indexstoredb_symbol_relation_get_symbol(_Nonnull indexstoredb_symbol_relation_t);
254294

255-
/// Loops through each relation that a passed in symbol has, and performs the passed in function.
295+
/// Iterates over the relations of the given symbol occurrence.
296+
///
256297
/// The relations are owned by the occurrence and shall not be used after the occurrence is freed.
257-
/// @param occurrence The symbol occurrence that whose relations should be found.
258-
/// @param applier The function that should be performed on each symbol relation.
298+
///
299+
/// \param occurrence The symbol occurrence that whose relations should be found.
300+
/// \param applier The function that should be performed on each symbol relation.
259301
/// The function should return a boolean indicating whether the looping should continue.
260302
INDEXSTOREDB_PUBLIC bool
261303
indexstoredb_symbol_occurrence_relations(_Nonnull indexstoredb_symbol_occurrence_t,
262304
bool(^ _Nonnull applier)(indexstoredb_symbol_relation_t _Nonnull ));
263305

264-
/// Get the SymbolKind of a Symbol
265-
/// @param symbol The symbol whose kind should be found.
306+
/// Returns the kind of the given symbol.
266307
INDEXSTOREDB_PUBLIC indexstoredb_symbol_kind_t
267308
indexstoredb_symbol_kind(_Nonnull indexstoredb_symbol_t);
268309

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)