Skip to content

Commit

Permalink
Merge pull request #11550 from DeterminateSystems/traceable-allocator…
Browse files Browse the repository at this point in the history
…-alias

Alias traceable_allocator to std::allocator when building without GC
  • Loading branch information
edolstra committed Sep 20, 2024
2 parents ca3fc16 + b2bb92e commit c5c6855
Show file tree
Hide file tree
Showing 12 changed files with 31 additions and 98 deletions.
7 changes: 1 addition & 6 deletions src/libcmd/command.cc
Original file line number Diff line number Diff line change
Expand Up @@ -127,14 +127,9 @@ ref<EvalState> EvalCommand::getEvalState()
{
if (!evalState) {
evalState =
#if HAVE_BOEHMGC
std::allocate_shared<EvalState>(
traceable_allocator<EvalState>(),
#else
std::make_shared<EvalState>(
#endif
lookupPath, getEvalStore(), fetchSettings, evalSettings, getStore())
;
lookupPath, getEvalStore(), fetchSettings, evalSettings, getStore());

evalState->repair = repair;

Expand Down
7 changes: 0 additions & 7 deletions src/libcmd/repl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,6 @@
#include "ref.hh"
#include "value.hh"

#if HAVE_BOEHMGC
#define GC_INCLUDE_NEW
#include <gc/gc_cpp.h>
#endif

#include "strings.hh"

namespace nix {
Expand Down Expand Up @@ -62,9 +57,7 @@ enum class ProcessLineResult {
struct NixRepl
: AbstractNixRepl
, detail::ReplCompleterMixin
#if HAVE_BOEHMGC
, gc
#endif
{
size_t debugTraceIndex;

Expand Down
6 changes: 0 additions & 6 deletions src/libexpr-c/nix_api_external.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,6 @@

#include <nlohmann/json.hpp>

#if HAVE_BOEHMGC
# include "gc/gc.h"
# define GC_INCLUDE_NEW 1
# include "gc_cpp.h"
#endif

void nix_set_string_return(nix_string_return * str, const char * c)
{
str->str = c;
Expand Down
6 changes: 0 additions & 6 deletions src/libexpr-c/nix_api_value.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,6 @@
#include "nix_api_value.h"
#include "value/context.hh"

#if HAVE_BOEHMGC
# include "gc/gc.h"
# define GC_INCLUDE_NEW 1
# include "gc_cpp.h"
#endif

// Internal helper functions to check [in] and [out] `Value *` parameters
static const nix::Value & check_value_not_null(const nix_value * value)
{
Expand Down
26 changes: 26 additions & 0 deletions src/libexpr/eval-gc.hh
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,32 @@

#include <cstddef>

#if HAVE_BOEHMGC

# define GC_INCLUDE_NEW

# include <gc/gc.h>
# include <gc/gc_cpp.h>
# include <gc/gc_allocator.h>

#else

/* Some dummy aliases for Boehm GC definitions to reduce the number of
#ifdefs. */

template<typename T>
using traceable_allocator = std::allocator<T>;

template<typename T>
using gc_allocator = std::allocator<T>;

# define GC_MALLOC_ATOMIC std::malloc
# define GC_STRDUP std::strdup
struct gc
{};

#endif

namespace nix {

/**
Expand Down
23 changes: 0 additions & 23 deletions src/libexpr/eval.cc
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#include "eval.hh"
#include "eval-gc.hh"
#include "eval-settings.hh"
#include "primops.hh"
#include "print-options.hh"
Expand Down Expand Up @@ -39,16 +38,6 @@
# include <sys/resource.h>
#endif

#if HAVE_BOEHMGC

# define GC_INCLUDE_NEW

# include <gc/gc.h>
# include <gc/gc_cpp.h>
# include <gc/gc_allocator.h>

#endif

#include "strings-inline.hh"

using json = nlohmann::json;
Expand All @@ -58,11 +47,7 @@ namespace nix {
static char * allocString(size_t size)
{
char * t;
#if HAVE_BOEHMGC
t = (char *) GC_MALLOC_ATOMIC(size);
#else
t = (char *) malloc(size);
#endif
if (!t) throw std::bad_alloc();
return t;
}
Expand All @@ -71,11 +56,7 @@ static char * allocString(size_t size)
static char * dupString(const char * s)
{
char * t;
#if HAVE_BOEHMGC
t = GC_STRDUP(s);
#else
t = strdup(s);
#endif
if (!t) throw std::bad_alloc();
return t;
}
Expand All @@ -99,11 +80,7 @@ static const char * makeImmutableString(std::string_view s)

RootValue allocRootValue(Value * v)
{
#if HAVE_BOEHMGC
return std::allocate_shared<Value *>(traceable_allocator<Value *>(), v);
#else
return std::make_shared<Value *>(v);
#endif
}

// Pretty print types for assertion errors
Expand Down
14 changes: 1 addition & 13 deletions src/libexpr/eval.hh
Original file line number Diff line number Diff line change
Expand Up @@ -139,11 +139,7 @@ struct Constant
bool impureOnly = false;
};

#if HAVE_BOEHMGC
typedef std::map<std::string, Value *, std::less<std::string>, traceable_allocator<std::pair<const std::string, Value *> > > ValMap;
#else
typedef std::map<std::string, Value *> ValMap;
#endif
typedef std::map<std::string, Value *, std::less<std::string>, traceable_allocator<std::pair<const std::string, Value *> > > ValMap;

typedef std::unordered_map<PosIdx, DocComment> DocCommentMap;

Expand Down Expand Up @@ -329,21 +325,13 @@ private:
/**
* A cache from path names to parse trees.
*/
#if HAVE_BOEHMGC
typedef std::unordered_map<SourcePath, Expr *, std::hash<SourcePath>, std::equal_to<SourcePath>, traceable_allocator<std::pair<const SourcePath, Expr *>>> FileParseCache;
#else
typedef std::unordered_map<SourcePath, Expr *> FileParseCache;
#endif
FileParseCache fileParseCache;

/**
* A cache from path names to values.
*/
#if HAVE_BOEHMGC
typedef std::unordered_map<SourcePath, Value, std::hash<SourcePath>, std::equal_to<SourcePath>, traceable_allocator<std::pair<const SourcePath, Value>>> FileEvalCache;
#else
typedef std::unordered_map<SourcePath, Value> FileEvalCache;
#endif
FileEvalCache fileEvalCache;

/**
Expand Down
17 changes: 2 additions & 15 deletions src/libexpr/gc-small-vector.hh
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,15 @@

#include <boost/container/small_vector.hpp>

#if HAVE_BOEHMGC

#include <gc/gc.h>
#include <gc/gc_cpp.h>
#include <gc/gc_allocator.h>

#endif
#include "value.hh"

namespace nix {

struct Value;

/**
* A GC compatible vector that may used a reserved portion of `nItems` on the stack instead of allocating on the heap.
*/
#if HAVE_BOEHMGC
template <typename T, size_t nItems>
using SmallVector = boost::container::small_vector<T, nItems, traceable_allocator<T>>;
#else
template <typename T, size_t nItems>
using SmallVector = boost::container::small_vector<T, nItems>;
#endif

/**
* A vector of value pointers. See `SmallVector`.
Expand All @@ -39,4 +26,4 @@ using SmallValueVector = SmallVector<Value *, nItems>;
template <size_t nItems>
using SmallTemporaryValueVector = SmallVector<Value, nItems>;

}
}
4 changes: 0 additions & 4 deletions src/libexpr/get-drvs.hh
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,7 @@ public:
};


#if HAVE_BOEHMGC
typedef std::list<PackageInfo, traceable_allocator<PackageInfo>> PackageInfos;
#else
typedef std::list<PackageInfo> PackageInfos;
#endif


/**
Expand Down
8 changes: 0 additions & 8 deletions src/libexpr/primops.cc
Original file line number Diff line number Diff line change
Expand Up @@ -631,11 +631,7 @@ struct CompareValues
};


#if HAVE_BOEHMGC
typedef std::list<Value *, gc_allocator<Value *>> ValueList;
#else
typedef std::list<Value *> ValueList;
#endif


static Bindings::const_iterator getAttr(
Expand Down Expand Up @@ -3136,11 +3132,7 @@ static void prim_zipAttrsWith(EvalState & state, const PosIdx pos, Value * * arg
std::optional<ListBuilder> list;
};

#if HAVE_BOEHMGC
std::map<Symbol, Item, std::less<Symbol>, traceable_allocator<std::pair<const Symbol, Item>>> attrsSeen;
#else
std::map<Symbol, Item> attrsSeen;
#endif

state.forceFunction(*args[0], pos, "while evaluating the first argument passed to builtins.zipAttrsWith");
state.forceList(*args[1], pos, "while evaluating the second argument passed to builtins.zipAttrsWith");
Expand Down
10 changes: 1 addition & 9 deletions src/libexpr/value.hh
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,13 @@
#include <cassert>
#include <span>

#include "eval-gc.hh"
#include "symbol-table.hh"
#include "value/context.hh"
#include "source-path.hh"
#include "print-options.hh"
#include "checked-arithmetic.hh"

#if HAVE_BOEHMGC
#include <gc/gc_allocator.h>
#endif
#include <nlohmann/json_fwd.hpp>

namespace nix {
Expand Down Expand Up @@ -498,15 +496,9 @@ void Value::mkBlackhole()
}


#if HAVE_BOEHMGC
typedef std::vector<Value *, traceable_allocator<Value *>> ValueVector;
typedef std::unordered_map<Symbol, Value *, std::hash<Symbol>, std::equal_to<Symbol>, traceable_allocator<std::pair<const Symbol, Value *>>> ValueMap;
typedef std::map<Symbol, ValueVector, std::less<Symbol>, traceable_allocator<std::pair<const Symbol, ValueVector>>> ValueVectorMap;
#else
typedef std::vector<Value *> ValueVector;
typedef std::unordered_map<Symbol, Value *> ValueMap;
typedef std::map<Symbol, ValueVector> ValueVectorMap;
#endif


/**
Expand Down
1 change: 0 additions & 1 deletion src/nix/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
#include "current-process.hh"
#include "command.hh"
#include "common-args.hh"
#include "eval-gc.hh"
#include "eval.hh"
#include "eval-settings.hh"
#include "globals.hh"
Expand Down

0 comments on commit c5c6855

Please sign in to comment.