Add free_value to wq_for API

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