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
27
27
<!-- available). In this situation the compiler "controls the world" in the sense of -->
28
28
<!-- it has power over the final link. Primarily this means that the allocator -->
29
29
<!-- decision can be left up the compiler. -->
30
- バイナリを生成する場合、既定では(もし可能なら)` alloc_jemalloc ` を使用します。この場合、コンパイラは最後のリンクにまで影響力を持っているという意味で、「全世界を支配」します 。従ってアロケータの選択はコンパイラに委ねることができます。
30
+ バイナリを生成する場合、既定では(もし可能なら)` alloc_jemalloc ` を使用します。この場合、コンパイラは最後のリンクにまで影響力を持っているという意味で、「全世界を支配」しています 。従ってアロケータの選択はコンパイラに委ねることができます。
31
31
32
32
<!-- Dynamic and static libraries, however, will use `alloc_system` by default. Here -->
33
33
<!-- Rust is typically a 'guest' in another application or another world where it -->
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
- # Switching Allocators
39
+ <!-- # Switching Allocators -->
40
+ # アロケータの切り替え
40
41
41
- Although the compiler's default choices may work most of the time, it's often
42
- necessary to tweak certain aspects. Overriding the compiler's decision about
43
- which allocator is in use is done simply by linking to the desired allocator:
42
+ <!-- Although the compiler's default choices may work most of the time, it's often -->
43
+ <!-- necessary to tweak certain aspects. Overriding the compiler's decision about -->
44
+ <!-- which allocator is in use is done simply by linking to the desired allocator: -->
45
+ コンパイラの既定の選択はほとんどの場合うまく動きますが、しばしば多少の調整が必要になることがあります。コンパイラのアロケータ選択を上書きするには、単に希望のアロケータをリンクするだけです。
44
46
45
47
``` rust,no_run
48
+
46
49
#![feature(alloc_system)]
47
50
48
51
extern crate alloc_system;
49
52
50
53
fn main() {
51
- let a = Box::new(4); // allocates from the system allocator
54
+ let a = Box::new(4); // システムアロケータからのメモリ割り当て
52
55
println!("{}", a);
53
56
}
54
57
```
55
58
56
- In this example the binary generated will not link to jemalloc by default but
57
- instead use the system allocator. Conversely to generate a dynamic library which
58
- uses jemalloc by default one would write:
59
+ <!-- In this example the binary generated will not link to jemalloc by default but -->
60
+ <!-- instead use the system allocator. Conversely to generate a dynamic library which -->
61
+ <!-- uses jemalloc by default one would write: -->
62
+ この例では生成されるバイナリは既定のjemallocにリンクするのではなく、システムアロケータを使います。逆に既定でjemallocを使う動的ライブラリを生成するには次のようにします。
59
63
60
64
``` rust,ignore
61
65
#![feature(alloc_jemalloc)]
@@ -64,53 +68,53 @@ uses jemalloc by default one would write:
64
68
extern crate alloc_jemalloc;
65
69
66
70
pub fn foo() {
67
- let a = Box::new(4); // allocates from jemalloc
71
+ let a = Box::new(4); // jemallocからのメモリ割り当て
68
72
println!("{}", a);
69
73
}
70
74
# fn main() {}
71
75
```
72
76
73
- # Writing a custom allocator
77
+ <!-- # Writing a custom allocator -->
78
+ # カスタムアロケータを書く
74
79
75
- Sometimes even the choices of jemalloc vs the system allocator aren't enough and
76
- an entirely new custom allocator is required. In this you'll write your own
77
- crate which implements the allocator API (e.g. the same as ` alloc_system ` or
78
- ` alloc_jemalloc ` ). As an example, let's take a look at a simplified and
79
- annotated version of ` alloc_system `
80
+ <!-- Sometimes even the choices of jemalloc vs the system allocator aren't enough and -->
81
+ <!-- an entirely new custom allocator is required. In this you'll write your own -->
82
+ <!-- crate which implements the allocator API (e.g. the same as `alloc_system` or -->
83
+ <!-- `alloc_jemalloc`). As an example, let's take a look at a simplified and -->
84
+ <!-- annotated version of `alloc_system` -->
85
+ 時々jemallocとシステムアロケータの選択では足りず、全く新しいカスタムアロケータが必要になることがあります。この場合、アロケータAPI(例えば` alloc_system ` や` alloc_jemalloc ` と同様の)を実装した独自のクレートを書くことになります。例として、` alloc_system ` の簡素な注釈付きバージョンを見てみましょう。
80
86
81
87
``` rust,no_run
82
88
# // only needed for rustdoc --test down below
83
89
# #![feature(lang_items)]
84
90
// The compiler needs to be instructed that this crate is an allocator in order
85
91
// to realize that when this is linked in another allocator like jemalloc should
86
92
// not be linked in
93
+ // コンパイラにjemallocのような他のアロケータにリンクすべきでないと理解させるため、
94
+ // このクレートがアロケータであることを示す必要があります。
87
95
#![feature(allocator)]
88
96
#![allocator]
89
97
90
98
// Allocators are not allowed to depend on the standard library which in turn
91
99
// requires an allocator in order to avoid circular dependencies. This crate,
92
100
// however, can use all of libcore.
101
+ // 循環依存を避けるため、アロケータがアロケータを要求する標準ライブラリに依存することは出来ません。
102
+ // しかしlibcoreは全て使用できます。
93
103
#![no_std]
94
104
95
105
// Let's give a unique name to our custom allocator
106
+ // カスタムアロケータ固有の名前を付けてください。
96
107
#![crate_name = "my_allocator"]
97
108
#![crate_type = "rlib"]
98
109
99
110
// Our system allocator will use the in-tree libc crate for FFI bindings. Note
100
111
// that currently the external (crates.io) libc cannot be used because it links
101
112
// to the standard library (e.g. `#![no_std]` isn't stable yet), so that's why
102
113
// this specifically requires the in-tree version.
114
+ // この独自アロケータはFFIバインディングのためにin-treeのlibcクレートを使います。
115
+ // 注記: 現在の外部(crate.io)libcは標準ライブラリにリンクしているため使用できません
116
+ // (`#![no_std]`がまだstableではないためです)。そのため特別にin-treeのlibcが必要になります。
103
117
#![feature(libc)]
104
- extern crate libc;
105
-
106
- // Listed below are the five allocation functions currently required by custom
107
- // allocators. Their signatures and symbol names are not currently typechecked
108
- // by the compiler, but this is a future extension and are required to match
109
- // what is found below.
110
- //
111
- // Note that the standard `malloc` and `realloc` functions do not provide a way
112
- // to communicate alignment so this implementation would need to be improved
113
- // with respect to alignment in that aspect.
114
118
115
119
#[no_mangle]
116
120
pub extern fn __rust_allocate(size: usize, _align: usize) -> *mut u8 {
@@ -133,7 +137,7 @@ pub extern fn __rust_reallocate(ptr: *mut u8, _old_size: usize, size: usize,
133
137
#[no_mangle]
134
138
pub extern fn __rust_reallocate_inplace(_ptr: *mut u8, old_size: usize,
135
139
_size: usize, _align: usize) -> usize {
136
- old_size // this api is not supported by libc
140
+ old_size // このAPIはlibcではサポートされていません。
137
141
}
138
142
139
143
#[no_mangle]
@@ -150,29 +154,34 @@ pub extern fn __rust_usable_size(size: usize, _align: usize) -> usize {
150
154
# #[no_mangle] pub extern fn rust_eh_unregister_frames () {}
151
155
```
152
156
153
- After we compile this crate, it can be used as follows:
157
+ <!-- After we compile this crate, it can be used as follows: -->
158
+ このクレートをコンパイルすると、次のように使えるようになります。
154
159
155
160
``` rust,ignore
156
161
extern crate my_allocator;
157
162
158
163
fn main() {
159
- let a = Box::new(8); // allocates memory via our custom allocator crate
164
+ let a = Box::new(8); // カスタムアロケータによるメモリ割り当て
160
165
println!("{}", a);
161
166
}
162
167
```
163
168
164
- # Custom allocator limitations
165
-
166
- There are a few restrictions when working with custom allocators which may cause
167
- compiler errors:
168
-
169
- * Any one artifact may only be linked to at most one allocator. Binaries,
170
- dylibs, and staticlibs must link to exactly one allocator, and if none have
171
- been explicitly chosen the compiler will choose one. On the other hand rlibs
172
- do not need to link to an allocator (but still can).
173
-
174
- * A consumer of an allocator is tagged with ` #![needs_allocator] ` (e.g. the
175
- ` liballoc ` crate currently) and an ` #[allocator] ` crate cannot transitively
176
- depend on a crate which needs an allocator (e.g. circular dependencies are not
177
- allowed). This basically means that allocators must restrict themselves to
178
- libcore currently.
169
+ <!-- # Custom allocator limitations -->
170
+ # カスタムアロケータの制限
171
+
172
+ <!-- There are a few restrictions when working with custom allocators which may cause -->
173
+ <!-- compiler errors: -->
174
+ カスタムアロケータを使用する場合、コンパイルエラーの原因となりうるいくつかの制限があります。
175
+
176
+ <!-- * Any one artifact may only be linked to at most one allocator. Binaries, -->
177
+ <!-- dylibs, and staticlibs must link to exactly one allocator, and if none have -->
178
+ <!-- been explicitly chosen the compiler will choose one. On the other hand rlibs -->
179
+ <!-- do not need to link to an allocator (but still can). -->
180
+ * 1つの成果物は高々1つのアロケータにしかリンクすることはできません。バイナリ、dynlib、staticlibは必ず1つのアロケータにリンクする必要があり、もし明示的に指定されなければコンパイラが選択します。一方、rlibはアロケータにリンクする必要はありません(リンクすることも可能です)。
181
+
182
+ <!-- * A consumer of an allocator is tagged with `#![needs_allocator]` (e.g. the -->
183
+ <!-- `liballoc` crate currently) and an `#[allocator]` crate cannot transitively -->
184
+ <!-- depend on a crate which needs an allocator (e.g. circular dependencies are not -->
185
+ <!-- allowed). This basically means that allocators must restrict themselves to -->
186
+ <!-- libcore currently. -->
187
+ * アロケータを使うコードは` #![needs_allocator] ` でタグ付けされ(例えば` liballoc ` クレート)、` #[allocator] ` (訳注: ` #![allocator] ` のtypo?)がついたクレートはアロケータを使うクレートに依存することは出来ません(循環依存は許されていません)。このためアロケータは今のところlibcoreにしか依存しないようにする必要があります。
0 commit comments