Quake II RTX doxygen  1.0 dev
fifo.c
Go to the documentation of this file.
1 /*
2 Copyright (C) 2003-2008 Andrey Nazarov
3 
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
8 
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13 
14 You should have received a copy of the GNU General Public License along
15 with this program; if not, write to the Free Software Foundation, Inc.,
16 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17 */
18 
19 #include "shared/shared.h"
20 #include "common/fifo.h"
21 #include "common/msg.h"
22 
23 size_t FIFO_Read(fifo_t *fifo, void *buffer, size_t len)
24 {
25  size_t wrapped, head = fifo->ay - fifo->ax;
26 
27  if (head > len) {
28  if (buffer) {
29  memcpy(buffer, fifo->data + fifo->ax, len);
30  fifo->ax += len;
31  }
32  return len;
33  }
34 
35  wrapped = len - head;
36  if (wrapped > fifo->bs) {
37  wrapped = fifo->bs;
38  }
39  if (buffer) {
40  memcpy(buffer, fifo->data + fifo->ax, head);
41  memcpy((byte *)buffer + head, fifo->data, wrapped);
42  fifo->ax = wrapped;
43  fifo->ay = fifo->bs;
44  fifo->bs = 0;
45  }
46 
47  return head + wrapped;
48 }
49 
50 size_t FIFO_Write(fifo_t *fifo, const void *buffer, size_t len)
51 {
52  size_t tail, wrapped, remaining;
53 
54  if (fifo->bs) {
55  remaining = fifo->ax - fifo->bs;
56  if (len > remaining) {
57  len = remaining;
58  }
59  if (buffer) {
60  memcpy(fifo->data + fifo->bs, buffer, len);
61  fifo->bs += len;
62  }
63  return len;
64  }
65 
66  tail = fifo->size - fifo->ay;
67  if (tail > len) {
68  if (buffer) {
69  memcpy(fifo->data + fifo->ay, buffer, len);
70  fifo->ay += len;
71  }
72  return len;
73  }
74 
75  wrapped = len - tail;
76  if (wrapped > fifo->ax) {
77  wrapped = fifo->ax;
78  }
79  if (buffer) {
80  memcpy(fifo->data + fifo->ay, buffer, tail);
81  memcpy(fifo->data, (byte *)buffer + tail, wrapped);
82  fifo->ay = fifo->size;
83  fifo->bs = wrapped;
84  }
85 
86  return tail + wrapped;
87 }
88 
89 qboolean FIFO_ReadMessage(fifo_t *fifo, size_t msglen)
90 {
91  size_t len;
92  byte *data;
93 
94  data = FIFO_Peek(fifo, &len);
95  if (len < msglen) {
96  // read in two chunks into message buffer
97  if (!FIFO_TryRead(fifo, msg_read_buffer, msglen)) {
98  return qfalse; // not yet available
99  }
101  } else {
102  // read in a single block without copying any memory
103  SZ_Init(&msg_read, data, msglen);
104  FIFO_Decommit(fifo, msglen);
105  }
106 
107  msg_read.cursize = msglen;
108  return qtrue;
109 }
110 
111 
msg_read
sizebuf_t msg_read
Definition: msg.c:37
msg_read_buffer
byte msg_read_buffer[MAX_MSGLEN]
Definition: msg.c:38
FIFO_ReadMessage
qboolean FIFO_ReadMessage(fifo_t *fifo, size_t msglen)
Definition: fifo.c:89
SZ_Init
void SZ_Init(sizebuf_t *buf, void *data, size_t size)
Definition: sizebuf.c:31
FIFO_Read
size_t FIFO_Read(fifo_t *fifo, void *buffer, size_t len)
Definition: fifo.c:23
head
unsigned head
Definition: screen.c:529
FIFO_Write
size_t FIFO_Write(fifo_t *fifo, const void *buffer, size_t len)
Definition: fifo.c:50