2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 // Licensed under the Apache License, Version 2.0 (the License);
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
8 // http://www.apache.org/licenses/LICENSE-2.0
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
18 * @file FBaseBufferBase.cpp
19 * @brief This is the implementation for BufferBase class.
24 #include <FBaseBufferBase.h>
25 #include <FBaseSysLog.h>
27 namespace Tizen { namespace Base
30 BufferBase::BufferBase(void)
37 , __pBufferBaseImpl(null)
41 BufferBase::~BufferBase(void)
43 if ((_pData != null) && (Release() == 0))
51 BufferBase::Construct(int capacity)
53 SysTryReturn(NID_BASE, capacity >= 0, E_INVALID_ARG, E_INVALID_ARG,
54 "[%s] Invalid argument is used. The capacity is negative", GetErrorMessage(E_INVALID_ARG));
59 // check whether the size of memory is larger than the maximum of unsigned int
60 unsigned long long size = static_cast< unsigned long long >(sizeof(_BufferData)) +
61 static_cast< unsigned long long >(capacity) * static_cast< unsigned long long >(GetTypeSize());
62 SysTryReturnResult(NID_BASE, size <= static_cast< unsigned long long >((unsigned int) -1), E_OUT_OF_MEMORY,
63 "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
65 pTemp = malloc(static_cast< unsigned int >(size));
66 SysTryReturnResult(NID_BASE, 0 != pTemp, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.",
67 GetErrorMessage(E_OUT_OF_MEMORY));
69 memset(pTemp, 0, static_cast< unsigned int >(size));
71 _pData = static_cast< _BufferData* >(pTemp);
74 _pData->capacityInByte = capacity * GetTypeSize();
81 __pArrayStart = _pData->GetArray();
88 BufferBase::Clear(void)
96 BufferBase::Compact(void)
98 int remaining = GetRemaining();
99 if (HasRemaining() && (0 != _position))
101 int offset = _position * GetTypeSize();
102 int byteNum = remaining * GetTypeSize();
103 for (int i = 0; i < byteNum; i++)
105 __pArrayStart[i] = __pArrayStart[offset + i];
108 _position = remaining;
114 BufferBase::Flip(PositionTo to)
117 if ((to == POSITION_TO_MARK) && (_mark > -1))
129 BufferBase::GetHashCode(void) const
131 int len = (GetRemaining() * GetTypeSize()) / sizeof(len);
133 // s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1]
135 int offset = _position * GetTypeSize();
136 for (int i = 0; i < len; ++i)
138 hash += (int) __pArrayStart[offset + (i * sizeof(hash))] * (int) pow((double) 31, len - (i + 1));
145 BufferBase::InvalidateMark(void)
151 BufferBase::Reset(void)
153 SysTryReturnResult(NID_BASE, _mark >= 0, E_INVALID_OPERATION, "[%s] The mark has not been set.",
154 GetErrorMessage(E_INVALID_OPERATION));
161 BufferBase::Rewind(void)
168 BufferBase::ShiftLimit(int amount)
170 SysTryReturnResult(NID_BASE, (((_limit + amount) <= _capacity) && ((_limit + amount) >= 0)), E_OUT_OF_RANGE,
171 "The amount(%d) is larger than the capacity(%d), or smaller than zero starting from the current limit(%d).",
172 amount, _capacity, _limit);
174 return SetLimit(_limit + amount);
178 BufferBase::GetCapacity(void) const
184 BufferBase::GetLimit(void) const
190 BufferBase::GetMark(void) const
196 BufferBase::GetPosition(void) const
202 BufferBase::GetRemaining(void) const
204 return _limit - _position;
208 BufferBase::SetLimit(int limit)
210 SysTryReturnResult(NID_BASE, limit <= _capacity && limit >= 0, E_OUT_OF_RANGE,
211 "The limit(%d) MUST be greater than or equal to 0 and less than the current capacity(%d).", limit, _capacity);
216 if (_position > limit)
229 BufferBase::SetMark(void)
235 BufferBase::SetPosition(int position)
237 SysTryReturnResult(NID_BASE, position <= _limit && position >= 0, E_OUT_OF_RANGE,
238 "The position(%d) MUST be greater than or equal to 0 and less than the current limit(%d).", position, _limit);
240 _position = position;
241 if (_mark > _position)
250 BufferBase::HasRemaining(void) const
252 return _limit > _position;
256 BufferBase::ExpandCapacity(int newCapacity)
258 SysTryReturnResult(NID_BASE, newCapacity > _capacity, E_INVALID_ARG,
259 "The capacity is less than the current capacity.");
261 // check whether the size of memory is larger than the maximum of unsigned int
262 unsigned long long size = static_cast< unsigned long long >(sizeof(_BufferData)) +
263 static_cast< unsigned long long >(newCapacity) * static_cast< unsigned long long >(GetTypeSize());
265 SysTryReturnResult(NID_BASE, size <= static_cast< unsigned long long >((unsigned int) -1), E_OUT_OF_MEMORY,
266 "Memory allocation failed.");
268 _pData = static_cast< _BufferData* >(realloc(_pData, static_cast< unsigned int >(size)));
269 SysTryReturnResult(NID_BASE, 0 != _pData, E_OUT_OF_MEMORY, "Memory allocation failed.");
271 _capacity = newCapacity;
272 _limit = newCapacity;
274 __pArrayStart = _pData->GetArray();
280 BufferBase::AddRef(void) const
282 SysAssertf(_pData->refCount > 0, "refCount(%d) is not greater than zero.", _pData->refCount);
286 return _pData->refCount;
290 BufferBase::Release(void) const
292 SysAssertf(_pData->refCount > 0, "refCount is not greater than zero(impossible!).");
296 return _pData->refCount;
300 BufferBase::Dispose(void)
304 BufferBase::_BufferData::_BufferData()
310 BufferBase::_BufferData::~_BufferData()
315 BufferBase::_BufferData::GetArray(void)
317 return reinterpret_cast< byte* >(this + 1);