InotifyUpdate: use std::list instead of GList
Let STL manage the WatchDirectory allocations.
This commit is contained in:
parent
9920a3e8fc
commit
8142080633
@ -28,6 +28,7 @@
|
|||||||
#include <glib.h>
|
#include <glib.h>
|
||||||
|
|
||||||
#include <map>
|
#include <map>
|
||||||
|
#include <forward_list>
|
||||||
|
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
#include <sys/inotify.h>
|
#include <sys/inotify.h>
|
||||||
@ -54,17 +55,18 @@ struct WatchDirectory {
|
|||||||
|
|
||||||
int descriptor;
|
int descriptor;
|
||||||
|
|
||||||
GList *children;
|
std::forward_list<WatchDirectory> children;
|
||||||
|
|
||||||
WatchDirectory(WatchDirectory *_parent, const char *_name,
|
WatchDirectory(WatchDirectory *_parent, const char *_name,
|
||||||
int _descriptor)
|
int _descriptor)
|
||||||
:parent(_parent), name(g_strdup(_name)),
|
:parent(_parent), name(g_strdup(_name)),
|
||||||
descriptor(_descriptor),
|
descriptor(_descriptor) {}
|
||||||
children(nullptr) {}
|
|
||||||
|
WatchDirectory(const WatchDirectory &) = delete;
|
||||||
|
WatchDirectory &operator=(const WatchDirectory &) = delete;
|
||||||
|
|
||||||
~WatchDirectory() {
|
~WatchDirectory() {
|
||||||
g_free(name);
|
g_free(name);
|
||||||
g_list_free(children);
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -100,6 +102,17 @@ tree_find_watch_directory(int wd)
|
|||||||
return i->second;
|
return i->second;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
disable_watch_directory(WatchDirectory &directory)
|
||||||
|
{
|
||||||
|
tree_remove_watch_directory(&directory);
|
||||||
|
|
||||||
|
for (WatchDirectory &child : directory.children)
|
||||||
|
disable_watch_directory(child);
|
||||||
|
|
||||||
|
inotify_source->Remove(directory.descriptor);
|
||||||
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
remove_watch_directory(WatchDirectory *directory)
|
remove_watch_directory(WatchDirectory *directory)
|
||||||
{
|
{
|
||||||
@ -111,18 +124,12 @@ remove_watch_directory(WatchDirectory *directory)
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
assert(directory->parent->children != NULL);
|
disable_watch_directory(*directory);
|
||||||
|
|
||||||
tree_remove_watch_directory(directory);
|
/* remove it from the parent, which effectively deletes it */
|
||||||
|
directory->parent->children.remove_if([directory](const WatchDirectory &child){
|
||||||
while (directory->children != NULL)
|
return &child == directory;
|
||||||
remove_watch_directory((WatchDirectory *)directory->children->data);
|
});
|
||||||
|
|
||||||
directory->parent->children =
|
|
||||||
g_list_remove(directory->parent->children, directory);
|
|
||||||
|
|
||||||
inotify_source->Remove(directory->descriptor);
|
|
||||||
delete directory;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static char *
|
static char *
|
||||||
@ -214,10 +221,8 @@ recursive_watch_subdirectories(WatchDirectory *directory,
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
child = new WatchDirectory(directory, ent->d_name, ret);
|
directory->children.emplace_front(directory, ent->d_name, ret);
|
||||||
|
child = &directory->children.front();
|
||||||
directory->children = g_list_prepend(directory->children,
|
|
||||||
child);
|
|
||||||
|
|
||||||
tree_add_watch_directory(child);
|
tree_add_watch_directory(child);
|
||||||
|
|
||||||
@ -353,11 +358,6 @@ mpd_inotify_finish(void)
|
|||||||
|
|
||||||
delete inotify_queue;
|
delete inotify_queue;
|
||||||
delete inotify_source;
|
delete inotify_source;
|
||||||
|
delete inotify_root;
|
||||||
for (auto i : inotify_directories) {
|
|
||||||
WatchDirectory *directory = i.second;
|
|
||||||
delete directory;
|
|
||||||
}
|
|
||||||
|
|
||||||
inotify_directories.clear();
|
inotify_directories.clear();
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user