aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorTuomas Tynkkynen <tuomas@tuxera.com>2018-02-13 05:00:17 +0200
committerTuomas Tynkkynen <tuomas@tuxera.com>2018-02-17 16:54:21 +0200
commit0845cdf9443a6b304c1bcec304a462ae4995c744 (patch)
tree747d08ec3f99be8820e9be3ac6aed24518213259 /src
parentb8bed7da14b26dcc328075522842dd16aa71b434 (diff)
libexpr: Rely on Boehm returning zeroed memory in EvalState::allocEnv()
Boehm guarantees that memory returned by GC_malloc() is zeroed, so take advantage of that.
Diffstat (limited to 'src')
-rw-r--r--src/libexpr/attr-set.cc3
-rw-r--r--src/libexpr/eval.cc7
2 files changed, 5 insertions, 5 deletions
diff --git a/src/libexpr/attr-set.cc b/src/libexpr/attr-set.cc
index 910428c02..b284daa3c 100644
--- a/src/libexpr/attr-set.cc
+++ b/src/libexpr/attr-set.cc
@@ -7,13 +7,14 @@
namespace nix {
+/* Note: Various places expect the allocated memory to be zeroed. */
static void * allocBytes(size_t n)
{
void * p;
#if HAVE_BOEHMGC
p = GC_malloc(n);
#else
- p = malloc(n);
+ p = calloc(n, 1);
#endif
if (!p) throw std::bad_alloc();
return p;
diff --git a/src/libexpr/eval.cc b/src/libexpr/eval.cc
index b94bc597b..48542d8e5 100644
--- a/src/libexpr/eval.cc
+++ b/src/libexpr/eval.cc
@@ -43,13 +43,14 @@ static char * dupString(const char * s)
}
+/* Note: Various places expect the allocated memory to be zeroed. */
static void * allocBytes(size_t n)
{
void * p;
#if HAVE_BOEHMGC
p = GC_malloc(n);
#else
- p = malloc(n);
+ p = calloc(n, 1);
#endif
if (!p) throw std::bad_alloc();
return p;
@@ -582,9 +583,7 @@ Env & EvalState::allocEnv(unsigned int size)
Env * env = (Env *) allocBytes(sizeof(Env) + size * sizeof(Value *));
env->size = size;
- /* Clear the values because maybeThunk() and lookupVar fromWith expect this. */
- for (unsigned i = 0; i < size; ++i)
- env->values[i] = 0;
+ /* We assume that env->values has been cleared by the allocator; maybeThunk() and lookupVar fromWith expect this. */
return *env;
}