XINU
rdsbufalloc.c
Go to the documentation of this file.
1 /* rdsbufalloc.c - rdsbufalloc */
2 
3 #include <xinu.h>
4 
5 /*------------------------------------------------------------------------
6  * rdsbufalloc - Allocate a buffer from the free list or the cache
7  *------------------------------------------------------------------------
8  */
9 struct rdbuff *rdsbufalloc (
10  struct rdscblk *rdptr /* Ptr to device control block */
11  )
12 {
13  struct rdbuff *bptr; /* Pointer to a buffer */
14  struct rdbuff *pptr; /* Pointer to previous buffer */
15  struct rdbuff *nptr; /* Pointer to next buffer */
16 
17  /* Wait for an available buffer */
18 
19  wait(rdptr->rd_availsem);
20 
21  /* If free list contains a buffer, extract it */
22 
23  bptr = rdptr->rd_free;
24 
25  if ( bptr != (struct rdbuff *)NULL ) {
26  rdptr->rd_free = bptr->rd_next;
27  return bptr;
28  }
29 
30  /* Extract oldest item in cache that has ref count zero (at */
31  /* least one such entry must exist because the semaphore */
32  /* had a nonzero count) */
33 
34  bptr = rdptr->rd_ctprev;
35  while (bptr != (struct rdbuff *) &rdptr->rd_chnext) {
36  if (bptr->rd_refcnt <= 0) {
37 
38  /* Remove from cache and return to caller */
39 
40  pptr = bptr->rd_prev;
41  nptr = bptr->rd_next;
42  pptr->rd_next = nptr;
43  nptr->rd_prev = pptr;
44  return bptr;
45  }
46  bptr = bptr->rd_prev;
47  }
48  panic("Remote disk cannot find an available buffer");
49  return (struct rdbuff *)SYSERR;
50 }
#define NULL
連結リスト用のNULLポインタ
Definition: kernel.h:68
struct rdbuff * rdsbufalloc(struct rdscblk *rdptr)
Definition: rdsbufalloc.c:9
全てのシステムヘッダファイルをインクルードする。
#define SYSERR
処理が失敗した場合
Definition: kernel.h:79
sid32 rd_availsem
Definition: rdisksys.h:91
struct rdbuff * rd_free
Definition: rdisksys.h:87
struct rdbuff * rd_prev
Definition: rdisksys.h:57
int32 rd_refcnt
Definition: rdisksys.h:59
struct rdbuff * rd_next
Definition: rdisksys.h:56
void panic(char *)
Panic状態に陥った旨のメッセージを表示し、全てのプロセスを停止させる。
Definition: panic.c:12
struct rdbuff * rd_ctprev
Definition: rdisksys.h:83
struct rdbuff * rd_chnext
Definition: rdisksys.h:80
syscall wait(sid32)
Definition: wait.c:9