home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / bsd_srcs / contrib / usr.x25 / nimd / buf.c next >
Encoding:
C/C++ Source or Header  |  1988-04-13  |  2.5 KB  |  155 lines

  1. /*
  2.  * Miscellanious buffer management routines
  3.  *
  4.  * Frank Pronk
  5.  * The University of British Columbia
  6.  * Laboratory for Computational Vision
  7.  * Copyright (c)
  8.  */
  9.  
  10. #include "buf.h"
  11.  
  12. char    *malloc();
  13.  
  14. /*
  15.  * return a buffer that contains room
  16.  * for 'size' bytes of data.
  17.  */
  18.  
  19. struct buf *
  20. getbuf (size)
  21. {
  22.     register struct buf *bp;
  23.  
  24.     if ((bp = (struct buf *)malloc (size + sizeof(char *)*4)) == 0)
  25.         abort ();
  26.     bp->b_next = bp->b_prev = bp;
  27.     bp->b_top = bp->b_bot = bp->b_data;
  28.     return (bp);
  29. }
  30.  
  31. /*
  32.  * Initialize a buffer queue by
  33.  * freeing all buffers on the queue
  34.  * and resetting the queue to its
  35.  * initial state.
  36.  */
  37.  
  38. InitQueue (qp)
  39. register struct bufhd *qp;
  40. {
  41.     register struct buf *bp;
  42.  
  43.     if (qp->b_next)
  44.         while (!QEMPTY (qp)) {
  45.             dequeue (bp = qp->b_next, qp);
  46.             free ((char *)bp);
  47.         }
  48.     qp->b_prev = qp->b_next = (struct buf *)qp;
  49.     qp->b_count = 0;
  50. }
  51.  
  52. /*
  53.  * Allocate a buffer and try to read 'size' bytes from 'fd'.
  54.  * Return zero on error; otherwise return a pointer to the buffer.
  55.  */
  56.  
  57. struct buf *
  58. FillBuf(fd, size, who)
  59. char *who;
  60. {
  61.     register int n;
  62.     register struct buf *bp;
  63.  
  64.     bp = getbuf (size);
  65.     if ((n = read (fd, bp->b_bot, size)) <= 0) {
  66.         free ((char *)bp);
  67.         return (0);
  68.     }
  69.     bp->b_top += n;
  70.     NimTrace (who, bp->b_bot, n);
  71.     return (bp);
  72. }
  73.  
  74. /*
  75.  * Try to flush the queue of buffers headed by 'qp'
  76.  * by writing to 'fd'.  Returns non-zero on error.
  77.  */
  78.  
  79. FlushQueue (fd, qp, who)
  80. register struct bufhd *qp;
  81. char *who;
  82. {
  83.     register int cc;
  84.     register struct buf *bp;
  85.  
  86.     while (!QEMPTY(qp)) {
  87.         bp = qp->b_next;
  88.         if ((cc = write (fd, bp->b_bot, SIZE (bp))) < 0)
  89.             return (-1);
  90.         NimTrace (who, bp->b_bot, cc);
  91.         bp->b_bot += cc;
  92.         qp->b_count -= cc;
  93.         if (ISEMPTY(bp)) {
  94.             dequeue (bp, qp);
  95.             free ((char *)bp);
  96.         }
  97.     }
  98.     return (0);
  99. }
  100.  
  101. /*
  102.  * Copy string 's' to buffer 'bp'.
  103.  */
  104.  
  105. StrToBuf(s, bp)
  106. register char *s;
  107. register struct buf *bp;
  108. {
  109.     while(*s)
  110.         PUTCHAR (*s++, bp);
  111. }
  112.  
  113. /*
  114.  * Copy the contents of buffer 'from' to 'to'.
  115.  */
  116.  
  117. BufCopy(from, to)
  118. struct buf *from, *to;
  119. {
  120.     register int l = SIZE (from);
  121.  
  122.     bcopy (from->b_bot, to->b_top, l);
  123.     to->b_top += l;
  124. }
  125.  
  126. /*
  127.  * place buffer 'bp' on the end of
  128.  * the queue headed by 'qp'.
  129.  */
  130.  
  131. enqueue (bp, qp)
  132. register struct buf *bp;
  133. register struct bufhd *qp;
  134. {
  135.     qp->b_prev->b_next = bp;
  136.     bp->b_prev = qp->b_prev;
  137.     qp->b_prev = bp;
  138.     bp->b_next = (struct buf *)qp;
  139.     qp->b_count += SIZE (bp);
  140. }
  141.  
  142. /*
  143.  * remove buffer 'bp' from the
  144.  * queue headed by 'qp'.
  145.  */
  146.  
  147. dequeue (bp, qp)
  148. register struct buf *bp;
  149. register struct bufhd *qp;
  150. {
  151.     bp->b_prev->b_next = bp->b_next;
  152.     bp->b_next->b_prev = bp->b_prev;
  153.     qp->b_count -= SIZE (bp);
  154. }
  155.