英語版
このページの英語版を見る

std.experimental.allocator.building_blocks.region

struct Region(ParentAllocator, uint minAlign = platformAlignment, Flag!"growDownwards" growDownwards = No.growDownwards);
A Regionアロケータは1つの連続したチャンクから直接メモリを確保する。 割り当て解除はなく、領域が一杯になると、割り当て要求は はnull を返す。したがって Regionアロケータはしばしば、(a) より洗練されたアロケータと組み合わせて使われる。 そのため、sは、(a)より洗練されたアロケータと併用されたり、(b)一度にすべてを割り当て解除するバッチ式の超高速アロケーションに使われたりする。 が使われることが多い。
リージョンは3つのポインタだけを格納する。 と限界に対応する3つのポインタを格納するだけである。1回の割り当ては、アラインメントのために割り当てサイズを切り上げ 現在のポインタをバンプし、それを制限値と比較する。 をリミットと比較する。
Region破壊時にメモリのチャンクを確保解除する。
minAlign パラメーターはアライメントを確立する。minAlign > 1 の場合、すべての割り当て要求のサイズは の倍数に切り上げられる。 minAlignの倍数に切り上げられる。 最高速度を目指すアプリケーションは、minAlign = 1 を選択し、アライメントを外部で制御することを望むかもしれない。 を選択し、外部でアライメントを制御することができる。
Examples:
import std.algorithm.comparison : max;
import std.experimental.allocator.building_blocks.allocator_list
    : AllocatorList;
import std.experimental.allocator.mallocator : Mallocator;
import std.typecons : Ternary;
// Create a scalable list of regions. Each gets at least 1MB at a time by
// using malloc.
auto batchAllocator = AllocatorList!(
    (size_t n) => Region!Mallocator(max(n, 1024 * 1024))
)();
writeln(batchAllocator.empty); // Ternary.yes
auto b = batchAllocator.allocate(101);
writeln(b.length); // 101
writeln(batchAllocator.empty); // Ternary.no
// This will cause a second allocation
b = batchAllocator.allocate(2 * 1024 * 1024);
writeln(b.length); // 2 * 1024 * 1024
// Destructor will free the memory
ParentAllocator parent;
親アロケータである。ParentAllocator 。 を保持しているかどうかによって、これはメンバ変数か ParentAllocator.instance.
pure nothrow @nogc this(ubyte[] store);

this(size_t n);

this(ParentAllocator parent, size_t n);
ユーザが提供するストアにバックアップされたリージョンを構築する。 メモリはParentAllocator で割り当てられたと仮定する。
Parameters:
ubyte[] store リージョンをバックアップするユーザー提供ストア。で割り当てられたと仮定する。 ParentAllocator で割り当てられたと仮定する。
size_t n ParentAllocator を使って割り当てるバイト数。もし parent.allocate(n)null を返す場合、領域は空として初期化される(正しく初期化されるが、アロケートできない)。 として初期化される。)
const pure nothrow @nogc @safe size_t goodAllocSize(size_t n);
指定されたサイズを、指定されたアラインメントの倍数に丸める。alignment
alias alignment = minAlign;
の倍数に丸める。
pure nothrow @nogc @trusted void[] allocate(size_t n);
バイトのメモリを割り当てる。 nバイトのメモリを確保する。最短パスは、アラインメント調整(もし )、インクリメント、比較を含む。 調整(もしalignment > 1 )、インクリメント、比較を含む。
Parameters:
size_t n 割り当てるバイト数
Returns:
サイズ nまたは、null 。 を割り当てる。
pure nothrow @nogc @trusted void[] alignedAllocate(size_t n, uint a);
を割り当てる。 nバイトのメモリを確保する。 a.
Parameters:
size_t n 確保するバイト数
uint a 割り当てブロックのアラインメント
Returns:
でアライメントされた nでアライメントされた適切なブロックのいずれかである。 aまたはnull
pure nothrow @nogc @trusted void[] allocateAll();
この領域で利用可能なすべてのメモリを割り当てて返す。
pure nothrow @nogc @safe bool expand(ref void[] b, size_t delta);
割り当てられたブロックをその場で展開する。拡張が成功するのは ブロックが最後に割り当てられた場合にのみ成功する。growDownwardsNo.growDownwards.
pure nothrow @nogc bool deallocate(void[] b);
デアロケートする b.これは bを最後に呼び出したときのみ動作する。 allocate を最後に呼び出したときにのみ動作する。 は何もしない。
Parameters:
void[] b このアロケータに対するallocate の呼び出しによって、以前に取得されたブロックが返される。 アロケータ (null は許可される)。
pure nothrow @nogc bool deallocateAll();
このリージョンによって割り当てられたすべてのメモリを解放する。 この領域によって割り当てられたすべてのメモリを解放する。
const pure nothrow @nogc @trusted Ternary owns(const void[] b);
この bこのリージョンで割り当てられたかどうかを問い合わせる。
Parameters:
void[] b 任意のメモリブロック (null が許される; owns(null)を返す)。 false).
Returns:
true もし bがこの領域に割り当てられていればfalse を返す。
const pure nothrow @nogc @safe Ternary empty();
この領域にメモリが割り当てられていない場合はTernary.yes を返す、 Ternary.no そうでなければ(Ternary.unknown を返すことはない)。
const pure nothrow @nogc @safe size_t available();
割り当て可能なバイト数を返す非標準プロパティ。
struct BorrowedRegion(uint minAlign = platformAlignment, Flag!"growDownwards" growDownwards = No.growDownwards);
A BorrowedRegionは、ユーザが用意したメモリ・ブロックから直接割り当てる。
Region とは異なり BorrowedRegionから割り当てたメモリは所有しない。 また、破棄時にそのメモリーの割り当てを解除することもない。その代わり、メモリが適切に確保されるようにするのはユーザーの責任である。 代わりに、メモリが適切に処分されるようにするのはユーザーの責任である。
その他の点では BorrowedRegionRegion とまったく同じように振る舞う。
Examples:
import std.typecons : Ternary;

ubyte[1024] store;
auto myRegion = BorrowedRegion!(1)(store[]);

writeln(myRegion.empty); // Ternary.yes
writeln(myRegion.available); // store.length

void[] b = myRegion.allocate(101);

writeln(b.length); // 101
writeln(myRegion.empty); // Ternary.no
writeln(myRegion.owns(b)); // Ternary.yes
writeln(myRegion.available); // store.length - b.length

void[] b2 = myRegion.allocate(256);

// Can only free the most recent allocation
writeln(myRegion.deallocate(b)); // false
writeln(myRegion.deallocate(b2)); // true

myRegion.deallocateAll();

writeln(myRegion.empty); // Ternary.yes
pure nothrow @nogc this(ubyte[] store);
ユーザーが提供するストアに支えられたリージョンを構築する。
Parameters:
ubyte[] store ユーザ提供のストアがリージョンをバックアップする。
const pure nothrow @nogc @safe size_t goodAllocSize(size_t n);
指定されたサイズをalignment
alias alignment = minAlign;
の倍数に丸める。
pure nothrow @nogc @trusted void[] allocate(size_t n);
バイトのメモリを割り当てる。 nバイトのメモリを割り当てる。最短パスは、アラインメント調整(もし )、インクリメント、比較を含む。 調整(もしalignment > 1 )、インクリメント、比較を含む。
Parameters:
size_t n 割り当てるバイト数
Returns:
サイズ nまたは、null 。 を割り当てる。
pure nothrow @nogc @trusted void[] alignedAllocate(size_t n, uint a);
を割り当てる。 nバイトのメモリを確保する。 a.
Parameters:
size_t n 確保するバイト数
uint a 割り当てブロックのアラインメント
Returns:
でアライメントされた nでアライメントされた適切なブロックのいずれかである。 aまたはnull
pure nothrow @nogc @trusted void[] allocateAll();
この領域で利用可能なすべてのメモリを割り当てて返す。
pure nothrow @nogc @safe bool expand(ref void[] b, size_t delta);
割り当てられたブロックをその場で展開する。拡張が成功するのは ブロックが最後に割り当てられた場合にのみ成功する。growDownwardsNo.growDownwards.
pure nothrow @nogc bool deallocate(void[] b);
デアロケートする b.これは bを最後に呼び出したときのみ動作する。 allocate を最後に呼び出したときにのみ動作する。 は何もしない。
Parameters:
void[] b このアロケータに対するallocate の呼び出しによって、以前に取得されたブロックが返される。 アロケータ (null は許可される)。
pure nothrow @nogc bool deallocateAll();
このリージョンによって割り当てられたすべてのメモリを解放する。 この領域によって割り当てられたすべてのメモリを解放する。
const pure nothrow @nogc @trusted Ternary owns(const void[] b);
この bこのリージョンで割り当てられたかどうかを問い合わせる。
Parameters:
void[] b 任意のメモリブロック (null が許される; owns(null)を返す)。 false).
Returns:
true もし bがこの領域に割り当てられていればfalse を返す。
const pure nothrow @nogc @safe Ternary empty();
この領域にメモリが割り当てられていない場合はTernary.yes を返す、 Ternary.no そうでなければ(Ternary.unknown を返すことはない)。
const pure nothrow @nogc @safe size_t available();
割り当て可能なバイト数を返す非標準プロパティ。
struct InSituRegion(size_t size, size_t minAlign = platformAlignment);
InSituRegionは、それ自身の中にストレージを保持する便利な領域である (静的なサイズの配列の形で)それ自身の中にストレージを運ぶ便利な領域である。
最初のテンプレート引数は領域のサイズであり、2番目は必要なアラインメントである。 必要なアライメントである。要求されるアライメントとプラットフォームの詳細によっては、実際に利用可能なストレージが 実際に利用可能なストレージは、コンパイル時のパラメータよりも小さいかもしれない。そのため 領域で少なくともn バイトが利用可能であることを確認するには InSituRegion!(n + a - 1, a).
を使用する。 InSituRegionはスタックアロケータとして最もよく使われることを考えると スタックが下へ下へと成長するシステムでは、ホットメモリが最初に使われるように、末尾からアロケートする。 ホットメモリが最初に使われる。
Examples:
// 128KB region, allocated to x86's cache line
InSituRegion!(128 * 1024, 16) r1;
auto a1 = r1.allocate(101);
writeln(a1.length); // 101

// 128KB region, with fallback to the garbage collector.
import std.experimental.allocator.building_blocks.fallback_allocator
    : FallbackAllocator;
import std.experimental.allocator.building_blocks.free_list
    : FreeList;
import std.experimental.allocator.building_blocks.bitmapped_block
    : BitmappedBlock;
import std.experimental.allocator.gc_allocator : GCAllocator;
FallbackAllocator!(InSituRegion!(128 * 1024), GCAllocator) r2;
const a2 = r2.allocate(102);
writeln(a2.length); // 102

// Reap with GC fallback.
InSituRegion!(128 * 1024, 8) tmp3;
FallbackAllocator!(BitmappedBlock!(64, 8), GCAllocator) r3;
r3.primary = BitmappedBlock!(64, 8)(cast(ubyte[]) (tmp3.allocateAll()));
const a3 = r3.allocate(103);
writeln(a3.length); // 103

// Reap/GC with a freelist for small objects up to 16 bytes.
InSituRegion!(128 * 1024, 64) tmp4;
FreeList!(FallbackAllocator!(BitmappedBlock!(64, 64), GCAllocator), 0, 16) r4;
r4.parent.primary = BitmappedBlock!(64, 64)(cast(ubyte[]) (tmp4.allocateAll()));
const a4 = r4.allocate(104);
writeln(a4.length); // 104
alias alignment = minAlign;
minAlign のエイリアスで、有効なアライメント(2の0乗でない)でなければならない。 の2乗)でなければならない。領域の開始とすべての割り当て要求は、アライメントの倍数に切り上げられる。 のエイリアスである。
InSituRegion!(4096) a1;
assert(a1.alignment == platformAlignment);
InSituRegion!(4096, 64) a2;
assert(a2.alignment == 64);
void[] allocate(size_t n);
bytes null を割り当てて返す。 を割り当てる。効率上の理由から、 関数は空の非NULLスライスを返す。bytes == 0
void[] alignedAllocate(size_t n, uint a);
上記と同様であるが、割り当てられたメモリは aバイトで整列される。
bool deallocate(void[] b);
割り当て解除 b.これは bを最後に呼び出したときのみ動作する。 allocate として取得された場合にのみ動作する。 は何もしない。このセマンティクスは厄介なので deallocateが定義されている。 Region でインスタンス化された場合のみ定義される。Yes.defineDeallocate でインスタンス化された場合のみ定義される。
Parameters:
void[] b このアロケータに対するallocate の呼び出しによって以前に取得されたブロックである。 アロケータに対してnull を呼び出すことで得られるブロック ( が許される)。
pure nothrow @nogc @safe Ternary owns(const void[] b);
が以前のアロケーションの結果である場合はTernary.yes を返す。 bが以前のアロケーションの結果である、 Ternary.no そうでなければ
bool expand(ref void[] b, size_t delta);
アロケートされたブロックをその場で展開する。拡張は ブロックが最後に割り当てられたものである場合にのみ成功する。
bool deallocateAll();
このアロケータで割り当てられたすべてのメモリを解放する。
void[] allocateAll();
このアロケータで利用可能なメモリをすべて割り当てる。
size_t available();
割り当て可能なバイト数を返す非標準関数。
struct SbrkRegion(uint minAlign = platformAlignment);
アロケータは sbrk によってバッキングされたアロケータである。sbrk はスレッドセーフではない。 ある、 SbrkRegionは内部的にミューテックスを使用する。このため このことは、brksbrk への制御不能な呼び出しが、SbrkRegion の動作に悪影響を及ぼす可能性があることを意味する。
static shared SbrkRegion instance;
すべての呼び出し元が共有するインスタンス。
enum uint alignment;
標準的なアロケータプリミティブ。
shared const pure nothrow @nogc @safe size_t goodAllocSize(size_t n);

shared nothrow @nogc @trusted void[] allocate(size_t bytes);

shared nothrow @nogc @trusted void[] alignedAllocate(size_t bytes, uint a);
与えられたサイズをwの倍数に丸める。alignment
shared nothrow @nogc @trusted bool expand(ref void[] b, size_t delta);

shared pure nothrow @nogc @trusted Ternary owns(const void[] b);
の倍数に丸める。 expandメソッドが成功するのは、引数が割り当てられた最後のブロック である場合にのみ成功する。その場合 expandはブレークポインタを をプッシュしようとする。
shared nothrow @nogc bool deallocate(void[] b);
この deallocateメソッドは、ブレークアドレスの縮小をサポートする(つまり、ブレークポインタの右への呼び出しを受け付ける)システムでのみ動作する(そして、true を返す)。 への呼び出しを受け付ける。sbrk への呼び出しを受け付ける)。OSXはこれを受け付けない。また、引数 は最後に割り当てられたブロックでなければならない。
shared nothrow @nogc bool deallocateAll();
この deallocateAllメソッドは、ブレークアドレスの削減をサポートしている(ブレークアドレスの削減の呼び出しを受け付ける)システムでのみ動作する(そして、true を返す)。 ブレークアドレスの減少をサポートする(つまり、負のオフセットを持つsbrk への呼び出しを受け付ける)。OSXはそのようなものを受け付けない。
shared pure nothrow @nogc @safe Ternary empty();
標準アロケータAPI。
struct SharedRegion(ParentAllocator, uint minAlign = platformAlignment, Flag!"growDownwards" growDownwards = No.growDownwards);
Region アロケータのスレッドセーフバージョン。 アロケーションとデアロケーションは core.atomic.cas.
ParentAllocator parent;
親アロケータ。ParentAllocator 。 を保持しているかどうかによって、これはメンバ変数または ParentAllocator.instance.
pure nothrow @nogc this(ubyte[] store);

this(size_t n);
ユーザが提供するストアにバックアップされたリージョンを構築する。 メモリはParentAllocator で割り当てられたと仮定する。
Parameters:
ubyte[] store リージョンをバックアップするユーザー提供ストア。で割り当てられたと仮定する。 ParentAllocator で割り当てられたと仮定する。
size_t n ParentAllocator を使って割り当てるバイト数。もし parent.allocate(n)null を返す場合、領域は空として初期化される(正しく初期化されるが、アロケートできない)。 として初期化される。)
const pure nothrow @nogc @safe size_t goodAllocSize(size_t n);
指定されたサイズを、指定されたアラインメントの倍数に丸める。alignment
alias alignment = minAlign;
の倍数に丸める。
pure nothrow @nogc @trusted void[] allocate(size_t n);
バイトのメモリを割り当てる。 nバイトのメモリを割り当てる。割り当ては、現在の使用領域を追跡する ポインタをアトミックにインクリメントすることで行われる。
Parameters:
size_t n 割り当てるバイト数
Returns:
サイズ nnull を割り当てる。 である。
pure nothrow @nogc bool deallocate(void[] b);
割り当てを解除する b.これは bを最後に呼び出したときのみ動作する。 allocate それ以外の場合(つまり、その後に別のアロケーションが行われた場合)、何もしない。 は何もしない。
Parameters:
void[] b このアロケータに対するallocate の呼び出しによって、以前に取得されたブロックが返される。 アロケータ (null は許可される)。
pure nothrow @nogc bool deallocateAll();
このリージョンによって割り当てられたすべてのメモリを解放する。 この領域によって割り当てられたすべてのメモリを解放する。
pure nothrow @nogc @trusted void[] alignedAllocate(size_t n, uint a);
を割り当てる。 nバイトのメモリを確保する。 a.
Parameters:
size_t n 確保するバイト数
uint a 割り当てブロックのアラインメント
Returns:
でアライメントされた nでアライメントされた適切なブロックのいずれかである。 aまたはnull
const pure nothrow @nogc @trusted Ternary owns(const void[] b);
でアライメントされた適切なバイトブロックのいずれかである。 bがこの領域に割り当てられているかどうかを問い合わせる。
Parameters:
void[] b 任意のメモリブロック(null が許される; owns(null)を返す)。 false).
Returns:
true もし bがこの領域に割り当てられていればfalse を返す。
const pure nothrow @nogc @safe Ternary empty();
この領域にメモリが割り当てられていない場合はTernary.yes を返す、 Ternary.no そうでなければ(Ternary.unknown を返すことはない)。
struct SharedBorrowedRegion(uint minAlign = platformAlignment, Flag!"growDownwards" growDownwards = No.growDownwards);
A SharedBorrowedRegionは、ユーザが提供したメモリ・ブロックから直接割り当てを行う。
SharedRegion とは異なり SharedBorrowedRegionとは異なり とは異なり、aは割り当てたメモリーを所有せず、破棄時にそのメモリーの割り当てを解除することもない。その代わりである、 代わりに、メモリが適切に処分されるようにするのはユーザーの責任である。 の責任である。
その他の点では SharedBorrowedRegionSharedRegion のように動作する。
shared pure nothrow @nogc this(ubyte[] store);
ユーザが提供するストアに支えられたリージョンを構築する。
Parameters:
ubyte[] store リージョンをバックアップするユーザー提供のストア。エイリアスであってはならない。
shared const pure nothrow @nogc @safe size_t goodAllocSize(size_t n);
指定されたサイズをalignment
alias alignment = minAlign;
の倍数に丸める。
shared pure nothrow @nogc @trusted void[] allocate(size_t n);
バイトのメモリを割り当てる。 nバイトのメモリを割り当てる。割り当ては、現在の使用領域を追跡するポインタ ポインタをアトミックにインクリメントすることで行われる。
Parameters:
size_t n 割り当てるバイト数
Returns:
サイズ nnull を割り当てる。 である。
shared pure nothrow @nogc @trusted void[] alignedAllocate(size_t n, uint a);
を割り当てる。 nバイトのメモリを確保する。 a.
Parameters:
size_t n 確保するバイト数
uint a 割り当てブロックのアラインメント
Returns:
でアライメントされた nでアライメントされた適切なブロックのいずれかである。 aまたはnull
shared pure nothrow @nogc bool deallocate(void[] b);
割り当てを解除する b.これは bを最後に呼び出したときのみ動作する。 allocate を最後に呼び出したときにのみ動作する。 は何もしない。
Parameters:
void[] b このアロケータに対するallocate の呼び出しによって、以前に取得されたブロックが返される。 アロケータ (null は許可されている) に対して、 をコールすることで、以前に獲得したブロック。
shared pure nothrow @nogc bool deallocateAll();
このリージョンによって割り当てられたすべてのメモリを解放する。 この領域によって割り当てられたすべてのメモリを解放する。
shared const pure nothrow @nogc @trusted Ternary owns(const void[] b);
この bこのリージョンで割り当てられたかどうかを問い合わせる。
Parameters:
void[] b 任意のメモリブロック (null が許される; owns(null)を返す)。 false).
Returns:
true もし bがこの領域に割り当てられていればfalse を返す。
shared const pure nothrow @nogc @safe Ternary empty();
この領域にメモリが割り当てられていない場合はTernary.yes を返す、 Ternary.no そうでなければ(Ternary.unknown を返すことはない)。