Directory: convert to fixed-size struct
Using a variable-size struct with embedded string is not worth the trouble here. There are not so many Directory objects.
This commit is contained in:
@@ -25,7 +25,7 @@
|
|||||||
#include "SongSort.hxx"
|
#include "SongSort.hxx"
|
||||||
#include "Song.hxx"
|
#include "Song.hxx"
|
||||||
#include "fs/Traits.hxx"
|
#include "fs/Traits.hxx"
|
||||||
#include "util/VarSize.hxx"
|
#include "util/Alloc.hxx"
|
||||||
#include "util/Error.hxx"
|
#include "util/Error.hxx"
|
||||||
|
|
||||||
extern "C" {
|
extern "C" {
|
||||||
@@ -38,23 +38,11 @@ extern "C" {
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
inline Directory *
|
Directory::Directory(const char *_path_utf8, Directory *_parent)
|
||||||
Directory::Allocate(const char *path)
|
:parent(_parent), path(_path_utf8)
|
||||||
{
|
|
||||||
assert(path != nullptr);
|
|
||||||
|
|
||||||
return NewVarSize<Directory>(sizeof(Directory::path),
|
|
||||||
strlen(path) + 1,
|
|
||||||
path);
|
|
||||||
}
|
|
||||||
|
|
||||||
Directory::Directory(const char *_path)
|
|
||||||
:mtime(0), have_stat(false)
|
|
||||||
{
|
{
|
||||||
INIT_LIST_HEAD(&children);
|
INIT_LIST_HEAD(&children);
|
||||||
INIT_LIST_HEAD(&songs);
|
INIT_LIST_HEAD(&songs);
|
||||||
|
|
||||||
strcpy(path, _path);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Directory::~Directory()
|
Directory::~Directory()
|
||||||
@@ -68,25 +56,6 @@ Directory::~Directory()
|
|||||||
child->Free();
|
child->Free();
|
||||||
}
|
}
|
||||||
|
|
||||||
Directory *
|
|
||||||
Directory::NewGeneric(const char *path, Directory *parent)
|
|
||||||
{
|
|
||||||
assert(path != nullptr);
|
|
||||||
assert((*path == 0) == (parent == nullptr));
|
|
||||||
|
|
||||||
Directory *directory = Allocate(path);
|
|
||||||
|
|
||||||
directory->parent = parent;
|
|
||||||
|
|
||||||
return directory;
|
|
||||||
}
|
|
||||||
|
|
||||||
void
|
|
||||||
Directory::Free()
|
|
||||||
{
|
|
||||||
DeleteVarSize(this);
|
|
||||||
}
|
|
||||||
|
|
||||||
void
|
void
|
||||||
Directory::Delete()
|
Directory::Delete()
|
||||||
{
|
{
|
||||||
@@ -102,7 +71,7 @@ Directory::GetName() const
|
|||||||
{
|
{
|
||||||
assert(!IsRoot());
|
assert(!IsRoot());
|
||||||
|
|
||||||
return PathTraitsUTF8::GetBase(path);
|
return PathTraitsUTF8::GetBase(path.c_str());
|
||||||
}
|
}
|
||||||
|
|
||||||
Directory *
|
Directory *
|
||||||
@@ -123,7 +92,7 @@ Directory::CreateChild(const char *name_utf8)
|
|||||||
path_utf8 = allocated;
|
path_utf8 = allocated;
|
||||||
}
|
}
|
||||||
|
|
||||||
Directory *child = NewGeneric(path_utf8, this);
|
Directory *child = new Directory(path_utf8, this);
|
||||||
g_free(allocated);
|
g_free(allocated);
|
||||||
|
|
||||||
list_add_tail(&child->siblings, &children);
|
list_add_tail(&child->siblings, &children);
|
||||||
@@ -264,7 +233,7 @@ directory_cmp(gcc_unused void *priv,
|
|||||||
{
|
{
|
||||||
const Directory *a = (const Directory *)_a;
|
const Directory *a = (const Directory *)_a;
|
||||||
const Directory *b = (const Directory *)_b;
|
const Directory *b = (const Directory *)_b;
|
||||||
return g_utf8_collate(a->path, b->path);
|
return g_utf8_collate(a->path.c_str(), b->path.c_str());
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
|
@@ -26,6 +26,8 @@
|
|||||||
#include "DatabaseVisitor.hxx"
|
#include "DatabaseVisitor.hxx"
|
||||||
#include "PlaylistVector.hxx"
|
#include "PlaylistVector.hxx"
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
|
|
||||||
#define DEVICE_INARCHIVE (dev_t)(-1)
|
#define DEVICE_INARCHIVE (dev_t)(-1)
|
||||||
@@ -49,10 +51,6 @@ class SongFilter;
|
|||||||
class Error;
|
class Error;
|
||||||
|
|
||||||
struct Directory {
|
struct Directory {
|
||||||
template<class T, typename... Args> friend T *
|
|
||||||
NewVarSize(size_t declared_tail_size, size_t real_tail_size,
|
|
||||||
Args&&... args);
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Pointers to the siblings of this directory within the
|
* Pointers to the siblings of this directory within the
|
||||||
* parent directory. It is unused (undefined) in the root
|
* parent directory. It is unused (undefined) in the root
|
||||||
@@ -86,36 +84,28 @@ struct Directory {
|
|||||||
ino_t inode;
|
ino_t inode;
|
||||||
dev_t device;
|
dev_t device;
|
||||||
bool have_stat; /* not needed if ino_t == dev_t == 0 is impossible */
|
bool have_stat; /* not needed if ino_t == dev_t == 0 is impossible */
|
||||||
char path[sizeof(long)];
|
|
||||||
|
|
||||||
protected:
|
std::string path;
|
||||||
Directory(const char *path);
|
|
||||||
|
|
||||||
gcc_malloc gcc_nonnull_all
|
|
||||||
static Directory *Allocate(const char *path);
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
Directory(const char *_path_utf8, Directory *_parent);
|
||||||
~Directory();
|
~Directory();
|
||||||
|
|
||||||
/**
|
|
||||||
* Generic constructor for #Directory object.
|
|
||||||
*/
|
|
||||||
gcc_malloc
|
|
||||||
static Directory *NewGeneric(const char *path_utf8, Directory *parent);
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a new root #Directory object.
|
* Create a new root #Directory object.
|
||||||
*/
|
*/
|
||||||
gcc_malloc
|
gcc_malloc
|
||||||
static Directory *NewRoot() {
|
static Directory *NewRoot() {
|
||||||
return NewGeneric("", nullptr);
|
return new Directory("", nullptr);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Free this #Directory object (and the whole object tree within it),
|
* Free this #Directory object (and the whole object tree within it),
|
||||||
* assuming it was already removed from the parent.
|
* assuming it was already removed from the parent.
|
||||||
*/
|
*/
|
||||||
void Free();
|
void Free() {
|
||||||
|
delete this;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Remove this #Directory object from its parent and free it. This
|
* Remove this #Directory object from its parent and free it. This
|
||||||
@@ -178,7 +168,7 @@ public:
|
|||||||
|
|
||||||
gcc_pure
|
gcc_pure
|
||||||
const char *GetPath() const {
|
const char *GetPath() const {
|
||||||
return path;
|
return path.c_str();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Reference in New Issue
Block a user