Fixed extern declaration from pointer to array
[minix.git] / servers / pfs / buffer.c
blobe49b1598e3fc70f4c9e94122d4df1b33afe5dd19
1 #include "fs.h"
2 #include "buf.h"
3 #include "inode.h"
4 #include <sys/types.h>
5 #include <stdlib.h>
6 #include <alloca.h>
7 #include <string.h>
10 /*===========================================================================*
11 * buf_pool *
12 *===========================================================================*/
13 PUBLIC void buf_pool(void)
15 /* Initialize the buffer pool. */
17 front = NIL_BUF;
18 rear = NIL_BUF;
23 /*===========================================================================*
24 * get_block *
25 *===========================================================================*/
26 PUBLIC struct buf *get_block(dev, inum)
27 Dev_t dev;
28 ino_t inum;
30 struct buf *bp;
32 bp = front;
33 while(bp != NIL_BUF) {
34 if (bp->b_dev == dev && bp->b_num == inum) {
35 bp->b_count++;
36 return(bp);
38 bp = bp->b_next;
41 /* Buffer was not found. Try to allocate a new one */
42 return new_block(dev, inum);
46 /*===========================================================================*
47 * new_block *
48 *===========================================================================*/
49 PUBLIC struct buf *new_block(dev, inum)
50 Dev_t dev;
51 ino_t inum;
53 /* Allocate a new buffer and add it to the double linked buffer list */
54 struct buf *bp;
56 bp = malloc(sizeof(struct buf));
57 if (bp == NULL) {
58 err_code = ENOSPC;
59 return(NIL_BUF);
61 bp->b_num = inum;
62 bp->b_dev = dev;
63 bp->b_bytes = 0;
64 bp->b_count = 1;
65 memset(bp->b_data, 0 , PIPE_BUF);
67 /* Add at the end of the buffer */
68 if (front == NIL_BUF) { /* Empty list? */
69 front = bp;
70 bp->b_prev = NIL_BUF;
71 } else {
72 rear->b_next = bp;
73 bp->b_prev = rear;
75 bp->b_next = NIL_BUF;
76 rear = bp;
78 return(bp);
82 /*===========================================================================*
83 * put_block *
84 *===========================================================================*/
85 PUBLIC void put_block(dev, inum)
86 dev_t dev;
87 ino_t inum;
89 struct buf *bp;
91 bp = get_block(dev, inum);
92 if (bp == NIL_BUF) return; /* We didn't find the block. Nothing to put. */
94 bp->b_count--; /* Compensate for above 'get_block'. */
95 if (--bp->b_count > 0) return;
97 /* Cut bp out of the loop */
98 if (bp->b_prev == NIL_BUF)
99 front = bp->b_next;
100 else
101 bp->b_prev->b_next = bp->b_next;
103 if (bp->b_next == NIL_BUF)
104 rear = bp->b_prev;
105 else
106 bp->b_next->b_prev = bp->b_prev;
108 /* Buffer administration is done. Now it's safe to free up bp. */
109 free(bp);