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.
This commit is contained in:
parent
43761441c9
commit
267b2cd6e6
@ -298,7 +298,7 @@ removeDeletedFromDirectory(char *path_max_tmp, Directory * directory)
|
|||||||
|
|
||||||
for (i = sv->nr; --i >= 0; ) { /* cleaner deletes if we go backwards */
|
for (i = sv->nr; --i >= 0; ) { /* cleaner deletes if we go backwards */
|
||||||
Song *song = sv->base[i];
|
Song *song = sv->base[i];
|
||||||
if (!song || !song->url)
|
if (!song || !*song->url)
|
||||||
continue; /* does this happen?, perhaps assert() */
|
continue; /* does this happen?, perhaps assert() */
|
||||||
|
|
||||||
if (dirname)
|
if (dirname)
|
||||||
|
22
src/song.c
22
src/song.c
@ -28,6 +28,19 @@
|
|||||||
|
|
||||||
#include "os_compat.h"
|
#include "os_compat.h"
|
||||||
|
|
||||||
|
Song *
|
||||||
|
song_alloc(const char *url, enum song_type type, struct _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 *newSong(const char *url, enum song_type type, Directory * parentDir)
|
||||||
{
|
{
|
||||||
@ -38,11 +51,7 @@ Song *newSong(const char *url, enum song_type type, Directory * parentDir)
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
song = xmalloc(sizeof(*song));
|
song = song_alloc(url, type, parentDir);
|
||||||
song->tag = NULL;
|
|
||||||
song->url = xstrdup(url);
|
|
||||||
song->type = type;
|
|
||||||
song->parentDir = parentDir;
|
|
||||||
|
|
||||||
assert(type == SONG_TYPE_URL || parentDir);
|
assert(type == SONG_TYPE_URL || parentDir);
|
||||||
|
|
||||||
@ -75,7 +84,6 @@ void freeSong(Song * song)
|
|||||||
|
|
||||||
void freeJustSong(Song * song)
|
void freeJustSong(Song * song)
|
||||||
{
|
{
|
||||||
free(song->url);
|
|
||||||
if (song->tag)
|
if (song->tag)
|
||||||
tag_free(song->tag);
|
tag_free(song->tag);
|
||||||
free(song);
|
free(song);
|
||||||
@ -114,7 +122,7 @@ char *get_song_url(char *path_max_tmp, Song *song)
|
|||||||
if (!song)
|
if (!song)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
assert(song->url != NULL);
|
assert(*song->url);
|
||||||
|
|
||||||
if (!song->parentDir || !song->parentDir->path)
|
if (!song->parentDir || !song->parentDir->path)
|
||||||
strcpy(path_max_tmp, song->url);
|
strcpy(path_max_tmp, song->url);
|
||||||
|
@ -20,6 +20,7 @@
|
|||||||
#define SONG_H
|
#define SONG_H
|
||||||
|
|
||||||
#include "os_compat.h"
|
#include "os_compat.h"
|
||||||
|
#include "gcc.h"
|
||||||
|
|
||||||
#define SONG_BEGIN "songList begin"
|
#define SONG_BEGIN "songList begin"
|
||||||
#define SONG_END "songList end"
|
#define SONG_END "songList end"
|
||||||
@ -35,12 +36,15 @@ enum song_type {
|
|||||||
struct client;
|
struct client;
|
||||||
|
|
||||||
typedef struct _Song {
|
typedef struct _Song {
|
||||||
char *url;
|
|
||||||
enum song_type type;
|
enum song_type type;
|
||||||
struct tag *tag;
|
struct tag *tag;
|
||||||
struct _Directory *parentDir;
|
struct _Directory *parentDir;
|
||||||
time_t mtime;
|
time_t mtime;
|
||||||
} Song;
|
char url[1];
|
||||||
|
} mpd_packed Song;
|
||||||
|
|
||||||
|
Song *
|
||||||
|
song_alloc(const char *url, enum song_type type, struct _Directory *parent);
|
||||||
|
|
||||||
Song *newSong(const char *url, enum song_type type,
|
Song *newSong(const char *url, enum song_type type,
|
||||||
struct _Directory *parentDir);
|
struct _Directory *parentDir);
|
||||||
|
@ -111,18 +111,14 @@ void readSongInfoIntoList(FILE *fp, struct songvec *sv,
|
|||||||
if (song)
|
if (song)
|
||||||
insertSongIntoList(sv, song);
|
insertSongIntoList(sv, song);
|
||||||
|
|
||||||
song = xmalloc(sizeof(*song));
|
song = song_alloc(buffer + strlen(SONG_KEY),
|
||||||
song->url = xstrdup(buffer + strlen(SONG_KEY));
|
SONG_TYPE_FILE, parentDir);
|
||||||
song->type = SONG_TYPE_FILE;
|
|
||||||
song->parentDir = parentDir;
|
|
||||||
} else if (*buffer == 0) {
|
} else if (*buffer == 0) {
|
||||||
/* ignore empty lines (starting with '\0') */
|
/* ignore empty lines (starting with '\0') */
|
||||||
} else if (song == NULL) {
|
} else if (song == NULL) {
|
||||||
FATAL("Problems reading song info\n");
|
FATAL("Problems reading song info\n");
|
||||||
} else if (0 == strncmp(SONG_FILE, buffer, strlen(SONG_FILE))) {
|
} else if (0 == strncmp(SONG_FILE, buffer, strlen(SONG_FILE))) {
|
||||||
/* we don't need this info anymore
|
/* we don't need this info anymore */
|
||||||
song->url = xstrdup(&(buffer[strlen(SONG_FILE)]));
|
|
||||||
*/
|
|
||||||
} else if (matchesAnMpdTagItemKey(buffer, &itemType)) {
|
} else if (matchesAnMpdTagItemKey(buffer, &itemType)) {
|
||||||
if (!song->tag) {
|
if (!song->tag) {
|
||||||
song->tag = tag_new();
|
song->tag = tag_new();
|
||||||
|
Loading…
Reference in New Issue
Block a user