* Handle out of memory condition.

This commit is contained in:
Eelco Dolstra 2010-10-28 12:29:40 +00:00
parent 8a788e38ac
commit e11e6fb1c6
5 changed files with 28 additions and 10 deletions

View file

@ -259,8 +259,7 @@ void mkString(Value & v, const string & s, const PathSet & context)
mkString(v, s.c_str()); mkString(v, s.c_str());
if (!context.empty()) { if (!context.empty()) {
unsigned int n = 0; unsigned int n = 0;
v.string.context = (const char * *) v.string.context = NEW const char *[context.size() + 1];
GC_MALLOC((context.size() + 1) * sizeof(char *));
foreach (PathSet::const_iterator, i, context) foreach (PathSet::const_iterator, i, context)
v.string.context[n++] = GC_STRDUP(i->c_str()); v.string.context[n++] = GC_STRDUP(i->c_str());
v.string.context[n] = 0; v.string.context[n] = 0;
@ -305,7 +304,7 @@ Value * EvalState::lookupVar(Env * env, const VarRef & var)
Value * EvalState::allocValue() Value * EvalState::allocValue()
{ {
nrValues++; nrValues++;
return (Value *) GC_MALLOC(sizeof(Value)); return NEW Value;
} }
@ -314,6 +313,7 @@ Env & EvalState::allocEnv(unsigned int size)
nrEnvs++; nrEnvs++;
nrValuesInEnvs += size; nrValuesInEnvs += size;
Env * env = (Env *) GC_MALLOC(sizeof(Env) + size * sizeof(Value *)); Env * env = (Env *) GC_MALLOC(sizeof(Env) + size * sizeof(Value *));
if (!env) throw std::bad_alloc();
/* Clear the values because maybeThunk() expects this. */ /* Clear the values because maybeThunk() expects this. */
for (unsigned i = 0; i < size; ++i) for (unsigned i = 0; i < size; ++i)
@ -335,7 +335,7 @@ void EvalState::mkList(Value & v, unsigned int length)
{ {
v.type = tList; v.type = tList;
v.list.length = length; v.list.length = length;
v.list.elems = (Value * *) GC_MALLOC(length * sizeof(Value *)); v.list.elems = NEW Value *[length];
nrListElems += length; nrListElems += length;
} }

View file

@ -2,7 +2,7 @@ pkglib_LTLIBRARIES = libmain.la
libmain_la_SOURCES = shared.cc libmain_la_SOURCES = shared.cc
libmain_la_LIBADD = ../libstore/libstore.la libmain_la_LIBADD = ../libstore/libstore.la @boehmgc_lib@
pkginclude_HEADERS = shared.hh pkginclude_HEADERS = shared.hh

View file

@ -13,6 +13,10 @@
#include <sys/stat.h> #include <sys/stat.h>
#include <unistd.h> #include <unistd.h>
#if HAVE_BOEHMGC
#include <gc/gc.h>
#endif
namespace nix { namespace nix {
@ -314,6 +318,14 @@ static void setuidInit()
} }
/* Called when the Boehm GC runs out of memory. */
static void * oomHandler(size_t requested)
{
/* Convert this to a proper C++ exception. */
throw std::bad_alloc();
}
} }
@ -335,6 +347,14 @@ int main(int argc, char * * argv)
std::ios::sync_with_stdio(false); std::ios::sync_with_stdio(false);
#if HAVE_BOEHMGC
/* Initialise the Boehm garbage collector. This isn't necessary
on most platforms, but for portability we do it anyway. */
GC_INIT();
GC_oom_fn = oomHandler;
#endif
try { try {
try { try {
initAndRun(argc, argv); initAndRun(argc, argv);

View file

@ -4,8 +4,7 @@ nix_env_SOURCES = nix-env.cc profiles.cc profiles.hh user-env.cc user-env.hh hel
nix_env_LDADD = ../libmain/libmain.la ../libexpr/libexpr.la \ nix_env_LDADD = ../libmain/libmain.la ../libexpr/libexpr.la \
../libstore/libstore.la ../libutil/libutil.la \ ../libstore/libstore.la ../libutil/libutil.la \
../boost/format/libformat.la @ADDITIONAL_NETWORK_LIBS@ \ ../boost/format/libformat.la @ADDITIONAL_NETWORK_LIBS@
@boehmgc_lib@
nix-env.o: help.txt.hh nix-env.o: help.txt.hh

View file

@ -3,8 +3,7 @@ bin_PROGRAMS = nix-instantiate
nix_instantiate_SOURCES = nix-instantiate.cc help.txt nix_instantiate_SOURCES = nix-instantiate.cc help.txt
nix_instantiate_LDADD = ../libmain/libmain.la ../libexpr/libexpr.la \ nix_instantiate_LDADD = ../libmain/libmain.la ../libexpr/libexpr.la \
../libstore/libstore.la ../libutil/libutil.la \ ../libstore/libstore.la ../libutil/libutil.la \
../boost/format/libformat.la @ADDITIONAL_NETWORK_LIBS@ \ ../boost/format/libformat.la @ADDITIONAL_NETWORK_LIBS@
@boehmgc_lib@
nix-instantiate.o: help.txt.hh nix-instantiate.o: help.txt.hh