7
7
<!-- out the default global allocator in use at compile time. The design is currently -->
8
8
<!-- spelled out in [RFC 1183][rfc] but this will walk you through how to get your -->
9
9
<!-- own allocator up and running. -->
10
- メモリ割り当ては常に簡単に出来るとは限りません。通常はRustが既定の方法でメモリ割り当てを行いますが、しばしば割り当て方法をカスタマイズする必要が出てきます 。現在、コンパイラと標準ライブラリはコンパイル時に既定のグローバルアロケータを切り替えることが出来ます。詳細は[ RFC 1183] [ rfc ] に書かれていますが、ここではどのように独自のアロケータを作成するか順を追って説明します。
10
+ メモリ割り当てが常に簡単に出来るとは限りません。ほとんどの場合、Rustが既定の方法でメモリ割り当てを行いますが、割り当て方法をカスタマイズする必要が出てくる場合があります 。現在、コンパイラと標準ライブラリはコンパイル時に既定のグローバルアロケータを切り替えることが出来ます。詳細は[ RFC 1183] [ rfc ] に書かれていますが、ここではどのように独自のアロケータを作成するか順を追って説明します。
11
11
12
12
[ rfc ] : https://github.com/rust-lang/rfcs/blob/master/text/1183-swap-out-jemalloc.md
13
13
21
21
<!-- allocate and deallocate memory. The standard library is not compiled assuming -->
22
22
<!-- either one, and the compiler will decide which allocator is in use at -->
23
23
<!-- compile-time depending on the type of output artifact being produced. -->
24
- 現在コンパイラは` alloc_system ` と` alloc_jemalloc ` (いくつかのターゲットにはありません)という2つの既定のアロケータを提供しています。これらのアロケータは単に普通のRustのクレートで、メモリの割り当てと解放の手続きが実装されています。標準ライブラリはどちらか一方を前提としてコンパイルされているわけではなく、生成する成果物の種類に応じてどちらのアロケータを使用するかをコンパイラが決定します 。
24
+ 現在コンパイラは` alloc_system ` と` alloc_jemalloc ` (いくつかのターゲットにはありません)という2つの既定のアロケータを提供しています。これらのアロケータは単に普通のRustのクレートで、メモリの割り当てと解放の手続きを実装しています。標準ライブラリはどちらか一方を前提としてコンパイルされているわけではありません。コンパイラは生成する成果物の種類に応じてどちらのアロケータを使用するかをコンパイル時に決定します 。
25
25
26
26
<!-- Binaries generated by the compiler will use `alloc_jemalloc` by default (where -->
27
27
<!-- available). In this situation the compiler "controls the world" in the sense of -->
34
34
<!-- cannot authoritatively decide what allocator is in use. As a result it resorts -->
35
35
<!-- back to the standard APIs (e.g. `malloc` and `free`) for acquiring and releasing -->
36
36
<!-- memory. -->
37
- 一方、動的あるいは静的ライブラリの場合、既定では` alloc_system ` を使用します。他のアプリケーションや使用するアロケータの決定権がない他の世界において、通常Rustは 「お客」です 。そのため、メモリの割り当てと解放を行うには、標準API(例えば` malloc ` と` free ` )に頼らざるを得ません 。
37
+ 一方、動的あるいは静的ライブラリの場合、既定では` alloc_system ` を使用します。他のアプリケーションや他の環境など、使用するアロケータの決定権がない世界において、Rustは 「お客」に過ぎません 。そのため、メモリの割り当てと解放を行うには、標準API(例えば` malloc ` と` free ` )に頼ることになります 。
38
38
39
39
<!-- # Switching Allocators -->
40
40
# アロケータの切り替え
41
41
42
42
<!-- Although the compiler's default choices may work most of the time, it's often -->
43
43
<!-- necessary to tweak certain aspects. Overriding the compiler's decision about -->
44
44
<!-- which allocator is in use is done simply by linking to the desired allocator: -->
45
- コンパイラの既定の選択はほとんどの場合うまく動きますが 、しばしば多少の調整が必要になることがあります。コンパイラのアロケータ選択を上書きするには、単に希望のアロケータをリンクするだけです 。
45
+ コンパイラによる既定の選択はほとんどの場合うまく動きますが 、しばしば多少の調整が必要になることがあります。コンパイラのアロケータ選択を上書きするには、単に希望のアロケータとリンクするだけです 。
46
46
47
47
``` rust,no_run
48
48
51
51
extern crate alloc_system;
52
52
53
53
fn main() {
54
+ let a = Box::new(4); // allocates from the system allocator
54
55
let a = Box::new(4); // システムアロケータからのメモリ割り当て
55
56
println!("{}", a);
56
57
}
@@ -59,7 +60,7 @@ fn main() {
59
60
<!-- In this example the binary generated will not link to jemalloc by default but -->
60
61
<!-- instead use the system allocator. Conversely to generate a dynamic library which -->
61
62
<!-- uses jemalloc by default one would write: -->
62
- この例では生成されるバイナリは既定のjemallocにリンクするのではなく 、システムアロケータを使います。逆に既定でjemallocを使う動的ライブラリを生成するには次のようにします。
63
+ この例で生成されるバイナリは既定のjemallocとリンクする代わりに 、システムアロケータを使います。逆に既定でjemallocを使う動的ライブラリを生成するには次のようにします。
63
64
64
65
``` rust,ignore
65
66
#![feature(alloc_jemalloc)]
@@ -68,6 +69,7 @@ fn main() {
68
69
extern crate alloc_jemalloc;
69
70
70
71
pub fn foo() {
72
+ let a = Box::new(4); // allocates from jemalloc
71
73
let a = Box::new(4); // jemallocからのメモリ割り当て
72
74
println!("{}", a);
73
75
}
@@ -82,28 +84,28 @@ pub fn foo() {
82
84
<!-- crate which implements the allocator API (e.g. the same as `alloc_system` or -->
83
85
<!-- `alloc_jemalloc`). As an example, let's take a look at a simplified and -->
84
86
<!-- annotated version of `alloc_system` -->
85
- 時々jemallocとシステムアロケータの選択では足りず、全く新しいカスタムアロケータが必要になることがあります。この場合、アロケータAPI(例えば` alloc_system ` や` alloc_jemalloc ` と同様の )を実装した独自のクレートを書くことになります。例として、` alloc_system ` の簡素な注釈付きバージョンを見てみましょう。
87
+ 時々jemallocとシステムアロケータの選択では足りず、全く新しいカスタムアロケータが必要になることがあります。この場合、アロケータAPI(例えば` alloc_system ` や` alloc_jemalloc ` と同様のもの )を実装した独自のクレートを書くことになります。例として、` alloc_system ` の簡素な注釈付きバージョンを見てみましょう。
86
88
87
89
``` rust,no_run
88
90
# // only needed for rustdoc --test down below
89
91
# #![feature(lang_items)]
90
92
// The compiler needs to be instructed that this crate is an allocator in order
91
93
// to realize that when this is linked in another allocator like jemalloc should
92
94
// not be linked in
93
- // コンパイラにjemallocのような他のアロケータにリンクすべきでないと理解させるため 、
95
+ // コンパイラがリンク時に他のアロケータ(例えばjemalloc)とリンクしてしまうことを防ぐため 、
94
96
// このクレートがアロケータであることを示す必要があります。
95
97
#![feature(allocator)]
96
98
#![allocator]
97
99
98
100
// Allocators are not allowed to depend on the standard library which in turn
99
101
// requires an allocator in order to avoid circular dependencies. This crate,
100
102
// however, can use all of libcore.
101
- // 循環依存を避けるため、アロケータがアロケータを要求する標準ライブラリに依存することは出来ません 。
102
- // しかしlibcoreは全て使用できます 。
103
+ // 循環依存を避けるため、アロケータはアロケータを使う標準ライブラリに依存してはいけません 。
104
+ // しかしlibcoreについては全ての機能を使用できます 。
103
105
#![no_std]
104
106
105
107
// Let's give a unique name to our custom allocator
106
- // カスタムアロケータ固有の名前を付けてください 。
108
+ // カスタムアロケータに固有の名前を付けてください 。
107
109
#![crate_name = "my_allocator"]
108
110
#![crate_type = "rlib"]
109
111
@@ -112,7 +114,7 @@ pub fn foo() {
112
114
// to the standard library (e.g. `#![no_std]` isn't stable yet), so that's why
113
115
// this specifically requires the in-tree version.
114
116
// この独自アロケータはFFIバインディングのためにin-treeのlibcクレートを使います。
115
- // 注記: 現在の外部 (crate.io)libcは標準ライブラリにリンクしているため使用できません
117
+ // 注記: 現在の外部libc (crate.ioのもの)は標準ライブラリとリンクしているため使用できません
116
118
// (`#![no_std]`がまだstableではないためです)。そのため特別にin-treeのlibcが必要になります。
117
119
#![feature(libc)]
118
120
@@ -137,6 +139,7 @@ pub extern fn __rust_reallocate(ptr: *mut u8, _old_size: usize, size: usize,
137
139
#[no_mangle]
138
140
pub extern fn __rust_reallocate_inplace(_ptr: *mut u8, old_size: usize,
139
141
_size: usize, _align: usize) -> usize {
142
+ old_size // this api is not supported by libc
140
143
old_size // このAPIはlibcではサポートされていません。
141
144
}
142
145
@@ -161,6 +164,7 @@ pub extern fn __rust_usable_size(size: usize, _align: usize) -> usize {
161
164
extern crate my_allocator;
162
165
163
166
fn main() {
167
+ let a = Box::new(8); // allocates memory via our custom allocator crate
164
168
let a = Box::new(8); // カスタムアロケータによるメモリ割り当て
165
169
println!("{}", a);
166
170
}
@@ -177,11 +181,11 @@ fn main() {
177
181
<!-- dylibs, and staticlibs must link to exactly one allocator, and if none have -->
178
182
<!-- been explicitly chosen the compiler will choose one. On the other hand rlibs -->
179
183
<!-- do not need to link to an allocator (but still can). -->
180
- * 1つの成果物は高々1つのアロケータにしかリンクすることはできません 。バイナリ、dynlib、staticlibは必ず1つのアロケータにリンクする必要があり、もし明示的に指定されなければコンパイラが選択します 。一方、rlibはアロケータにリンクする必要はありません (リンクすることも可能です)。
184
+ * 1つの成果物は高々1つのアロケータとしかリンクすることはできません 。バイナリ、dynlib、staticlibは必ず1つのアロケータとリンクする必要があり、もし明示的に指定されなければコンパイラがアロケータを選択します 。一方、rlibはアロケータとリンクする必要はありません (リンクすることも可能です)。
181
185
182
186
<!-- * A consumer of an allocator is tagged with `#![needs_allocator]` (e.g. the -->
183
187
<!-- `liballoc` crate currently) and an `#[allocator]` crate cannot transitively -->
184
188
<!-- depend on a crate which needs an allocator (e.g. circular dependencies are not -->
185
189
<!-- allowed). This basically means that allocators must restrict themselves to -->
186
190
<!-- libcore currently. -->
187
- * アロケータを使うコードは` #![needs_allocator] ` でタグ付けされ(例えば ` liballoc ` クレート)、` #[allocator] ` (訳注: ` #![allocator] ` のtypo?)がついたクレートはアロケータを使うクレートに依存することは出来ません( 循環依存は許されていません)。このためアロケータは今のところlibcoreにしか依存しないようにする必要があります 。
191
+ * アロケータを使うコードは` #![needs_allocator] ` でタグ付けされます(例えば現時点での ` liballoc ` クレート)。また 、` #[allocator] ` がついたクレートはアロケータを使うクレートに依存することが出来ません( 循環依存は許されていません)。このためアロケータは原則としてlibcoreにしか依存しないようにする必要があります 。
0 commit comments