Skip to content

Commit 5e9fcb9

Browse files
committed
fix translation
1 parent f2b0c33 commit 5e9fcb9

File tree

1 file changed

+17
-13
lines changed

1 file changed

+17
-13
lines changed

1.6/ja/book/custom-allocators.md

Lines changed: 17 additions & 13 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

@@ -21,7 +21,7 @@
2121
<!-- allocate and deallocate memory. The standard library is not compiled assuming -->
2222
<!-- either one, and the compiler will decide which allocator is in use at -->
2323
<!-- 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のクレートで、メモリの割り当てと解放の手続きを実装しています。標準ライブラリはどちらか一方を前提としてコンパイルされているわけではありません。コンパイラは生成する成果物の種類に応じてどちらのアロケータを使用するかをコンパイル時に決定します
2525

2626
<!-- Binaries generated by the compiler will use `alloc_jemalloc` by default (where -->
2727
<!-- available). In this situation the compiler "controls the world" in the sense of -->
@@ -34,15 +34,15 @@
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

3939
<!-- # Switching Allocators -->
4040
# アロケータの切り替え
4141

4242
<!-- Although the compiler's default choices may work most of the time, it's often -->
4343
<!-- necessary to tweak certain aspects. Overriding the compiler's decision about -->
4444
<!-- which allocator is in use is done simply by linking to the desired allocator: -->
45-
コンパイラの既定の選択はほとんどの場合うまく動きますが、しばしば多少の調整が必要になることがあります。コンパイラのアロケータ選択を上書きするには、単に希望のアロケータをリンクするだけです
45+
コンパイラによる既定の選択はほとんどの場合うまく動きますが、しばしば多少の調整が必要になることがあります。コンパイラのアロケータ選択を上書きするには、単に希望のアロケータとリンクするだけです
4646

4747
```rust,no_run
4848
@@ -51,6 +51,7 @@
5151
extern crate alloc_system;
5252
5353
fn main() {
54+
let a = Box::new(4); // allocates from the system allocator
5455
let a = Box::new(4); // システムアロケータからのメモリ割り当て
5556
println!("{}", a);
5657
}
@@ -59,7 +60,7 @@ fn main() {
5960
<!-- In this example the binary generated will not link to jemalloc by default but -->
6061
<!-- instead use the system allocator. Conversely to generate a dynamic library which -->
6162
<!-- uses jemalloc by default one would write: -->
62-
この例では生成されるバイナリは既定のjemallocにリンクするのではなく、システムアロケータを使います。逆に既定でjemallocを使う動的ライブラリを生成するには次のようにします。
63+
この例で生成されるバイナリは既定のjemallocとリンクする代わりに、システムアロケータを使います。逆に既定でjemallocを使う動的ライブラリを生成するには次のようにします。
6364

6465
```rust,ignore
6566
#![feature(alloc_jemalloc)]
@@ -68,6 +69,7 @@ fn main() {
6869
extern crate alloc_jemalloc;
6970
7071
pub fn foo() {
72+
let a = Box::new(4); // allocates from jemalloc
7173
let a = Box::new(4); // jemallocからのメモリ割り当て
7274
println!("{}", a);
7375
}
@@ -82,28 +84,28 @@ pub fn foo() {
8284
<!-- crate which implements the allocator API (e.g. the same as `alloc_system` or -->
8385
<!-- `alloc_jemalloc`). As an example, let's take a look at a simplified and -->
8486
<!-- annotated version of `alloc_system` -->
85-
時々jemallocとシステムアロケータの選択では足りず、全く新しいカスタムアロケータが必要になることがあります。この場合、アロケータAPI(例えば`alloc_system``alloc_jemalloc`と同様の)を実装した独自のクレートを書くことになります。例として、`alloc_system`の簡素な注釈付きバージョンを見てみましょう。
87+
時々jemallocとシステムアロケータの選択では足りず、全く新しいカスタムアロケータが必要になることがあります。この場合、アロケータAPI(例えば`alloc_system``alloc_jemalloc`と同様のもの)を実装した独自のクレートを書くことになります。例として、`alloc_system`の簡素な注釈付きバージョンを見てみましょう。
8688

8789
```rust,no_run
8890
# // only needed for rustdoc --test down below
8991
# #![feature(lang_items)]
9092
// The compiler needs to be instructed that this crate is an allocator in order
9193
// to realize that when this is linked in another allocator like jemalloc should
9294
// not be linked in
93-
// コンパイラにjemallocのような他のアロケータにリンクすべきでないと理解させるため
95+
// コンパイラがリンク時に他のアロケータ(例えばjemalloc)とリンクしてしまうことを防ぐため
9496
// このクレートがアロケータであることを示す必要があります。
9597
#![feature(allocator)]
9698
#![allocator]
9799
98100
// Allocators are not allowed to depend on the standard library which in turn
99101
// requires an allocator in order to avoid circular dependencies. This crate,
100102
// however, can use all of libcore.
101-
// 循環依存を避けるため、アロケータがアロケータを要求する標準ライブラリに依存することは出来ません
102-
// しかしlibcoreは全て使用できます
103+
// 循環依存を避けるため、アロケータはアロケータを使う標準ライブラリに依存してはいけません
104+
// しかしlibcoreについては全ての機能を使用できます
103105
#![no_std]
104106
105107
// Let's give a unique name to our custom allocator
106-
// カスタムアロケータ固有の名前を付けてください
108+
// カスタムアロケータに固有の名前を付けてください
107109
#![crate_name = "my_allocator"]
108110
#![crate_type = "rlib"]
109111
@@ -112,7 +114,7 @@ pub fn foo() {
112114
// to the standard library (e.g. `#![no_std]` isn't stable yet), so that's why
113115
// this specifically requires the in-tree version.
114116
// この独自アロケータはFFIバインディングのためにin-treeのlibcクレートを使います。
115-
// 注記: 現在の外部(crate.io)libcは標準ライブラリにリンクしているため使用できません
117+
// 注記: 現在の外部libc(crate.ioのもの)は標準ライブラリとリンクしているため使用できません
116118
// (`#![no_std]`がまだstableではないためです)。そのため特別にin-treeのlibcが必要になります。
117119
#![feature(libc)]
118120
@@ -137,6 +139,7 @@ pub extern fn __rust_reallocate(ptr: *mut u8, _old_size: usize, size: usize,
137139
#[no_mangle]
138140
pub extern fn __rust_reallocate_inplace(_ptr: *mut u8, old_size: usize,
139141
_size: usize, _align: usize) -> usize {
142+
old_size // this api is not supported by libc
140143
old_size // このAPIはlibcではサポートされていません。
141144
}
142145
@@ -161,6 +164,7 @@ pub extern fn __rust_usable_size(size: usize, _align: usize) -> usize {
161164
extern crate my_allocator;
162165
163166
fn main() {
167+
let a = Box::new(8); // allocates memory via our custom allocator crate
164168
let a = Box::new(8); // カスタムアロケータによるメモリ割り当て
165169
println!("{}", a);
166170
}
@@ -177,11 +181,11 @@ fn main() {
177181
<!-- dylibs, and staticlibs must link to exactly one allocator, and if none have -->
178182
<!-- been explicitly chosen the compiler will choose one. On the other hand rlibs -->
179183
<!-- 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はアロケータとリンクする必要はありません(リンクすることも可能です)。
181185

182186
<!-- * A consumer of an allocator is tagged with `#![needs_allocator]` (e.g. the -->
183187
<!-- `liballoc` crate currently) and an `#[allocator]` crate cannot transitively -->
184188
<!-- depend on a crate which needs an allocator (e.g. circular dependencies are not -->
185189
<!-- allowed). This basically means that allocators must restrict themselves to -->
186190
<!-- libcore currently. -->
187-
* アロケータを使うコードは`#![needs_allocator]`でタグ付けされ(例えば`liballoc`クレート)、`#[allocator]`(訳注: `#![allocator]`のtypo?)がついたクレートはアロケータを使うクレートに依存することは出来ません(循環依存は許されていません)。このためアロケータは今のところlibcoreにしか依存しないようにする必要があります
191+
* アロケータを使うコードは`#![needs_allocator]`でタグ付けされます(例えば現時点での`liballoc`クレート)。また`#[allocator]`がついたクレートはアロケータを使うクレートに依存することが出来ません(循環依存は許されていません)。このためアロケータは原則としてlibcoreにしか依存しないようにする必要があります

0 commit comments

Comments
 (0)