minix/servers/pfs/buffer.c

104 lines
2.4 KiB
C
Raw Normal View History

2009-12-20 21:41:50 +01:00
#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);
2009-12-20 21:41:50 +01:00
/*===========================================================================*
* buf_pool *
*===========================================================================*/
2012-03-25 20:25:53 +02:00
void buf_pool(void)
2009-12-20 21:41:50 +01:00
{
/* Initialize the buffer pool. */
front = NULL;
rear = NULL;
2009-12-20 21:41:50 +01:00
}
/*===========================================================================*
* get_block *
*===========================================================================*/
struct buf *get_block(dev_t dev, ino_t inum)
2009-12-20 21:41:50 +01:00
{
struct buf *bp = front;
2009-12-20 21:41:50 +01:00
while(bp != NULL) {
2012-02-13 16:28:04 +01:00
if (bp->b_dev == dev && bp->b_num == inum) {
bp->b_count++;
return(bp);
}
bp = bp->b_next;
2009-12-20 21:41:50 +01:00
}
/* 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)
2009-12-20 21:41:50 +01:00
{
/* Allocate a new buffer and add it to the double linked buffer list */
struct buf *bp;
2012-02-13 16:28:04 +01:00
bp = malloc(sizeof(struct buf));
2009-12-20 21:41:50 +01:00
if (bp == NULL) {
err_code = ENOSPC;
2012-02-13 16:28:04 +01:00
return(NULL);
2009-12-20 21:41:50 +01:00
}
bp->b_num = inum;
bp->b_dev = dev;
bp->b_bytes = 0;
bp->b_count = 1;
memset(bp->b_data, 0 , PIPE_BUF);
2012-02-13 16:28:04 +01:00
2009-12-20 21:41:50 +01:00
/* Add at the end of the buffer */
if (front == NULL) { /* Empty list? */
2012-02-13 16:28:04 +01:00
front = bp;
bp->b_prev = NULL;
2009-12-20 21:41:50 +01:00
} else {
2012-02-13 16:28:04 +01:00
rear->b_next = bp;
bp->b_prev = rear;
2009-12-20 21:41:50 +01:00
}
bp->b_next = NULL;
2009-12-20 21:41:50 +01:00
rear = bp;
return(bp);
}
/*===========================================================================*
* put_block *
*===========================================================================*/
void put_block(dev_t dev, ino_t inum)
2009-12-20 21:41:50 +01:00
{
struct buf *bp;
bp = get_block(dev, inum);
if (bp == NULL) return; /* We didn't find the block. Nothing to put. */
2009-12-20 21:41:50 +01:00
2012-02-13 16:28:04 +01:00
bp->b_count--; /* Compensate for above 'get_block'. */
2009-12-20 21:41:50 +01:00
if (--bp->b_count > 0) return;
/* Cut bp out of the loop */
if (bp->b_prev == NULL)
2012-02-13 16:28:04 +01:00
front = bp->b_next;
2009-12-20 21:41:50 +01:00
else
2012-02-13 16:28:04 +01:00
bp->b_prev->b_next = bp->b_next;
2009-12-20 21:41:50 +01:00
if (bp->b_next == NULL)
2012-02-13 16:28:04 +01:00
rear = bp->b_prev;
2009-12-20 21:41:50 +01:00
else
2012-02-13 16:28:04 +01:00
bp->b_next->b_prev = bp->b_prev;
2009-12-20 21:41:50 +01:00
/* Buffer administration is done. Now it's safe to free up bp. */
free(bp);
}