Add free_value to wq_for API

上级 58f3bfbd
......@@ -51,15 +51,12 @@ du(int fd)
[](dirit &i)->bool { return !i.end(); },
[&size, &fd](const char *name)->void
{
if (!strcmp(name, ".") || !strcmp(name, "..")) {
free((void*)name);
if (!strcmp(name, ".") || !strcmp(name, ".."))
return;
}
int nfd = openat(fd, name, 0);
if (nfd >= 0)
size += du(nfd); // should go into work queue
free((void*)name);
});
} else {
close(fd);
......
......@@ -67,14 +67,12 @@ ls(const char *path)
struct stat st;
if (xfstatat(fd, name, &st) < 0){
printf("ls: cannot stat %s\n", name);
free((void*)name);
return;
}
if (!silent)
printf("%u %10lu %10lu %s\n",
ST_TYPE(st), ST_INO(st), ST_SIZE(st), name);
free((void*)name);
});
} else {
close(fd);
......
......@@ -42,8 +42,14 @@ private:
};
static inline const char*
copy_value(const dirit &it)
copy_value(dirit &it)
{
char *buf = (char*)malloc(256);
return it.name(buf, 256);
}
static inline void
free_value(dirit &it, const char *name)
{
free((void*)name);
}
......@@ -21,6 +21,7 @@ struct forwork : public work {
wq_push(w);
}
body_(v);
free_value(it_, v);
frame_.dec();
delete this;
}
......@@ -60,22 +61,36 @@ wq_for(IT &init, bool (*cond)(IT &it), BODY body)
}
body(v);
free_value(init, v);
while (!frame.zero())
wq_trywork();
}
// For debugging
// Same API as wq_for but serially executes body
template <typename IT, typename BODY>
static inline void
wq_for_serial(IT &init, bool (*cond)(IT &it), BODY body)
{
for (; cond(init); ++init)
body(copy_value(init));
for (; cond(init); ++init) {
decltype(copy_value(init)) v = copy_value(init);
body(v);
free_value(init, v);
}
}
// Default copy_value
template <typename T>
static inline T
copy_value(const T &it)
copy_value(T &it)
{
return it;
}
// Default free_value
template <typename T>
static inline void
free_value(T &it, T &v)
{
}
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论