提交 2b285c52 创建 作者: Nickolai Zeldovich's avatar Nickolai Zeldovich

some more out-of-memory handling

上级 ba71a527
...@@ -152,3 +152,26 @@ extern void *__dso_handle; ...@@ -152,3 +152,26 @@ extern void *__dso_handle;
kmfree(p, sizeof(classname)); \ kmfree(p, sizeof(classname)); \
} }
template<class T>
class scoped_cleanup_obj {
private:
T handler_;
bool active_;
public:
scoped_cleanup_obj(const T& h) : handler_(h), active_(true) {};
~scoped_cleanup_obj() { if (active_) handler_(); }
void dismiss() { active_ = false; }
void operator=(const scoped_cleanup_obj&) = delete;
scoped_cleanup_obj(const scoped_cleanup_obj&) = delete;
scoped_cleanup_obj(scoped_cleanup_obj&& other) :
handler_(other.handler_), active_(other.active_) { other.dismiss(); }
};
template<class T>
scoped_cleanup_obj<T>
scoped_cleanup(const T& h)
{
return scoped_cleanup_obj<T>(h);
}
...@@ -8,8 +8,6 @@ public: ...@@ -8,8 +8,6 @@ public:
filetable* copy() { filetable* copy() {
filetable* t = alloc(); filetable* t = alloc();
if (t == nullptr)
return nullptr;
for(int fd = 0; fd < NOFILE; fd++) { for(int fd = 0; fd < NOFILE; fd++) {
sref<file> f; sref<file> f;
......
...@@ -354,19 +354,18 @@ fork(int flags) ...@@ -354,19 +354,18 @@ fork(int flags)
if((np = proc::alloc()) == 0) if((np = proc::alloc()) == 0)
throw std::bad_alloc(); throw std::bad_alloc();
// XXX use a scoped cleanup handler to do xnspid->remove & freeproc auto proc_cleanup = scoped_cleanup([&np]() {
if (!xnspid->remove(np->pid, &np))
panic("fork: ns_remove");
freeproc(np);
});
if(flags & FORK_SHARE_VMAP) { if(flags & FORK_SHARE_VMAP) {
np->vmap = myproc()->vmap; np->vmap = myproc()->vmap;
np->vmap->ref++; np->vmap->ref++;
} else { } else {
// Copy process state from p. // Copy process state from p.
if((np->vmap = myproc()->vmap->copy(cow)) == 0){ np->vmap = myproc()->vmap->copy(cow);
if (!xnspid->remove(np->pid, &np))
panic("fork: ns_remove");
freeproc(np);
throw std::bad_alloc();
}
} }
np->parent = myproc(); np->parent = myproc();
...@@ -383,11 +382,6 @@ fork(int flags) ...@@ -383,11 +382,6 @@ fork(int flags)
np->ftable = myproc()->ftable; np->ftable = myproc()->ftable;
} else { } else {
np->ftable = myproc()->ftable->copy(); np->ftable = myproc()->ftable->copy();
if (np->ftable == nullptr) {
// XXX(sbw) leaking?
freeproc(np);
throw std::bad_alloc();
}
} }
np->cwd = idup(myproc()->cwd); np->cwd = idup(myproc()->cwd);
...@@ -402,6 +396,7 @@ fork(int flags) ...@@ -402,6 +396,7 @@ fork(int flags)
addrun(np); addrun(np);
release(&np->lock); release(&np->lock);
proc_cleanup.dismiss();
return pid; return pid;
} }
...@@ -412,8 +407,6 @@ finishproc(struct proc *p) ...@@ -412,8 +407,6 @@ finishproc(struct proc *p)
p->vmap->decref(); p->vmap->decref();
if (p->uwq != nullptr) if (p->uwq != nullptr)
p->uwq->dec(); p->uwq->dec();
ksfree(slab_stack, p->kstack);
p->kstack = 0;
p->pid = 0; p->pid = 0;
p->parent = 0; p->parent = 0;
p->name[0] = 0; p->name[0] = 0;
......
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论