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