*** empty log message ***
[mmondor.git] / mmsoftware / mmlib / mmfifo.c
CommitLineData
d4aaa170 1/* $Id: mmfifo.c,v 1.4 2004/03/22 06:59:34 mmondor Exp $ */
47071c2b
MM
2
3/*
d4aaa170 4 * Copyright (C) 2000-2004, Matthew Mondor
47071c2b
MM
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
d4aaa170 17 * This product includes software developed by Matthew Mondor.
47071c2b
MM
18 * 4. The name of Matthew Mondor may not be used to endorse or promote
19 * products derived from this software without specific prior written
20 * permission.
d4aaa170
MM
21 * 5. Redistribution of source code may not be released under the terms of
22 * any GNU Public License derivate.
47071c2b
MM
23 *
24 * THIS SOFTWARE IS PROVIDED BY MATTHEW MONDOR ``AS IS'' AND ANY EXPRESS OR
25 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
27 * IN NO EVENT SHALL MATTHEW MONDOR BE LIABLE FOR ANY DIRECT, INDIRECT,
28 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
29 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
30 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
33 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 */
35
36
37
38
39#include <sys/types.h>
40
41#include <mmtypes.h>
42#include <mmstring.h>
43#include <mmfifo.h>
44
45
46
47
d4aaa170 48MMCOPYRIGHT("@(#) Copyright (c) 2000-2004\n\
47071c2b 49\tMatthew Mondor. All rights reserved.\n");
d4aaa170 50MMRCSID("$Id: mmfifo.c,v 1.4 2004/03/22 06:59:34 mmondor Exp $");
47071c2b
MM
51
52
53
54
47071c2b
MM
55/* This function reads all available bytes from the specified FIFO and
56 * copies them in the supplied buffer until there are none to read, or
57 * until max bytes have been transfered. Returns the number of bytes that
58 * were successfully read.
59 */
60size_t
4f87c15b 61getnfifo8(u_int8_t *buffer, fifo8_t *fifo, size_t max)
47071c2b 62{
4f87c15b 63 register u_int8_t *pos;
47071c2b
MM
64 register size_t len, len2, done;
65
66 if (max == 1) {
4f87c15b
MM
67 if (STATFIFO(fifo) > 0) {
68 GETFIFO(fifo, buffer);
47071c2b 69 return (1);
4f87c15b 70 }
47071c2b
MM
71 return (0);
72 }
73
74 len = fifo->elements;
75 if (len > max)
76 len = max;
77
78 if (len) {
79 pos = fifo->tail;
80 len2 = fifo->endbuffer - pos;
81 if (len2 >= len)
82 done = len;
83 else
84 done = len2;
85 mm_memcpy(buffer, pos, done);
86 buffer += done;
87 pos += done;
88 if (pos == fifo->endbuffer)
89 pos = fifo->buffer;
90 len2 -= done;
91 if (len2) {
92 mm_memcpy(buffer, pos, len2);
93 pos += len2;
94 }
95 fifo->tail = pos;
96 fifo->elements -= len;
97 }
98
99 return (len);
100}
101
102
47071c2b
MM
103/* This function inserts size elements into fifo, reading them from buffer.
104 * It returns the number of elements that could be inserted.
105 */
106size_t
4f87c15b 107putnfifo8(fifo8_t *fifo, u_int8_t *buffer, size_t size)
47071c2b 108{
4f87c15b 109 u_int8_t *pos;
47071c2b
MM
110 size_t len, len2, done;
111
112 if (size == 1) {
4f87c15b
MM
113 if (FULLFIFO(fifo))
114 return (0);
115 PUTFIFO(fifo, buffer);
116 return (1);
47071c2b
MM
117 }
118
119 len = size;
120 len2 = fifo->size - fifo->elements;
121 if (len2 > len)
122 len = len2;
123
124 if (len) {
125 pos = fifo->head;
126 len2 = fifo->endbuffer - pos;
127 if (len2 > len)
128 done = len;
129 else
130 done = len2;
131 mm_memcpy(pos, buffer, done);
132 buffer += done;
133 pos += done;
134 if (pos == fifo->endbuffer)
135 pos = fifo->buffer;
136 len2 -= done;
137 if (len2) {
138 mm_memcpy(pos, buffer, len2);
139 pos += len2;
140 }
141 fifo->head = pos;
142 fifo->elements += len;
143 }
144
145 return (len);
146}