Beamlib 3.3.4
This is the Beam C++ class library.
Loading...
Searching...
No Matches
BFifoCirc.inc
Go to the documentation of this file.
1/*******************************************************************************
2 * BFifoCirc.inc FIFO Buffer Class
3 * T.Barnaby, BEAM Ltd, 2006-02-22
4 *******************************************************************************
5 */
6#include <BFifoCirc.h>
7#include <fcntl.h>
8#include <errno.h>
9#include <sys/mman.h>
10#include <unistd.h>
11
12#define DEBUG_BFifoCirc 0
13
14#if DEBUG_BFifoCirc
15#define BFifoCirc_dprintf(fmt, a...) printf(fmt, ##a);
16#else
17#define BFifoCirc_dprintf(fmt, a...)
18#endif
19
20
21template <class Type> Type* BFifoCirc<Type>::writeData(){
22 BMutexLock lock(olock);
23
24 return &odata[owritePos];
25}
26
27template <class Type> BError BFifoCirc<Type>::write(const Type* data, uint32_t numTypes){
28 BError err;
29
30 olock.lock();
31 if(numTypes > osize)
32 return BError(1, "Fifo to small for write");
33
34 memcpy(&odata[owritePos], data, numTypes * sizeof(Type));
35 olock.unlock();
36
37 writeDone(numTypes);
38
39 return err;
40}
41
42template <class Type> BError BFifoCirc<Type>::read(Type* data, uint32_t numTypes){
43 BError err;
44
45 olock.lock();
46 memcpy(data, &odata[oreadPos], numTypes * sizeof(Type));
47 olock.unlock();
48
49 readDone(numTypes);
50
51 return err;
52}
53
54template <class Type> Type* BFifoCirc<Type>::readData(){
55 BMutexLock lock(olock);
56
57 return &odata[oreadPos];
58}
59
60template <class Type> Type& BFifoCirc<Type>::operator[](int pos){
61 BMutexLock lock(olock);
62
63 return odata[oreadPos + pos];
64}
65
66template <class Type> BFifoCirc<Type>::BFifoCirc(uint32_t size) : olock(BMutex::Recursive), owritePos(size), oreadPos(size){
67 BMutexLock lock(olock);
68 BError err;
69
70 if(err = mapCircularBuffer(size)){
71 fprintf(stderr, "%s\n", err.getString().retStr());
72 exit(1);
73 }
74 owritePos.setSize(osize);
75 oreadPos.setSize(osize);
76}
77
78template <class Type> BFifoCirc<Type>::~BFifoCirc(){
79 BMutexLock lock(olock);
80
81 if(odata)
82 unmapCircularBuffer();
83 odata = 0;
84 osize = 0;
85}
86
87template <class Type> uint32_t BFifoCirc<Type>::size(){
88 BMutexLock lock(olock);
89
90 return osize;
91}
92
93template <class Type> void BFifoCirc<Type>::clear(){
94 BMutexLock lock(olock);
95
96 owritePos.set(0);
97 owriteNumFifoSamples.setValue(0);
98 oreadPos.set(0);
99}
100
101template <class Type> BError BFifoCirc<Type>::mapCircularBuffer(uint32_t size){
102 BError err;
103 int shmFd = -1;
104 BString segName = BString("/BFifoCirc-") + BString(getpid());
105 void* m1;
106 void* m2;
107 uint32_t sizeBytes = size * sizeof(Type);
108
109 // Round up to a multiple of the page size
110 if(sizeBytes % 4096 != 0)
111 sizeBytes = ((sizeBytes / 4096) + 1) * 4096;
112
113 ovmSize = 2 * sizeBytes;
114
115 if((shmFd = shm_open(segName, O_RDWR | O_CREAT | O_EXCL, 0600)) == -1){
116 err.set(1, "Unable to open shared memory");
117 return err;
118 }
119
120 // Set length of shared memory segment
121 if(ftruncate(shmFd, ovmSize) == -1){
122 close(shmFd);
123 err.set(1, "Unable to set shared memory size");
124 return err;
125 }
126
127 if((m1 = mmap(0, ovmSize, PROT_READ | PROT_WRITE, MAP_SHARED, shmFd, 0)) == MAP_FAILED){
128 close(shmFd);
129 err.set(1, BString("Unable to map shared memory: ") + strerror(errno));
130 return err;
131 }
132
133 // Unmap the 2nd half
134 if(munmap((char*)m1 + sizeBytes, sizeBytes) == -1){
135 close(shmFd);
136 err.set(1, "Unable to unmap shared memory1");
137 return err;
138 }
139
140 // Map the first half into the now available hole where the second half used to be.
141 if((m2 = mmap((char*)m1 + sizeBytes, sizeBytes, PROT_READ | PROT_WRITE, MAP_SHARED, shmFd, 0)) == MAP_FAILED){
142 close(shmFd);
143 err.set(1, "Unable to map shared memory");
144 return err;
145 }
146
147 // fd no longer needed. The mapping is retained.
148 close(shmFd);
149 shm_unlink(segName);
150
151 odata = (Type*)m1;
152 osize = sizeBytes / sizeof(Type);
153
154 return err;
155}
156
157template <class Type> void BFifoCirc<Type>::unmapCircularBuffer(){
158 munmap(odata, ovmSize);
159}
160
161template <class Type> uint32_t BFifoCirc<Type>::writeAvailable(){
162 BMutexLock lock(olock);
163 uint32_t ns;
164
165 if(oreadPos > owritePos)
166 ns = oreadPos - owritePos - 1;
167 else
168 ns = osize - owritePos + oreadPos - 1;
169
170 return ns;
171}
172
173template <class Type> BError BFifoCirc<Type>::writeWaitAvailable(uint32_t numFifoSamples){
174 BError err;
175
176 owriteNumFifoSamples.waitLessThan(osize - numFifoSamples);
177
178 return err;
179}
180
181template <class Type> BError BFifoCirc<Type>::readWaitAvailable(uint32_t numFifoSamples){
182 BError err;
183
184 BFifoCirc_dprintf("BFifoCirc<Type>::readWaitAvailable: %d\n", numFifoSamples);
185 owriteNumFifoSamples.waitMoreThanOrEqual(numFifoSamples);
186
187 return err;
188}
189
190template <class Type> void BFifoCirc<Type>::writeDone(uint32_t numFifoSamples){
191 BMutexLock lock(olock);
192 BIter i;
193
194 owritePos += numFifoSamples;
195 owriteNumFifoSamples += numFifoSamples;
196
197 BFifoCirc_dprintf("BFifoCirc<Type>::writeDone: %d (%d)\n", numFifoSamples, owriteNumFifoSamples.value());
198}
199
200template <class Type> BError BFifoCirc<Type>::readDone(uint32_t numFifoSamples){
201 BMutexLock lock(olock);
202 BError err;
203 BIter i;
204
205 oreadPos += numFifoSamples;
206 owriteNumFifoSamples -= numFifoSamples;
207
208 return err;
209}
210
211template <class Type> uint32_t BFifoCirc<Type>::readAvailable(){
212 BMutexLock lock(olock);
213 uint32_t r;
214
215 if(oreadPos == owritePos)
216 r = 0;
217 else
218 r = owritePos.difference(oreadPos);
219
220 return r;
221}
char data[8]
Definition BoapMc1.h:2
Error return class. This class is used to return the error status from a function....
Definition BError.h:31
BString getString() const
Get error message.
Definition BError.cpp:40
BError & set(int errNo, BString errStr="")
Set error number and message.
Definition BError.cpp:24
void writeDone(uint32_t numFifoSamples)
Update the write pointer.
BFifoCirc(uint32_t size=defaultSize)
BError readDone(uint32_t numFifoSamples)
Updates read pointer.
BError mapCircularBuffer(uint32_t size)
Type & operator[](int pos)
Direct access to read samples in buffer.
uint32_t size()
Return the buffers actual size.
BError write(const Type *data, uint32_t numFifoSamples)
Writes the data to the buffer. Blocks until complete.
BError writeWaitAvailable(uint32_t numFifoSamples)
Wait for the given number of samples.
void unmapCircularBuffer()
BError read(Type *data, uint32_t numFifoSamples)
uint32_t writeAvailable()
Returns the space available to write.
BError readWaitAvailable(uint32_t numFifoSamples)
Wait for given number of samples.
void clear()
Clear all of the data in the buffer.
Type * readData()
Pointer to raw data.
uint32_t readAvailable()
Returns the number of bytes of data available.
Type * writeData()
Return a pointer to the current start of the buffer.
Iterator for BLists.
Definition BList.h:20
Mutex class that removes the lock on deletion and so is useful to lock data in a function call.
Definition BMutex.h:33
Mutex class. Note these are recursive Mutexes and so you need to make sure the number of unlocks equa...
Definition BMutex.h:14
This class stores and manipulates ASCII strings.
Definition BString.h:20
const char * retStr() const
Ptr to char* representation.
Definition BString.cpp:1001