aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorEric Wong <normalperson@yhbt.net>2008-10-05 17:38:15 -0700
committerEric Wong <normalperson@yhbt.net>2008-10-05 17:38:15 -0700
commit36eba830fc097279f568e801b2d2a35a08004717 (patch)
tree26e2069be33a4a1c3afb44c6f1f34bcf78d84ed4 /src
parentc461fe6377836b486ad6dc6a1558a63e7c32eaa1 (diff)
downloadmpd-36eba830fc097279f568e801b2d2a35a08004717.tar.gz
mpd-36eba830fc097279f568e801b2d2a35a08004717.tar.xz
mpd-36eba830fc097279f568e801b2d2a35a08004717.zip
song: use flex arrays to store song->url
Reduce the number of allocations we make, so there's less pressure on the allocator and less overhead to keep track of the allocations in.
Diffstat (limited to 'src')
-rw-r--r--src/directory.c2
-rw-r--r--src/song.c34
-rw-r--r--src/song.h7
3 files changed, 24 insertions, 19 deletions
diff --git a/src/directory.c b/src/directory.c
index 54f1af09c..de4532e75 100644
--- a/src/directory.c
+++ b/src/directory.c
@@ -296,7 +296,7 @@ removeDeletedFromDirectory(char *path_max_tmp, Directory * directory)
for (i = sv->nr; --i >= 0; ) { /* cleaner deletes if we go backwards */
Song *song = sv->base[i];
- if (!song || !song->url)
+ if (!song || !*song->url)
continue; /* does this happen?, perhaps assert() */
if (dirname)
diff --git a/src/song.c b/src/song.c
index 3bd6fa52e..e1d8bb1b8 100644
--- a/src/song.c
+++ b/src/song.c
@@ -29,6 +29,20 @@
#include "os_compat.h"
+static Song *
+song_alloc(const char *url, enum song_type type, Directory *parent)
+{
+ size_t urllen = strlen(url);
+ Song *song = xmalloc(sizeof(Song) + urllen);
+
+ song->tag = NULL;
+ memcpy(song->url, url, urllen + 1);
+ song->type = type;
+ song->parentDir = parent;
+
+ return song;
+}
+
Song *newSong(const char *url, enum song_type type, Directory * parentDir)
{
Song *song;
@@ -38,11 +52,7 @@ Song *newSong(const char *url, enum song_type type, Directory * parentDir)
return NULL;
}
- song = xmalloc(sizeof(Song));
- song->tag = NULL;
- song->url = xstrdup(url);
- song->type = type;
- song->parentDir = parentDir;
+ song = song_alloc(url, type, parentDir);
assert(type == SONG_TYPE_URL || parentDir);
@@ -75,7 +85,6 @@ void freeSong(Song * song)
void freeJustSong(Song * song)
{
- free(song->url);
if (song->tag)
tag_free(song->tag);
free(song);
@@ -157,19 +166,14 @@ void readSongInfoIntoList(FILE * fp, Directory * parentDir)
if (!prefixcmp(buffer, SONG_KEY)) {
if (song)
insertSongIntoList(sv, song);
- song = xmalloc(sizeof(Song));
- song->tag = NULL;
- song->url = xstrdup(buffer + strlen(SONG_KEY));
- song->type = SONG_TYPE_FILE;
- song->parentDir = parentDir;
+ song = song_alloc(buffer + strlen(SONG_KEY),
+ SONG_TYPE_FILE, parentDir);
} else if (*buffer == 0) {
/* ignore empty lines (starting with '\0') */
} else if (song == NULL) {
FATAL("Problems reading song info\n");
} else if (!prefixcmp(buffer, SONG_FILE)) {
- /* we don't need this info anymore
- song->url = xstrdup(&(buffer[strlen(SONG_FILE)]));
- */
+ /* we don't need this info anymore */
} else if (matchesAnMpdTagItemKey(buffer, &itemType)) {
if (!song->tag) {
song->tag = tag_new();
@@ -234,7 +238,7 @@ char *get_song_url(char *path_max_tmp, Song *song)
if (!song)
return NULL;
- assert(song->url != NULL);
+ assert(*song->url);
if (!song->parentDir || !song->parentDir->path)
strcpy(path_max_tmp, song->url);
diff --git a/src/song.h b/src/song.h
index 6de962493..aebc8f044 100644
--- a/src/song.h
+++ b/src/song.h
@@ -23,6 +23,7 @@
#include "os_compat.h"
#include "tag.h"
#include "list.h"
+#include "gcc.h"
#define SONG_KEY "key: "
#define SONG_MTIME "mtime: "
@@ -38,12 +39,12 @@ enum song_type {
#define SONG_TIME "Time: "
typedef struct _Song {
- char *url;
struct mpd_tag *tag;
- enum song_type type;
struct _Directory *parentDir;
time_t mtime;
-} Song;
+ enum song_type type;
+ char url[1];
+} mpd_packed Song;
Song *newSong(const char *url, enum song_type type,
struct _Directory *parentDir);