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.
25 #include <FBaseBufferBase.h>
26 #include <FBaseSysLog.h>
29 namespace Tizen { namespace Base
32 BufferBase::BufferBase(void)
39 , __pBufferBaseImpl(null)
43 BufferBase::~BufferBase(void)
45 if ((_pData != null) && (Release() == 0))
53 BufferBase::Construct(int capacity)
55 SysTryReturn(NID_BASE, capacity >= 0, E_INVALID_ARG, E_INVALID_ARG,
56 "[%s] Invalid argument is used. The capacity is negative", GetErrorMessage(E_INVALID_ARG));
61 // check whether the size of memory is larger than the maximum of unsigned int
62 unsigned long long size = static_cast <unsigned long long>(sizeof(_BufferData)) +
63 static_cast <unsigned long long>(capacity) * static_cast <unsigned long long>(GetTypeSize());
64 SysTryReturnResult(NID_BASE, size <= static_cast <unsigned long long>((unsigned int) -1), E_OUT_OF_MEMORY,
65 "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
67 pTemp = malloc(static_cast <unsigned int>(size));
68 SysTryReturnResult(NID_BASE, 0 != pTemp, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.",
69 GetErrorMessage(E_OUT_OF_MEMORY));
71 memset(pTemp, 0, static_cast <unsigned int>(size));
73 _pData = static_cast <_BufferData*>(pTemp);
76 _pData->capacityInByte = capacity * GetTypeSize();
83 __pArrayStart = _pData->GetArray();
90 BufferBase::Clear(void)
98 BufferBase::Compact(void)
100 int remaining = GetRemaining();
101 if (HasRemaining() && (0 != _position))
103 int offset = _position * GetTypeSize();
104 int byteNum = remaining * GetTypeSize();
105 for (int i = 0; i < byteNum; i++)
107 __pArrayStart[i] = __pArrayStart[offset + i];
110 _position = remaining;
116 BufferBase::Flip(PositionTo to)
119 if ((to == POSITION_TO_MARK) && (_mark > -1))
131 BufferBase::GetHashCode(void) const
133 int len = (GetRemaining() * GetTypeSize()) / sizeof(len);
135 // s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1]
137 int offset = _position * GetTypeSize();
138 for (int i = 0; i < len; ++i)
140 hash += (int) __pArrayStart[offset + (i * sizeof(hash))] * (int) pow((double) 31, len - (i + 1));
147 BufferBase::InvalidateMark(void)
153 BufferBase::Reset(void)
155 SysTryReturnResult(NID_BASE, _mark >= 0, E_INVALID_OPERATION, "[%s] The mark has not been set.",
156 GetErrorMessage(E_INVALID_OPERATION));
163 BufferBase::Rewind(void)
170 BufferBase::ShiftLimit(int amount)
172 SysTryReturnResult(NID_BASE, (((_limit + amount) <= _capacity) && ((_limit + amount) >= 0)), E_OUT_OF_RANGE,
173 "The amount(%d) is larger than the capacity(%d), or smaller than zero starting from the current limit(%d).",
174 amount, _capacity, _limit);
176 return SetLimit(_limit + amount);
180 BufferBase::GetCapacity(void) const
186 BufferBase::GetLimit(void) const
192 BufferBase::GetMark(void) const
198 BufferBase::GetPosition(void) const
204 BufferBase::GetRemaining(void) const
206 return _limit - _position;
210 BufferBase::SetLimit(int limit)
212 SysTryReturnResult(NID_BASE, limit <= _capacity && limit >= 0, E_OUT_OF_RANGE,
213 "The limit(%d) MUST be greater than or equal to 0 and less than the current capacity(%d).", limit, _capacity);
218 if (_position > limit)
231 BufferBase::SetMark(void)
237 BufferBase::SetPosition(int position)
239 SysTryReturnResult(NID_BASE, position <= _limit && position >= 0, E_OUT_OF_RANGE,
240 "The position(%d) MUST be greater than or equal to 0 and less than the current limit(%d).", position, _limit);
242 _position = position;
243 if (_mark > _position)
252 BufferBase::HasRemaining(void) const
254 return _limit > _position;
258 BufferBase::ExpandCapacity(int newCapacity)
260 SysTryReturnResult(NID_BASE, newCapacity > _capacity, E_INVALID_ARG,
261 "The capacity is less than the current capacity.");
263 // check whether the size of memory is larger than the maximum of unsigned int
264 unsigned long long size = static_cast <unsigned long long>(sizeof(_BufferData)) +
265 static_cast <unsigned long long>(newCapacity) * static_cast <unsigned long long>(GetTypeSize());
267 SysTryReturnResult(NID_BASE, size <= static_cast <unsigned long long>((unsigned int) -1), E_OUT_OF_MEMORY,
268 "Memory allocation failed.");
270 _pData = static_cast<_BufferData*>(realloc(_pData, static_cast<unsigned int>(size)));
271 SysTryReturnResult(NID_BASE, 0 != _pData, E_OUT_OF_MEMORY, "Memory allocation failed.");
273 _capacity = newCapacity;
274 _limit = newCapacity;
276 __pArrayStart = _pData->GetArray();
282 BufferBase::AddRef(void) const
284 SysAssertf(_pData->refCount > 0, "refCount(%d) is not greater than zero.", _pData->refCount);
288 return _pData->refCount;
292 BufferBase::Release(void) const
294 SysAssertf(_pData->refCount > 0, "refCount is not greater than zero(impossible!).");
298 return _pData->refCount;
302 BufferBase::Dispose(void)
306 BufferBase::_BufferData::_BufferData()
312 BufferBase::_BufferData::~_BufferData()
317 BufferBase::_BufferData::GetArray(void)
319 return reinterpret_cast <byte*>(this + 1);