2 // Open Service Platform
3 // Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
5 // Licensed under the Flora License, Version 1.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://floralicense.org/license/
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 FGrp_FontMemoryManager.cpp
20 * @brief This is the cpp file for _FontMemoryManager class.
26 #include <FBaseTypes.h>
28 #include "FGrp_FontMemoryManager.h"
31 namespace Tizen { namespace Graphics
34 class CountBasedMemoryMgr
35 : public _FontMemoryManager::IMemoryAllocator
38 CountBasedMemoryMgr(unsigned long size)
44 virtual ~CountBasedMemoryMgr(void) {}
46 virtual void* Alloc(unsigned long size)
48 if (__currentCount >= __maxCount)
55 return new (std::nothrow) char[size];
58 virtual void Free(void* pMem)
60 if (__currentCount <= 0)
67 delete [] (char*)(pMem);
70 virtual unsigned long GetRemainedMemory()
76 unsigned long __maxCount;
77 unsigned long __currentCount;
80 class SizeBasedMemoryMgr
81 : public _FontMemoryManager::IMemoryAllocator
83 typedef unsigned long PtrDiffType;
86 SizeBasedMemoryMgr(unsigned long size)
88 , __currentUsedMemory(0)
92 virtual ~SizeBasedMemoryMgr(void)
96 virtual void* Alloc(unsigned long size)
98 size += sizeof(PtrDiffType);
100 if (__currentUsedMemory + size > __maxMemory)
105 void* pMem = new (std::nothrow) char[size];
108 *((PtrDiffType*)pMem) = size;
109 pMem = (void*)((char*)pMem + sizeof(PtrDiffType));
110 __currentUsedMemory += size;
116 virtual void Free(void* pMem)
118 if (__currentUsedMemory <= 0 || pMem == null)
123 pMem = (void*)((char*)pMem - sizeof(PtrDiffType));
124 __currentUsedMemory -= *((PtrDiffType*)pMem);
126 delete [] (char*)(pMem);
129 virtual unsigned long GetRemainedMemory()
131 return __maxMemory - __currentUsedMemory;
135 unsigned long __maxMemory;
136 unsigned long __currentUsedMemory;
139 class FixedSizeMemoryMgr
140 : public _FontMemoryManager::IMemoryAllocator
143 FixedSizeMemoryMgr(unsigned long size)
147 virtual ~FixedSizeMemoryMgr(void) {}
149 virtual void* Alloc(unsigned long size)
154 virtual void Free(void* pMem)
158 virtual unsigned long GetRemainedMemory()
164 _FontMemoryManager::_FontMemoryManager(_FontMemoryManager::Type type, int size)
167 IMemoryAllocator* p = null;
170 case _FontMemoryManager::TYPE_COUNT:
171 p = new (std::nothrow) CountBasedMemoryMgr(size);
173 case _FontMemoryManager::TYPE_SIZE:
174 p = new (std::nothrow) SizeBasedMemoryMgr(size);
176 case _FontMemoryManager::TYPE_FIXED_MEMORY:
177 p = new (std::nothrow) FixedSizeMemoryMgr(size);
186 _FontMemoryManager::~_FontMemoryManager(void)
196 _FontMemoryManager::Alloc(unsigned long size)
198 if (__pAllocator == null)
203 return __pAllocator->Alloc(size);
207 _FontMemoryManager::Free(void* pMemory)
211 return __pAllocator->Free(pMemory);
216 _FontMemoryManager::GetRemainedMemory()
220 return __pAllocator->GetRemainedMemory();
227 _FontMemoryManager::IsValid() const
229 return (__pAllocator != null);
232 }} // Tizen::Graphics