-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFifoSync.cpp
More file actions
185 lines (142 loc) · 4.7 KB
/
FifoSync.cpp
File metadata and controls
185 lines (142 loc) · 4.7 KB
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
/*******************************************************************
// Copyright (c) 2002, Robert Umbehant
// mailto:rumbehant@wheresjames.com
//
// 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.1 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
//
*******************************************************************/
// FifoSync.cpp: implementation of the CFifoSync class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CFifoSync::CFifoSync()
{_STT();
m_dwMaxBuffers = 1024;
m_dwHeadPtr = 0;
m_dwTailPtr = 0;
}
CFifoSync::~CFifoSync()
{_STT();
}
BOOL CFifoSync::Write( void const * pBuf, DWORD dwSize, DWORD dwEncode )
{_STT();
// Lock the buffer
CTlLocalLock ll( *this );
if ( !ll.IsLocked() ) return FALSE;
// Prepare to write data
if ( !InitFifoWrite() )
return FALSE;
// Add data to the buffer
if ( !AddFifo( pBuf, dwSize, dwEncode ) )
return FALSE;
// Commit buffer changes
return EndFifoWrite();
}
BOOL CFifoSync::AllocateBuffers()
{_STT();
// Ensure size tag space
if ( m_rdwSize.size() != m_dwMaxBuffers )
if ( !m_rdwSize.grow( m_dwMaxBuffers ) )
return FALSE;
return TRUE;
}
BOOL CFifoSync::AddFifo(void const * pBuf, DWORD dwSize, DWORD dwEncode)
{_STT();
// Poke the data into the buffer
if ( !Poke( pBuf, dwSize, dwEncode ) )
return FALSE;
// Keep track of the size of this sub buffer
m_rdwSize[ m_dwHeadPtr ] += dwSize;
return TRUE;
}
BOOL CFifoSync::InitFifoWrite()
{_STT();
// Ensure buffer space
if ( !AllocateBuffers() ) return FALSE;
// Do we have a buffer
if ( !GetMaxWrite( m_dwTailPtr, m_dwHeadPtr, m_dwMaxBuffers ) )
return FALSE;
// Normalize the head pointer
m_dwHeadPtr = NormalizePtr( m_dwHeadPtr, m_dwMaxBuffers );
// Initialize buffer pointers
m_rdwSize[ m_dwHeadPtr ] = 0;
// Get ready to poke the buffer
InitPoke();
return TRUE;
}
BOOL CFifoSync::EndFifoWrite()
{_STT();
// Advance the head pointer
m_dwHeadPtr = AdvancePtr( m_dwHeadPtr, 1, m_dwMaxBuffers );
return EndPoke();
}
BOOL CFifoSync::Read( LPVOID pBuf, DWORD dwSize, LPDWORD pdwRead, DWORD dwEncode )
{_STT();
// Read the data block
BOOL bRet = Peek( pBuf, dwSize, pdwRead, 0, dwEncode );
// Next read block
if ( pBuf && dwSize ) SkipBlock();
return bRet;
}
BOOL CFifoSync::SkipBlock()
{_STT();
// Lock the buffer
CTlLocalLock ll( *this );
if ( !ll.IsLocked() ) return FALSE;
// Anything to read?
if ( !GetMaxRead( m_dwTailPtr, m_dwHeadPtr, m_dwMaxBuffers ) )
return FALSE;
// Skip to the next block
AdvanceReadPtr( m_rdwSize[ m_dwTailPtr ] );
// Advance the tail pointer
m_dwTailPtr = NormalizePtr( AdvancePtr( m_dwTailPtr, 1, m_dwMaxBuffers ), m_dwMaxBuffers );
// Wrap pointers if buffer is empty
if ( m_dwTailPtr == NormalizePtr( m_dwHeadPtr, m_dwMaxBuffers ) && m_dwTailPtr == 0 )
m_dwTailPtr = m_dwHeadPtr = 0;
return TRUE;
}
BOOL CFifoSync::Peek( LPVOID pBuf, DWORD dwSize, LPDWORD pdwRead, long lOffset, DWORD dwEncode )
{_STT();
// Lock the buffer
CTlLocalLock ll( *this );
if ( !ll.IsLocked() ) return FALSE;
// Anything to read?
if ( !GetMaxRead( m_dwTailPtr, m_dwHeadPtr, m_dwMaxBuffers ) )
return FALSE;
// Are they asking for the size?
if ( pBuf == NULL || !dwSize )
{ if ( !pdwRead ) return FALSE;
// Get the buffer size
*pdwRead = m_rdwSize[ m_dwTailPtr ];
// Correct for offset
if ( lOffset > (long)*pdwRead ) return FALSE;
*pdwRead -= lOffset;
return TRUE;
} // end if
// What's the maximum amount of data in this block
if ( dwSize > m_rdwSize[ m_dwTailPtr ] )
dwSize = m_rdwSize[ m_dwTailPtr ];
// Anything left to read?
if ( dwSize <= (DWORD)lOffset ) return FALSE;
// Subtract the offset
dwSize -= (DWORD)lOffset;
// Peek the data
return CCircBuf::Peek( pBuf, dwSize, pdwRead, lOffset, dwEncode );
}