Repository navigation
Expand file tree
/
Copy pathbuffer.c
More file actions
103 lines (83 loc) · 2.4 KB
/
Copy pathbuffer.c
File metadata and controls
103 lines (83 loc) · 2.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
1819
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include "fs.h"
#include "buf.h"
#include "inode.h"
#include <sys/types.h>
#include <stdlib.h>
#include <string.h>
static struct buf *new_block(dev_t dev, ino_t inum);
/*===========================================================================*
* buf_pool *
*===========================================================================*/
void buf_pool(void)
{
/* Initialize the buffer pool. */
front = NULL;
rear = NULL;
}
/*===========================================================================*
* get_block *
*===========================================================================*/
struct buf *get_block(dev_t dev, ino_t inum)
{
struct buf *bp = front;
while(bp != NULL) {
if (bp->b_dev == dev && bp->b_num == inum) {
bp->b_count++;
return(bp);
}
bp = bp->b_next;
}
/* Buffer was not found. Try to allocate a new one */
return new_block(dev, inum);
}
/*===========================================================================*
* new_block *
*===========================================================================*/
static struct buf *new_block(dev_t dev, ino_t inum)
{
/* Allocate a new buffer and add it to the double linked buffer list */
struct buf *bp;
bp = malloc(sizeof(struct buf));
if (bp == NULL) {
err_code = ENOSPC;
return(NULL);
}
bp->b_num = inum;
bp->b_dev = dev;
bp->b_bytes = 0;
bp->b_count = 1;
memset(bp->b_data, 0 , PIPE_BUF);
/* Add at the end of the buffer */
if (front == NULL) { /* Empty list? */
front = bp;
bp->b_prev = NULL;
} else {
rear->b_next = bp;
bp->b_prev = rear;
}
bp->b_next = NULL;
rear = bp;
return(bp);
}
/*===========================================================================*
* put_block *
*===========================================================================*/
void put_block(dev_t dev, ino_t inum)
{
struct buf *bp;
bp = get_block(dev, inum);
if (bp == NULL) return; /* We didn't find the block. Nothing to put. */
bp->b_count--; /* Compensate for above 'get_block'. */
if (--bp->b_count > 0) return;
/* Cut bp out of the loop */
if (bp->b_prev == NULL)
front = bp->b_next;
else
bp->b_prev->b_next = bp->b_next;
if (bp->b_next == NULL)
rear = bp->b_prev;
else
bp->b_next->b_prev = bp->b_prev;
/* Buffer administration is done. Now it's safe to free up bp. */
free(bp);
}