diff options
author | Eric Wong <normalperson@yhbt.net> | 2005-12-06 00:23:47 -0800 |
---|---|---|
committer | Eric Wong <normalperson@yhbt.net> | 2008-09-18 03:44:16 -0700 |
commit | 3cd645c97f8a026541b911c2cbb3c2e294903208 (patch) | |
tree | a544daaf79e13b0b658f37665c8196695adf4e17 /src | |
parent | d1e6397e84f612c091eb9479031aed77a8131b93 (diff) | |
download | mpd-3cd645c97f8a026541b911c2cbb3c2e294903208.tar.gz mpd-3cd645c97f8a026541b911c2cbb3c2e294903208.tar.xz mpd-3cd645c97f8a026541b911c2cbb3c2e294903208.zip |
Directory: don't allocate stat information dynamically
This should save a few thousand ops. Not worth it to malloc
for such a small (3-words on 32-bit ARM and x86) structures.
Signed-off-by: Eric Wong <normalperson@yhbt.net>
Diffstat (limited to 'src')
-rw-r--r-- | src/directory.c | 63 | ||||
-rw-r--r-- | src/directory.h | 9 |
2 files changed, 22 insertions, 50 deletions
diff --git a/src/directory.c b/src/directory.c index 2175c54a0..c8c3c9372 100644 --- a/src/directory.c +++ b/src/directory.c @@ -231,19 +231,11 @@ int updateInit(int fd, List * pathList) return 0; } -static DirectoryStat *newDirectoryStat(struct stat *st) +static void directory_set_stat(Directory * dir, const struct stat *st) { - DirectoryStat *ret = xmalloc(sizeof(DirectoryStat)); - ret->inode = st->st_ino; - ret->device = st->st_dev; - return ret; -} - -static void freeDirectoryStatFromDirectory(Directory * dir) -{ - if (dir->stat) - free(dir->stat); - dir->stat = NULL; + dir->inode = st->st_ino; + dir->device = st->st_dev; + dir->stat = 1; } static DirectoryList *newDirectoryList(void) @@ -263,7 +255,9 @@ static Directory *newDirectory(const char *dirname, Directory * parent) directory->path = NULL; directory->subDirectories = newDirectoryList(); directory->songs = newSongList(); - directory->stat = NULL; + directory->stat = 0; + directory->inode = 0; + directory->device = 0; directory->parent = parent; return directory; @@ -275,7 +269,6 @@ static void freeDirectory(Directory * directory) freeSongList(directory->songs); if (directory->path) free(directory->path); - freeDirectoryStatFromDirectory(directory); free(directory); /* this resets last dir returned */ /*getDirectoryPath(NULL); */ @@ -345,8 +338,7 @@ static int updateInDirectory(Directory * directory, } else if (S_ISDIR(st.st_mode)) { if (findInList (directory->subDirectories, shortname, (void **)&subDir)) { - freeDirectoryStatFromDirectory(subDir); - ((Directory *) subDir)->stat = newDirectoryStat(&st); + directory_set_stat((Directory *)subDir, &st); return updateDirectory((Directory *) subDir); } else { return addSubDirectoryToDirectory(directory, shortname, @@ -531,8 +523,8 @@ static int updatePath(const char *utf8path) } /* if this song update is successfull, we are done */ else if (0 == inodeFoundInParent(parentDirectory->parent, - parentDirectory->stat->inode, - parentDirectory->stat->device) + parentDirectory->inode, + parentDirectory->device) && song && isMusic(get_song_url(path_max_tmp, song), &mtime, 0)) { free(path); @@ -563,9 +555,8 @@ static int updatePath(const char *utf8path) if (!parentDirectory || (!parentDirectory->stat && statDirectory(parentDirectory) < 0)) { } else if (0 == inodeFoundInParent(parentDirectory->parent, - parentDirectory->stat->inode, - parentDirectory->stat-> - device) + parentDirectory->inode, + parentDirectory->device) && addToDirectory(parentDirectory, shortname, path) > 0) { ret = 1; @@ -601,8 +592,8 @@ static int updateDirectory(Directory * directory) if (!directory->stat && statDirectory(directory) < 0) return -1; else if (inodeFoundInParent(directory->parent, - directory->stat->inode, - directory->stat->device)) + directory->inode, + directory->device)) return -1; dir = opendir(opendir_path(path_max_tmp, dirname)); @@ -684,7 +675,7 @@ static int statDirectory(Directory * dir) if (myStat(getDirectoryPath(dir), &st) < 0) return -1; - dir->stat = newDirectoryStat(&st); + directory_set_stat(dir, &st); return 0; } @@ -692,12 +683,9 @@ static int statDirectory(Directory * dir) static int inodeFoundInParent(Directory * parent, ino_t inode, dev_t device) { while (parent) { - if (!parent->stat) { - if (statDirectory(parent) < 0) - return -1; - } - if (parent->stat->inode == inode && - parent->stat->device == device) { + if (!parent->stat && statDirectory(parent) < 0) + return -1; + if (parent->inode == inode && parent->device == device) { DEBUG("recursive directory found\n"); return 1; } @@ -717,7 +705,7 @@ static int addSubDirectoryToDirectory(Directory * directory, return 0; subDirectory = newDirectory(name, directory); - subDirectory->stat = newDirectoryStat(st); + directory_set_stat(subDirectory, st); if (exploreDirectory(subDirectory) < 1) { freeDirectory(subDirectory); @@ -1234,23 +1222,10 @@ int traverseAllIn(const char *name, data); } -static void freeAllDirectoryStats(Directory * directory) -{ - ListNode *node = directory->subDirectories->firstNode; - - while (node != NULL) { - freeAllDirectoryStats((Directory *) node->data); - node = node->nextNode; - } - - freeDirectoryStatFromDirectory(directory); -} - void initMp3Directory(void) { mp3rootDirectory = newDirectory(NULL, NULL); exploreDirectory(mp3rootDirectory); - freeAllDirectoryStats(mp3rootDirectory); stats.numberOfSongs = countSongsIn(NULL); stats.dbPlayTime = sumSongTimesIn(NULL); } diff --git a/src/directory.h b/src/directory.h index 19dada309..9553095b3 100644 --- a/src/directory.h +++ b/src/directory.h @@ -23,17 +23,14 @@ typedef List DirectoryList; -typedef struct _DirectoryStat { - ino_t inode; - dev_t device; -} DirectoryStat; - typedef struct _Directory { char *path; DirectoryList *subDirectories; SongList *songs; struct _Directory *parent; - DirectoryStat *stat; + ino_t inode; + dev_t device; + unsigned stat; /* not needed if ino_t == dev_t == 0 is impossible */ } Directory; void readDirectoryDBIfUpdateIsFinished(void); |