db/simple/Song: convert "uri" to a std::string
No longer allocate it as a "VarSize". This used to be a clever trick to save memory 10 years ago, but these days, keeping the code maintainable seems more important than saving a few kilobytes of memory.
This commit is contained in:
@@ -20,7 +20,6 @@
|
||||
#include "Directory.hxx"
|
||||
#include "SongSort.hxx"
|
||||
#include "Song.hxx"
|
||||
#include "Disposer.hxx"
|
||||
#include "Mount.hxx"
|
||||
#include "db/LightDirectory.hxx"
|
||||
#include "song/LightSong.hxx"
|
||||
@@ -51,7 +50,7 @@ Directory::~Directory() noexcept
|
||||
mounted_database.reset();
|
||||
}
|
||||
|
||||
songs.clear_and_dispose(SongDisposer());
|
||||
songs.clear_and_dispose(DeleteDisposer());
|
||||
children.clear_and_dispose(DeleteDisposer());
|
||||
}
|
||||
|
||||
@@ -192,7 +191,7 @@ Directory::FindSong(const char *name_utf8) const noexcept
|
||||
for (auto &song : songs) {
|
||||
assert(song.parent == this);
|
||||
|
||||
if (strcmp(song.uri, name_utf8) == 0)
|
||||
if (song.uri == name_utf8)
|
||||
return &song;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,29 +0,0 @@
|
||||
/*
|
||||
* Copyright 2003-2019 The Music Player Daemon Project
|
||||
* http://www.musicpd.org
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program; if not, write to the Free Software Foundation, Inc.,
|
||||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*/
|
||||
|
||||
#ifndef MPD_SONG_DISPOSER_HXX
|
||||
#define MPD_SONG_DISPOSER_HXX
|
||||
|
||||
struct Song;
|
||||
|
||||
struct SongDisposer {
|
||||
void operator()(Song *song) const noexcept;
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -20,10 +20,10 @@
|
||||
#ifndef MPD_SONG_PTR_HXX
|
||||
#define MPD_SONG_PTR_HXX
|
||||
|
||||
#include "Disposer.hxx"
|
||||
|
||||
#include <memory>
|
||||
|
||||
using SongPtr = std::unique_ptr<Song, SongDisposer>;
|
||||
struct Song;
|
||||
|
||||
using SongPtr = std::unique_ptr<Song>;
|
||||
|
||||
#endif
|
||||
|
||||
@@ -18,36 +18,22 @@
|
||||
*/
|
||||
|
||||
#include "Song.hxx"
|
||||
#include "Disposer.hxx"
|
||||
#include "Directory.hxx"
|
||||
#include "tag/Tag.hxx"
|
||||
#include "util/VarSize.hxx"
|
||||
#include "song/DetachedSong.hxx"
|
||||
#include "song/LightSong.hxx"
|
||||
#include "util/StringView.hxx"
|
||||
|
||||
#include <assert.h>
|
||||
#include <string.h>
|
||||
|
||||
inline
|
||||
Song::Song(StringView _uri, Directory &_parent) noexcept
|
||||
:parent(&_parent)
|
||||
{
|
||||
memcpy(uri, _uri.data, _uri.size + 1);
|
||||
}
|
||||
|
||||
inline
|
||||
Song::~Song() noexcept
|
||||
:parent(&_parent), uri(_uri.data, _uri.size)
|
||||
{
|
||||
}
|
||||
|
||||
static SongPtr
|
||||
song_alloc(StringView uri, Directory &parent) noexcept
|
||||
{
|
||||
auto *song = NewVarSize<Song>(sizeof(Song::uri),
|
||||
uri.size + 1,
|
||||
uri, parent);
|
||||
return SongPtr(song);
|
||||
return std::make_unique<Song>(uri, parent);
|
||||
}
|
||||
|
||||
SongPtr
|
||||
@@ -67,30 +53,16 @@ Song::NewFile(const char *path, Directory &parent) noexcept
|
||||
return SongPtr(song_alloc(path, parent));
|
||||
}
|
||||
|
||||
void
|
||||
Song::Free() noexcept
|
||||
{
|
||||
DeleteVarSize(this);
|
||||
}
|
||||
|
||||
void
|
||||
SongDisposer::operator()(Song *song) const noexcept
|
||||
{
|
||||
song->Free();
|
||||
}
|
||||
|
||||
std::string
|
||||
Song::GetURI() const noexcept
|
||||
{
|
||||
assert(*uri);
|
||||
|
||||
if (parent->IsRoot())
|
||||
return std::string(uri);
|
||||
return uri;
|
||||
else {
|
||||
const char *path = parent->GetPath();
|
||||
|
||||
std::string result;
|
||||
result.reserve(strlen(path) + 1 + strlen(uri));
|
||||
result.reserve(strlen(path) + 1 + uri.length());
|
||||
result.assign(path);
|
||||
result.push_back('/');
|
||||
result.append(uri);
|
||||
@@ -101,7 +73,7 @@ Song::GetURI() const noexcept
|
||||
LightSong
|
||||
Song::Export() const noexcept
|
||||
{
|
||||
LightSong dest(uri, tag);
|
||||
LightSong dest(uri.c_str(), tag);
|
||||
dest.directory = parent->IsRoot()
|
||||
? nullptr : parent->GetPath();
|
||||
dest.real_uri = nullptr;
|
||||
|
||||
@@ -92,10 +92,9 @@ struct Song {
|
||||
/**
|
||||
* The file name.
|
||||
*/
|
||||
char uri[sizeof(int)];
|
||||
std::string uri;
|
||||
|
||||
Song(StringView _uri, Directory &parent) noexcept;
|
||||
~Song() noexcept;
|
||||
|
||||
static SongPtr NewFrom(DetachedSong &&other, Directory &parent) noexcept;
|
||||
|
||||
@@ -115,8 +114,6 @@ struct Song {
|
||||
static SongPtr LoadFile(Storage &storage, const char *name_utf8,
|
||||
Directory &parent);
|
||||
|
||||
void Free() noexcept;
|
||||
|
||||
/**
|
||||
* Throws on error.
|
||||
*
|
||||
|
||||
@@ -96,7 +96,7 @@ song_cmp(const Song &a, const Song &b) noexcept
|
||||
return ret < 0;
|
||||
|
||||
/* still no difference? compare file name */
|
||||
return IcuCollate(a.uri, b.uri) < 0;
|
||||
return IcuCollate(a.uri.c_str(), b.uri.c_str()) < 0;
|
||||
}
|
||||
|
||||
void
|
||||
|
||||
Reference in New Issue
Block a user