Skip to content

Commit 45bf1ed

Browse files
committed
rustc: Allow changing the default allocator
This commit is an implementation of [RFC 1183][rfc] which allows swapping out the default allocator on nightly Rust. No new stable surface area should be added as a part of this commit. [rfc]: rust-lang/rfcs#1183 Two new attributes have been added to the compiler: * `#![needs_allocator]` - this is used by liballoc (and likely only liballoc) to indicate that it requires an allocator crate to be in scope. * `#![allocator]` - this is a indicator that the crate is an allocator which can satisfy the `needs_allocator` attribute above. The ABI of the allocator crate is defined to be a set of symbols that implement the standard Rust allocation/deallocation functions. The symbols are not currently checked for exhaustiveness or typechecked. There are also a number of restrictions on these crates: * An allocator crate cannot transitively depend on a crate that is flagged as needing an allocator (e.g. allocator crates can't depend on liballoc). * There can only be one explicitly linked allocator in a final image. * If no allocator is explicitly requested one will be injected on behalf of the compiler. Binaries and Rust dylibs will use jemalloc by default where available and staticlibs/other dylibs will use the system allocator by default. Two allocators are provided by the distribution by default, `alloc_system` and `alloc_jemalloc` which operate as advertised. Closes rust-lang#27389
1 parent e7261f3 commit 45bf1ed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+1287
-647
lines changed

Diff for: mk/crates.mk

+19-6
Original file line numberDiff line numberDiff line change
@@ -52,23 +52,23 @@
5252
TARGET_CRATES := libc std flate arena term \
5353
serialize getopts collections test rand \
5454
log graphviz core rbml alloc \
55-
rustc_unicode rustc_bitflags
55+
rustc_unicode rustc_bitflags \
56+
alloc_system
5657
RUSTC_CRATES := rustc rustc_typeck rustc_borrowck rustc_resolve rustc_driver \
5758
rustc_trans rustc_back rustc_llvm rustc_privacy rustc_lint \
5859
rustc_data_structures
5960
HOST_CRATES := syntax $(RUSTC_CRATES) rustdoc fmt_macros
60-
CRATES := $(TARGET_CRATES) $(HOST_CRATES)
6161
TOOLS := compiletest rustdoc rustc rustbook error-index-generator
6262

6363
DEPS_core :=
6464
DEPS_libc := core
6565
DEPS_rustc_unicode := core
66-
DEPS_alloc := core libc native:jemalloc
66+
DEPS_alloc := core libc alloc_system
6767
DEPS_std := core libc rand alloc collections rustc_unicode \
6868
native:rust_builtin native:backtrace \
69-
rustc_bitflags
69+
alloc_system
7070
DEPS_graphviz := std
71-
DEPS_syntax := std term serialize log fmt_macros arena libc
71+
DEPS_syntax := std term serialize log fmt_macros arena libc rustc_bitflags
7272
DEPS_rustc_driver := arena flate getopts graphviz libc rustc rustc_back rustc_borrowck \
7373
rustc_typeck rustc_resolve log syntax serialize rustc_llvm \
7474
rustc_trans rustc_privacy rustc_lint
@@ -82,7 +82,7 @@ DEPS_rustc_privacy := rustc log syntax
8282
DEPS_rustc_lint := rustc log syntax
8383
DEPS_rustc := syntax flate arena serialize getopts rbml \
8484
log graphviz rustc_llvm rustc_back rustc_data_structures
85-
DEPS_rustc_llvm := native:rustllvm libc std
85+
DEPS_rustc_llvm := native:rustllvm libc std rustc_bitflags
8686
DEPS_rustc_back := std syntax rustc_llvm flate log libc
8787
DEPS_rustc_data_structures := std log serialize
8888
DEPS_rustdoc := rustc rustc_driver native:hoedown serialize getopts \
@@ -102,6 +102,7 @@ DEPS_test := std getopts serialize rbml term native:rust_test_helpers
102102
DEPS_rand := core
103103
DEPS_log := std
104104
DEPS_fmt_macros = std
105+
DEPS_alloc_system := core libc
105106

106107
TOOL_DEPS_compiletest := test getopts
107108
TOOL_DEPS_rustdoc := rustdoc
@@ -121,14 +122,26 @@ ONLY_RLIB_rand := 1
121122
ONLY_RLIB_collections := 1
122123
ONLY_RLIB_rustc_unicode := 1
123124
ONLY_RLIB_rustc_bitflags := 1
125+
ONLY_RLIB_alloc_system := 1
124126

125127
# Documented-by-default crates
126128
DOC_CRATES := std alloc collections core libc rustc_unicode
127129

130+
ifeq ($(CFG_DISABLE_JEMALLOC),)
131+
TARGET_CRATES += alloc_jemalloc
132+
DEPS_std += alloc_jemalloc
133+
DEPS_alloc_jemalloc := core libc native:jemalloc
134+
ONLY_RLIB_alloc_jemalloc := 1
135+
else
136+
RUSTFLAGS_rustc_back := --cfg disable_jemalloc
137+
endif
138+
128139
################################################################################
129140
# You should not need to edit below this line
130141
################################################################################
131142

143+
CRATES := $(TARGET_CRATES) $(HOST_CRATES)
144+
132145
# This macro creates some simple definitions for each crate being built, just
133146
# some munging of all of the parameters above.
134147
#

Diff for: mk/rt.mk

-6
Original file line numberDiff line numberDiff line change
@@ -184,8 +184,6 @@ $$(JEMALLOC_LOCAL_$(1)): $$(JEMALLOC_DEPS) $$(MKFILE_DEPS)
184184
EXTRA_CFLAGS="-g1 -ffunction-sections -fdata-sections"
185185
$$(Q)$$(MAKE) -C "$$(JEMALLOC_BUILD_DIR_$(1))" build_lib_static
186186

187-
ifeq ($$(CFG_DISABLE_JEMALLOC),)
188-
RUSTFLAGS_alloc := --cfg jemalloc
189187
ifeq ($(1),$$(CFG_BUILD))
190188
ifneq ($$(CFG_JEMALLOC_ROOT),)
191189
$$(JEMALLOC_LIB_$(1)): $$(CFG_JEMALLOC_ROOT)/libjemalloc_pic.a
@@ -199,10 +197,6 @@ else
199197
$$(JEMALLOC_LIB_$(1)): $$(JEMALLOC_LOCAL_$(1))
200198
$$(Q)cp $$< $$@
201199
endif
202-
else
203-
$$(JEMALLOC_LIB_$(1)): $$(MKFILE_DEPS)
204-
$$(Q)touch $$@
205-
endif
206200

207201
################################################################################
208202
# compiler-rt

Diff for: mk/tests.mk

+2-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ $(eval $(call RUST_CRATE,coretest))
2222
DEPS_collectionstest :=
2323
$(eval $(call RUST_CRATE,collectionstest))
2424

25-
TEST_TARGET_CRATES = $(filter-out core rustc_unicode,$(TARGET_CRATES)) \
25+
TEST_TARGET_CRATES = $(filter-out core rustc_unicode alloc_system \
26+
alloc_jemalloc,$(TARGET_CRATES)) \
2627
collectionstest coretest
2728
TEST_DOC_CRATES = $(DOC_CRATES)
2829
TEST_HOST_CRATES = $(filter-out rustc_typeck rustc_borrowck rustc_resolve \

0 commit comments

Comments
 (0)