home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / X / mit / server / os / io.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-07-03  |  19.0 KB  |  772 lines

  1. /***********************************************************
  2. Copyright 1987, 1989 by Digital Equipment Corporation, Maynard, Massachusetts,
  3. and the Massachusetts Institute of Technology, Cambridge, Massachusetts.
  4.  
  5.                         All Rights Reserved
  6.  
  7. Permission to use, copy, modify, and distribute this software and its 
  8. documentation for any purpose and without fee is hereby granted, 
  9. provided that the above copyright notice appear in all copies and that
  10. both that copyright notice and this permission notice appear in 
  11. supporting documentation, and that the names of Digital or MIT not be
  12. used in advertising or publicity pertaining to distribution of the
  13. software without specific, written prior permission.  
  14.  
  15. DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
  16. ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
  17. DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
  18. ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  19. WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
  20. ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  21. SOFTWARE.
  22.  
  23. ******************************************************************/
  24. /* $XConsortium: io.c,v 1.68 91/03/29 09:34:07 rws Exp $ */
  25. /*****************************************************************
  26.  * i/o functions
  27.  *
  28.  *   WriteToClient, ReadRequestFromClient
  29.  *   InsertFakeRequest, ResetCurrentRequest
  30.  *
  31.  *****************************************************************/
  32.  
  33. #include <stdio.h>
  34. #include "Xos.h"
  35. #include "Xmd.h"
  36. #include <errno.h>
  37. #include <sys/param.h>
  38. #include <sys/uio.h>
  39. #include "X.h"
  40. #include "Xproto.h"
  41. #include "os.h"
  42. #include "osdep.h"
  43. #include "opaque.h"
  44. #include "dixstruct.h"
  45. #include "misc.h"
  46.  
  47. /* check for both EAGAIN and EWOULDBLOCK, because some supposedly POSIX
  48.  * systems are broken and return EWOULDBLOCK when they should return EAGAIN
  49.  */
  50. #if defined(EAGAIN) && defined(EWOULDBLOCK)
  51. #define ETEST(err) (err == EAGAIN || err == EWOULDBLOCK)
  52. #else
  53. #ifdef EAGAIN
  54. #define ETEST(err) (err == EAGAIN)
  55. #else
  56. #define ETEST(err) (err == EWOULDBLOCK)
  57. #endif
  58. #endif
  59.  
  60. extern void MarkClientException();
  61. extern long ClientsWithInput[];
  62. extern long ClientsWriteBlocked[];
  63. extern long OutputPending[];
  64. extern int ConnectionTranslation[];
  65. extern Bool NewOutputPending;
  66. extern Bool AnyClientsWriteBlocked;
  67. static Bool CriticalOutputPending;
  68. static int timesThisConnection = 0;
  69. static ConnectionInputPtr FreeInputs = (ConnectionInputPtr)NULL;
  70. static ConnectionOutputPtr FreeOutputs = (ConnectionOutputPtr)NULL;
  71. static OsCommPtr AvailableInput = (OsCommPtr)NULL;
  72.  
  73. static ConnectionInputPtr AllocateInputBuffer();
  74. static ConnectionOutputPtr AllocateOutputBuffer();
  75.  
  76. extern int errno;
  77.  
  78. #define request_length(req, cli) ((cli->swapped ? \
  79.     lswaps((req)->length) : (req)->length) << 2)
  80. #define MAX_TIMES_PER         10
  81.  
  82. /*****************************************************************
  83.  * ReadRequestFromClient
  84.  *    Returns one request in client->requestBuffer.  Return status is:
  85.  *
  86.  *    > 0  if  successful, specifies length in bytes of the request
  87.  *    = 0  if  entire request is not yet available
  88.  *    < 0  if  client should be terminated
  89.  *
  90.  *    The request returned must be contiguous so that it can be
  91.  *    cast in the dispatcher to the correct request type.  Because requests
  92.  *    are variable length, ReadRequestFromClient() must look at the first 4
  93.  *    bytes of a request to determine the length (the request length is
  94.  *    always the 3rd and 4th bytes of the request).  
  95.  *
  96.  *    Note: in order to make the server scheduler (WaitForSomething())
  97.  *    "fair", the ClientsWithInput mask is used.  This mask tells which
  98.  *    clients have FULL requests left in their buffers.  Clients with
  99.  *    partial requests require a read.  Basically, client buffers
  100.  *    are drained before select() is called again.  But, we can't keep
  101.  *    reading from a client that is sending buckets of data (or has
  102.  *    a partial request) because others clients need to be scheduled.
  103.  *****************************************************************/
  104.  
  105. #define YieldControl()                \
  106.         { isItTimeToYield = TRUE;        \
  107.       timesThisConnection = 0; }
  108. #define YieldControlNoInput()            \
  109.         { YieldControl();            \
  110.       BITCLEAR(ClientsWithInput, fd); }
  111. #define YieldControlDeath()            \
  112.         { timesThisConnection = 0; }
  113.  
  114. int
  115. ReadRequestFromClient(client)
  116.     ClientPtr client;
  117. {
  118.     OsCommPtr oc = (OsCommPtr)client->osPrivate;
  119.     register ConnectionInputPtr oci = oc->input;
  120.     int fd = oc->fd;
  121.     register int gotnow, needed;
  122.     int result;
  123.     register xReq *request;
  124.  
  125.     if (AvailableInput)
  126.     {
  127.     if (AvailableInput != oc)
  128.     {
  129.         register ConnectionInputPtr aci = AvailableInput->input;
  130.         if (aci->size > BUFWATERMARK)
  131.         {
  132.         xfree(aci->buffer);
  133.         xfree(aci);
  134.         }
  135.         else
  136.         {
  137.         aci->next = FreeInputs;
  138.         FreeInputs = aci;
  139.         }
  140.         AvailableInput->input = (ConnectionInputPtr)NULL;
  141.     }
  142.     AvailableInput = (OsCommPtr)NULL;
  143.     }
  144.     if (!oci)
  145.     {
  146.     if (oci = FreeInputs)
  147.     {
  148.         FreeInputs = oci->next;
  149.     }
  150.     else if (!(oci = AllocateInputBuffer()))
  151.     {
  152.         YieldControlDeath();
  153.         return -1;
  154.     }
  155.     oc->input = oci;
  156.     }
  157.     oci->bufptr += oci->lenLastReq;
  158.  
  159.     request = (xReq *)oci->bufptr;
  160.     gotnow = oci->bufcnt + oci->buffer - oci->bufptr;
  161.     if ((gotnow < sizeof(xReq)) ||
  162.     (gotnow < (needed = request_length(request, client))))
  163.     {
  164.     oci->lenLastReq = 0;
  165.     if ((gotnow < sizeof(xReq)) || (needed == 0))
  166.        needed = sizeof(xReq);
  167.     else if (needed > MAXBUFSIZE)
  168.     {
  169.         YieldControlDeath();
  170.         return -1;
  171.     }
  172.     if ((gotnow == 0) ||
  173.         ((oci->bufptr - oci->buffer + needed) > oci->size))
  174.     {
  175.         if ((gotnow > 0) && (oci->bufptr != oci->buffer))
  176.         bcopy(oci->bufptr, oci->buffer, gotnow);
  177.         if (needed > oci->size)
  178.         {
  179.         char *ibuf;
  180.  
  181.         ibuf = (char *)xrealloc(oci->buffer, needed);
  182.         if (!ibuf)
  183.         {
  184.             YieldControlDeath();
  185.             return -1;
  186.         }
  187.         oci->size = needed;
  188.         oci->buffer = ibuf;
  189.         }
  190.         oci->bufptr = oci->buffer;
  191.         oci->bufcnt = gotnow;
  192.     }
  193.     result = read(fd, oci->buffer + oci->bufcnt, 
  194.               oci->size - oci->bufcnt); 
  195.     if (result <= 0)
  196.     {
  197.         if ((result < 0) && ETEST(errno))
  198.         {
  199.         YieldControlNoInput();
  200.         return 0;
  201.         }
  202.         YieldControlDeath();
  203.         return -1;
  204.     }
  205.     oci->bufcnt += result;
  206.     gotnow += result;
  207.     /* free up some space after huge requests */
  208.     if ((oci->size > BUFWATERMARK) &&
  209.         (oci->bufcnt < BUFSIZE) && (needed < BUFSIZE))
  210.     {
  211.         char *ibuf;
  212.  
  213.         ibuf = (char *)xrealloc(oci->buffer, BUFSIZE);
  214.         if (ibuf)
  215.         {
  216.         oci->size = BUFSIZE;
  217.         oci->buffer = ibuf;
  218.         oci->bufptr = ibuf + oci->bufcnt - gotnow;
  219.         }
  220.     }
  221.     request = (xReq *)oci->bufptr;
  222.     if ((gotnow < sizeof(xReq)) ||
  223.         (gotnow < (needed = request_length(request, client))))
  224.     {
  225.         YieldControlNoInput();
  226.         return 0;
  227.     }
  228.     }
  229.     if (needed == 0)
  230.     needed = sizeof(xReq);
  231.     oci->lenLastReq = needed;
  232.  
  233.     /*
  234.      *  Check to see if client has at least one whole request in the
  235.      *  buffer.  If there is only a partial request, treat like buffer
  236.      *  is empty so that select() will be called again and other clients
  237.      *  can get into the queue.   
  238.      */
  239.  
  240.     if (gotnow >= needed + sizeof(xReq)) 
  241.     {
  242.     request = (xReq *)(oci->bufptr + needed);
  243.         if (gotnow >= needed + request_length(request, client))
  244.         BITSET(ClientsWithInput, fd);
  245.         else
  246.         YieldControlNoInput();
  247.     }
  248.     else
  249.     {
  250.     if (gotnow == needed)
  251.         AvailableInput = oc;
  252.     YieldControlNoInput();
  253.     }
  254.     if (++timesThisConnection >= MAX_TIMES_PER)
  255.     YieldControl();
  256.  
  257.     client->requestBuffer = (pointer)oci->bufptr;
  258.     return needed;
  259. }
  260.  
  261. /*****************************************************************
  262.  * InsertFakeRequest
  263.  *    Splice a consed up (possibly partial) request in as the next request.
  264.  *
  265.  **********************/
  266.  
  267. Bool
  268. InsertFakeRequest(client, data, count)
  269.     ClientPtr client;
  270.     char *data;
  271.     int count;
  272. {
  273.     OsCommPtr oc = (OsCommPtr)client->osPrivate;
  274.     register ConnectionInputPtr oci = oc->input;
  275.     int fd = oc->fd;
  276.     register xReq *request;
  277.     register int gotnow, moveup;
  278.  
  279.     if (AvailableInput)
  280.     {
  281.     if (AvailableInput != oc)
  282.     {
  283.         register ConnectionInputPtr aci = AvailableInput->input;
  284.         if (aci->size > BUFWATERMARK)
  285.         {
  286.         xfree(aci->buffer);
  287.         xfree(aci);
  288.         }
  289.         else
  290.         {
  291.         aci->next = FreeInputs;
  292.         FreeInputs = aci;
  293.         }
  294.         AvailableInput->input = (ConnectionInputPtr)NULL;
  295.     }
  296.     AvailableInput = (OsCommPtr)NULL;
  297.     }
  298.     if (!oci)
  299.     {
  300.     if (oci = FreeInputs)
  301.         FreeInputs = oci->next;
  302.     else if (!(oci = AllocateInputBuffer()))
  303.         return FALSE;
  304.     oc->input = oci;
  305.     }
  306.     oci->bufptr += oci->lenLastReq;
  307.     oci->lenLastReq = 0;
  308.     gotnow = oci->bufcnt + oci->buffer - oci->bufptr;
  309.     if ((gotnow + count) > oci->size)
  310.     {
  311.     char *ibuf;
  312.  
  313.     ibuf = (char *)xrealloc(oci->buffer, gotnow + count);
  314.     if (!ibuf)
  315.         return(FALSE);
  316.     oci->size = gotnow + count;
  317.     oci->buffer = ibuf;
  318.     oci->bufptr = ibuf + oci->bufcnt - gotnow;
  319.     }
  320.     moveup = count - (oci->bufptr - oci->buffer);
  321.     if (moveup > 0)
  322.     {
  323.     if (gotnow > 0)
  324.         bcopy(oci->bufptr, oci->bufptr + moveup, gotnow);
  325.     oci->bufptr += moveup;
  326.     oci->bufcnt += moveup;
  327.     }
  328.     bcopy(data, oci->bufptr - count, count);
  329.     oci->bufptr -= count;
  330.     request = (xReq *)oci->bufptr;
  331.     gotnow += count;
  332.     if ((gotnow >= sizeof(xReq)) &&
  333.     (gotnow >= request_length(request, client)))
  334.     BITSET(ClientsWithInput, fd);
  335.     else
  336.     YieldControlNoInput();
  337.     return(TRUE);
  338. }
  339.  
  340. /*****************************************************************
  341.  * ResetRequestFromClient
  342.  *    Reset to reexecute the current request, and yield.
  343.  *
  344.  **********************/
  345.  
  346. ResetCurrentRequest(client)
  347.     ClientPtr client;
  348. {
  349.     OsCommPtr oc = (OsCommPtr)client->osPrivate;
  350.     register ConnectionInputPtr oci = oc->input;
  351.     int fd = oc->fd;
  352.     register xReq *request;
  353.     int gotnow;
  354.  
  355.     if (AvailableInput == oc)
  356.     AvailableInput = (OsCommPtr)NULL;
  357.     oci->lenLastReq = 0;
  358.     request = (xReq *)oci->bufptr;
  359.     gotnow = oci->bufcnt + oci->buffer - oci->bufptr;
  360.     if ((gotnow >= sizeof(xReq)) &&
  361.     (gotnow >= request_length(request, client)))
  362.     {
  363.     BITSET(ClientsWithInput, fd);
  364.     YieldControl();
  365.     }
  366.     else
  367.     YieldControlNoInput();
  368. }
  369.  
  370.     /* lookup table for adding padding bytes to data that is read from
  371.         or written to the X socket.  */
  372. static int padlength[4] = {0, 3, 2, 1};
  373.  
  374.  /********************
  375.  * FlushClient()
  376.  *    If the client isn't keeping up with us, then we try to continue
  377.  *    buffering the data and set the apropriate bit in ClientsWritable
  378.  *    (which is used by WaitFor in the select).  If the connection yields
  379.  *    a permanent error, or we can't allocate any more space, we then
  380.  *    close the connection.
  381.  *
  382.  **********************/
  383.  
  384. int
  385. FlushClient(who, oc, extraBuf, extraCount)
  386.     ClientPtr who;
  387.     OsCommPtr oc;
  388.     char *extraBuf;
  389.     int extraCount; /* do not modify... returned below */
  390. {
  391.     register ConnectionOutputPtr oco = oc->output;
  392.     int connection = oc->fd;
  393.     struct iovec iov[3];
  394.     char padBuffer[3];
  395.     long written;
  396.     long padsize;
  397.     long notWritten;
  398.     long todo;
  399.  
  400.     if (!oco)
  401.     return 0;
  402.     written = 0;
  403.     padsize = padlength[extraCount & 3];
  404.     notWritten = oco->count + extraCount + padsize;
  405.     todo = notWritten;
  406.     while (notWritten) {
  407.     long before = written;    /* amount of whole thing written */
  408.     long remain = todo;    /* amount to try this time, <= notWritten */
  409.     int i = 0;
  410.     long len;
  411.  
  412.     /* You could be very general here and have "in" and "out" iovecs
  413.      * and write a loop without using a macro, but what the heck.  This
  414.      * translates to:
  415.      *
  416.      *     how much of this piece is new?
  417.      *     if more new then we are trying this time, clamp
  418.      *     if nothing new
  419.      *         then bump down amount already written, for next piece
  420.      *         else put new stuff in iovec, will need all of next piece
  421.      *
  422.      * Note that todo had better be at least 1 or else we'll end up
  423.      * writing 0 iovecs.
  424.      */
  425. #define InsertIOV(pointer, length) \
  426.     len = (length) - before; \
  427.     if (len > remain) \
  428.         len = remain; \
  429.     if (len <= 0) { \
  430.         before = (-len); \
  431.     } else { \
  432.         iov[i].iov_len = len; \
  433.         iov[i].iov_base = (pointer) + before; \
  434.         i++; \
  435.         remain -= len; \
  436.         before = 0; \
  437.     }
  438.  
  439.     InsertIOV ((char *)oco->buf, oco->count)
  440.     InsertIOV (extraBuf, extraCount)
  441.     InsertIOV (padBuffer, padsize)
  442.  
  443.     errno = 0;
  444.     if ((len = writev(connection, iov, i)) >= 0)
  445.     {
  446.         written += len;
  447.         notWritten -= len;
  448.         todo = notWritten;
  449.     }
  450.     else if (ETEST(errno)
  451. #ifdef SUNSYSV /* check for another brain-damaged OS bug */
  452.          || (errno == 0)
  453. #endif
  454. #ifdef EMSGSIZE /* check for another brain-damaged OS bug */
  455.          || ((errno == EMSGSIZE) && (todo == 1))
  456. #endif
  457.         )
  458.     {
  459.         /* If we've arrived here, then the client is stuffed to the gills
  460.            and not ready to accept more.  Make a note of it and buffer
  461.            the rest. */
  462.         BITSET(ClientsWriteBlocked, connection);
  463.         AnyClientsWriteBlocked = TRUE;
  464.  
  465.         if (written < oco->count)
  466.         {
  467.         if (written > 0)
  468.         {
  469.             oco->count -= written;
  470.             bcopy((char *)oco->buf + written,
  471.               (char *)oco->buf,
  472.               oco->count);
  473.             written = 0;
  474.         }
  475.         }
  476.         else
  477.         {
  478.         written -= oco->count;
  479.         oco->count = 0;
  480.         }
  481.  
  482.         if (notWritten > oco->size)
  483.         {
  484.         unsigned char *obuf;
  485.  
  486.         obuf = (unsigned char *)xrealloc(oco->buf,
  487.                          notWritten + BUFSIZE);
  488.         if (!obuf)
  489.         {
  490.             close(connection);
  491.             MarkClientException(who);
  492.             oco->count = 0;
  493.             return(-1);
  494.         }
  495.         oco->size = notWritten + BUFSIZE;
  496.         oco->buf = obuf;
  497.         }
  498.  
  499.         /* If the amount written extended into the padBuffer, then the
  500.            difference "extraCount - written" may be less than 0 */
  501.         if ((len = extraCount - written) > 0)
  502.         bcopy (extraBuf + written,
  503.                (char *)oco->buf + oco->count,
  504.                len);
  505.  
  506.         oco->count = notWritten; /* this will include the pad */
  507.         /* return only the amount explicitly requested */
  508.         return extraCount;
  509.     }
  510. #ifdef EMSGSIZE /* check for another brain-damaged OS bug */
  511.     else if (errno == EMSGSIZE)
  512.     {
  513.         todo >>= 1;
  514.     }
  515. #endif
  516.     else
  517.     {
  518.         close(connection);
  519.         MarkClientException(who);
  520.         oco->count = 0;
  521.         return(-1);
  522.     }
  523.     }
  524.  
  525.     /* everything was flushed out */
  526.     oco->count = 0;
  527.     /* check to see if this client was write blocked */
  528.     if (AnyClientsWriteBlocked)
  529.     {
  530.     BITCLEAR(ClientsWriteBlocked, oc->fd);
  531.      if (! ANYSET(ClientsWriteBlocked))
  532.         AnyClientsWriteBlocked = FALSE;
  533.     }
  534.     if (oco->size > BUFWATERMARK)
  535.     {
  536.     xfree(oco->buf);
  537.     xfree(oco);
  538.     }
  539.     else
  540.     {
  541.     oco->next = FreeOutputs;
  542.     FreeOutputs = oco;
  543.     }
  544.     oc->output = (ConnectionOutputPtr)NULL;
  545.     return extraCount; /* return only the amount explicitly requested */
  546. }
  547.  
  548.  /********************
  549.  * FlushAllOutput()
  550.  *    Flush all clients with output.  However, if some client still
  551.  *    has input in the queue (more requests), then don't flush.  This
  552.  *    will prevent the output queue from being flushed every time around
  553.  *    the round robin queue.  Now, some say that it SHOULD be flushed
  554.  *    every time around, but...
  555.  *
  556.  **********************/
  557.  
  558. void
  559. FlushAllOutput()
  560. {
  561.     register int index, base, mask;
  562.     OsCommPtr oc;
  563.     register ClientPtr client;
  564.  
  565.     if (! NewOutputPending)
  566.     return;
  567.  
  568.     /*
  569.      * It may be that some client still has critical output pending,
  570.      * but he is not yet ready to receive it anyway, so we will
  571.      * simply wait for the select to tell us when he's ready to receive.
  572.      */
  573.     CriticalOutputPending = FALSE;
  574.     NewOutputPending = FALSE;
  575.  
  576.     for (base = 0; base < mskcnt; base++)
  577.     {
  578.     mask = OutputPending[ base ];
  579.     OutputPending[ base ] = 0;
  580.     while (mask)
  581.     {
  582.         index = ffs(mask) - 1;
  583.         mask &= ~lowbit(mask);
  584.         if ((index = ConnectionTranslation[(base << 5) + index]) == 0)
  585.         continue;
  586.         client = clients[index];
  587.         if (client->clientGone)
  588.         continue;
  589.         oc = (OsCommPtr)client->osPrivate;
  590.         if (GETBIT(ClientsWithInput, oc->fd))
  591.         {
  592.         BITSET(OutputPending, oc->fd); /* set the bit again */
  593.         NewOutputPending = TRUE;
  594.         }
  595.         else
  596.         (void)FlushClient(client, oc, (char *)NULL, 0);
  597.     }
  598.     }
  599.  
  600. }
  601.  
  602. void
  603. FlushIfCriticalOutputPending()
  604. {
  605.     if (CriticalOutputPending)
  606.     FlushAllOutput();
  607. }
  608.  
  609. void
  610. SetCriticalOutputPending()
  611. {
  612.     CriticalOutputPending = TRUE;
  613. }
  614.  
  615. /*****************
  616.  * WriteToClient
  617.  *    Copies buf into ClientPtr.buf if it fits (with padding), else
  618.  *    flushes ClientPtr.buf and buf to client.  As of this writing,
  619.  *    every use of WriteToClient is cast to void, and the result
  620.  *    is ignored.  Potentially, this could be used by requests
  621.  *    that are sending several chunks of data and want to break
  622.  *    out of a loop on error.  Thus, we will leave the type of
  623.  *    this routine as int.
  624.  *****************/
  625.  
  626. int
  627. WriteToClient (who, count, buf)
  628.     ClientPtr who;
  629.     char *buf;
  630.     int count;
  631. {
  632.     OsCommPtr oc = (OsCommPtr)who->osPrivate;
  633.     register ConnectionOutputPtr oco = oc->output;
  634.     int padBytes;
  635.  
  636.     if (!count)
  637.     return(0);
  638.  
  639.     if (!oco)
  640.     {
  641.     if (oco = FreeOutputs)
  642.     {
  643.         FreeOutputs = oco->next;
  644.     }
  645.     else if (!(oco = AllocateOutputBuffer()))
  646.     {
  647.         close(oc->fd);
  648.         MarkClientException(who);
  649.         return -1;
  650.     }
  651.     oc->output = oco;
  652.     }
  653.  
  654.     padBytes =  padlength[count & 3];
  655.  
  656.     if (oco->count + count + padBytes > oco->size)
  657.     {
  658.     BITCLEAR(OutputPending, oc->fd);
  659.     CriticalOutputPending = FALSE;
  660.     NewOutputPending = FALSE;
  661.     return FlushClient(who, oc, buf, count);
  662.     }
  663.  
  664.     NewOutputPending = TRUE;
  665.     BITSET(OutputPending, oc->fd);
  666.     bcopy(buf, (char *)oco->buf + oco->count, count);
  667.     oco->count += count + padBytes;
  668.     
  669.     return(count);
  670. }
  671.  
  672. static ConnectionInputPtr
  673. AllocateInputBuffer()
  674. {
  675.     register ConnectionInputPtr oci;
  676.  
  677.     oci = (ConnectionInputPtr)xalloc(sizeof(ConnectionInput));
  678.     if (!oci)
  679.     return (ConnectionInputPtr)NULL;
  680.     oci->buffer = (char *)xalloc(BUFSIZE);
  681.     if (!oci->buffer)
  682.     {
  683.     xfree(oci);
  684.     return (ConnectionInputPtr)NULL;
  685.     }
  686.     oci->size = BUFSIZE;
  687.     oci->bufptr = oci->buffer;
  688.     oci->bufcnt = 0;
  689.     oci->lenLastReq = 0;
  690.     return oci;
  691. }
  692.  
  693. static ConnectionOutputPtr
  694. AllocateOutputBuffer()
  695. {
  696.     register ConnectionOutputPtr oco;
  697.  
  698.     oco = (ConnectionOutputPtr)xalloc(sizeof(ConnectionOutput));
  699.     if (!oco)
  700.     return (ConnectionOutputPtr)NULL;
  701.     oco->buf = (unsigned char *) xalloc(BUFSIZE);
  702.     if (!oco->buf)
  703.     {
  704.     xfree(oco);
  705.     return (ConnectionOutputPtr)NULL;
  706.     }
  707.     oco->size = BUFSIZE;
  708.     oco->count = 0;
  709.     return oco;
  710. }
  711.  
  712. void
  713. FreeOsBuffers(oc)
  714.     OsCommPtr oc;
  715. {
  716.     register ConnectionInputPtr oci;
  717.     register ConnectionOutputPtr oco;
  718.  
  719.     if (AvailableInput == oc)
  720.     AvailableInput = (OsCommPtr)NULL;
  721.     if (oci = oc->input)
  722.     {
  723.     if (FreeInputs)
  724.     {
  725.         xfree(oci->buffer);
  726.         xfree(oci);
  727.     }
  728.     else
  729.     {
  730.         FreeInputs = oci;
  731.         oci->next = (ConnectionInputPtr)NULL;
  732.         oci->bufptr = oci->buffer;
  733.         oci->bufcnt = 0;
  734.         oci->lenLastReq = 0;
  735.     }
  736.     }
  737.     if (oco = oc->output)
  738.     {
  739.     if (FreeOutputs)
  740.     {
  741.         xfree(oco->buf);
  742.         xfree(oco);
  743.     }
  744.     else
  745.     {
  746.         FreeOutputs = oco;
  747.         oco->next = (ConnectionOutputPtr)NULL;
  748.         oco->count = 0;
  749.     }
  750.     }
  751. }
  752.  
  753. void
  754. ResetOsBuffers()
  755. {
  756.     register ConnectionInputPtr oci;
  757.     register ConnectionOutputPtr oco;
  758.  
  759.     while (oci = FreeInputs)
  760.     {
  761.     FreeInputs = oci->next;
  762.     xfree(oci->buffer);
  763.     xfree(oci);
  764.     }
  765.     while (oco = FreeOutputs)
  766.     {
  767.     FreeOutputs = oco->next;
  768.     xfree(oco->buf);
  769.     xfree(oco);
  770.     }
  771. }
  772.