diff --git a/src/libexpr/eval.cc b/src/libexpr/eval.cc index 919ebd4ba9..c36a679d35 100644 --- a/src/libexpr/eval.cc +++ b/src/libexpr/eval.cc @@ -259,9 +259,8 @@ void mkString(Value & v, const string & s, const PathSet & context) mkString(v, s.c_str()); if (!context.empty()) { unsigned int n = 0; - v.string.context = (const char * *) - GC_MALLOC((context.size() + 1) * sizeof(char *)); - foreach (PathSet::const_iterator, i, context) + v.string.context = NEW const char *[context.size() + 1]; + foreach (PathSet::const_iterator, i, context) v.string.context[n++] = GC_STRDUP(i->c_str()); v.string.context[n] = 0; } @@ -305,7 +304,7 @@ Value * EvalState::lookupVar(Env * env, const VarRef & var) Value * EvalState::allocValue() { nrValues++; - return (Value *) GC_MALLOC(sizeof(Value)); + return NEW Value; } @@ -314,6 +313,7 @@ Env & EvalState::allocEnv(unsigned int size) nrEnvs++; nrValuesInEnvs += size; Env * env = (Env *) GC_MALLOC(sizeof(Env) + size * sizeof(Value *)); + if (!env) throw std::bad_alloc(); /* Clear the values because maybeThunk() expects this. */ for (unsigned i = 0; i < size; ++i) @@ -335,7 +335,7 @@ void EvalState::mkList(Value & v, unsigned int length) { v.type = tList; v.list.length = length; - v.list.elems = (Value * *) GC_MALLOC(length * sizeof(Value *)); + v.list.elems = NEW Value *[length]; nrListElems += length; } diff --git a/src/libmain/Makefile.am b/src/libmain/Makefile.am index a9ee660425..1a2146e044 100644 --- a/src/libmain/Makefile.am +++ b/src/libmain/Makefile.am @@ -2,7 +2,7 @@ pkglib_LTLIBRARIES = libmain.la libmain_la_SOURCES = shared.cc -libmain_la_LIBADD = ../libstore/libstore.la +libmain_la_LIBADD = ../libstore/libstore.la @boehmgc_lib@ pkginclude_HEADERS = shared.hh diff --git a/src/libmain/shared.cc b/src/libmain/shared.cc index eddc4e64b3..440949bd2a 100644 --- a/src/libmain/shared.cc +++ b/src/libmain/shared.cc @@ -13,6 +13,10 @@ #include #include +#if HAVE_BOEHMGC +#include +#endif + 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); +#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 { initAndRun(argc, argv); diff --git a/src/nix-env/Makefile.am b/src/nix-env/Makefile.am index a876b3eb38..7dfa7425a0 100644 --- a/src/nix-env/Makefile.am +++ b/src/nix-env/Makefile.am @@ -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 \ ../libstore/libstore.la ../libutil/libutil.la \ - ../boost/format/libformat.la @ADDITIONAL_NETWORK_LIBS@ \ - @boehmgc_lib@ + ../boost/format/libformat.la @ADDITIONAL_NETWORK_LIBS@ nix-env.o: help.txt.hh diff --git a/src/nix-instantiate/Makefile.am b/src/nix-instantiate/Makefile.am index bc9792818c..a65907a8d4 100644 --- a/src/nix-instantiate/Makefile.am +++ b/src/nix-instantiate/Makefile.am @@ -3,8 +3,7 @@ bin_PROGRAMS = nix-instantiate nix_instantiate_SOURCES = nix-instantiate.cc help.txt nix_instantiate_LDADD = ../libmain/libmain.la ../libexpr/libexpr.la \ ../libstore/libstore.la ../libutil/libutil.la \ - ../boost/format/libformat.la @ADDITIONAL_NETWORK_LIBS@ \ - @boehmgc_lib@ + ../boost/format/libformat.la @ADDITIONAL_NETWORK_LIBS@ nix-instantiate.o: help.txt.hh