mmlib/mmat: replace some variables by literal constants
[mmondor.git] / mmsoftware / mmlib / mmfifo.c
CommitLineData
b232bd02 1/* $Id: mmfifo.c,v 1.5 2007/12/05 23:47:56 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>
b232bd02 40#include <stdint.h>
47071c2b
MM
41
42#include <mmtypes.h>
43#include <mmstring.h>
44#include <mmfifo.h>
45
46
47
48
d4aaa170 49MMCOPYRIGHT("@(#) Copyright (c) 2000-2004\n\
47071c2b 50\tMatthew Mondor. All rights reserved.\n");
b232bd02 51MMRCSID("$Id: mmfifo.c,v 1.5 2007/12/05 23:47:56 mmondor Exp $");
47071c2b
MM
52
53
54
55
47071c2b
MM
56/* This function reads all available bytes from the specified FIFO and
57 * copies them in the supplied buffer until there are none to read, or
58 * until max bytes have been transfered. Returns the number of bytes that
59 * were successfully read.
60 */
61size_t
b232bd02 62getnfifo8(uint8_t *buffer, fifo8_t *fifo, size_t max)
47071c2b 63{
b232bd02 64 register uint8_t *pos;
47071c2b
MM
65 register size_t len, len2, done;
66
67 if (max == 1) {
4f87c15b
MM
68 if (STATFIFO(fifo) > 0) {
69 GETFIFO(fifo, buffer);
47071c2b 70 return (1);
4f87c15b 71 }
47071c2b
MM
72 return (0);
73 }
74
75 len = fifo->elements;
76 if (len > max)
77 len = max;
78
79 if (len) {
80 pos = fifo->tail;
81 len2 = fifo->endbuffer - pos;
82 if (len2 >= len)
83 done = len;
84 else
85 done = len2;
86 mm_memcpy(buffer, pos, done);
87 buffer += done;
88 pos += done;
89 if (pos == fifo->endbuffer)
90 pos = fifo->buffer;
91 len2 -= done;
92 if (len2) {
93 mm_memcpy(buffer, pos, len2);
94 pos += len2;
95 }
96 fifo->tail = pos;
97 fifo->elements -= len;
98 }
99
100 return (len);
101}
102
103
47071c2b
MM
104/* This function inserts size elements into fifo, reading them from buffer.
105 * It returns the number of elements that could be inserted.
106 */
107size_t
b232bd02 108putnfifo8(fifo8_t *fifo, uint8_t *buffer, size_t size)
47071c2b 109{
b232bd02 110 uint8_t *pos;
47071c2b
MM
111 size_t len, len2, done;
112
113 if (size == 1) {
4f87c15b
MM
114 if (FULLFIFO(fifo))
115 return (0);
116 PUTFIFO(fifo, buffer);
117 return (1);
47071c2b
MM
118 }
119
120 len = size;
121 len2 = fifo->size - fifo->elements;
122 if (len2 > len)
123 len = len2;
124
125 if (len) {
126 pos = fifo->head;
127 len2 = fifo->endbuffer - pos;
128 if (len2 > len)
129 done = len;
130 else
131 done = len2;
132 mm_memcpy(pos, buffer, done);
133 buffer += done;
134 pos += done;
135 if (pos == fifo->endbuffer)
136 pos = fifo->buffer;
137 len2 -= done;
138 if (len2) {
139 mm_memcpy(pos, buffer, len2);
140 pos += len2;
141 }
142 fifo->head = pos;
143 fifo->elements += len;
144 }
145
146 return (len);
147}