GBDKライブラリドキュメント
GBDK libraries documentation |
||
---|---|---|
前
Prev |
5章
ライブラリ
Chapter 5.Libraries |
次
Next |
void *malloc( size_t numbytes )空きのメモリプールからnumbytesのメモリを割り付け、新しく割り付けられた領域の基礎へのポインタを返します。
割付けでメモリがクリアされないことに注意してください。
Note that the memory is not cleared upon allocation.
numbytes:割り付けるバイト数
numbytes: The number of bytes to allocate
成功した場合、新しく割り付けられた領域のポインタを返します。
失敗した場合、
NULL
を返します。
On success, returns a pointer to the newly allocated region.On failure returns NULL.
void *calloc( size_t nmem, size_t size )sizeサイズのnmemオブジェクトのためのスペースを割り付けて割り付けられた領域へのポインタを返すことを試みます。
callocはmallocに非常に似ていますが、さらに、返る前にメモリ領域をクリアします(0で埋める)。
calloc is very similar to malloc but it also clears (fills with zero) the memory region before returning.
size:1つのオブジェクトのサイズ
size: size of one object
nmem:スペースを割り付けるオブジェクトの数
nmem: Number of objects to allocate space for
成功した場合、新しく割り付けられ取り除かれた領域へのポインタを返します。
失敗した場合、
NULL
を返します。
On success, returns a pointer to the newly allocated and cleared region.On failure, returns NULL.
void *realloc( void *current, size_t size )再サイズに現在割り付けられた領域を試みます。
reallocは内部に含まれるデータを破壊せずに、現在割り付けられたブロックをサイズ変更するために使用されます。
現在のブロックが要求されたより大きい場合、閉鎖する、その、引きずるデータが失われます。
現在のブロックが要求された1つより小さい場合、ブロックの終了がクリアされないことに注意してください。
currentがNULLの場合、これは
malloc
と同じです。
サイズが0の場合、これは
free
と同じです。
realloc is used to resize a currently allocated block without loosing the data contained within.If the current block is larger than the requested block the the trailing data is lost.Note that the end of the block is not cleared if the current block is smaller than the requested one.If current is NULL, then this is equivalent to malloc.If size is zero, then this is equivalent to free.
current:現在割り付けられているブロックへのポインタ。
current: Pointer to the currently allocated block.
size:新しいブロックのサイズ
size: Size of the new block
成功した場合、新しく割り付けられたブロックへのポインタを返します。
古いものと異なる位置に新しいブロックがあるかもしれないことに注意してください。
失敗した場合や、サイズが0の場合、NULLを返します。
On success returns a pointer to the newly allocated block.Note that the new block may be at a different location to the old.On failure or if size is zero, then NULL is returned.
int free( void *region )以前に割り付けられたregionを解放します。
malloc
、
realloc
や
calloc
によって以前に割り付けられた領域を解放しようとします。
有効な領域だけを解放できることに注意してください。
さらに、エラーコードを返すので、このプロトタイプが標準Cのfreeと異なることに注意してください。
Attempts to free a region previously allocated by malloc, realloc or calloc.Note that only valid regions can be freed.Note also that this prototype differs from the standard C free as it returns an error code.
region:以前に割り付けられた領域へのポインタ。
region: Pointer to a previously allocated region.
成功した場合、0を返します。
領域が既にfreeされている場合-1を返し、領域が割り付けられなかった場合-2を返します。
On success, returns zero (0).If the region is already free, returns -1. If the region was never allocated, returns -2.
NULLは失敗上の
malloc
と他のものによって返されます。
現在0と等しいと定義された。
NULL is returned by malloc and others upon failure.Currently defined to be equal to zero.
size_tはバイトでのオブジェクトのサイズを指定するために使用されます。
GBが16ビットのアドレスNULL間を備えた8ビットのプロセッサを持っているとともに、size_tは現在UWORDとして定義されます。
ほとんどのGBプログラムのバンクに預けた性質により、それが後の日付でUDWORDに変更されるかもしれないことに注意してください。
size_t is used to specify the size of an object in bytes.As the GB has an 8 bit processor with a 16 bit address space, size_t is currently defined as a UWORD.Note that due to the banked nature of most GB programs, it might be changed to UDWORD at a later date.
このmallocライブラリは実装されます、大きなブロックがそうの場合に、大きなブロックの単純な著しくリンクリストを使用すること、1つの、自由かや使用されているヘッダとどちらかでありうるメモリのセクション。
割付けアルゴリズムに特に抜け目がないものは何もありません。
コンピュータ科学者に対立するものとしてのエンジニアです。したがって、コードがそのとき仕事を行う場合、それは、十分に接近してあります。
多くの小さなブロックを割り付けてとそれらのうちのいくつかを維持することによって引き起こされた、重い分割に対してこのシステムがどれくらいよく上げるか私が知らないことに注意してください。
しかしながら、さらに信心ぶった物言いをします、これを行うGB上で実行したいプログラムについて考える。
This malloc library is implemented using a simple signally linked list of hunks where a hunk is a a header and section of memory that can be either free or used.There is nothing particularly clever about the allocation algorithms.I'm an engineer as opposed to a computer scientist, so if the code gets the job done then it's close enough.Note that I do not know how well this system will hold up against heavy fragmentation caused by allocating many small blocks and keeping some of them.However, I also cant think of a program that you'd want to run on a GB that would do this.
int malloc_init( void )mallocシステムが現在uninitialisedされる場合、それを初期化します。
ヘッダmalloc_firstがそのマジックナンバーのチェックにより有効かどうかチェックします。
場合、それ、でない、それのマークによりそれを初期化する、として、自由、D000hへのエリアC000hの中でそれに空きRAMをすべて占めさせること、
領域ヘッダ
ポインタのセット、に、NULL、最終的に、
マジックナンバー
のセット。
Checks to see if the header malloc_first is valid by checking its magic number.If it is not, initialise it by marking it as free, setting it to occupy all of the free ram in the area C000h to D000h, setting the region header pointer to NULL and finally setting the magic number.
無
None
成功した場合、0を返します。
mallocシステムが既にセットアップの場合、返ります。 -1.
On success, returns zero (0).If the malloc system is already setup, return -1.
void malloc_gc( void )連続した空きブロックの連結によりmalloc大きなブロックリスト上でガーベジコレクションを実行します。
領域を割り付ける試みが失敗する場合、
malloc_gc
はmallocによって呼ばれます。
malloc_gcは、malloc大きなブロックリストによって走査し、より大きな1個のブロックへ隣接した空きブロックを連結することにより状況を改善することを試みます。
各走査中で、隣接した空き2個のブロックがそのとき見つかる場合、それらは組み合わせられます。また、走査は第1のブロックから継続しました。
組み合わせがそのとき作られた場合、リストは再走査されます。(走査の終わりに、)
理論上、組み合わせがすべて第1のパスで作られたほうがよいとともに、なぜ再走査がそこにでか実際に確かではありません。
malloc_gc is called by malloc when an attempt to allocate a region fails.malloc_gc attempts to improve the situation by scanning through the malloc hunk list and joining adjacent free blocks into one larger block.In each scan, if two adjacent free blocks are found then they are combined and the scan continued from the first block.At the end of a scan, if any combinations were made then the list is rescanned.I'm not really sure why the rescan is there as in theory all combinations should be made on the first pass.
無
None
無
Nothing
struct smalloc_hunk { UBYTE magic; pmmalloc_hunk next; UWORD size; int status; };mmalloc_hunk はメモリの割り付けられた領域と空き領域の跡を追うためにmallocライブラリによって使用される内部構造体です。
マジック:これを有効なmmalloc_hunkであると次に確認するマジックナンバー:次の大きなブロックへのポインタ、これが最後の場合、
NULL
。
サイズ:大きなブロックのバイトでのサイズ。
ステータス:この大きなブロックが参照するブロックの現在のステータス。
MALLOC_UNUSED(0)、MALLOC_FREE(1)とMALLOC_USEDのうちの1つ(2)。
なぜMALLOC_UNUSEDとMALLOC_FREE:)の両方を定義したかという考えを持っていません。
magic: A magic number that identifies this as a valid mmalloc_hunk next: Pointer to the next hunk, NULL if this is the last.size: Size in bytes of the hunk.status: Current status of the block that this hunk refers to.One of MALLOC_UNUSED (0), MALLOC_FREE (1) and MALLOC_USED (2).I have no idea why I defined both a MALLOC_UNUSED and a MALLOC_FREE :)
マジックナンバーは有効なmalloc大きなブロックヘッダと提携しました。
現在実際に穿つ123にセット。
よりよいもののための提案は非常に評価されます。
The magic number associated with a valid malloc hunk header.Currently set at a really boring 123.Suggestions for something better will be greatly appreciated.
大きなブロックのための
ヘッダ
はちょうど領域の前に生じます。
それらの領域を過ぎて書くいかなる遍歴するプログラムもこのヘッダに上書きし、リンクリストを壊すことができました。
しかし、それを得ます。
ほとんどのルーチンはリストを歩かせる間にマジックナンバーをチェックし、故障したヘッダが見つかる場合、異常終了します。
The header for a hunk occurs just before the region.Any errant programs that write past their region could overwrite this header and break the linked list.But you get that.Most routines check the magic number while walking the list and abort if a broken header is found.
空き(静的変数が割り付けられた後、)量のメモリ、crt0.sに定義された_HEAPと呼ばれる新しいリンカエリアの使用によって決定される。
_HEAPは_BSSの後に生じます。したがって、それは空きメモリの最初に生じたほうがよい。
ただ一つのデータは、_HEAPの中に最初に
malloc_init
によって使用されるmalloc_heap_startへの言及です。
マジックナンバーが悪い場所で生じるために恐らく実際のGBの上にそれがあることに注意してください。
これは初期化時にcrt0.sに修正されたほうがよい。
mallocは、さらにスタックとRAMを共有します。
現在、割り付けられたメモリに下へなるスタックの問題は減少します、によって、割り付ける、低いメモリから最初に512バイトのバッファ(
malloc_init
の中のセット)の提供によって。
The amount of memory free after static variables are allocated is determined by using a new linker area called _HEAP, defined in crt0.s._HEAP occurs after _BSS, so it should occur at the start of free memory.The only data initially in _HEAP is a reference to malloc_heap_start which is used by malloc_init.Note that it is possibly on a real GB for the magic number to occur in a bad place.This should be fixed in crt0.s at the initialisation time.malloc also shares the ram with the stack.Currently the problem of the stack growing down into allocated memory is lessened by allocating from low memory first and by providing a 512 byte buffer (set in malloc_init).
内部メモリを備えたカートリッジ上に、余分な8kはA000hからBFFFhに利用可能です。
不運にも
free
、大きなブロックが連続したと仮定する、それは問題を引き起こす。
2つの解決は、A000hからスタートする_BSSを変えているか、や、次の大きなブロックが現在のものに連続した場合、セットされる
ヘッダ
の中で余分なフラグを定義しています。
それは注意深く管理されなければならないが、第2のオプションはさらにページがつけられたRAMが使用されることを可能にします。
On cartridges with internal memory an extra 8k from A000h to BFFFh is available.Unfortunately free assumes that hunks are consecutive which causes problems.Two solutions are shifting _BSS to start at A000h or defining an extra flag in the header that is set if the next hunk is consecutive to the current one.The second option would also allow paged ram to be used, although it would have to be managed carefully.
前
Prev |
ホーム
Home |
次
Next |
型 - types.h
Types - types.h |
上
Up |
複数のフォントのサポート - font.h
Support for multiple fonts - font.h |