2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
9 // http://www.apache.org/licenses/LICENSE-2.0
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
19 * @file FBaseBufferBase.cpp
20 * @brief This is the implementation for BufferBase class.
26 #include <FBaseBufferBase.h>
27 #include <FBaseSysLog.h>
30 namespace Tizen { namespace Base
33 BufferBase::BufferBase(void)
40 , __pBufferBaseImpl(null)
44 BufferBase::~BufferBase(void)
46 if ((_pData != null) && (Release() == 0))
54 BufferBase::Construct(int capacity)
56 SysTryReturn(NID_BASE, capacity >= 0, E_INVALID_ARG, E_INVALID_ARG,
57 "[%s] Invalid argument is used. The capacity is negative", GetErrorMessage(E_INVALID_ARG));
62 // check whether the size of memory is larger than the maximum of unsigned int
63 unsigned long long size = static_cast <unsigned long long>(sizeof(_BufferData)) +
64 static_cast <unsigned long long>(capacity) * static_cast <unsigned long long>(GetTypeSize());
65 SysTryReturnResult(NID_BASE, size <= static_cast <unsigned long long>((unsigned int) -1), E_OUT_OF_MEMORY,
66 "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
68 pTemp = malloc(static_cast <unsigned int>(size));
69 SysTryReturnResult(NID_BASE, 0 != pTemp, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.",
70 GetErrorMessage(E_OUT_OF_MEMORY));
72 memset(pTemp, 0, static_cast <unsigned int>(size));
74 _pData = static_cast <_BufferData*>(pTemp);
77 _pData->capacityInByte = capacity * GetTypeSize();
84 __pArrayStart = _pData->GetArray();
91 BufferBase::Clear(void)
99 BufferBase::Compact(void)
101 int remaining = GetRemaining();
102 if (HasRemaining() && (0 != _position))
104 int offset = _position * GetTypeSize();
105 int byteNum = remaining * GetTypeSize();
106 for (int i = 0; i < byteNum; i++)
108 __pArrayStart[i] = __pArrayStart[offset + i];
111 _position = remaining;
117 BufferBase::Flip(PositionTo to)
120 if ((to == POSITION_TO_MARK) && (_mark > -1))
132 BufferBase::GetHashCode(void) const
134 int len = (GetRemaining() * GetTypeSize()) / sizeof(len);
136 // s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1]
138 int offset = _position * GetTypeSize();
139 for (int i = 0; i < len; ++i)
141 hash += (int) __pArrayStart[offset + (i * sizeof(hash))] * (int) pow((double) 31, len - (i + 1));
148 BufferBase::InvalidateMark(void)
154 BufferBase::Reset(void)
156 SysTryReturnResult(NID_BASE, _mark >= 0, E_INVALID_OPERATION, "[%s] The mark has not been set.",
157 GetErrorMessage(E_INVALID_OPERATION));
164 BufferBase::Rewind(void)
171 BufferBase::ShiftLimit(int amount)
173 SysTryReturnResult(NID_BASE, (((_limit + amount) <= _capacity) && ((_limit + amount) >= 0)), E_OUT_OF_RANGE,
174 "The amount(%d) is larger than the capacity(%d), or smaller than zero starting from the current limit(%d).",
175 amount, _capacity, _limit);
177 return SetLimit(_limit + amount);
181 BufferBase::GetCapacity(void) const
187 BufferBase::GetLimit(void) const
193 BufferBase::GetMark(void) const
199 BufferBase::GetPosition(void) const
205 BufferBase::GetRemaining(void) const
207 return _limit - _position;
211 BufferBase::SetLimit(int limit)
213 SysTryReturnResult(NID_BASE, limit <= _capacity && limit >= 0, E_OUT_OF_RANGE,
214 "The limit(%d) MUST be greater than or equal to 0 and less than the current capacity(%d).", limit, _capacity);
219 if (_position > limit)
232 BufferBase::SetMark(void)
238 BufferBase::SetPosition(int position)
240 SysTryReturnResult(NID_BASE, position <= _limit && position >= 0, E_OUT_OF_RANGE,
241 "The position(%d) MUST be greater than or equal to 0 and less than the current limit(%d).", position, _limit);
243 _position = position;
244 if (_mark > _position)
253 BufferBase::HasRemaining(void) const
255 return _limit > _position;
259 BufferBase::ExpandCapacity(int newCapacity)
261 SysTryReturnResult(NID_BASE, newCapacity > _capacity, E_INVALID_ARG,
262 "The capacity is less than the current capacity.");
264 // check whether the size of memory is larger than the maximum of unsigned int
265 unsigned long long size = static_cast <unsigned long long>(sizeof(_BufferData)) +
266 static_cast <unsigned long long>(newCapacity) * static_cast <unsigned long long>(GetTypeSize());
268 SysTryReturnResult(NID_BASE, size <= static_cast <unsigned long long>((unsigned int) -1), E_OUT_OF_MEMORY,
269 "Memory allocation failed.");
271 _pData = static_cast<_BufferData*>(realloc(_pData, static_cast<unsigned int>(size)));
272 SysTryReturnResult(NID_BASE, 0 != _pData, E_OUT_OF_MEMORY, "Memory allocation failed.");
274 _capacity = newCapacity;
275 _limit = newCapacity;
277 __pArrayStart = _pData->GetArray();
283 BufferBase::AddRef(void) const
285 SysAssertf(_pData->refCount > 0, "refCount(%d) is not greater than zero.", _pData->refCount);
289 return _pData->refCount;
293 BufferBase::Release(void) const
295 SysAssertf(_pData->refCount > 0, "refCount is not greater than zero(impossible!).");
299 return _pData->refCount;
303 BufferBase::Dispose(void)
307 BufferBase::_BufferData::_BufferData()
313 BufferBase::_BufferData::~_BufferData()
318 BufferBase::_BufferData::GetArray(void)
320 return reinterpret_cast <byte*>(this + 1);