Commit 3b8bc33a authored by Eric Wong's avatar Eric Wong Committed by Max Kellermann

directory: replace DirectoryList with dirvec

Small memory reduction compared to songvec since most users have much fewer dirs than songs, but still nice to have.
parent 029f607e
......@@ -53,6 +53,7 @@ mpd_headers = \
decoder_api.h \
decoder_internal.h \
directory.h \
dirvec.h \
gcc.h \
decoder_list.h \
inputPlugins/_flac_common.h \
......
......@@ -23,11 +23,14 @@
#include "songvec.h"
#include "list.h"
typedef List DirectoryList;
struct dirvec {
struct _Directory **base;
size_t nr;
};
typedef struct _Directory {
char *path;
DirectoryList *subDirectories;
struct dirvec children;
struct songvec songs;
struct _Directory *parent;
ino_t inode;
......
#ifndef DIRVEC_H
#define DIRVEC_H
#include "directory.h"
#include "os_compat.h"
#include "utils.h"
static size_t dv_size(struct dirvec *dv)
{
return dv->nr * sizeof(Directory *);
}
/* Only used for sorting/searching a dirvec, not general purpose compares */
static int dirvec_cmp(const void *d1, const void *d2)
{
const Directory *a = ((const Directory * const *)d1)[0];
const Directory *b = ((const Directory * const *)d2)[0];
return strcmp(a->path, b->path);
}
static void dirvec_sort(struct dirvec *dv)
{
qsort(dv->base, dv->nr, sizeof(Directory *), dirvec_cmp);
}
static Directory *dirvec_find(struct dirvec *dv, const char *path)
{
int i;
for (i = dv->nr; --i >= 0; )
if (!strcmp(dv->base[i]->path, path))
return dv->base[i];
return NULL;
}
static int dirvec_delete(struct dirvec *dv, Directory *del)
{
int i;
for (i = dv->nr; --i >= 0; ) {
if (dv->base[i] != del)
continue;
/* we _don't_ call freeDirectory() here */
if (!--dv->nr) {
free(dv->base);
dv->base = NULL;
} else {
memmove(&dv->base[i], &dv->base[i + 1],
(dv->nr - i + 1) * sizeof(Directory *));
dv->base = xrealloc(dv->base, dv_size(dv));
}
return i;
}
return -1; /* not found */
}
static void dirvec_add(struct dirvec *dv, Directory *add)
{
++dv->nr;
dv->base = xrealloc(dv->base, dv_size(dv));
dv->base[dv->nr - 1] = add;
}
static void dirvec_destroy(struct dirvec *dv)
{
if (dv->base) {
free(dv->base);
dv->base = NULL;
}
dv->nr = 0;
}
#endif /* DIRVEC_H */
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment