Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*
3 : : * This file is part of the LibreOffice project.
4 : : *
5 : : * This Source Code Form is subject to the terms of the Mozilla Public
6 : : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : : *
9 : : * This file incorporates work covered by the following license notice:
10 : : *
11 : : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : : * contributor license agreements. See the NOTICE file distributed
13 : : * with this work for additional information regarding copyright
14 : : * ownership. The ASF licenses this file to you under the Apache
15 : : * License, Version 2.0 (the "License"); you may not use this file
16 : : * except in compliance with the License. You may obtain a copy of
17 : : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : : */
19 : :
20 : : #include <rtl/alloc.h>
21 : :
22 : : #include <limits>
23 : : #include <string.h>
24 : :
25 : : #include <com/sun/star/uno/Sequence.hxx>
26 : :
27 : : #include <com/sun/star/uno/Exception.hpp>
28 : :
29 : : using namespace ::com::sun::star::uno;
30 : :
31 : : #include "streamhelper.hxx"
32 : :
33 : : namespace io_stm {
34 : :
35 : 1506 : void MemFIFO::write( const Sequence< sal_Int8 > &seq )
36 : : throw ( I_FIFO_OutOfMemoryException,
37 : : I_FIFO_OutOfBoundsException )
38 : : {
39 : : try
40 : : {
41 [ + - ]: 1506 : writeAt(getSize(), seq );
42 : : }
43 : 0 : catch( IRingBuffer_OutOfMemoryException & )
44 : : {
45 [ # # ]: 0 : throw I_FIFO_OutOfMemoryException();
46 : : }
47 [ # # # ]: 0 : catch( IRingBuffer_OutOfBoundsException & )
48 : : {
49 [ # # ]: 0 : throw I_FIFO_OutOfBoundsException();
50 : : }
51 : 1506 : }
52 : :
53 : 3614 : void MemFIFO::read( Sequence<sal_Int8> &seq , sal_Int32 nBufferLen ) throw (I_FIFO_OutOfBoundsException)
54 : : {
55 : : try
56 : : {
57 [ + - ]: 3614 : readAt(0, seq , nBufferLen);
58 [ + - ]: 3614 : forgetFromStart( nBufferLen );
59 : : }
60 [ # # ]: 0 : catch ( IRingBuffer_OutOfBoundsException & )
61 : : {
62 [ # # ]: 0 : throw I_FIFO_OutOfBoundsException();
63 : : }
64 : 3614 : }
65 : :
66 : 0 : void MemFIFO::skip( sal_Int32 nBytesToSkip ) throw ( I_FIFO_OutOfBoundsException )
67 : : {
68 : : try
69 : : {
70 [ # # ]: 0 : forgetFromStart( nBytesToSkip );
71 : : }
72 [ # # ]: 0 : catch( IRingBuffer_OutOfBoundsException & )
73 : : {
74 [ # # ]: 0 : throw I_FIFO_OutOfBoundsException();
75 : : }
76 : 0 : }
77 : :
78 : :
79 : :
80 : 246 : MemRingBuffer::MemRingBuffer()
81 : : {
82 : 246 : m_nBufferLen = 0;
83 : 246 : m_p = 0;
84 : 246 : m_nStart = 0;
85 : 246 : m_nOccupiedBuffer = 0;
86 : 246 : }
87 : :
88 : 0 : MemRingBuffer::~MemRingBuffer()
89 : : {
90 [ # # ]: 0 : if( m_p ) {
91 : 0 : rtl_freeMemory( m_p );
92 : : }
93 [ # # ]: 0 : }
94 : :
95 : 5840 : void MemRingBuffer::resizeBuffer( sal_Int32 nMinSize ) throw( IRingBuffer_OutOfMemoryException)
96 : : {
97 : 5840 : sal_Int32 nNewLen = 1;
98 : :
99 [ + + ]: 40738 : while( nMinSize > nNewLen ) {
100 : 34898 : nNewLen = nNewLen << 1;
101 : : }
102 : :
103 : : // buffer never shrinks !
104 [ + + ]: 5840 : if( nNewLen < m_nBufferLen ) {
105 : 434 : nNewLen = m_nBufferLen;
106 : : }
107 : :
108 [ + + ]: 5840 : if( nNewLen != m_nBufferLen ) {
109 : 574 : m_p = ( sal_Int8 * ) rtl_reallocateMemory( m_p , nNewLen );
110 [ - + ]: 574 : if( !m_p ) {
111 [ # # ]: 0 : throw IRingBuffer_OutOfMemoryException();
112 : : }
113 : :
114 [ - + ]: 574 : if( m_nStart + m_nOccupiedBuffer > m_nBufferLen ) {
115 : 0 : memmove( &( m_p[m_nStart+(nNewLen-m_nBufferLen)]) , &(m_p[m_nStart]) , m_nBufferLen - m_nStart );
116 : 0 : m_nStart += nNewLen - m_nBufferLen;
117 : : }
118 : 574 : m_nBufferLen = nNewLen;
119 : : }
120 : 5840 : }
121 : :
122 : :
123 : 5948 : void MemRingBuffer::readAt( sal_Int32 nPos, Sequence<sal_Int8> &seq , sal_Int32 nBytesToRead ) const
124 : : throw(IRingBuffer_OutOfBoundsException)
125 : : {
126 [ - + ]: 5948 : if( nPos + nBytesToRead > m_nOccupiedBuffer ) {
127 [ # # ]: 0 : throw IRingBuffer_OutOfBoundsException();
128 : : }
129 : :
130 : 5948 : sal_Int32 nStartReadingPos = nPos + m_nStart;
131 [ + + ]: 5948 : if( nStartReadingPos >= m_nBufferLen ) {
132 : 98 : nStartReadingPos -= m_nBufferLen;
133 : : }
134 : :
135 : 5948 : seq.realloc( nBytesToRead );
136 : :
137 [ + + ]: 5948 : if( nStartReadingPos + nBytesToRead > m_nBufferLen ) {
138 : 32 : sal_Int32 nDeltaLen = m_nBufferLen - nStartReadingPos;
139 : 32 : memcpy( seq.getArray() , &(m_p[nStartReadingPos]) , nDeltaLen );
140 : 32 : memcpy( &(seq.getArray()[nDeltaLen]), m_p , nBytesToRead - nDeltaLen );
141 : : }
142 : : else {
143 : 5916 : memcpy( seq.getArray() , &(m_p[nStartReadingPos]) , nBytesToRead );
144 : : }
145 : 5948 : }
146 : :
147 : :
148 : 6016 : void MemRingBuffer::writeAt( sal_Int32 nPos, const Sequence<sal_Int8> &seq )
149 : : throw (IRingBuffer_OutOfBoundsException,
150 : : IRingBuffer_OutOfMemoryException )
151 : : {
152 : 6016 : checkInvariants();
153 : 6016 : sal_Int32 nLen = seq.getLength();
154 : :
155 [ - + ][ - + ]: 6016 : if( nPos < 0 || nPos > std::numeric_limits< sal_Int32 >::max() - nLen )
[ + - ]
156 : : {
157 [ # # ]: 0 : throw IRingBuffer_OutOfBoundsException();
158 : : }
159 : :
160 [ + + ]: 6016 : if( nPos + nLen - m_nOccupiedBuffer > 0 ) {
161 : 5840 : resizeBuffer( nPos + seq.getLength() );
162 : : }
163 : :
164 : 6016 : sal_Int32 nStartWritingIndex = m_nStart + nPos;
165 [ + + ]: 6016 : if( nStartWritingIndex >= m_nBufferLen ) {
166 : 212 : nStartWritingIndex -= m_nBufferLen;
167 : : }
168 : :
169 [ + + ]: 6016 : if( nLen + nStartWritingIndex > m_nBufferLen ) {
170 : : // two area copy
171 : 16 : memcpy( &(m_p[nStartWritingIndex]) , seq.getConstArray(), m_nBufferLen-nStartWritingIndex );
172 : 16 : memcpy( m_p , &( seq.getConstArray()[m_nBufferLen-nStartWritingIndex] ),
173 : 16 : nLen - (m_nBufferLen-nStartWritingIndex) );
174 : :
175 : : }
176 : : else {
177 : : // one area copy
178 : 6000 : memcpy( &( m_p[nStartWritingIndex]), seq.getConstArray() , nLen );
179 : : }
180 [ + + ]: 6016 : m_nOccupiedBuffer = Max( nPos + seq.getLength() , m_nOccupiedBuffer );
181 : 6016 : checkInvariants();
182 : 6016 : }
183 : :
184 : :
185 : 14720 : sal_Int32 MemRingBuffer::getSize() const throw()
186 : : {
187 : 14720 : return m_nOccupiedBuffer;
188 : : }
189 : :
190 : 3750 : void MemRingBuffer::forgetFromStart( sal_Int32 nBytesToForget ) throw (IRingBuffer_OutOfBoundsException)
191 : : {
192 : 3750 : checkInvariants();
193 [ - + ]: 3750 : if( nBytesToForget > m_nOccupiedBuffer ) {
194 [ # # ]: 0 : throw IRingBuffer_OutOfBoundsException();
195 : : }
196 : 3750 : m_nStart += nBytesToForget;
197 [ + + ]: 3750 : if( m_nStart >= m_nBufferLen ) {
198 : 24 : m_nStart = m_nStart - m_nBufferLen;
199 : : }
200 : 3750 : m_nOccupiedBuffer -= nBytesToForget;
201 : 3750 : checkInvariants();
202 : 3750 : }
203 : :
204 : :
205 : 0 : void MemRingBuffer::forgetFromEnd( sal_Int32 nBytesToForget ) throw (IRingBuffer_OutOfBoundsException)
206 : : {
207 : 0 : checkInvariants();
208 [ # # ]: 0 : if( nBytesToForget > m_nOccupiedBuffer ) {
209 [ # # ]: 0 : throw IRingBuffer_OutOfBoundsException();
210 : : }
211 : 0 : m_nOccupiedBuffer -= nBytesToForget;
212 : 0 : checkInvariants();
213 : 0 : }
214 : :
215 : :
216 : 0 : void MemRingBuffer::shrink() throw ()
217 : : {
218 : 0 : checkInvariants();
219 : :
220 : : // Up to now, only shrinking of while buffer works.
221 : : // No other shrinking supported up to now.
222 [ # # ]: 0 : if( ! m_nOccupiedBuffer ) {
223 [ # # ]: 0 : if( m_p ) {
224 : 0 : free( m_p );
225 : : }
226 : 0 : m_p = 0;
227 : 0 : m_nBufferLen = 0;
228 : 0 : m_nStart = 0;
229 : : }
230 : :
231 : 0 : checkInvariants();
232 : 0 : }
233 : :
234 : : }
235 : :
236 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|