|
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 -->
|
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 | 39 | <!-- # Switching Allocators -->
|
40 | 40 | # アロケータの切り替え
|
@@ -84,7 +84,7 @@ pub fn foo() {
|
84 | 84 | <!-- crate which implements the allocator API (e.g. the same as `alloc_system` or -->
|
85 | 85 | <!-- `alloc_jemalloc`). As an example, let's take a look at a simplified and -->
|
86 | 86 | <!-- annotated version of `alloc_system` -->
|
87 |
| -時々jemallocとシステムアロケータの選択では足りず、全く新しいカスタムアロケータが必要になることがあります。この場合、アロケータAPI(例えば`alloc_system`や`alloc_jemalloc`と同様のもの)を実装した独自のクレートを書くことになります。例として、`alloc_system`の簡素な注釈付きバージョンを見てみましょう。 |
| 87 | +時々jemallocとシステムアロケータの選択では足りず、全く新しいカスタムアロケータが必要になることがあります。この場合、アロケータAPI(例えば `alloc_system` や `alloc_jemalloc` と同様のもの)を実装した独自のクレートを書くことになります。例として、 `alloc_system` の簡素な注釈付きバージョンを見てみましょう。 |
88 | 88 |
|
89 | 89 | ```rust,no_run
|
90 | 90 | # // only needed for rustdoc --test down below
|
@@ -115,7 +115,7 @@ pub fn foo() {
|
115 | 115 | // this specifically requires the in-tree version.
|
116 | 116 | // この独自アロケータはFFIバインディングのためにin-treeのlibcクレートを使います。
|
117 | 117 | // 注記: 現在の外部libc(crate.ioのもの)は標準ライブラリとリンクしているため使用できません
|
118 |
| -// (`#![no_std]`がまだstableではないためです)。そのため特別にin-treeのlibcが必要になります。 |
| 118 | +// ( `#![no_std]` がまだstableではないためです)。そのため特別にin-treeのlibcが必要になります。 |
119 | 119 | #![feature(libc)]
|
120 | 120 |
|
121 | 121 | #[no_mangle]
|
@@ -188,4 +188,4 @@ fn main() {
|
188 | 188 | <!-- depend on a crate which needs an allocator (e.g. circular dependencies are not -->
|
189 | 189 | <!-- allowed). This basically means that allocators must restrict themselves to -->
|
190 | 190 | <!-- libcore currently. -->
|
191 |
| -* アロケータを使うコードは`#![needs_allocator]`でタグ付けされます(例えば現時点での`liballoc`クレート)。また、`#[allocator]`がついたクレートはアロケータを使うクレートに依存することが出来ません(循環依存は許されていません)。このためアロケータは原則としてlibcoreにしか依存しないようにする必要があります。 |
| 191 | +* アロケータを使うコードは `#![needs_allocator]` でタグ付けされます(例えば現時点での `liballoc` クレート)。また、 `#[allocator]` がついたクレートはアロケータを使うクレートに依存することが出来ません(循環依存は許されていません)。このためアロケータは原則としてlibcoreにしか依存しないようにする必要があります。 |
0 commit comments