/*****************************************************************************

 @(#) sdl_bufq.h,v 0.7 2000/12/02 13:04:23 brian Exp

 -----------------------------------------------------------------------------

     Copyright (C) 2000  Brian Bidulock.  All Rights Reserved.


                                  PUBLIC LICENSE

     This license is provided without fee, provided that the above copy-
     right notice and this public license must be retained on all copies,
     extracts, compilations and derivative works.  Use or distribution of
     this work in a manner that restricts its use except as provided here
     will render this license void.

     The author(s) hereby waive any and all other restrictions in respect
     of their copyright in this software and its associated documentation.
     The authors(s) of this software place in the public domain any novel
     methods or processes which are embodied in this software.

     The author(s) undertook to write it for the sake of the advancement
     of the Arts and Sciences, but it is provided as is, and the author(s)
     will not take any responsibility in it.

 -----------------------------------------------------------------------------

 Last Modified 2000/12/02 13:04:23 by brian

 sdl_bufq.h,v
 Revision 0.7  2000/12/02 13:04:23  brian
 Extracted buffer queue handling.

 *****************************************************************************/

#ident @(#) sdl_bufq.h,v 0.7 2000/12/02 13:04:23 brian Exp

#ifndef __SDLI_BUFQ_H__
#define __SDLI_BUFQ_H__

typedef struct bufq {
    spinlock_t  lock;
    mblk_t      *head;
    mblk_t      **tail;
    mblk_t      count;
    mblk_t      size;
} bufq_t;

static __inline void bufq_init(bufq_t *q)
{
    spin_lock_init(&q->lock);
    q->head = NULL;
    q->tail = &q->head;
    q->count = 0;
    q->size = 0;
}

static __inline int bufq_length(bufq_t *q)
{
    return q->count;
}

static __inline int bufq_size(bufq_t *q)
{
    return q->size;
}

static __inline void __bufq_queue(bufq_t *q, mblk_t *mp)
{
    mblk_t *md;
    if ( (md = mp) ) {
        mp->b_next = NULL;
        *(q->tail) = mp;
        q->tail = &mp->b_cont;
        q->count++;
        while ( md ) {
            q->size += md->b_wptr - md->b_rptr;
            md = md->b_cont;
        }
    }
}

static __inline mblk_t *bufq_dequeue(bufq_t *q)
{
    mblk_t *mp, md;
    if ( (mp = md = q->head) ) {
        q->head = q->head->b_next;
        q->count--;
        while ( md ) {
            q->size -= md->b_wptr = md->b_rptr;
            md = md->b_cont;
        }
    }
    return mp;
}

static __inline mblk_t *bufq_supply(bufq_t *q, mblk_t *mp)
{
    mblk_t *md = mp;
    while ( md ) {
        md->b_datap->db_type = M_DATA;
        md->b_rptr = md->b_wptr = md->b_datap->db_base;
        bufq_queue(q, md);
        md = unlinkb(md);
    }
}

static __inline mblk_t *bufq_resupply(bufq_t *q, mblk_t *mp, int maxsize, int maxcount)
{
    if ( bufq_length(q) > maxcount || bufq_size(q) > maxsize )
        return mp;
    bufq_supply(q, mp);
    return NULL;
}

static __inline mblk_t *bufq_purge(bufq_t *q)
{
    while ( q->count ) freemsg(bufq_dequeue(q));
}




#endif  __SDLI_BUFQ_H__
