コードは金属マシンで実行されているようです。通常、物理アドレス空間のみを直接使用するシステムでは、仮想アドレス マッピングはありません。
私の理解を参照してください。32 ビット システムでは、sizeof(ptr) = 4 バイトです。
extern block_t *block_head; // the real heap, and its address
// is >= 0x80000000, see below "my_size < 0"
extern void *get_block(int index); // get a block from the heap
// (lead by block_head)
int heap[10000]; // just the indicators, not the real heap
void* malloc(int size)
{
int sz = (size + 3) / 4; // make the size aligns with 4 bytes,
// you know, allocated size would be aligned.
int chunk = 0; // the first check point
if(heap[chunk] > sz) { // the value is either a valid free-block size
// which meets my requirement, or an
// address of an allocated block
int my_size = heap[chunk]; // verify size or address
if (my_size < 0) { // it is an address, say a 32-bit value which
// is >0x8000...., not a size.
my_size = -my_size // the algo, convert it
}
chunk = chunk + my_size + 2; // the algo too, get available
// block index
if (chunk == heap_size) { // no free chunks left
return NULL; // Out of Memory
}
void *block = get_block(chunk);
heap[chunk] = (int)block;
return block;
}
// my blocks is too small initially, none of the blocks
// will meet the requirement
return NULL;
}
編集:誰かがアルゴを説明するのを手伝ってくれますか?つまり、アドレス -> my_size -> チャンクを変換しますか? reclaim を呼び出すと、free(void *addr) と言うと、このアドレス -> my_size -> チャンク アルゴも使用され、ブロックをヒープに戻した後、それに応じてヒープ [チャンク] を更新します。