songvec: move code to songvec_find_pointer()

This commit is contained in:
Max Kellermann 2012-01-24 18:05:28 +01:00
parent 7e7e3eb8d5
commit 5d5c753c74

View File

@ -142,15 +142,29 @@ songvec_find(const struct songvec *sv, const char *uri)
return ret; return ret;
} }
/**
* Determine the index of the specified #song inside the #songvec, and
* returns the index. The caller must hold the db_mutex.
*/
G_GNUC_PURE
static size_t
songvec_find_pointer(const struct songvec *sv, const struct song *song)
{
for (size_t i = 0;; ++i) {
assert(i < sv->nr); /* the song must exist */
if (sv->base[i] == song)
return i;
}
}
void void
songvec_delete(struct songvec *sv, const struct song *del) songvec_delete(struct songvec *sv, const struct song *del)
{ {
size_t i;
db_lock(); db_lock();
for (i = 0; i < sv->nr; ++i) {
if (sv->base[i] != del) const size_t i = songvec_find_pointer(sv, del);
continue;
/* we _don't_ call song_free() here */ /* we _don't_ call song_free() here */
if (!--sv->nr) { if (!--sv->nr) {
g_free(sv->base); g_free(sv->base);
@ -160,12 +174,8 @@ songvec_delete(struct songvec *sv, const struct song *del)
(sv->nr - i) * sizeof(struct song *)); (sv->nr - i) * sizeof(struct song *));
sv->base = g_realloc(sv->base, sv_size(sv)); sv->base = g_realloc(sv->base, sv_size(sv));
} }
db_unlock();
return;
}
db_unlock();
assert(false); db_unlock();
} }
void void