Tizen 2.1 base
[framework/osp/uifw.git] / src / graphics / FGrp_FontMemoryManager.cpp
1 //
2 // Open Service Platform
3 // Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
4 //
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
8 //
9 //     http://floralicense.org/license/
10 //
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.
16 //
17
18 /*
19  * @file        FGrp_FontMemoryManager.cpp
20  * @brief       This is the cpp file for _FontMemoryManager class.
21  *
22  */
23
24 #include <new>
25
26 #include <FBaseTypes.h>
27
28 #include "FGrp_FontMemoryManager.h"
29
30
31 namespace Tizen { namespace Graphics
32 {
33
34 class CountBasedMemoryMgr
35         : public _FontMemoryManager::IMemoryAllocator
36 {
37 public:
38         CountBasedMemoryMgr(unsigned long size)
39                 : __maxCount(size)
40                 , __currentCount(0)
41         {
42         }
43
44         virtual ~CountBasedMemoryMgr(void) {}
45
46         virtual void* Alloc(unsigned long size)
47         {
48                 if (__currentCount >= __maxCount)
49                 {
50                         return null;
51                 }
52
53                 __currentCount++;
54
55                 return new (std::nothrow) char[size];
56         }
57
58         virtual void Free(void* pMem)
59         {
60                 if (__currentCount <= 0)
61                 {
62                         return;
63                 }
64
65                 __currentCount--;
66
67                 delete [] (char*)(pMem);
68         }
69
70         virtual unsigned long GetRemainedMemory()
71         {
72                 return 0;
73         }
74
75 private:
76         unsigned long __maxCount;
77         unsigned long __currentCount;
78 };
79
80 class SizeBasedMemoryMgr
81         : public _FontMemoryManager::IMemoryAllocator
82 {
83         typedef unsigned long PtrDiffType;
84
85 public:
86         SizeBasedMemoryMgr(unsigned long  size)
87                 : __maxMemory(size)
88                 , __currentUsedMemory(0)
89         {
90         }
91
92         virtual ~SizeBasedMemoryMgr(void)
93         {
94         }
95
96         virtual void* Alloc(unsigned long size)
97         {
98                 size += sizeof(PtrDiffType);
99
100                 if (__currentUsedMemory + size > __maxMemory)
101                 {
102                         return null;
103                 }
104
105                 void* pMem = new (std::nothrow) char[size];
106                 if (pMem)
107                 {
108                         *((PtrDiffType*)pMem) = size;
109                         pMem = (void*)((char*)pMem + sizeof(PtrDiffType));
110                         __currentUsedMemory += size;
111                 }
112
113                 return pMem;
114         }
115
116         virtual void Free(void* pMem)
117         {
118                 if (__currentUsedMemory <= 0 || pMem == null)
119                 {
120                         return;
121                 }
122
123                 pMem = (void*)((char*)pMem - sizeof(PtrDiffType));
124                 __currentUsedMemory -= *((PtrDiffType*)pMem);
125
126                 delete [] (char*)(pMem);
127         }
128
129         virtual unsigned long GetRemainedMemory()
130         {
131                 return __maxMemory - __currentUsedMemory;
132         }
133
134 private:
135         unsigned long __maxMemory;
136         unsigned long __currentUsedMemory;
137 };
138
139 class FixedSizeMemoryMgr
140         : public _FontMemoryManager::IMemoryAllocator
141 {
142 public:
143         FixedSizeMemoryMgr(unsigned long size)
144         {
145         }
146
147         virtual ~FixedSizeMemoryMgr(void) {}
148
149         virtual void* Alloc(unsigned long size)
150         {
151                 return null;
152         }
153
154         virtual void Free(void* pMem)
155         {
156         }
157
158         virtual unsigned long GetRemainedMemory()
159         {
160                 return 0;
161         }
162 };
163
164 _FontMemoryManager::_FontMemoryManager(_FontMemoryManager::Type type, int size)
165         : __pAllocator(null)
166 {
167         IMemoryAllocator* p = null;
168         switch (type)
169         {
170         case _FontMemoryManager::TYPE_COUNT:
171                 p = new (std::nothrow) CountBasedMemoryMgr(size);
172                 break;
173         case _FontMemoryManager::TYPE_SIZE:
174                 p = new (std::nothrow) SizeBasedMemoryMgr(size);
175                 break;
176         case _FontMemoryManager::TYPE_FIXED_MEMORY:
177                 p = new (std::nothrow) FixedSizeMemoryMgr(size);
178                 break;
179         default:
180                 break;
181         }
182
183         __pAllocator = p;
184 }
185
186 _FontMemoryManager::~_FontMemoryManager(void)
187 {
188         if (__pAllocator)
189         {
190                 delete __pAllocator;
191         }
192         __pAllocator = null;
193 }
194
195 void*
196 _FontMemoryManager::Alloc(unsigned long size)
197 {
198         if (__pAllocator == null)
199         {
200                 return null;
201         }
202
203         return __pAllocator->Alloc(size);
204 }
205
206 void
207 _FontMemoryManager::Free(void* pMemory)
208 {
209         if (__pAllocator)
210         {
211                 return __pAllocator->Free(pMemory);
212         }
213 }
214
215 unsigned long
216 _FontMemoryManager::GetRemainedMemory()
217 {
218         if (__pAllocator)
219         {
220                 return __pAllocator->GetRemainedMemory();
221         }
222
223         return 0;
224 }
225
226 bool
227 _FontMemoryManager::IsValid() const
228 {
229         return (__pAllocator != null);
230 }
231
232 }} // Tizen::Graphics