XINU
Functions
getmem.c File Reference

ヒープ領域を割り当て、最下位のワードアドレスを返す。 More...

#include <xinu.h>
Include dependency graph for getmem.c:

Go to the source code of this file.

Functions

char * getmem (uint32 nbytes)
 ヒープ領域を割り当て、最下位のワードアドレスを返す。 More...
 

Detailed Description

ヒープ領域を割り当て、最下位のワードアドレスを返す。

Definition in file getmem.c.

Function Documentation

◆ getmem()

char* getmem ( uint32  nbytes)

ヒープ領域を割り当て、最下位のワードアドレスを返す。

Step1. 割り込みを禁止する。
Step2. 要求されたメモリのByte数が0の場合は、割り込み状態を復元し、処理を終了する。
Step3. 要求されたメモリのByte数を8の倍数で丸める。
Step4. フリーメモリリスト先頭の次のブロックから順番に、最適なメモリブロック探索を始める。
メモリブロック探索では、以下の3通りいずれかを行う。
・現在のメモリブロックがユーザの要求サイズと一致する場合、そのメモリブロックを返す。
・ユーザの要求よりメモリブロックサイズが大きい場合、必要なメモリブロックサイズ分だけを返す。
・ユーザの要求よりメモリブロックサイズが小さい場合、次のメモリブロックを探索対象とする。
メモリブロック提供時は、フリーメモリリストを連結し直し、メモリブロック合計サイズから提供サイズ分を引く。
また、割り込み状態を許可状態に復元する。

Parameters
[in]nbytes必要なメモリサイズ(Byte)
Returns
成功時はユーザ要求サイズ分のメモリへのアドレスを返し、「要求されたメモリのByte数が0の場合」や
「メモリに空きがない場合」はSYSERRを返す。
Note
フリーメモリブロックはリンクリストで保持され、各ブロックはアドレスの昇順で管理されている。

Definition at line 25 of file getmem.c.

References disable(), memlist, memblk::mlength, memblk::mnext, NULL, restore(), roundmb, and SYSERR.

Referenced by ethinit(), mkbufpool(), ptinit(), and rdsinit().

26 {
27  intmask mask; /* Saved interrupt mask */
28  struct memblk *prev, *curr, *leftover;
29 
30  mask = disable();
31  if (nbytes == 0)
32  {
33  restore(mask);
34  return (char *)SYSERR;
35  }
36 
37  nbytes = (uint32)roundmb(nbytes); /* Use memblk multiples */
38 
39  prev = &memlist;
40  curr = memlist.mnext;
41  while (curr != NULL)
42  { /* Search free list */
43 
44  if (curr->mlength == nbytes)
45  { /* Block is exact match */
46  prev->mnext = curr->mnext;
47  memlist.mlength -= nbytes;
48  restore(mask);
49  return (char *)(curr);
50  }
51  else if (curr->mlength > nbytes)
52  { /* Split big block */
53  leftover = (struct memblk *)((uint32)curr +
54  nbytes);
55  prev->mnext = leftover;
56  leftover->mnext = curr->mnext;
57  leftover->mlength = curr->mlength - nbytes;
58  memlist.mlength -= nbytes;
59  restore(mask);
60  return (char *)(curr);
61  }
62  else
63  { /* Move to next block */
64  prev = curr;
65  curr = curr->mnext;
66  }
67  }
68  restore(mask);
69  return (char *)SYSERR;
70 }
#define NULL
連結リスト用のNULLポインタ
Definition: kernel.h:68
メモリブロックを管理するための構造体。
Definition: memory.h:53
void restore(intmask)
#define SYSERR
処理が失敗した場合
Definition: kernel.h:79
#define roundmb(x)
メモリブロックサイズ(8の倍数)にアドレスを変換するために、8の倍数で丸める。
Definition: memory.h:25
struct memblk * mnext
次のフリーメモリブロックへのポインタ
Definition: memory.h:56
struct memblk memlist
フリーメモリリストの先頭
Definition: initialize.c:27
uint32 intmask
保存された割り込みマスク
Definition: kernel.h:38
uint32 mlength
memblk構造体のサイズを含むブロックサイズ
Definition: memory.h:58
unsigned int uint32
符号なし32ビット整数(unsigned int)
Definition: kernel.h:15
intmask disable(void)
割り込み禁止(intr.Sに定義がある)
Here is the call graph for this function:
Here is the caller graph for this function: