OLD | NEW |
1 // Copyright 2014 PDFium Authors. All rights reserved. | 1 // Copyright 2014 PDFium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com | 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com |
6 | 6 |
7 #ifndef _FX_MEMORY_H_ | 7 #ifndef _FX_MEMORY_H_ |
8 #define _FX_MEMORY_H_ | 8 #define _FX_MEMORY_H_ |
9 #ifndef _FX_SYSTEM_H_ | 9 #ifndef _FX_SYSTEM_H_ |
10 #include "fx_system.h" | 10 #include "fx_system.h" |
11 #endif | 11 #endif |
12 #define FXMEM_NONLEAVE 1 | |
13 #define FXMEM_MOVABLE 2 | |
14 #define FXMEM_DISCARDABLE 4 | |
15 #ifdef __cplusplus | 12 #ifdef __cplusplus |
16 extern "C" { | 13 extern "C" { |
17 #endif | 14 #endif |
18 typedef struct _FXMEM_SystemMgr { | 15 #define FX_Alloc(type, size)» » » » » » (type*)c
alloc(size, sizeof(type)) |
19 | 16 #define FX_Realloc(type, ptr, size)» » » » » (type*)r
ealloc(ptr, sizeof(type) * (size)) |
20 void* (*Alloc)(struct _FXMEM_SystemMgr* pMgr, size_t size, int flags); | 17 #define FX_AllocNL(type, size)» » » » » » FX_Alloc
(type, size) |
21 | 18 #define FX_ReallocNL(type, ptr, size)» » » » FX_Realloc(type,
ptr, size) |
22 void* (*AllocDebug)(struct _FXMEM_SystemMgr* pMgr, size_t size, int flags, F
X_LPCSTR file, int line); | 19 #define FX_Free(ptr)» » » » » » » »
free(ptr) |
23 | |
24 void* (*Realloc)(struct _FXMEM_SystemMgr* pMgr, void* pointer, size_t size,
int flags); | |
25 | |
26 void* (*ReallocDebug)(struct _FXMEM_SystemMgr* pMgr, void* pointer, size_t s
ize, int flags, FX_LPCSTR file, int line); | |
27 | |
28 void* (*Lock)(struct _FXMEM_SystemMgr* pMgr, void* handle); | |
29 | |
30 void (*Unlock)(struct _FXMEM_SystemMgr* pMgr, void* handle); | |
31 | |
32 void (*Free)(struct _FXMEM_SystemMgr* pMgr, void* pointer, int flags); | |
33 | |
34 void (*Purge)(struct _FXMEM_SystemMgr* pMgr); | |
35 | |
36 void (*CollectAll)(struct _FXMEM_SystemMgr* pMgr); | |
37 | |
38 | |
39 void* user; | |
40 } FXMEM_SystemMgr; | |
41 FX_DEFINEHANDLE(FXMEM_FoxitMgr) | |
42 typedef struct _FXMEM_SystemMgr2 { | |
43 | |
44 FX_BOOL» (*More)(struct _FXMEM_SystemMgr2* pMgr, size_t alloc_size, void*
* new_memory, size_t* new_size); | |
45 | |
46 void» (*Free)(struct _FXMEM_SystemMgr2* pMgr, void* memory); | |
47 } FXMEM_SystemMgr2; | |
48 FXMEM_FoxitMgr* FXMEM_CreateMemoryMgr(size_t size, FX_BOOL extensible); | |
49 void» FXMEM_DestroyFoxitMgr(FXMEM_FoxitMgr* pFoxitMgr); | |
50 void* FXMEM_DefaultAlloc(size_t byte_size, int flags); | 20 void* FXMEM_DefaultAlloc(size_t byte_size, int flags); |
51 void* FXMEM_DefaultAlloc2(size_t units, size_t unit_size, int flags); | |
52 void* FXMEM_DefaultRealloc(void* pointer, size_t new_size, int flags); | 21 void* FXMEM_DefaultRealloc(void* pointer, size_t new_size, int flags); |
53 void* FXMEM_DefaultRealloc2(void* pointer, size_t units, size_t unit_size, int
flags); | |
54 void FXMEM_DefaultFree(void* pointer, int flags); | 22 void FXMEM_DefaultFree(void* pointer, int flags); |
55 #define FX_Alloc(type, size) (type*)FXMEM_DefaultAlloc2(size,
sizeof(type), 0) | |
56 #define FX_Realloc(type, ptr, size) (type*)FXMEM_DefaultRealloc2(ptr
, size, sizeof(type), 0) | |
57 #define FX_AllocNL(type, size) (type*)FXMEM_DefaultAlloc2(size,
sizeof(type), FXMEM_NONLEAVE) | |
58 #define FX_ReallocNL(type, ptr, size) (type*)FXMEM_DefaultRealloc2(ptr, size,
sizeof(type), FXMEM_NONLEAVE) | |
59 #define FX_Free(pointer) FXMEM_DefaultFree(pointer, 0) | |
60 #ifdef __cplusplus | 23 #ifdef __cplusplus |
61 } | 24 } |
62 #endif | 25 #endif |
63 #ifdef __cplusplus | 26 #ifdef __cplusplus |
| 27 class CFX_Object |
| 28 { |
| 29 public: |
| 30 void* operator new (size_t size, FX_LPCSTR file, int l
ine) |
| 31 { |
| 32 return malloc(size); |
| 33 } |
| 34 void operator delete (void* p, FX_LPCSTR file, int li
ne) |
| 35 { |
| 36 free(p); |
| 37 } |
| 38 void* operator new (size_t size) |
| 39 { |
| 40 return malloc(size); |
| 41 } |
| 42 void operator delete (void* p) |
| 43 { |
| 44 free(p); |
| 45 } |
| 46 void* operator new[] (size_t size, FX_LPCSTR file, int
line) |
| 47 { |
| 48 return malloc(size); |
| 49 } |
| 50 void operator delete[] (void* p, FX_LPCSTR file, int
line) |
| 51 { |
| 52 free(p); |
| 53 } |
| 54 void* operator new[] (size_t size) |
| 55 { |
| 56 return malloc(size); |
| 57 } |
| 58 void operator delete[] (void* p) |
| 59 { |
| 60 free(p); |
| 61 } |
| 62 void* operator new (size_t, void* buf) |
| 63 { |
| 64 return buf; |
| 65 } |
| 66 void operator delete (void*, void*)
{} |
| 67 }; |
| 68 #endif |
| 69 #ifdef __cplusplus |
64 #if defined(_DEBUG) | 70 #if defined(_DEBUG) |
65 #define FX_NEW new(__FILE__, __LINE__) | 71 #define FX_NEW new(__FILE__, __LINE__) |
66 #else | 72 #else |
67 | 73 |
68 #define FX_NEW new | 74 #define FX_NEW new |
69 #endif | 75 #endif |
70 class CFX_Object | |
71 { | |
72 public: | |
73 | |
74 void* operator new (size_t size, FX_LPCSTR file, int l
ine); | |
75 | |
76 void operator delete (void* p, FX_LPCSTR file, int li
ne); | |
77 | |
78 void* operator new (size_t size); | |
79 | |
80 void operator delete (void* p); | |
81 | |
82 void* operator new[] (size_t size, FX_LPCSTR file, int
line); | |
83 | |
84 void operator delete[] (void* p, FX_LPCSTR file, int
line); | |
85 | |
86 void* operator new[] (size_t size); | |
87 | |
88 void operator delete[] (void* p); | |
89 | |
90 void* operator new (size_t, void* buf) | |
91 { | |
92 return buf; | |
93 } | |
94 | |
95 void operator delete (void*, void*) {} | |
96 }; | |
97 #define FX_NEW_VECTOR(Pointer, Class, Count) \ | 76 #define FX_NEW_VECTOR(Pointer, Class, Count) \ |
98 { \ | 77 { \ |
99 Pointer = FX_Alloc(Class, Count); \ | 78 Pointer = FX_Alloc(Class, Count); \ |
100 if (Pointer) { \ | 79 if (Pointer) { \ |
101 for (int i = 0; i < (Count); i ++) new (Pointer + i) Class; \ | 80 for (int i = 0; i < (Count); i ++) new (Pointer + i) Class; \ |
102 } \ | 81 } \ |
103 } | 82 } |
104 #define FX_DELETE_VECTOR(Pointer, Class, Count) \ | 83 #define FX_DELETE_VECTOR(Pointer, Class, Count) \ |
105 { \ | 84 { \ |
106 for (int i = 0; i < (Count); i ++) Pointer[i].~Class(); \ | 85 for (int i = 0; i < (Count); i ++) Pointer[i].~Class(); \ |
107 FX_Free(Pointer); \ | 86 FX_Free(Pointer); \ |
108 } | 87 } |
109 class CFX_DestructObject : public CFX_Object | 88 class CFX_DestructObject : public CFX_Object |
110 { | 89 { |
111 public: | 90 public: |
112 | 91 |
113 virtual ~CFX_DestructObject() {} | 92 virtual ~CFX_DestructObject() {} |
114 }; | 93 }; |
115 #ifdef __cplusplus | 94 class CFX_GrowOnlyPool : public CFX_Object |
116 extern "C" { | |
117 #endif | |
118 typedef struct _IFX_Allocator { | |
119 | |
120 void*» (*m_AllocDebug)(struct _IFX_Allocator* pAllocator, size_t num, s
ize_t size, FX_LPCSTR file, int line); | |
121 | |
122 void*» (*m_Alloc)(struct _IFX_Allocator* pAllocator, size_t num, size_t
size); | |
123 | |
124 void*» (*m_ReallocDebug)(struct _IFX_Allocator* pAllocator, void* p, si
ze_t num, size_t size, FX_LPCSTR file, int line); | |
125 | |
126 void*» (*m_Realloc)(struct _IFX_Allocator* pAllocator, void* p, size_t
num, size_t size); | |
127 | |
128 void» (*m_Free)(struct _IFX_Allocator* pAllocator, void* p); | |
129 } IFX_Allocator; | |
130 IFX_Allocator* FXMEM_GetDefAllocator(); | |
131 #ifdef __cplusplus | |
132 } | |
133 #endif | |
134 #ifdef _DEBUG | |
135 | |
136 #define FX_Allocator_Alloc(fxAllocator, type, size) \ | |
137 ((fxAllocator) ? (type*)(fxAllocator)->m_AllocDebug((fxAllocator), (size), s
izeof(type), __FILE__, __LINE__) : (FX_Alloc(type, size))) | |
138 | |
139 #define FX_Allocator_Realloc(fxAllocator, type, ptr, new_size) \ | |
140 ((fxAllocator) ? (type*)(fxAllocator)->m_ReallocDebug((fxAllocator), (ptr),
(new_size) , sizeof(type), __FILE__, __LINE__) : (FX_Realloc(type, ptr, new_size
))) | |
141 #else | |
142 | |
143 #define FX_Allocator_Alloc(fxAllocator, type, size) \ | |
144 ((fxAllocator) ? (type*)(fxAllocator)->m_Alloc((fxAllocator), (size), sizeof
(type)) : (FX_Alloc(type, size))) | |
145 | |
146 #define FX_Allocator_Realloc(fxAllocator, type, ptr, new_size) \ | |
147 ((fxAllocator) ? (type*)(fxAllocator)->m_Realloc((fxAllocator), (ptr), (new_
size), sizeof(type)) : (FX_Realloc(type, ptr, new_size))) | |
148 #endif | |
149 #define FX_Allocator_Free(fxAllocator, ptr) \ | |
150 ((fxAllocator) ? (fxAllocator)->m_Free((fxAllocator), (ptr)) : (FX_Free(ptr)
)) | |
151 inline void* operator new(size_t size, IFX_Allocator* fxAllocator) | |
152 { | |
153 return (void*)FX_Allocator_Alloc(fxAllocator, FX_BYTE, size); | |
154 } | |
155 inline void operator delete(void* ptr, IFX_Allocator* fxAllocator) | |
156 { | |
157 } | |
158 #define FX_NewAtAllocator(fxAllocator) \ | |
159 ::new(fxAllocator) | |
160 #define FX_DeleteAtAllocator(pointer, fxAllocator, __class__) \ | |
161 (pointer)->~__class__(); \ | |
162 FX_Allocator_Free(fxAllocator, pointer) | |
163 class CFX_AllocObject | |
164 { | 95 { |
165 public: | 96 public: |
166 | 97 |
167 void*» » » operator new (size_t size, IFX_Allocator* pAlloc
ator, FX_LPCSTR file, int line); | 98 CFX_GrowOnlyPool(size_t trunk_size = 16384); |
168 #ifndef _FX_NO_EXCEPTION_ | |
169 | |
170 void» » » operator delete (void* p, IFX_Allocator* pAlloca
tor, FX_LPCSTR file, int line); | |
171 #endif | |
172 | |
173 void*» » » operator new (size_t size, IFX_Allocator* pAlloc
ator); | |
174 | |
175 void» » » operator delete (void* p); | |
176 #ifndef _FX_NO_EXCEPTION_ | |
177 | |
178 void» » » operator delete (void* p, IFX_Allocator* pAlloca
tor); | |
179 #endif | |
180 | |
181 void*» » » operator new (size_t, void* buf) | |
182 { | |
183 return buf; | |
184 } | |
185 #ifndef _FX_NO_EXCEPTION_ | |
186 | |
187 void» » » operator delete (void*, void*) {} | |
188 #endif | |
189 | |
190 IFX_Allocator*» GetAllocator() const | |
191 { | |
192 return m_pAllocator; | |
193 } | |
194 private: | |
195 | |
196 void*» » » operator new[] (size_t size, IFX_Allocator* pAll
ocator, FX_LPCSTR file, int line) | |
197 { | |
198 return operator new(size, pAllocator, file, line); | |
199 } | |
200 #ifndef _FX_NO_EXCEPTION_ | |
201 | |
202 void» » » operator delete[] (void* p, IFX_Allocator* pAllo
cator, FX_LPCSTR file, int line) {} | |
203 #endif | |
204 | |
205 void*» » » operator new[] (size_t size, IFX_Allocator* pAll
ocator) | |
206 { | |
207 return operator new(size, pAllocator); | |
208 } | |
209 | |
210 void» » » operator delete[] (void* p) {} | |
211 #ifndef _FX_NO_EXCEPTION_ | |
212 | |
213 void» » » operator delete[] (void* p, IFX_Allocator* pAllo
cator) {} | |
214 #endif | |
215 protected: | |
216 | |
217 IFX_Allocator*» m_pAllocator; | |
218 }; | |
219 #if defined(_DEBUG) | |
220 #define FX_NEWAT(pAllocator) new(pAllocator, __FILE__, __LINE__) | |
221 #else | |
222 | |
223 #define FX_NEWAT(pAllocator) new(pAllocator) | |
224 #endif | |
225 class CFX_GrowOnlyPool : public IFX_Allocator, public CFX_Object | |
226 { | |
227 public: | |
228 | |
229 CFX_GrowOnlyPool(IFX_Allocator* pAllocator = NULL, size_t trunk_size = 16384
); | |
230 | 99 |
231 ~CFX_GrowOnlyPool(); | 100 ~CFX_GrowOnlyPool(); |
232 | 101 |
233 void SetAllocator(IFX_Allocator* pAllocator); | |
234 | |
235 void SetTrunkSize(size_t trunk_size) | 102 void SetTrunkSize(size_t trunk_size) |
236 { | 103 { |
237 m_TrunkSize = trunk_size; | 104 m_TrunkSize = trunk_size; |
238 } | 105 } |
239 | 106 |
240 void* AllocDebug(size_t size, FX_LPCSTR file, int line) | 107 void* AllocDebug(size_t size, FX_LPCSTR file, int line) |
241 { | 108 { |
242 return Alloc(size); | 109 return Alloc(size); |
243 } | 110 } |
244 | 111 |
(...skipping 10 matching lines...) Expand all Loading... |
255 } | 122 } |
256 | 123 |
257 void Free(void*) {} | 124 void Free(void*) {} |
258 | 125 |
259 void FreeAll(); | 126 void FreeAll(); |
260 private: | 127 private: |
261 | 128 |
262 size_t m_TrunkSize; | 129 size_t m_TrunkSize; |
263 | 130 |
264 void* m_pFirstTrunk; | 131 void* m_pFirstTrunk; |
265 | |
266 IFX_Allocator* m_pAllocator; | |
267 }; | 132 }; |
268 #endif | 133 #endif |
269 #ifdef __cplusplus | |
270 extern "C" { | |
271 #endif | 134 #endif |
272 #define FX_FIXEDMEM_PAGESIZE (4096 * 16) | |
273 #define FX_FIXEDMEM_MIDBLOCKSIZE (4096) | |
274 typedef struct _FX_MEMCONFIG { | |
275 | |
276 size_t nPageNum_Init8; | |
277 | |
278 size_t nPageNum_Init16; | |
279 | |
280 size_t nPageNum_Init32; | |
281 | |
282 size_t nPageNum_More16; | |
283 | |
284 size_t nPageNum_More32; | |
285 | |
286 size_t nPageSize_Mid; | |
287 | |
288 size_t nPageNum_InitMid; | |
289 | |
290 size_t nPageNum_MoreMid; | |
291 | |
292 size_t nPageSize_Large; | |
293 | |
294 size_t nPageSize_Alone; | |
295 } FX_MEMCONFIG; | |
296 void FXMEM_SetConfig(const FX_MEMCONFIG* memConfig); | |
297 #ifdef __cplusplus | |
298 } | |
299 #endif | |
300 #endif | |
OLD | NEW |