Foxit PDF SDK

fx_memory.h
Go to the documentation of this file.
1
14 //<<<+++OPENSOURCE
15 //<<<+++OPENSOURCE_LICENSE
16 //<<<+++OPENSOURCE_BEGIN LIC==FOXIT||LIC==GOOGLE
17
23 /*
24 *
25 * FPDFAPI may be using different heap as the application (in case of DLL version of FPDFAPI),
26 * therefore, the application should use the following memory allocation and free functions
27 * in order to exchange pointers between the API and application.
28 *
29 *
30 * There are two layers of memory manager in FOXIT API:
31 *
32 * 1. System level memory manager. This level is system dependant.
33 * The API provides a default system memory manager (which uses CRT malloc/free functions),
34 * but application can implement their own manager to cater to different system environment.
35 *
36 * 2. Foxit memory manager. This level is system independant, but it relies on a system level manager.
37 * The API provides a default Foxit memory manager, which relies on the default system manager.
38 * Applications can not customize the Foxit memory manager, but they can create different
39 * Foxit memory manager instance, that rely on different underlying system manager
40 * (for example, each system manager may use a different heap, then, different Foxit manager
41 * will be able to use separate heaps.)
42 *
43 *
44 * Foxit memory manager also provide sub-heap feature for aggregating objects together to
45 * avoid memory fragmentation.
46 */
47
48 //<<<+++OPENSOURCE_MUST_BEGIN
49 #ifndef _FX_MEMORY_H_
50 #define _FX_MEMORY_H_
51
52 #ifndef _FX_SYSTEM_H_
53 #include "fx_system.h"
54 #endif
55 //<<<+++OPENSOURCE_MUST_END
56
58 #define FXMEM_NONLEAVE 1
59
60 #define FXMEM_MOVABLE 2
61
62 #define FXMEM_DISCARDABLE 4
63
64 // Base memory routines
65 #ifdef __cplusplus
66 extern "C" {
67 #endif
68
72 typedef struct _FXMEM_SystemMgr {
85 void* (*Alloc)(struct _FXMEM_SystemMgr* pMgr, size_t size, int flags);
100 void* (*AllocDebug)(struct _FXMEM_SystemMgr* pMgr, size_t size, int flags, FX_LPCSTR file, int line);
114 void* (*Realloc)(struct _FXMEM_SystemMgr* pMgr, void* pointer, size_t size, int flags);
130 void* (*ReallocDebug)(struct _FXMEM_SystemMgr* pMgr, void* pointer, size_t size, int flags, FX_LPCSTR file, int line);
142 void* (*Lock)(struct _FXMEM_SystemMgr* pMgr, void* handle);
154 void (*Unlock)(struct _FXMEM_SystemMgr* pMgr, void* handle);
167 void (*Free)(struct _FXMEM_SystemMgr* pMgr, void* pointer, int flags);
179 void (*Purge)(struct _FXMEM_SystemMgr* pMgr);
190 void (*CollectAll)(struct _FXMEM_SystemMgr* pMgr);
191
193 void* user;
194 } FXMEM_SystemMgr;
195
201 FX_DEFINEHANDLE(FXMEM_FoxitMgr)
202
203
215 FXMEM_FoxitMgr* FXMEM_CreateFoxitMgr(FXMEM_SystemMgr* pSystemMgr);
216
228 FXMEM_FoxitMgr* FXMEM_CreatePyMgr();
229
245 int FXMEM_SetPyConfig(size_t lowByteRange, size_t highByteRange);
246
250 typedef struct _FXMEM_SystemMgr2
251 {
262 FX_BOOL (*More)(struct _FXMEM_SystemMgr2* pMgr, size_t alloc_size, void** new_memory, size_t* new_size);
271 void (*Free)(struct _FXMEM_SystemMgr2* pMgr, void* memory);
272 } FXMEM_SystemMgr2;
273
289 FXMEM_FoxitMgr* FXMEM_CreateFixedMgr(void* pMemory, size_t size, FXMEM_SystemMgr2* pExtender);
290
304 FXMEM_FoxitMgr* FXMEM_CreateMemoryMgr(size_t size, FX_BOOL extensible);
305
314 size_t FXMEM_GetBlockSizeInFixedMgr(FXMEM_FoxitMgr* pFoxitMgr, void* ptr);
315
321 FXMEM_FoxitMgr* FXMEM_GetDefaultMgr();
322
329 void FXMEM_SetDefaultMgr(FXMEM_FoxitMgr* pFoxitMgr);
330
336 void FXMEM_ResetSystemMgr();
337
345 void FXMEM_DestroyFoxitMgr(FXMEM_FoxitMgr* pFoxitMgr);
346
351
361 void* FXMEM_Alloc(FXMEM_FoxitMgr* pFoxitMgr, size_t size, int flags);
373 void* FXMEM_AllocDebug(FXMEM_FoxitMgr* pFoxitMgr, size_t size, int flags, FX_LPCSTR file, int line);
384 void* FXMEM_Realloc(FXMEM_FoxitMgr* pFoxitMgr, void* pointer, size_t new_size, int flags);
397 void* FXMEM_ReallocDebug(FXMEM_FoxitMgr* pFoxitMgr, void* pointer, size_t new_size, int flags, FX_LPCSTR file, int line);
407 void FXMEM_Free(FXMEM_FoxitMgr* pFoxitMgr, void* pointer, int flags);
408
418 void FXMEM_CollectAll(FXMEM_FoxitMgr* pFoxitMgr);
419
429 void FXMEM_PurgeMgr(FXMEM_FoxitMgr* pFoxitMgr);
430
438 void FXMEM_ReportOOM(FXMEM_FoxitMgr* pFoxitMgr);
439
443 typedef struct {
457 void (*OnAlloc)(FXMEM_FoxitMgr* pMgr, void* p, size_t size, int flags);
473 void (*OnAllocDebug)(FXMEM_FoxitMgr* pMgr, void* p, size_t size, int flags, FX_LPCSTR file, int line);
488 void (*OnRealloc)(FXMEM_FoxitMgr* pMgr, void* old_p, void* new_p, size_t size, int flags);
505 void (*OnReallocDebug)(FXMEM_FoxitMgr* pMgr, void* old_p, void* new_p, size_t size, int flags, FX_LPCSTR file, int line);
518 void (*OnFree)(FXMEM_FoxitMgr* pMgr, void* p, int flags);
530 void (*OnTag)(FXMEM_FoxitMgr* pMgr, FX_LPCSTR tag);
531 } FXMEM_Debugger;
532
541 void FXMEM_UseDebugger(FXMEM_FoxitMgr* pFoxitMgr, FXMEM_Debugger* pDebugger);
542
551 void FXMEM_OutputDebugTag(FXMEM_FoxitMgr* pFoxitMgr, FX_LPCSTR tag);
552
561 typedef void (*FPDF_OOM_Handler)(FXMEM_FoxitMgr* pFoxitMgr, void* param);
562
572 void FXMEM_SetOOMHandler(FXMEM_FoxitMgr* pFoxitMgr, FPDF_OOM_Handler pOOMReportFunc, void* param);
573
578
587 void* FXMEM_DefaultAlloc(size_t byte_size, int flags);
597 void* FXMEM_DefaultAlloc2(size_t units, size_t unit_size, int flags);
608 void* FXMEM_DefaultAllocDebug(size_t size, int flags, FX_LPCSTR file, int line);
620 void* FXMEM_DefaultAllocDebug2(size_t units, size_t unit_size, int flags, FX_LPCSTR file, int line);
630 void* FXMEM_DefaultRealloc(void* pointer, size_t new_size, int flags);
641 void* FXMEM_DefaultRealloc2(void* pointer, size_t units, size_t unit_size, int flags);
653 void* FXMEM_DefaultReallocDebug(void* pointer, size_t new_size, int flags, FX_LPCSTR file, int line);
666 void* FXMEM_DefaultReallocDebug2(void* pointer, size_t units, size_t unit_size, int flags, FX_LPCSTR file, int line);
675 void FXMEM_DefaultFree(void* pointer, int flags);
676
679 /* FPDFAPI applications should use the FX_Alloc macro for non-class data types */
680 #ifdef _DEBUG
681 #define FX_Alloc(type, size) (type*)FXMEM_DefaultAllocDebug2(size, sizeof(type), 0, __FILE__, __LINE__)
682 #define FX_Realloc(type, ptr, new_size) (type*)FXMEM_DefaultReallocDebug2(ptr, new_size, sizeof(type), 0, __FILE__, __LINE__)
683 #else
684
688 #define FX_Alloc(type, size) (type*)FXMEM_DefaultAlloc2(size, sizeof(type), 0)
689
693 #define FX_Realloc(type, ptr, size) (type*)FXMEM_DefaultRealloc2(ptr, size, sizeof(type), 0)
694 #endif
695
696 #ifdef _DEBUG
697 #define FX_AllocNL(type, size) (type*)FXMEM_DefaultAllocDebug2(size, sizeof(type), FXMEM_NONLEAVE, __FILE__, __LINE__)
698 #define FX_ReallocNL(type, ptr, new_size) (type*)FXMEM_DefaultReallocDebug2(ptr, new_size, sizeof(type), FXMEM_NONLEAVE, __FILE__, __LINE__)
699 #else
700
704 #define FX_AllocNL(type, size) (type*)FXMEM_DefaultAlloc2(size, sizeof(type), FXMEM_NONLEAVE)
705
709 #define FX_ReallocNL(type, ptr, size) (type*)FXMEM_DefaultRealloc2(ptr, size, sizeof(type), FXMEM_NONLEAVE)
710 #endif
711
713 #define FX_Free(pointer) FXMEM_DefaultFree(pointer, 0)
714
715 #ifdef __cplusplus
716 }
717 #endif
718
719 #ifdef __cplusplus
720
721 #include <memory>
722 #include <algorithm>
723 #include <utility>
724
725 #if __cplusplus >= 201103L
726 #define FX_EQDELETE = delete
727 #define FX_NOEXCEPT noexcept
728 #define FX_EXPLICIT_OPERATOR explicit
729 #else
730 #define FX_EQDELETE //= delete
731 #define FX_NOEXCEPT //noexcept
732 #define FX_EXPLICIT_OPERATOR //explicit
733 #endif
734 // A template that can hold either owned or unowned references, and cleans up
735 // appropriately. Possibly the most pernicious anti-pattern imaginable, but
736 // it crops up throughout the codebase due to a desire to avoid copying-in
737 // objects or data.
738 template <typename T, typename D = std::default_delete<T>>
739 class CFX_MaybeOwned {
740 public:
741 CFX_MaybeOwned() : m_pObj(nullptr) {}
742 explicit CFX_MaybeOwned(T* ptr) : m_pObj(ptr) {}
743 explicit CFX_MaybeOwned(std::unique_ptr<T, D> ptr)
744 : m_pOwnedObj(std::move(ptr)), m_pObj(m_pOwnedObj.get()) {}
745
746 CFX_MaybeOwned(CFX_MaybeOwned&& that) FX_NOEXCEPT
747 : m_pOwnedObj(that.m_pOwnedObj.release()), m_pObj(that.m_pObj) {
748 that.m_pObj = nullptr;
749 }
750
751 void Reset(std::unique_ptr<T, D> ptr) {
752 m_pOwnedObj = std::move(ptr);
753 m_pObj = m_pOwnedObj.get();
754 }
755 void Reset(T* ptr = nullptr) {
756 m_pOwnedObj.reset();
757 m_pObj = ptr;
758 }
759
760 bool IsOwned() const { return !!m_pOwnedObj; }
761 T* Get() const { return m_pObj; }
762 std::unique_ptr<T, D> Release() {
763 ASSERT(IsOwned());
764 return std::move(m_pOwnedObj);
765 }
766
767 CFX_MaybeOwned& operator=(CFX_MaybeOwned&& that) {
768 m_pOwnedObj = std::move(that.m_pOwnedObj);
769 m_pObj = that.m_pObj;
770 that.m_pObj = nullptr;
771 return *this;
772 }
773 CFX_MaybeOwned& operator=(T* ptr) {
774 Reset(ptr);
775 return *this;
776 }
777 CFX_MaybeOwned& operator=(std::unique_ptr<T, D> ptr) {
778 Reset(std::move(ptr));
779 return *this;
780 }
781
782 bool operator==(const CFX_MaybeOwned& that) const {
783 return Get() == that.Get();
784 }
785 bool operator==(const std::unique_ptr<T, D>& ptr) const {
786 return Get() == ptr.get();
787 }
788 bool operator==(T* ptr) const { return Get() == ptr; }
789
790 bool operator!=(const CFX_MaybeOwned& that) const { return !(*this == that); }
791 bool operator!=(const std::unique_ptr<T, D> ptr) const {
792 return !(*this == ptr);
793 }
794 bool operator!=(T* ptr) const { return !(*this == ptr); }
795
796 FX_EXPLICIT_OPERATOR operator bool() const { return !!m_pObj; }
797 T& operator*() const { return *m_pObj; }
798 T* operator->() const { return m_pObj; }
799 private:
800 CFX_MaybeOwned(const CFX_MaybeOwned& that) FX_EQDELETE;
801 CFX_MaybeOwned& operator=(const CFX_MaybeOwned& that) FX_EQDELETE;
802
803 private:
804 std::unique_ptr<T, D> m_pOwnedObj;
805 T* m_pObj;
806 };
807
808 // Used with std::unique_ptr to FX_Free raw memory.
809 struct CFX_FreeDeleter {
810 inline void operator()(void* ptr) const { FX_Free(ptr); }
811 };
819 class CFX_Object
820 {
821 public:
831 void* operator new (size_t size, FX_LPCSTR file, int line);
832 #ifndef _FX_NO_EXCEPTION_
833
842 void operator delete (void* p, FX_LPCSTR file, int line);
843 #endif
844
852 void* operator new (size_t size);
860 void operator delete (void* p);
861
871 void* operator new[] (size_t size, FX_LPCSTR file, int line);
872 #ifndef _FX_NO_EXCEPTION_
873
882 void operator delete[] (void* p, FX_LPCSTR file, int line);
883 #endif
884
892 void* operator new[] (size_t size);
900 void operator delete[] (void* p);
901
905 void* operator new (size_t, void* buf) { return buf; }
906 #ifndef _FX_NO_EXCEPTION_
907
910 void operator delete (void*, void*) {}
911 #endif
912 protected:
914 ~CFX_Object() {}
915 };
916
921 #if (_FX_OS_ == _FX_WIN32_DESKTOP_ || _FX_OS_ == _FX_WIN32_MOBILE_ || _FX_OS_ == _FX_WIN64_) && !defined(__PLACEMENT_NEW_INLINE) && !defined(_MFC_VER) && !defined(_NEW)
922 #define __PLACEMENT_NEW_INLINE
923
924 inline void* operator new(size_t size, void* pos)
925 {
926 return pos;
927 }
928
929 inline void operator delete(void* ptr, void* pos)
930 {
931 }
932 #endif //__PLACEMENT_NEW_INLINE
933
934 #endif //__cplusplus
935
936 //<<<+++OPENSOURCE_MUST_END
937
938 //<<<+++OPENSOURCE_MUST_BEGIN
939 #ifdef __cplusplus
940
941 #if defined(_DEBUG)
942 #define FX_NEW new(__FILE__, __LINE__)
943 #else
944
948 #define FX_NEW new
949 #endif
950 //<<<+++OPENSOURCE_MUST_END
951
952 #ifndef _FPDFAPI_MINI_
953 //<<<+++OPENSOURCE_BEGIN LIC==FOXIT
955 #define FX_NEW_VECTOR(Pointer, Class, Count) Pointer = FX_NEW Class[Count]
956
957 #define FX_DELETE_VECTOR(Pointer, Class, Count) delete[] Pointer
958 //<<<+++OPENSOURCE_END
959 #else
960 //<<<+++OPENSOURCE_BEGIN LIC==GOOGLE
961 #define FX_NEW_VECTOR(Pointer, Class, Count) \
962 { \
963 Pointer = FX_Alloc(Class, Count); \
964 if (Pointer) { \
965 for (int i = 0; i < (Count); i ++) new (Pointer + i) Class; \
966 } \
967 }
968 #define FX_DELETE_VECTOR(Pointer, Class, Count) \
969 { \
970 for (int i = 0; i < (Count); i ++) Pointer[i].~Class(); \
971 FX_Free(Pointer); \
972 }
973 //<<<+++OPENSOURCE_END
974 #endif
975
976 //<<<+++OPENSOURCE_MUST_BEGIN
981 class CFX_DestructObject : public CFX_Object
982 {
983 public:
985 virtual ~CFX_DestructObject() {}
986 };
987 //<<<+++OPENSOURCE_MUST_END
988 #endif //__cplusplus
989
990 #ifdef __cplusplus
991 extern "C" {
992 #endif
993
997 typedef struct _IFX_Allocator
998 {
1012 void* (*m_AllocDebug)(struct _IFX_Allocator* pAllocator, size_t size, FX_LPCSTR file, int line);
1024 void* (*m_Alloc)(struct _IFX_Allocator* pAllocator, size_t size);
1039 void* (*m_ReallocDebug)(struct _IFX_Allocator* pAllocator, void* p, size_t size, FX_LPCSTR file, int line);
1052 void* (*m_Realloc)(struct _IFX_Allocator* pAllocator, void* p, size_t size);
1064 void (*m_Free)(struct _IFX_Allocator* pAllocator, void* p);
1065 } IFX_Allocator;
1066
1072 IFX_Allocator* FXMEM_GetDefAllocator();
1073
1074 #ifdef __cplusplus
1075 }
1076 #endif
1077
1078 #ifdef _DEBUG
1079
1080 #define FX_Allocator_Alloc(fxAllocator, type, size) \
1081 ((fxAllocator) ? (type*)(fxAllocator)->m_AllocDebug((fxAllocator), (size) * sizeof(type), __FILE__, __LINE__) : (FX_Alloc(type, size)))
1082
1083 #define FX_Allocator_Realloc(fxAllocator, type, ptr, new_size) \
1084 ((fxAllocator) ? (type*)(fxAllocator)->m_ReallocDebug((fxAllocator), (ptr), (new_size) * sizeof(type), __FILE__, __LINE__) : (FX_Realloc(type, ptr, new_size)))
1085 #else
1086
1087 #define FX_Allocator_Alloc(fxAllocator, type, size) \
1088 ((fxAllocator) ? (type*)(fxAllocator)->m_Alloc((fxAllocator), (size) * sizeof(type)) : (FX_Alloc(type, size)))
1089
1090 #define FX_Allocator_Realloc(fxAllocator, type, ptr, new_size) \
1091 ((fxAllocator) ? (type*)(fxAllocator)->m_Realloc((fxAllocator), (ptr), (new_size) * sizeof(type)) : (FX_Realloc(type, ptr, new_size)))
1092 #endif
1093
1094 #define FX_Allocator_Free(fxAllocator, ptr) \
1095 ((fxAllocator) ? (fxAllocator)->m_Free((fxAllocator), (ptr)) : (FX_Free(ptr)))
1096 //<<<+++OPENSOURCE_MUST_END
1097
1098 #ifdef __cplusplus
1099
1101 inline void* operator new(size_t size, IFX_Allocator* fxAllocator)
1102 {
1103 return (void*)FX_Allocator_Alloc(fxAllocator, FX_BYTE, size);
1104 }
1105
1106 inline void operator delete(void* ptr, IFX_Allocator* fxAllocator)
1107 {
1108 FX_Allocator_Free(fxAllocator, ptr);
1109 }
1110
1112 #define FX_NewAtAllocator(fxAllocator) \
1113 ::new(static_cast<IFX_Allocator*>(fxAllocator))
1114
1115 #define FX_DeleteAtAllocator(pointer, fxAllocator, __class__) \
1116 do { (pointer)->~__class__(); ::operator delete(static_cast<void*>(pointer), static_cast<IFX_Allocator*>(fxAllocator)); } while(false)
1117
1123 class CFX_AllocObject
1124 {
1125 public:
1136 void* operator new (size_t size, IFX_Allocator* pAllocator, FX_LPCSTR file, int line);
1137 #ifndef _FX_NO_EXCEPTION_
1138
1148 void operator delete (void* p, IFX_Allocator* pAllocator, FX_LPCSTR file, int line);
1149 #endif
1150
1159 void* operator new (size_t size, IFX_Allocator* pAllocator);
1167 void operator delete (void* p);
1168 #ifndef _FX_NO_EXCEPTION_
1169
1177 void operator delete (void* p, IFX_Allocator* pAllocator);
1178 #endif
1179
1183 void* operator new (size_t, void* buf) { return buf; }
1184 #ifndef _FX_NO_EXCEPTION_
1185
1188 void operator delete (void*, void*) {}
1189 #endif
1190
1196 IFX_Allocator* GetAllocator() const { return m_pAllocator; }
1197
1198 private: /* all vector operators are disabled */
1209 void* operator new[] (size_t size, IFX_Allocator* pAllocator, FX_LPCSTR file, int line) { return operator new(size, pAllocator, file, line); }
1210 #ifndef _FX_NO_EXCEPTION_
1211
1221 void operator delete[] (void* p, IFX_Allocator* pAllocator, FX_LPCSTR file, int line) {}
1222 #endif
1223
1232 void* operator new[] (size_t size, IFX_Allocator* pAllocator) { return operator new(size, pAllocator); }
1240 void operator delete[] (void* p) {}
1241 #ifndef _FX_NO_EXCEPTION_
1242
1250 void operator delete[] (void* p, IFX_Allocator* pAllocator) {}
1251 #endif
1252
1253 protected:
1254 /*
1255 * The memory allocator.
1256 */
1257 IFX_Allocator* m_pAllocator;
1258 };
1259
1260 #if defined(_DEBUG)
1261 #define FX_NEWAT(pAllocator) new(pAllocator, __FILE__, __LINE__)
1262 #else
1263
1267 #define FX_NEWAT(pAllocator) new(pAllocator)
1268 #endif
1269
1271 //Cocurrency controls
1273
1274 #if !defined(_FPDFAPI_MT_NONE_) && !defined(_FPDFAPI_MT_)
1275 // We enable multi-threading by default on all systems. However, MT can be disabled by compilation macro
1276 #define _FPDFAPI_MT_
1277 #endif
1278
1279 #ifdef _FPDFAPI_MT_
1280
1283 class CFX_LockObject : public CFX_Object
1284 {
1285 public:
1287 CFX_LockObject() {FX_InitializeCriticalSection(&m_Lock);}
1289 ~CFX_LockObject() {FX_DeleteCriticalSection(&m_Lock);}
1290
1292 FX_BOOL TryLock() {return FX_TryEnterCriticalSection(&m_Lock);}
1294 void Lock() {FX_EnterCriticalSection(&m_Lock);}
1296 void Unlock() {FX_LeaveCriticalSection(&m_Lock);}
1297
1298 protected:
1299 /* Critical section. */
1300 FX_CRITICAL_SECTION m_Lock;
1301 friend class CFX_CSLock;
1302 };
1303
1307 class CFX_CSLock
1308 {
1309 public:
1311 CFX_CSLock() : m_pCS(NULL) {}
1313 CFX_CSLock(FX_CRITICAL_SECTION* pCS) : m_pCS(pCS) {if (m_pCS) FX_EnterCriticalSection(m_pCS);}
1315 CFX_CSLock(CFX_LockObject* pObj) {m_pCS = &pObj->m_Lock; FX_EnterCriticalSection(m_pCS);}
1317 ~CFX_CSLock() {if (m_pCS) FX_LeaveCriticalSection(m_pCS);}
1318
1320 FX_CRITICAL_SECTION* m_pCS;
1321 };
1322
1324 #define FXMT_CSLOCK_THIS CFX_CSLock _fx_lock((CFX_LockObject*)this)
1325
1326 #define FXMT_CSLOCK_OBJ(lock) CFX_CSLock _fx_lock((CFX_LockObject*)lock)
1327
1328 #define FXMT_CSLOCK_DEFINEOBJ(csLock, lock) CFX_CSLock csLock((CFX_LockObject*)lock)
1329
1331 #define FXMT_LOCKOBJECT_DEFINE(lockObj) CFX_LockObject lockObj
1332
1333 #define FXMT_LOCKOBJECT_TRYLOCK(lockObj) (lockObj)->TryLock()
1334
1335 #define FXMT_LOCKOBJECT_TRYLOCK_IF(lockObj) if ((lockObj)->TryLock())
1336
1337 #define FXMT_LOCKOBJECT_LOCK(lockObj) (lockObj)->Lock()
1338
1339 #define FXMT_LOCKOBJECT_UNLOCK(lockObj) (lockObj)->Unlock()
1340 #else
1341 class CFX_LockObject : public CFX_Object {};
1342 #define FXMT_CSLOCK_THIS
1343 #define FXMT_CSLOCK_OBJ(lock)
1344 #define FXMT_CSLOCK_DEFINEOBJ(csLock, lock)
1345
1346 #define FXMT_LOCKOBJECT_DEFINE(lockObj)
1347 #define FXMT_LOCKOBJECT_TRYLOCK(lockObj)
1348 #define FXMT_LOCKOBJECT_TRYLOCK_IF(lockObj) if (1)
1349 #define FXMT_LOCKOBJECT_LOCK(lockObj)
1350 #define FXMT_LOCKOBJECT_UNLOCK(lockObj)
1351 #endif
1352 //<<<+++OPENSOURCE_MUST_END
1353
1361 class CFX_GrowOnlyPool : public IFX_Allocator, public CFX_Object
1362
1363 {
1364 public:
1373 CFX_GrowOnlyPool(IFX_Allocator* pAllocator = NULL, size_t trunk_size = 16384);
1374
1378 ~CFX_GrowOnlyPool();
1379
1387 void SetAllocator(IFX_Allocator* pAllocator);
1388
1396 void SetTrunkSize(size_t trunk_size) { m_TrunkSize = trunk_size; }
1397
1407 void* AllocDebug(size_t size, FX_LPCSTR file, int line) { return Alloc(size); }
1415 void* Alloc(size_t size);
1423 void* ReallocDebug(void* p, size_t new_size, FX_LPCSTR file, int line) { return NULL; }
1431 void* Realloc(void* p, size_t new_size) { return NULL; }
1437 void Free(void* mem) {}
1438
1444 void FreeAll();
1445
1446 private:
1447 /* Trunk size. */
1448 size_t m_TrunkSize;
1449 /* Pointer to the first trunk. */
1450 void* m_pFirstTrunk;
1451
1452 /* Memory allocator. */
1453 IFX_Allocator* m_pAllocator;
1454 /* Critical section used for synchronization. */
1455 FX_CRITICAL_SECTION m_Lock;
1456
1457 };
1458
1459 // A Proxy class for allocate memory.
1460
1461 template <class T> class AllocProxy : public CFX_Object {
1462 public:
1463 AllocProxy(size_t size) { buffer = (T *)FX_Alloc(T, size); }
1464 ~AllocProxy() { if (buffer) FX_Free(buffer); }
1465 T &operator[](int index) { return buffer[index]; }
1466 operator T *() { return buffer; }
1467 T *operator +(int offset) { return buffer + offset; }
1468 operator bool() const { return !!buffer; }
1469 T *operator->() const { return buffer; }
1470
1471 private:
1472 AllocProxy(const AllocProxy &) {}
1473 AllocProxy &operator=(const AllocProxy &) {}
1474 T* buffer;
1475 };
1476
1477 // Used with std::unique_ptr to Release() objects that can't be deleted.
1478 template <class T>
1479 struct CFX_ReleaseDeleter {
1480 inline void operator()(T* ptr) const { ptr->Release(); }
1481 };
1482
1483 //<<<+++OPENSOURCE_MUST_END
1484
1485 //<<<+++OPENSOURCE_MUST_BEGIN
1486 #endif //__cplusplus
1487 //<<<+++OPENSOURCE_MUST_END
1488
1489 #ifdef __cplusplus
1490 extern "C" {
1491 #endif
1492
1493 //*****************************************************************************
1494 //* Fixed memory management
1495 //*****************************************************************************
1496 #define _FXMEM_NO64_
1497 //#define _FXMEM_LIT_
1498
1500 #define FX_FIXEDMEM_PAGESIZE (4096 * 16)
1501
1502 #define FX_FIXEDMEM_MIDBLOCKSIZE (4096)
1503
1505 typedef struct _FX_MEMCONFIG
1506 {
1508 size_t nPageNum_Init8;
1510 size_t nPageNum_Init16;
1512 size_t nPageNum_Init32;
1513 #if !defined(_FXMEM_NO64_)
1514 size_t nPageNum_Init64;
1515 #endif
1516
1517 size_t nPageNum_More16;
1519 size_t nPageNum_More32;
1520 #if !defined(_FXMEM_NO64_)
1521 size_t nPageNum_More64;
1522 #endif
1523 #if defined(_FXMEM_LIT_)
1524 size_t nPageSize_Lit;
1525 size_t nPageNum_InitLit;
1526 size_t nPageNum_MoreLit;
1527 size_t nPageNum_ReservedLit;
1528 #endif
1529
1530 size_t nPageSize_Mid;
1532 size_t nPageNum_InitMid;
1534 size_t nPageNum_MoreMid;
1536 size_t nPageSize_Large;
1538 size_t nPageSize_Alone;
1539 }FX_MEMCONFIG;
1540
1551 void FXMEM_SetConfig(const FX_MEMCONFIG* memConfig);
1552
1554 //#define _FX_MEMSTATE_
1555 #if defined(_FX_MEMSTATE_)
1556
1557 typedef struct _FX_MEMPAGESTATE_
1558 {
1559 size_t nCurMemSize;
1560 size_t nMinMemSize;
1561 size_t nMaxMemSize;
1562 size_t nCurAvailSize;
1563 size_t nMinAvailSize;
1564 size_t nMaxAvailSize;
1565 size_t nCurUsedSize;
1566 size_t nMinUsedSize;
1567 size_t nMaxUsedSize;
1568 size_t nCurUsedRate;
1569 size_t nMinUsedRate;
1570 size_t nMaxUsedRate;
1571 size_t bValid;
1572 }FX_MEMPAGESTATE;
1573 typedef struct _FX_MEMINFO_
1574 {
1575 size_t memBlockCount[64];
1576 FX_MEMPAGESTATE pageState8;
1577 FX_MEMPAGESTATE pageState16;
1578 FX_MEMPAGESTATE pageState32;
1579 FX_MEMPAGESTATE pageStateMid;
1580 FX_MEMPAGESTATE pageStateLarge;
1581 FX_MEMPAGESTATE totalState;
1582 }FX_MEMINFO;
1583
1584 #define FX_MEMSTATE_RATEFRACTION 100000
1585
1586 void FX_MemState_MergeInfo(FX_MEMINFO *mi1, const FX_MEMINFO *mi2);
1587 FX_MEMINFO* FX_MemState_GetInfo();
1588 void FX_MemState_ResetInfo();
1589
1590 #endif //_FX_MEMSTATE_
1591
1592 #ifdef __cplusplus
1593 }
1594 #endif
1595
1596 //<<<+++OPENSOURCE_MUST_END
1597
1598 //<<<+++OPENSOURCE_MUST_BEGIN
1599 #endif //_FX_MEMORY_H_
1600 //<<<+++OPENSOURCE_MUST_END
1601
1604 //<<<+++OPENSOURCE_END
FX_MEMCONFIG::nPageSize_Mid
size_t nPageSize_Mid
Size of memory pages for middle data range (> 32-bytes & <= FX_FIXEDMEM_MIDBLOCKSIZE)....
Definition: fx_memory.h:1530
FXMEM_ReallocDebug
void * FXMEM_ReallocDebug(FXMEM_FoxitMgr *pFoxitMgr, void *pointer, size_t new_size, int flags, FX_LPCSTR file, int line)
Foxit basic memory reallocation function in debug-mode.
FXMEM_GetBlockSizeInFixedMgr
size_t FXMEM_GetBlockSizeInFixedMgr(FXMEM_FoxitMgr *pFoxitMgr, void *ptr)
Get the size of a memory block to which ptr points.
FXMEM_SetPyConfig
int FXMEM_SetPyConfig(size_t lowByteRange, size_t highByteRange)
Set configuration of Python memory.
FXMEM_DefaultAlloc2
void * FXMEM_DefaultAlloc2(size_t units, size_t unit_size, int flags)
Default allocation function using default Foxit memory manager for current module.
FX_Allocator_Alloc
#define FX_Allocator_Alloc(fxAllocator, type, size)
Release-mode allocation on an allocator.
Definition: fx_memory.h:1087
FXMEM_Free
void FXMEM_Free(FXMEM_FoxitMgr *pFoxitMgr, void *pointer, int flags)
Foxit basic memory free function.
FXMEM_PurgeMgr
void FXMEM_PurgeMgr(FXMEM_FoxitMgr *pFoxitMgr)
Release all excessive memory without touching any used memory. This is useful for extensible fixed me...
ASSERT
#define ASSERT(a)
Assertion for debug mode, do nothing for release mode.
Definition: fx_system.h:819
FXMEM_Alloc
void * FXMEM_Alloc(FXMEM_FoxitMgr *pFoxitMgr, size_t size, int flags)
Foxit basic memory allocation function.
FX_MEMCONFIG::nPageNum_Init8
size_t nPageNum_Init8
Initial number of memory pages for 8-bytes fixed data size. 1 for desktop platforms,...
Definition: fx_memory.h:1508
FXMEM_Realloc
void * FXMEM_Realloc(FXMEM_FoxitMgr *pFoxitMgr, void *pointer, size_t new_size, int flags)
Foxit basic memory reallocation function.
FXMEM_DefaultFree
void FXMEM_DefaultFree(void *pointer, int flags)
Default free function using default Foxit memory manager for current module.
FXMEM_GetDefAllocator
IFX_Allocator * FXMEM_GetDefAllocator()
Get default allocator used by the library.
FX_MEMCONFIG::nPageSize_Large
size_t nPageSize_Large
Minimum size of memory page for large data (> FX_FIXEDMEM_MIDBLOCKSIZE). 128 for desktop platforms,...
Definition: fx_memory.h:1536
FX_Alloc
#define FX_Alloc(type, size)
A macro for Foxit memory allocation operation.
Definition: fx_memory.h:688
FX_MEMCONFIG::nPageNum_MoreMid
size_t nPageNum_MoreMid
More number of memory pages for middle data range. 4 for desktop platforms, 4 for limited memory envi...
Definition: fx_memory.h:1534
FXMEM_CreateFoxitMgr
FXMEM_FoxitMgr * FXMEM_CreateFoxitMgr(FXMEM_SystemMgr *pSystemMgr)
Create a Foxit manager. A system manager must be provided for actual allocation.
FXMEM_SystemMgr::user
void * user
A generic typeless pointer for user data.
Definition: fx_memory.h:193
FXMEM_SetDefaultMgr
void FXMEM_SetDefaultMgr(FXMEM_FoxitMgr *pFoxitMgr)
Set default Foxit manager for current compile module (EXE, DLL, etc.).
FXMEM_Debugger
Memory debugger interface. All functions must be implemented.
Definition: fx_memory.h:443
FXMEM_CollectAll
void FXMEM_CollectAll(FXMEM_FoxitMgr *pFoxitMgr)
Release all memory blocks allocated by a Foxit manager. This function is only supported on embedded s...
operator==
bool operator==(const char *str1, const CFX_ByteString &str2)
Check if two byte strings are equal.
Definition: fs_basictypes.h:128
FX_BOOL
int FX_BOOL
Boolean variable (should be TRUE or FALSE).
Definition: fx_system.h:691
FXMEM_SetConfig
void FXMEM_SetConfig(const FX_MEMCONFIG *memConfig)
Set configuration of fixed memory.
FXMEM_DefaultRealloc
void * FXMEM_DefaultRealloc(void *pointer, size_t new_size, int flags)
Default reallocation function using default Foxit memory manager for current module.
FX_LPCSTR
char const * FX_LPCSTR
Pointer to constant 8-bit Windows (ANSI) characters.
Definition: fx_system.h:705
FXMEM_ResetSystemMgr
void FXMEM_ResetSystemMgr()
Reset Foxit system memory manager for current module.
FXMEM_GetDefaultMgr
FXMEM_FoxitMgr * FXMEM_GetDefaultMgr()
Get default memory manager for current module.
FXMEM_UseDebugger
void FXMEM_UseDebugger(FXMEM_FoxitMgr *pFoxitMgr, FXMEM_Debugger *pDebugger)
Use a memory debugger which capturing all memory activities. Use NULL for parameter pDebugger to stop...
FXMEM_SystemMgr2
Fixed memory manager.
Definition: fx_memory.h:250
FXMEM_DefaultAllocDebug2
void * FXMEM_DefaultAllocDebug2(size_t units, size_t unit_size, int flags, FX_LPCSTR file, int line)
Default debug-mode allocation function using default Foxit memory manager for current module.
FXMEM_DestroyFoxitMgr
void FXMEM_DestroyFoxitMgr(FXMEM_FoxitMgr *pFoxitMgr)
Destroy a Foxit manager instance. If the platform supports auto-collection, then all allocated memory...
FXMEM_OutputDebugTag
void FXMEM_OutputDebugTag(FXMEM_FoxitMgr *pFoxitMgr, FX_LPCSTR tag)
Output a memory debug tag.
FX_MEMCONFIG::nPageNum_Init32
size_t nPageNum_Init32
Initial number of memory pages for 32-bytes fixed data size. 24 for desktop platforms,...
Definition: fx_memory.h:1512
FX_MEMCONFIG::nPageNum_More16
size_t nPageNum_More16
More number of memory pages for 16-bytes fixed data size. 8 for desktop platforms,...
Definition: fx_memory.h:1517
FXMEM_DefaultReallocDebug2
void * FXMEM_DefaultReallocDebug2(void *pointer, size_t units, size_t unit_size, int flags, FX_LPCSTR file, int line)
Default debug-mode reallocation function using default Foxit memory manager for current module.
FXMEM_CreatePyMgr
FXMEM_FoxitMgr * FXMEM_CreatePyMgr()
Create a Python manager. A fast, special-purpose memory allocator for small blocks,...
operator+
CFX_ByteString operator+(FX_BSTR str1, FX_BSTR str2)
Concatenate a non-buffered byte string and a non-buffered byte string.
Definition: fx_string.h:980
IFX_Allocator
Foxit allocator interface.
Definition: fx_memory.h:997
FXMEM_DefaultAlloc
void * FXMEM_DefaultAlloc(size_t byte_size, int flags)
Default allocation function using default Foxit memory manager for current module.
FX_Free
#define FX_Free(pointer)
A macro for Foxit memory free operation.
Definition: fx_memory.h:713
FXMEM_CreateFixedMgr
FXMEM_FoxitMgr * FXMEM_CreateFixedMgr(void *pMemory, size_t size, FXMEM_SystemMgr2 *pExtender)
Create a Foxit manager from a pre-allocated, fixed memory buffer.
FXMEM_SystemMgr
System level memory manager. Application can implement their own system memory manager.
Definition: fx_memory.h:72
FX_MEMCONFIG::nPageNum_InitMid
size_t nPageNum_InitMid
Initial number of memory pages for middle data range. 2 for desktop platforms, 2 for limited memory e...
Definition: fx_memory.h:1532
FPDF_OOM_Handler
void(* FPDF_OOM_Handler)(FXMEM_FoxitMgr *pFoxitMgr, void *param)
the prototype of the Out-Of-Memory handler.
Definition: fx_memory.h:561
FX_MEMCONFIG::nPageNum_Init16
size_t nPageNum_Init16
Initial number of memory pages for 16-bytes fixed data size. 8 for desktop platforms,...
Definition: fx_memory.h:1510
FXMEM_AllocDebug
void * FXMEM_AllocDebug(FXMEM_FoxitMgr *pFoxitMgr, size_t size, int flags, FX_LPCSTR file, int line)
Foxit basic memory allocation function in debug-mode.
FXMEM_ReportOOM
void FXMEM_ReportOOM(FXMEM_FoxitMgr *pFoxitMgr)
Report Out-of-memory (OOM).
FXMEM_SetOOMHandler
void FXMEM_SetOOMHandler(FXMEM_FoxitMgr *pFoxitMgr, FPDF_OOM_Handler pOOMReportFunc, void *param)
Setup A Out-Of-Memory handler for a Foxit memory manager.
FX_MEMCONFIG::nPageNum_More32
size_t nPageNum_More32
More number of memry pages for 32-bytes fixed data size. 24 for desktop platforms,...
Definition: fx_memory.h:1519
FXMEM_CreateMemoryMgr
FXMEM_FoxitMgr * FXMEM_CreateMemoryMgr(size_t size, FX_BOOL extensible)
Create a fixed memory manager as default implementation.
FXMEM_DefaultAllocDebug
void * FXMEM_DefaultAllocDebug(size_t size, int flags, FX_LPCSTR file, int line)
Default debug-mode allocation function using default Foxit memory manager for current module.
FX_MEMCONFIG
Structure of fixed memory configuration.
Definition: fx_memory.h:1505
FXMEM_DefaultRealloc2
void * FXMEM_DefaultRealloc2(void *pointer, size_t units, size_t unit_size, int flags)
Default reallocation function using default Foxit memory manager for current module.
FX_MEMCONFIG::nPageSize_Alone
size_t nPageSize_Alone
Minimum size of alone memory page for large data. 64 for desktop platforms, 64 for limited memory env...
Definition: fx_memory.h:1538
NULL
#define NULL
The null-pointer value.
Definition: fx_system.h:792
FX_DEFINEHANDLE
#define FX_DEFINEHANDLE(name)
Macro to define a handle type.
Definition: fx_system.h:772
operator!=
bool operator!=(const char *str1, const CFX_ByteString &str2)
Check if two byte strings are not equal.
Definition: fs_basictypes.h:140
FX_Allocator_Free
#define FX_Allocator_Free(fxAllocator, ptr)
Free memory block on an allocator.
Definition: fx_memory.h:1094
FXMEM_DefaultReallocDebug
void * FXMEM_DefaultReallocDebug(void *pointer, size_t new_size, int flags, FX_LPCSTR file, int line)
Default debug-mode reallocation function using default Foxit memory manager for current module.
FX_BYTE
unsigned char FX_BYTE
Byte (8 bits).
Definition: fx_system.h:665
fx_system.h
Header file for system related definitions.