aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEric Wong <normalperson@yhbt.net>2008-10-11 20:24:26 -0700
committerEric Wong <normalperson@yhbt.net>2008-10-12 05:27:32 -0700
commitc6ee14dca5d7336dad20a6057aa2a8006d4447b8 (patch)
treec0d5097a4b9664f03716a96639859118e9bd9db8
parentaed02f0fedf56f6684cb98e33d0c616d4f80e81f (diff)
downloadmpd-c6ee14dca5d7336dad20a6057aa2a8006d4447b8.tar.gz
mpd-c6ee14dca5d7336dad20a6057aa2a8006d4447b8.tar.xz
mpd-c6ee14dca5d7336dad20a6057aa2a8006d4447b8.zip
directory: use mpd_sizeof_str_flex_array for path, too
This way we avoid unnecessary heap allocations.
-rw-r--r--src/directory.c12
-rw-r--r--src/directory.h5
2 files changed, 9 insertions, 8 deletions
diff --git a/src/directory.c b/src/directory.c
index b90b477fd..699b6fbc1 100644
--- a/src/directory.c
+++ b/src/directory.c
@@ -23,15 +23,16 @@
#include "myfprintf.h"
#include "dirvec.h"
-struct directory * directory_new(const char *dirname, struct directory * parent)
+struct directory * directory_new(const char *path, struct directory * parent)
{
struct directory *dir;
+ size_t pathlen = strlen(path);
- assert(dirname != NULL);
- assert((*dirname == 0) == (parent == NULL));
+ assert(path != NULL);
+ assert((*path == 0) == (parent == NULL));
- dir = xcalloc(1, sizeof(struct directory));
- dir->path = xstrdup(dirname);
+ dir = xcalloc(1, sizeof(*dir) - sizeof(dir->path) + pathlen + 1);
+ memcpy(dir->path, path, pathlen + 1);
dir->parent = parent;
return dir;
@@ -41,7 +42,6 @@ void directory_free(struct directory *dir)
{
dirvec_destroy(&dir->children);
songvec_destroy(&dir->songs);
- free(dir->path);
free(dir);
/* this resets last dir returned */
/*directory_get_path(NULL); */
diff --git a/src/directory.h b/src/directory.h
index 949df0c0e..546e55183 100644
--- a/src/directory.h
+++ b/src/directory.h
@@ -22,6 +22,7 @@
#include "song.h"
#include "dirvec.h"
#include "songvec.h"
+#include "gcc.h"
#define DIRECTORY_DIR "directory: "
#define DIRECTORY_MTIME "mtime: " /* DEPRECATED, noop-read-only */
@@ -33,14 +34,14 @@
#define DIRECTORY_FS_CHARSET "fs_charset: "
struct directory {
- char *path;
struct dirvec children;
struct songvec songs;
struct directory *parent;
ino_t inode;
dev_t device;
unsigned stat; /* not needed if ino_t == dev_t == 0 is impossible */
-};
+ char path[mpd_sizeof_str_flex_array];
+} mpd_packed;
static inline int isRootDirectory(const char *name)
{