Category : Files from Magazines
Archive   : PJ86.ZIP
Filename : BUFFER.C

 
Output of file : BUFFER.C contained in archive : PJ86.ZIP
else
{
ele->qprev->qnext = ele->qnext;
ele->qnext->qprev = ele->qprev;
}
return usr_space;
}
/*-------------------------------------------------------------------*/
walkqueue( queue *q, int forward, void (*funct)(), void *arg )
{
_queue_ele *p;
p = forward ? q->head : q->tail ;
for( ; p ; p = forward ? p->qnext : p->qprev )
(*funct)( p + 1, arg );
}

/* LISTING 4: Virt.c<197>buffer management */

#include
#include

#define BUFFERS 40 /* Number of buffers. */
static buffer *Free_bufs = NULL; /* Available buffers. */
static queue *Active = NULL; /* Queue of active buffers. Least */
/* recently used buffer at head. */
/*-----------------------------------------------------------------*/
int init_freelist() /* Allocate all the buffers and */
{ /* put them into the free list. */
buffer *b; /* Return false on failure. */
int i;

for( i = BUFFERS; --i >= 0; )
{
if( !(b = (buffer *) q_malloc(sizeof(buffer)) ))
return 0;
b->next = Free_bufs; /* add new buffer to head of list */
Free_bufs = b;
}

return 1;
}
/*-----------------------------------------------------------------*/
buffer *get_buffer( vmem *obj )
{
/* Get a new buffer for the virtual-memory manager and return a */
/* pointer to it. Note that a stolen buffer is removed from */
/* the queue. The calling function should update the queue with */
/* a call to touch_buffer(). get_buffer() initializes the */
/* Active queue and freelist the first time it's called. */

buffer *b;

if( !Active && !( (Active=newqueue()) && init_freelist() ))
{
raise( SIGABRT ); /* Try to abort the program. */
return NULL; /* If SIGABRT returns, return NULL */
}
if( Free_bufs ) /* If there's a buffer in */
{ /* the free list, use it; */
b = Free_bufs;
b->dirty = 0; /* suppress write before read */
Free_bufs = b->next ;
}
else /* otherwise, steal one */
{ /* from an object. */
b = dequeue( Active ); /* Get least-used buf.*/
if( *(b->prev) = b->next ) /* remove it from */
b->next->prev = b->prev; /* the object. */
}
return b;
}
/*-----------------------------------------------------------------*/
touch_buffer( buffer *bp, int in_queue )
{
if( !in_queue )
enqueue( Active, bp );
else if( tail(Active) != bp ) /* if not already at tail */
enqueue( Active, qremove( Active, bp ) ); /* move to end */
}
/*-----------------------------------------------------------------*/
void release_buffer( buffer *bp ) /* release the buffer */
{
/* It's an error if *bp is already in the free list. It must */
/* be an active buffer, enqueued in the Active queue, and not */
/* in the free list. */

qremove( Active, bp ); /* Pull it out of the active list */
bp->next = Free_bufs; /* and move it to the free list */
Free_bufs = bp;
}



  3 Responses to “Category : Files from Magazines
Archive   : PJ86.ZIP
Filename : BUFFER.C

  1. Very nice! Thank you for this wonderful archive. I wonder why I found it only now. Long live the BBS file archives!

  2. This is so awesome! 😀 I’d be cool if you could download an entire archive of this at once, though.

  3. But one thing that puzzles me is the “mtswslnkmcjklsdlsbdmMICROSOFT” string. There is an article about it here. It is definitely worth a read: http://www.os2museum.com/wp/mtswslnk/