XINU
Functions
getstk.c File Reference

スタックメモリを割り当て、最上位のワードアドレスを返す。 More...

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

Go to the source code of this file.

Functions

char * getstk (uint32 nbytes)
 スタックメモリを割り当て、最上位のワードアドレスを返す。 More...
 

Detailed Description

スタックメモリを割り当て、最上位のワードアドレスを返す。

Definition in file getstk.c.

Function Documentation

◆ getstk()

char* getstk ( uint32  nbytes)

スタックメモリを割り当て、最上位のワードアドレスを返す。

Step1. 割り込みを禁止する。
Step2. 要求メモリサイズが0の場合は、割り込み状態を復元し、処理を終了する。
Step3. 要求メモリサイズを8の倍数で丸める。
Step4. フリーメモリリストの先頭から、要求メモリサイズ以上のメモリブロックを探索する。
Step5. 要求メモリサイズを満たすメモリブロックがない場合は、割り込み状態を復元し、処理を終了する。
Step6. 要求メモリサイズとメモリブロックサイズが一致する場合は、そのブロックをメモリフリーリストから除く
Step7.要求メモリサイズよりメモリブロックサイズが大きい場合は、2つに分割し、
要求サイズと一致するブロックをスタックとし、残りをメモリフリーリストに残す。
Step8. 割り込み状態を復元する。
Step9. スタック(メモリブロックの最上位アドレス)を返す。

Parameters
[in]nbytes要求メモリサイズ(byte)
Returns
成功時はスタック(メモリブロックの最上位アドレス)を返し、以下の場合はSYSERRを返す。
 ・要求メモリサイズが0の場合
 ・要求メモリサイズを確保できなかった場合

Definition at line 25 of file getstk.c.

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

Referenced by create(), and sysinit().

26 {
27  intmask mask; /* Saved interrupt mask */
28  struct memblk *prev, *curr; /* Walk through memory list */
29  struct memblk *fits, *fitsprev; /* Record block that fits */
30 
31  mask = disable();
32  if (nbytes == 0)
33  {
34  restore(mask);
35  return (char *)SYSERR;
36  }
37 
38  nbytes = (uint32)roundmb(nbytes); /* Use mblock multiples */
39 
40  prev = &memlist;
41  curr = memlist.mnext;
42  fits = NULL;
43  fitsprev = NULL; /* Just to avoid a compiler warning */
44 
45  while (curr != NULL)
46  { /* Scan entire list */
47  if (curr->mlength >= nbytes)
48  { /* Record block address */
49  fits = curr; /* when request fits */
50  fitsprev = prev;
51  }
52  prev = curr;
53  curr = curr->mnext;
54  }
55 
56  if (fits == NULL)
57  { /* No block was found */
58  restore(mask);
59  return (char *)SYSERR;
60  }
61  if (nbytes == fits->mlength)
62  { /* Block is exact match */
63  fitsprev->mnext = fits->mnext;
64  }
65  else
66  { /* Remove top section */
67  fits->mlength -= nbytes;
68  fits = (struct memblk *)((uint32)fits + fits->mlength);
69  }
70  memlist.mlength -= nbytes;
71  restore(mask);
72  return (char *)((uint32)fits + nbytes - sizeof(uint32));
73 }
#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: