don't check when its clang masquerading as gcc
[AROS.git] / workbench / fs / pipe / pipebuf.c
blobb43a8ab11c9322794aa270c99411493f8cdfa995
1 /****************************************************************************
2 ** File: pipebuf.c
3 ** Program: pipe-handler - an AmigaDOS handler for named pipes
4 ** Version: 1.1
5 ** Author: Ed Puckett qix@mit-oz
6 **
7 ** Copyright 1987 by EpAc Software. All Rights Reserved.
8 **
9 ** History: 05-Jan-87 Original Version (1.0)
12 #include <libraries/dos.h>
13 #include <libraries/dosextens.h>
14 #include <libraries/filehandler.h>
15 #include <exec/exec.h>
17 #include "pipelists.h"
18 #include "pipename.h"
19 #include "pipebuf.h"
20 #include "pipecreate.h"
21 #include "pipesched.h"
22 #include "pipe-handler.h"
26 /*---------------------------------------------------------------------------
27 ** pipebuf.c
28 ** ---------
29 ** This module contains functions that manage the circular buffer for pipes.
31 ** Visible Functions
32 ** -----------------
33 ** PIPEBUF *AllocPipebuf (len)
34 ** ULONG MoveFromPipebuf (pb, dest, amt)
35 ** ULONG MoveToPipebuf (pb, src, amt)
37 ** Macros (in pipebuf.h)
38 ** ---------------------
39 ** PipebufEmpty (pb)
40 ** PipebufFull (pb)
41 ** FreePipebuf (pb)
43 ** Local Functions
44 ** ---------------
45 ** - none -
50 /*---------------------------------------------------------------------------
51 ** AllocPipebuf() returns a pointer to a new PIPEBUF structure if there is
52 ** enough free memory to allocate one with the requested ("len") storage.
53 ** The structure is iinitialized as empty. Notice that the buffer storage
54 ** area is the tail part of the structure.
57 PIPEBUF *AllocPipebuf (len)
59 ULONG len;
61 { PIPEBUF *pb = NULL;
64 if ( (len > 0) && (len <= MAX_PIPELEN) &&
65 ((pb= (PIPEBUF *) AllocMem (sizeof (PIPEBUF) - 1 + len, ALLOCMEM_FLAGS)) != NULL) )
66 { pb->head= pb->tail= 0;
67 pb->full= FALSE;
68 pb->len= len;
71 return pb;
76 /*---------------------------------------------------------------------------
77 ** Move bytes from the PIPEBUF to the memory pointed to by "dest". At most
78 ** "amt" bytes are moved. The actual number moved is returned.
81 ULONG MoveFromPipebuf (pb, dest, amt)
83 PIPEBUF *pb;
84 register BYTE *dest;
85 ULONG amt;
87 { register BYTE *src;
88 register LONG ct;
89 ULONG amtleft;
92 if ((amt <= 0) || PipebufEmpty (pb))
93 return 0L;
95 amtleft= amt;
96 src= pb->buf + pb->tail;
98 if (pb->tail >= pb->head) /* then have to wrap around */
99 { if ((ct= (pb->len - pb->tail)) > amtleft)
100 ct= amtleft; /* more than needed in end of pipebuf */
102 CopyMem (src, dest, ct);
103 pb->tail= (pb->tail + ct) % pb->len;
104 amtleft -= ct;
106 src= pb->buf + pb->tail;
107 dest += ct;
110 if ( (amtleft > 0) && (ct= (pb->head - pb->tail)) )
111 { if (ct > amtleft)
112 ct= amtleft; /* more than needed */
114 CopyMem (src, dest, ct);
115 pb->tail += ct; /* no need to mod */
116 amtleft -= ct;
119 pb->full= FALSE; /* has to be: nonzero amt */
121 return (amt - amtleft);
126 /*---------------------------------------------------------------------------
127 ** Move bytes to the PIPEBUF from the memory pointed to by "src". At most
128 ** "amt" bytes are moved. The actual number moved is returned.
131 ULONG MoveToPipebuf (pb, src, amt)
133 PIPEBUF *pb;
134 register BYTE *src;
135 ULONG amt;
137 { register BYTE *dest;
138 register LONG ct;
139 ULONG amtleft;
142 if ((amt <= 0) || PipebufFull (pb))
143 return 0L;
145 amtleft= amt;
146 dest= pb->buf + pb->head;
148 if (pb->head >= pb->tail) /* then have to wrap around */
149 { if ((ct= (pb->len - pb->head)) > amtleft)
150 ct= amtleft; /* more than will fit in end of pipebuf */
152 CopyMem (src, dest, ct);
153 pb->head= (pb->head + ct) % pb->len;
154 amtleft -= ct;
156 src += ct;
157 dest= pb->buf + pb->head;
160 if ( (amtleft > 0) && (ct= (pb->tail - pb->head)) )
161 { if (ct > amtleft)
162 ct= amtleft; /* more than will fit */
164 CopyMem (src, dest, ct);
165 pb->head += ct; /* no need to mod */
166 amtleft -= ct;
169 pb->full= (pb->head == pb->tail);
171 return (amt - amtleft);