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 ` (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 -->
45
45
コンパイラによる既定の選択はほとんどの場合うまく動きますが、しばしば多少の調整が必要になることがあります。コンパイラのアロケータ選択を上書きするには、単に希望のアロケータとリンクするだけです。
46
46
47
47
``` rust,no_run
48
-
49
48
#![feature(alloc_system)]
50
49
51
50
extern crate alloc_system;
52
51
53
52
fn main() {
54
- let a = Box::new(4); // allocates from the system allocator
53
+ # // let a = Box::new(4); // allocates from the system allocator
55
54
let a = Box::new(4); // システムアロケータからのメモリ割り当て
56
55
println!("{}", a);
57
56
}
@@ -69,7 +68,7 @@ fn main() {
69
68
extern crate alloc_jemalloc;
70
69
71
70
pub fn foo() {
72
- let a = Box::new(4); // allocates from jemalloc
71
+ # // let a = Box::new(4); // allocates from jemalloc
73
72
let a = Box::new(4); // jemallocからのメモリ割り当て
74
73
println!("{}", a);
75
74
}
@@ -89,34 +88,52 @@ pub fn foo() {
89
88
``` rust,no_run
90
89
# // only needed for rustdoc --test down below
91
90
# #![feature(lang_items)]
92
- // The compiler needs to be instructed that this crate is an allocator in order
93
- // to realize that when this is linked in another allocator like jemalloc should
94
- // not be linked in
91
+ # // // The compiler needs to be instructed that this crate is an allocator in order
92
+ # // // to realize that when this is linked in another allocator like jemalloc should
93
+ # // // not be linked in
95
94
// コンパイラがリンク時に他のアロケータ(例えばjemalloc)とリンクしてしまうことを防ぐため、
96
95
// このクレートがアロケータであることを示す必要があります。
97
96
#![feature(allocator)]
98
97
#![allocator]
99
98
100
- // Allocators are not allowed to depend on the standard library which in turn
101
- // requires an allocator in order to avoid circular dependencies. This crate,
102
- // however, can use all of libcore.
99
+ # // // Allocators are not allowed to depend on the standard library which in turn
100
+ # // // requires an allocator in order to avoid circular dependencies. This crate,
101
+ # // // however, can use all of libcore.
103
102
// 循環依存を避けるため、アロケータはアロケータを使う標準ライブラリに依存してはいけません。
104
103
// しかしlibcoreについては全ての機能を使用できます。
105
104
#![no_std]
106
105
107
- // Let's give a unique name to our custom allocator
106
+ # // // Let's give a unique name to our custom allocator
108
107
// カスタムアロケータに固有の名前を付けてください。
109
108
#![crate_name = "my_allocator"]
110
109
#![crate_type = "rlib"]
111
110
112
- // Our system allocator will use the in-tree libc crate for FFI bindings. Note
113
- // that currently the external (crates.io) libc cannot be used because it links
114
- // to the standard library (e.g. `#![no_std]` isn't stable yet), so that's why
115
- // this specifically requires the in-tree version.
116
- // この独自アロケータはFFIバインディングのためにin-treeのlibcクレートを使います。
111
+ # //// Our system allocator will use the in-tree libc crate for FFI bindings. Note
112
+ # //// that currently the external (crates.io) libc cannot be used because it links
113
+ # //// to the standard library (e.g. `#![no_std]` isn't stable yet), so that's why
114
+ # //// this specifically requires the in-tree version.
115
+ // この独自アロケータはFFIバインディングのためにRustコンパイラのソースコードに
116
+ // 同梱されているlibcクレートを使います。
117
117
// 注記: 現在の外部libc(crate.ioのもの)は標準ライブラリとリンクしているため使用できません
118
- // ( `#![no_std]` がまだstableではないためです)。そのため特別にin-treeのlibcが必要になります 。
118
+ // ( `#![no_std]` がまだstableではないためです)。そのため特別に同梱版のlibcが必要になります 。
119
119
#![feature(libc)]
120
+ extern crate libc;
121
+
122
+ # //// Listed below are the five allocation functions currently required by custom
123
+ # //// allocators. Their signatures and symbol names are not currently typechecked
124
+ # //// by the compiler, but this is a future extension and are required to match
125
+ # //// what is found below.
126
+ # ////
127
+ # //// Note that the standard `malloc` and `realloc` functions do not provide a way
128
+ # //// to communicate alignment so this implementation would need to be improved
129
+ # //// with respect to alignment in that aspect.
130
+ // 今のところ、カスタムアロケータには以下の5つのメモリ割り当て関数が必要です。
131
+ // コンパイラは現時点では関数のシグネチャとシンボル名の型検査を行いません。
132
+ // しかし、将来的には以下の型に一致する必要があります。
133
+ //
134
+ // 注記: 標準の `malloc` と `realloc` 関数へはアラインメントについての情報を
135
+ // 渡すことができません。そのためアラインメントを考慮するためにはこの実装は
136
+ // 改良の必要があります。
120
137
121
138
#[no_mangle]
122
139
pub extern fn __rust_allocate(size: usize, _align: usize) -> *mut u8 {
@@ -139,7 +156,7 @@ pub extern fn __rust_reallocate(ptr: *mut u8, _old_size: usize, size: usize,
139
156
#[no_mangle]
140
157
pub extern fn __rust_reallocate_inplace(_ptr: *mut u8, old_size: usize,
141
158
_size: usize, _align: usize) -> usize {
142
- old_size // this api is not supported by libc
159
+ # // old_size // this api is not supported by libc
143
160
old_size // このAPIはlibcではサポートされていません。
144
161
}
145
162
@@ -164,7 +181,7 @@ pub extern fn __rust_usable_size(size: usize, _align: usize) -> usize {
164
181
extern crate my_allocator;
165
182
166
183
fn main() {
167
- let a = Box::new(8); // allocates memory via our custom allocator crate
184
+ # // let a = Box::new(8); // allocates memory via our custom allocator crate
168
185
let a = Box::new(8); // カスタムアロケータによるメモリ割り当て
169
186
println!("{}", a);
170
187
}
@@ -188,4 +205,4 @@ fn main() {
188
205
<!-- depend on a crate which needs an allocator (e.g. circular dependencies are not -->
189
206
<!-- allowed). This basically means that allocators must restrict themselves to -->
190
207
<!-- libcore currently. -->
191
- * アロケータを使うコードは ` #![needs_allocator] ` でタグ付けされます(例えば現時点での ` liballoc ` クレート)。また、 ` #[allocator] ` がついたクレートはアロケータを使うクレートに依存することが出来ません( 循環依存は許されていません)。このためアロケータは原則としてlibcoreにしか依存しないようにする必要があります。
208
+ * アロケータを使うコードは ` #![needs_allocator] ` でタグ付けされます(例えば現時点での ` liballoc ` クレート)。また、 ` #[allocator] ` がついたクレートはアロケータを使うクレートに直接的にも間接的にも依存することが出来ません(例えば、 循環依存は許されていません)。このためアロケータは原則としてlibcoreにしか依存しないようにする必要があります。
0 commit comments