XINU
system
getmem.c
Go to the documentation of this file.
1
5
#include <
xinu.h
>
6
25
char
*
getmem
(
uint32
nbytes)
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
}
NULL
#define NULL
連結リスト用のNULLポインタ
Definition:
kernel.h:68
memblk
メモリブロックを管理するための構造体。
Definition:
memory.h:53
restore
void restore(intmask)
xinu.h
全てのシステムヘッダファイルをインクルードする。
SYSERR
#define SYSERR
処理が失敗した場合
Definition:
kernel.h:79
roundmb
#define roundmb(x)
メモリブロックサイズ(8の倍数)にアドレスを変換するために、8の倍数で丸める。
Definition:
memory.h:25
memblk::mnext
struct memblk * mnext
次のフリーメモリブロックへのポインタ
Definition:
memory.h:56
memlist
struct memblk memlist
フリーメモリリストの先頭
Definition:
initialize.c:27
intmask
uint32 intmask
保存された割り込みマスク
Definition:
kernel.h:38
getmem
char * getmem(uint32 nbytes)
ヒープ領域を割り当て、最下位のワードアドレスを返す。
Definition:
getmem.c:25
memblk::mlength
uint32 mlength
memblk構造体のサイズを含むブロックサイズ
Definition:
memory.h:58
uint32
unsigned int uint32
符号なし32ビット整数(unsigned int)
Definition:
kernel.h:15
disable
intmask disable(void)
割り込み禁止(intr.Sに定義がある)
Generated by
1.8.13