hc
2024-11-01 2f529f9b558ca1c1bd74be7437a84e4711743404
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/*
* Copyright (C) 2008 Niklaus Giger <niklaus.giger@member.fsf.org>.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
* Lesser General Public License for more details.
 
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.
*/
 
#include <stdlib.h>
#include <boilerplate/lock.h>
#include <copperplate/heapobj.h>
#include <vxworks/errnoLib.h>
#include "rngLib.h"
 
#define ring_magic 0x5432affe
 
static struct wind_ring *find_ring_from_id(RING_ID rid)
{
   struct wind_ring *ring = mainheap_deref(rid, struct wind_ring);
 
   if (ring == NULL || ((uintptr_t)ring & (sizeof(uintptr_t)-1)) != 0 ||
       ring->magic != ring_magic)
       return NULL;
 
   return ring;
}
 
RING_ID rngCreate(int nbytes)
{
   struct wind_ring *ring;
   struct service svc;
   void *ring_mem;
   RING_ID rid;
 
   if (nbytes <= 0) {
       errnoSet(S_memLib_NOT_ENOUGH_MEMORY);
       return 0;
   }
 
   CANCEL_DEFER(svc);
 
   ring_mem = xnmalloc(sizeof(*ring) + nbytes + 1);
   if (ring_mem == NULL) {
       rid = 0;
       errno = errnoSet(S_memLib_NOT_ENOUGH_MEMORY);
       goto out;
   }
 
   ring = ring_mem;
   ring->magic = ring_magic;
   ring->bufSize = nbytes;
   ring->readPos = 0;
   ring->writePos = 0;
   rid = mainheap_ref(ring, RING_ID);
out:
   CANCEL_RESTORE(svc);
 
   return rid;
}
 
void rngDelete(RING_ID rid)
{
   struct wind_ring *ring = find_ring_from_id(rid);
   struct service svc;
 
   if (ring) {
       ring->magic = 0;
       CANCEL_DEFER(svc);
       xnfree(ring);
       CANCEL_RESTORE(svc);
   }
}
 
void rngFlush(RING_ID rid)
{
   struct wind_ring *ring = find_ring_from_id(rid);
 
   if (ring) {
       ring->readPos = 0;
       ring->writePos = 0;
   }
}
 
int rngBufGet(RING_ID rid, char *buffer, int maxbytes)
{
   struct wind_ring *ring = find_ring_from_id(rid);
   unsigned int savedWritePos;
   int j, bytesRead = 0;
 
   if (ring == NULL)
       return ERROR;
 
   savedWritePos = ring->writePos;
 
   for (j = 0; j < maxbytes; j++) {
       if ((ring->readPos) % (ring->bufSize + 1) == savedWritePos)
           break;
       buffer[j] = ring->buffer[ring->readPos];
       ++bytesRead;
       ring->readPos = (ring->readPos + 1) % (ring->bufSize + 1);
   }
 
   return bytesRead;
}
 
int rngBufPut(RING_ID rid, char *buffer, int nbytes)
{
   struct wind_ring *ring = find_ring_from_id(rid);
   unsigned int savedReadPos;
   int j, bytesWritten = 0;
 
   if (ring == NULL)
       return ERROR;
 
   savedReadPos = ring->readPos;
 
   for (j = 0; j < nbytes; j++) {
       if ((ring->writePos + 1) % (ring->bufSize + 1) == savedReadPos)
           break;
       ring->buffer[ring->writePos] = buffer[j];
       ++bytesWritten;
       ring->writePos = (ring->writePos + 1) % (ring->bufSize + 1);
   }
 
   return bytesWritten;
}
 
BOOL rngIsEmpty(RING_ID rid)
{
   struct wind_ring *ring = find_ring_from_id(rid);
 
   if (ring == NULL)
       return ERROR;
 
   return rngFreeBytes(rid) == (int)ring->bufSize;
}
 
BOOL rngIsFull(RING_ID rid)
{
   struct wind_ring *ring = find_ring_from_id(rid);
 
   if (ring == NULL)
       return ERROR;
 
   return rngFreeBytes(rid) == 0;
}
 
int rngFreeBytes(RING_ID rid)
{
   struct wind_ring *ring = find_ring_from_id(rid);
 
   if (ring == NULL)
       return ERROR;
 
   return ((ring->bufSize -
        (ring->writePos - ring->readPos)) % (ring->bufSize + 1));
}
 
int rngNBytes(RING_ID rid)
{
   struct wind_ring *ring = find_ring_from_id(rid);
 
   if (ring == NULL)
       return ERROR;
 
   return ring->bufSize - rngFreeBytes(rid);
}
 
void rngPutAhead(RING_ID rid, char byte, int offset)
{
   struct wind_ring *ring = find_ring_from_id(rid);
   int where;
 
   if (ring) {
       where = (ring->writePos + offset) % (ring->bufSize + 1);
       ring->buffer[where] = byte;
   }
}
 
void rngMoveAhead(RING_ID rid, int n)
{
   struct wind_ring *ring = find_ring_from_id(rid);
 
   if (ring) {
       ring->writePos += n;
       ring->writePos %= (ring->bufSize + 1);
   }
}