Skip to content

Commit f2b0c33

Browse files
committed
translate to the last
1 parent 133a313 commit f2b0c33

File tree

1 file changed

+55
-46
lines changed

1 file changed

+55
-46
lines changed

1.6/ja/book/custom-allocators.md

Lines changed: 55 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<!-- out the default global allocator in use at compile time. The design is currently -->
88
<!-- spelled out in [RFC 1183][rfc] but this will walk you through how to get your -->
99
<!-- own allocator up and running. -->
10-
メモリ割り当ては常に簡単に出来るとは限りません。通常はRustが既定の方法でメモリ割り当てを行いますが、しばしば割り当て方をカスタマイズする必要が出てきます。現在、コンパイラと標準ライブラリはコンパイル時に既定のグローバル・アロケータを変更することが出来ます。詳細は[RFC 1183][rfc]に書かれていますが、ここではどのように独自のアロケータを作成するか順を追って説明します。
10+
メモリ割り当ては常に簡単に出来るとは限りません。通常はRustが既定の方法でメモリ割り当てを行いますが、しばしば割り当て方法をカスタマイズする必要が出てきます。現在、コンパイラと標準ライブラリはコンパイル時に既定のグローバルアロケータを切り替えることが出来ます。詳細は[RFC 1183][rfc]に書かれていますが、ここではどのように独自のアロケータを作成するか順を追って説明します。
1111

1212
[rfc]: https://github.com/rust-lang/rfcs/blob/master/text/1183-swap-out-jemalloc.md
1313

@@ -27,35 +27,39 @@
2727
<!-- available). In this situation the compiler "controls the world" in the sense of -->
2828
<!-- it has power over the final link. Primarily this means that the allocator -->
2929
<!-- decision can be left up the compiler. -->
30-
バイナリを生成する場合、既定では(もし可能なら)`alloc_jemalloc`を使用します。この場合、コンパイラは最後のリンクにまで影響力を持っているという意味で、「全世界を支配」します。従ってアロケータの選択はコンパイラに委ねることができます。
30+
バイナリを生成する場合、既定では(もし可能なら)`alloc_jemalloc`を使用します。この場合、コンパイラは最後のリンクにまで影響力を持っているという意味で、「全世界を支配」しています。従ってアロケータの選択はコンパイラに委ねることができます。
3131

3232
<!-- Dynamic and static libraries, however, will use `alloc_system` by default. Here -->
3333
<!-- Rust is typically a 'guest' in another application or another world where it -->
3434
<!-- cannot authoritatively decide what allocator is in use. As a result it resorts -->
3535
<!-- back to the standard APIs (e.g. `malloc` and `free`) for acquiring and releasing -->
3636
<!-- memory. -->
37-
しかし、動的あるいは静的ライブラリの場合、既定では`alloc_system`を使用します。他のアプリケーションや使用するアロケータの決定権がない他の世界において、通常Rustは「お客」です。そのため、メモリの割り当てと解放を行うには、標準API(例えば`malloc``free`)に頼らざるを得ません。
37+
一方、動的あるいは静的ライブラリの場合、既定では`alloc_system`を使用します。他のアプリケーションや使用するアロケータの決定権がない他の世界において、通常Rustは「お客」です。そのため、メモリの割り当てと解放を行うには、標準API(例えば`malloc``free`)に頼らざるを得ません。
3838

39-
# Switching Allocators
39+
<!-- # Switching Allocators -->
40+
# アロケータの切り替え
4041

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+
コンパイラの既定の選択はほとんどの場合うまく動きますが、しばしば多少の調整が必要になることがあります。コンパイラのアロケータ選択を上書きするには、単に希望のアロケータをリンクするだけです。
4446

4547
```rust,no_run
48+
4649
#![feature(alloc_system)]
4750
4851
extern crate alloc_system;
4952
5053
fn main() {
51-
let a = Box::new(4); // allocates from the system allocator
54+
let a = Box::new(4); // システムアロケータからのメモリ割り当て
5255
println!("{}", a);
5356
}
5457
```
5558

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を使う動的ライブラリを生成するには次のようにします。
5963

6064
```rust,ignore
6165
#![feature(alloc_jemalloc)]
@@ -64,53 +68,53 @@ uses jemalloc by default one would write:
6468
extern crate alloc_jemalloc;
6569
6670
pub fn foo() {
67-
let a = Box::new(4); // allocates from jemalloc
71+
let a = Box::new(4); // jemallocからのメモリ割り当て
6872
println!("{}", a);
6973
}
7074
# fn main() {}
7175
```
7276

73-
# Writing a custom allocator
77+
<!-- # Writing a custom allocator -->
78+
# カスタムアロケータを書く
7479

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`の簡素な注釈付きバージョンを見てみましょう。
8086

8187
```rust,no_run
8288
# // only needed for rustdoc --test down below
8389
# #![feature(lang_items)]
8490
// The compiler needs to be instructed that this crate is an allocator in order
8591
// to realize that when this is linked in another allocator like jemalloc should
8692
// not be linked in
93+
// コンパイラにjemallocのような他のアロケータにリンクすべきでないと理解させるため、
94+
// このクレートがアロケータであることを示す必要があります。
8795
#![feature(allocator)]
8896
#![allocator]
8997
9098
// Allocators are not allowed to depend on the standard library which in turn
9199
// requires an allocator in order to avoid circular dependencies. This crate,
92100
// however, can use all of libcore.
101+
// 循環依存を避けるため、アロケータがアロケータを要求する標準ライブラリに依存することは出来ません。
102+
// しかしlibcoreは全て使用できます。
93103
#![no_std]
94104
95105
// Let's give a unique name to our custom allocator
106+
// カスタムアロケータ固有の名前を付けてください。
96107
#![crate_name = "my_allocator"]
97108
#![crate_type = "rlib"]
98109
99110
// Our system allocator will use the in-tree libc crate for FFI bindings. Note
100111
// that currently the external (crates.io) libc cannot be used because it links
101112
// to the standard library (e.g. `#![no_std]` isn't stable yet), so that's why
102113
// this specifically requires the in-tree version.
114+
// この独自アロケータはFFIバインディングのためにin-treeのlibcクレートを使います。
115+
// 注記: 現在の外部(crate.io)libcは標準ライブラリにリンクしているため使用できません
116+
// (`#![no_std]`がまだstableではないためです)。そのため特別にin-treeのlibcが必要になります。
103117
#![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.
114118
115119
#[no_mangle]
116120
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,
133137
#[no_mangle]
134138
pub extern fn __rust_reallocate_inplace(_ptr: *mut u8, old_size: usize,
135139
_size: usize, _align: usize) -> usize {
136-
old_size // this api is not supported by libc
140+
old_size // このAPIはlibcではサポートされていません。
137141
}
138142
139143
#[no_mangle]
@@ -150,29 +154,34 @@ pub extern fn __rust_usable_size(size: usize, _align: usize) -> usize {
150154
# #[no_mangle] pub extern fn rust_eh_unregister_frames () {}
151155
```
152156

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+
このクレートをコンパイルすると、次のように使えるようになります。
154159

155160
```rust,ignore
156161
extern crate my_allocator;
157162
158163
fn main() {
159-
let a = Box::new(8); // allocates memory via our custom allocator crate
164+
let a = Box::new(8); // カスタムアロケータによるメモリ割り当て
160165
println!("{}", a);
161166
}
162167
```
163168

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

Comments
 (0)