1 // Copyright 2014 PDFium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
10 #include "fx_system.h"
12 #define FXMEM_NONLEAVE 1
13 #define FXMEM_MOVABLE 2
14 #define FXMEM_DISCARDABLE 4
18 typedef struct _FXMEM_SystemMgr {
20 void* (*Alloc)(struct _FXMEM_SystemMgr* pMgr, size_t size, int flags);
22 void* (*AllocDebug)(struct _FXMEM_SystemMgr* pMgr, size_t size, int flags, FX_LPCSTR file, int line);
24 void* (*Realloc)(struct _FXMEM_SystemMgr* pMgr, void* pointer, size_t size, int flags);
26 void* (*ReallocDebug)(struct _FXMEM_SystemMgr* pMgr, void* pointer, size_t size, int flags, FX_LPCSTR file, int line);
28 void* (*Lock)(struct _FXMEM_SystemMgr* pMgr, void* handle);
30 void (*Unlock)(struct _FXMEM_SystemMgr* pMgr, void* handle);
32 void (*Free)(struct _FXMEM_SystemMgr* pMgr, void* pointer, int flags);
34 void (*Purge)(struct _FXMEM_SystemMgr* pMgr);
36 void (*CollectAll)(struct _FXMEM_SystemMgr* pMgr);
41 FX_DEFINEHANDLE(FXMEM_FoxitMgr)
42 typedef struct _FXMEM_SystemMgr2 {
44 FX_BOOL (*More)(struct _FXMEM_SystemMgr2* pMgr, size_t alloc_size, void** new_memory, size_t* new_size);
46 void (*Free)(struct _FXMEM_SystemMgr2* pMgr, void* memory);
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);
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);
53 void* FXMEM_DefaultRealloc2(void* pointer, size_t units, size_t unit_size, int flags);
54 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)
65 #define FX_NEW new(__FILE__, __LINE__)
74 void* operator new (size_t size, FX_LPCSTR file, int line);
76 void operator delete (void* p, FX_LPCSTR file, int line);
78 void* operator new (size_t size);
80 void operator delete (void* p);
82 void* operator new[] (size_t size, FX_LPCSTR file, int line);
84 void operator delete[] (void* p, FX_LPCSTR file, int line);
86 void* operator new[] (size_t size);
88 void operator delete[] (void* p);
90 void* operator new (size_t, void* buf)
95 void operator delete (void*, void*) {}
97 #define FX_NEW_VECTOR(Pointer, Class, Count) \
99 Pointer = FX_Alloc(Class, Count); \
101 for (int i = 0; i < (Count); i ++) new (Pointer + i) Class; \
104 #define FX_DELETE_VECTOR(Pointer, Class, Count) \
106 for (int i = 0; i < (Count); i ++) Pointer[i].~Class(); \
109 class CFX_DestructObject : public CFX_Object
113 virtual ~CFX_DestructObject() {}
118 typedef struct _IFX_Allocator {
120 void* (*m_AllocDebug)(struct _IFX_Allocator* pAllocator, size_t size, FX_LPCSTR file, int line);
122 void* (*m_Alloc)(struct _IFX_Allocator* pAllocator, size_t size);
124 void* (*m_ReallocDebug)(struct _IFX_Allocator* pAllocator, void* p, size_t size, FX_LPCSTR file, int line);
126 void* (*m_Realloc)(struct _IFX_Allocator* pAllocator, void* p, size_t size);
128 void (*m_Free)(struct _IFX_Allocator* pAllocator, void* p);
130 IFX_Allocator* FXMEM_GetDefAllocator();
136 #define FX_Allocator_Alloc(fxAllocator, type, size) \
137 ((fxAllocator) ? (type*)(fxAllocator)->m_AllocDebug((fxAllocator), (size) * sizeof(type), __FILE__, __LINE__) : (FX_Alloc(type, size)))
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)))
143 #define FX_Allocator_Alloc(fxAllocator, type, size) \
144 ((fxAllocator) ? (type*)(fxAllocator)->m_Alloc((fxAllocator), (size) * sizeof(type)) : (FX_Alloc(type, size)))
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)))
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)
153 return (void*)FX_Allocator_Alloc(fxAllocator, FX_BYTE, size);
155 inline void operator delete(void* ptr, IFX_Allocator* fxAllocator)
158 #define FX_NewAtAllocator(fxAllocator) \
160 #define FX_DeleteAtAllocator(pointer, fxAllocator, __class__) \
161 (pointer)->~__class__(); \
162 FX_Allocator_Free(fxAllocator, pointer)
163 class CFX_AllocObject
167 void* operator new (size_t size, IFX_Allocator* pAllocator, FX_LPCSTR file, int line);
168 #ifndef _FX_NO_EXCEPTION_
170 void operator delete (void* p, IFX_Allocator* pAllocator, FX_LPCSTR file, int line);
173 void* operator new (size_t size, IFX_Allocator* pAllocator);
175 void operator delete (void* p);
176 #ifndef _FX_NO_EXCEPTION_
178 void operator delete (void* p, IFX_Allocator* pAllocator);
181 void* operator new (size_t, void* buf)
185 #ifndef _FX_NO_EXCEPTION_
187 void operator delete (void*, void*) {}
190 IFX_Allocator* GetAllocator() const
196 void* operator new[] (size_t size, IFX_Allocator* pAllocator, FX_LPCSTR file, int line)
198 return operator new(size, pAllocator, file, line);
200 #ifndef _FX_NO_EXCEPTION_
202 void operator delete[] (void* p, IFX_Allocator* pAllocator, FX_LPCSTR file, int line) {}
205 void* operator new[] (size_t size, IFX_Allocator* pAllocator)
207 return operator new(size, pAllocator);
210 void operator delete[] (void* p) {}
211 #ifndef _FX_NO_EXCEPTION_
213 void operator delete[] (void* p, IFX_Allocator* pAllocator) {}
217 IFX_Allocator* m_pAllocator;
220 #define FX_NEWAT(pAllocator) new(pAllocator, __FILE__, __LINE__)
223 #define FX_NEWAT(pAllocator) new(pAllocator)
225 class CFX_GrowOnlyPool : public IFX_Allocator, public CFX_Object
229 CFX_GrowOnlyPool(IFX_Allocator* pAllocator = NULL, size_t trunk_size = 16384);
233 void SetAllocator(IFX_Allocator* pAllocator);
235 void SetTrunkSize(size_t trunk_size)
237 m_TrunkSize = trunk_size;
240 void* AllocDebug(size_t size, FX_LPCSTR file, int line)
245 void* Alloc(size_t size);
247 void* ReallocDebug(void* p, size_t new_size, FX_LPCSTR file, int line)
252 void* Realloc(void* p, size_t new_size)
266 IFX_Allocator* m_pAllocator;
272 #define FX_FIXEDMEM_PAGESIZE (4096 * 16)
273 #define FX_FIXEDMEM_MIDBLOCKSIZE (4096)
274 typedef struct _FX_MEMCONFIG {
276 size_t nPageNum_Init8;
278 size_t nPageNum_Init16;
280 size_t nPageNum_Init32;
282 size_t nPageNum_More16;
284 size_t nPageNum_More32;
286 size_t nPageSize_Mid;
288 size_t nPageNum_InitMid;
290 size_t nPageNum_MoreMid;
292 size_t nPageSize_Large;
294 size_t nPageSize_Alone;
296 void FXMEM_SetConfig(const FX_MEMCONFIG* memConfig);