aboutsummaryrefslogtreecommitdiffstats
path: root/src/dirvec.h
diff options
context:
space:
mode:
authorEric Wong <normalperson@yhbt.net>2008-09-29 13:11:40 +0200
committerMax Kellermann <max@duempel.org>2008-09-29 13:11:40 +0200
commit3b8bc33a0454caf0521b4eb836ac0bbba293ece5 (patch)
tree901ae6f4ea8d0889216a75c4607289ccd4556c9c /src/dirvec.h
parent029f607e159c17e4a66b30bf083d6c1ae02c669b (diff)
downloadmpd-3b8bc33a0454caf0521b4eb836ac0bbba293ece5.tar.gz
mpd-3b8bc33a0454caf0521b4eb836ac0bbba293ece5.tar.xz
mpd-3b8bc33a0454caf0521b4eb836ac0bbba293ece5.zip
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.
Diffstat (limited to '')
-rw-r--r--src/dirvec.h73
1 files changed, 73 insertions, 0 deletions
diff --git a/src/dirvec.h b/src/dirvec.h
new file mode 100644
index 000000000..8b2f634e2
--- /dev/null
+++ b/src/dirvec.h
@@ -0,0 +1,73 @@
+#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 */