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 964 : void MemFIFO::write( const Sequence< sal_Int8 > &seq )
36 : throw ( I_FIFO_OutOfMemoryException,
37 : I_FIFO_OutOfBoundsException )
38 : {
39 : try
40 : {
41 964 : 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 964 : }
52 :
53 1812 : void MemFIFO::read( Sequence<sal_Int8> &seq , sal_Int32 nBufferLen ) throw (I_FIFO_OutOfBoundsException)
54 : {
55 : try
56 : {
57 1812 : readAt(0, seq , nBufferLen);
58 1812 : forgetFromStart( nBufferLen );
59 : }
60 0 : catch ( IRingBuffer_OutOfBoundsException & )
61 : {
62 0 : throw I_FIFO_OutOfBoundsException();
63 : }
64 1812 : }
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 127 : MemRingBuffer::MemRingBuffer()
81 : {
82 127 : m_nBufferLen = 0;
83 127 : m_p = 0;
84 127 : m_nStart = 0;
85 127 : m_nOccupiedBuffer = 0;
86 127 : }
87 :
88 12 : MemRingBuffer::~MemRingBuffer()
89 : {
90 5 : if( m_p ) {
91 2 : rtl_freeMemory( m_p );
92 : }
93 7 : }
94 :
95 3050 : void MemRingBuffer::resizeBuffer( sal_Int32 nMinSize ) throw( IRingBuffer_OutOfMemoryException)
96 : {
97 3050 : sal_Int32 nNewLen = 1;
98 :
99 25755 : while( nMinSize > nNewLen ) {
100 19655 : nNewLen = nNewLen << 1;
101 : }
102 :
103 : // buffer never shrinks !
104 3050 : if( nNewLen < m_nBufferLen ) {
105 418 : nNewLen = m_nBufferLen;
106 : }
107 :
108 3050 : if( nNewLen != m_nBufferLen ) {
109 287 : m_p = static_cast<sal_Int8 *>(rtl_reallocateMemory( m_p , nNewLen ));
110 287 : if( !m_p ) {
111 0 : throw IRingBuffer_OutOfMemoryException();
112 : }
113 :
114 287 : 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 287 : m_nBufferLen = nNewLen;
119 : }
120 3050 : }
121 :
122 :
123 2936 : void MemRingBuffer::readAt( sal_Int32 nPos, Sequence<sal_Int8> &seq , sal_Int32 nBytesToRead ) const
124 : throw(IRingBuffer_OutOfBoundsException)
125 : {
126 2936 : if( nPos + nBytesToRead > m_nOccupiedBuffer ) {
127 0 : throw IRingBuffer_OutOfBoundsException();
128 : }
129 :
130 2936 : sal_Int32 nStartReadingPos = nPos + m_nStart;
131 2936 : if( nStartReadingPos >= m_nBufferLen ) {
132 49 : nStartReadingPos -= m_nBufferLen;
133 : }
134 :
135 2936 : seq.realloc( nBytesToRead );
136 :
137 2936 : if( nStartReadingPos + nBytesToRead > m_nBufferLen ) {
138 29 : sal_Int32 nDeltaLen = m_nBufferLen - nStartReadingPos;
139 29 : memcpy( seq.getArray() , &(m_p[nStartReadingPos]) , nDeltaLen );
140 29 : memcpy( &(seq.getArray()[nDeltaLen]), m_p , nBytesToRead - nDeltaLen );
141 : }
142 : else {
143 2907 : memcpy( seq.getArray() , &(m_p[nStartReadingPos]) , nBytesToRead );
144 : }
145 2936 : }
146 :
147 :
148 3136 : void MemRingBuffer::writeAt( sal_Int32 nPos, const Sequence<sal_Int8> &seq )
149 : throw (IRingBuffer_OutOfBoundsException,
150 : IRingBuffer_OutOfMemoryException )
151 : {
152 3136 : checkInvariants();
153 3136 : sal_Int32 nLen = seq.getLength();
154 :
155 3136 : if( nPos < 0 || nPos > std::numeric_limits< sal_Int32 >::max() - nLen )
156 : {
157 0 : throw IRingBuffer_OutOfBoundsException();
158 : }
159 :
160 3136 : if( nPos + nLen - m_nOccupiedBuffer > 0 ) {
161 3050 : resizeBuffer( nPos + seq.getLength() );
162 : }
163 :
164 3136 : sal_Int32 nStartWritingIndex = m_nStart + nPos;
165 3136 : if( nStartWritingIndex >= m_nBufferLen ) {
166 142 : nStartWritingIndex -= m_nBufferLen;
167 : }
168 :
169 3136 : if( nLen + nStartWritingIndex > m_nBufferLen ) {
170 : // two area copy
171 8 : memcpy( &(m_p[nStartWritingIndex]) , seq.getConstArray(), m_nBufferLen-nStartWritingIndex );
172 16 : memcpy( m_p , &( seq.getConstArray()[m_nBufferLen-nStartWritingIndex] ),
173 24 : nLen - (m_nBufferLen-nStartWritingIndex) );
174 :
175 : }
176 : else {
177 : // one area copy
178 3128 : memcpy( &( m_p[nStartWritingIndex]), seq.getConstArray() , nLen );
179 : }
180 3136 : m_nOccupiedBuffer = Max( nPos + seq.getLength() , m_nOccupiedBuffer );
181 3136 : checkInvariants();
182 3136 : }
183 :
184 :
185 7627 : sal_Int32 MemRingBuffer::getSize() const throw()
186 : {
187 7627 : return m_nOccupiedBuffer;
188 : }
189 :
190 1878 : void MemRingBuffer::forgetFromStart( sal_Int32 nBytesToForget ) throw (IRingBuffer_OutOfBoundsException)
191 : {
192 1878 : checkInvariants();
193 1878 : if( nBytesToForget > m_nOccupiedBuffer ) {
194 0 : throw IRingBuffer_OutOfBoundsException();
195 : }
196 1878 : m_nStart += nBytesToForget;
197 1878 : if( m_nStart >= m_nBufferLen ) {
198 25 : m_nStart = m_nStart - m_nBufferLen;
199 : }
200 1878 : m_nOccupiedBuffer -= nBytesToForget;
201 1878 : checkInvariants();
202 1878 : }
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: */
|