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;
195 
201 FX_DEFINEHANDLE(FXMEM_FoxitMgr)
202 
203 
215 FXMEM_FoxitMgr* FXMEM_CreateFoxitMgr(FXMEM_SystemMgr* pSystemMgr);
216 
220 typedef struct _FXMEM_SystemMgr2
221 {
232  FX_BOOL (*More)(struct _FXMEM_SystemMgr2* pMgr, size_t alloc_size, void** new_memory, size_t* new_size);
241  void (*Free)(struct _FXMEM_SystemMgr2* pMgr, void* memory);
243 
259 FXMEM_FoxitMgr* FXMEM_CreateFixedMgr(void* pMemory, size_t size, FXMEM_SystemMgr2* pExtender);
260 
274 FXMEM_FoxitMgr* FXMEM_CreateMemoryMgr(size_t size, FX_BOOL extensible);
275 
284 size_t FXMEM_GetBlockSizeInFixedMgr(FXMEM_FoxitMgr* pFoxitMgr, void* ptr);
285 
291 FXMEM_FoxitMgr* FXMEM_GetDefaultMgr();
292 
299 void FXMEM_SetDefaultMgr(FXMEM_FoxitMgr* pFoxitMgr);
300 
308 void FXMEM_DestroyFoxitMgr(FXMEM_FoxitMgr* pFoxitMgr);
309 
314 
324 void* FXMEM_Alloc(FXMEM_FoxitMgr* pFoxitMgr, size_t size, int flags);
336 void* FXMEM_AllocDebug(FXMEM_FoxitMgr* pFoxitMgr, size_t size, int flags, FX_LPCSTR file, int line);
347 void* FXMEM_Realloc(FXMEM_FoxitMgr* pFoxitMgr, void* pointer, size_t new_size, int flags);
360 void* FXMEM_ReallocDebug(FXMEM_FoxitMgr* pFoxitMgr, void* pointer, size_t new_size, int flags, FX_LPCSTR file, int line);
370 void FXMEM_Free(FXMEM_FoxitMgr* pFoxitMgr, void* pointer, int flags);
371 
381 void FXMEM_CollectAll(FXMEM_FoxitMgr* pFoxitMgr);
382 
392 void FXMEM_PurgeMgr(FXMEM_FoxitMgr* pFoxitMgr);
393 
401 void FXMEM_ReportOOM(FXMEM_FoxitMgr* pFoxitMgr);
402 
406 typedef struct {
420  void (*OnAlloc)(FXMEM_FoxitMgr* pMgr, void* p, size_t size, int flags);
436  void (*OnAllocDebug)(FXMEM_FoxitMgr* pMgr, void* p, size_t size, int flags, FX_LPCSTR file, int line);
451  void (*OnRealloc)(FXMEM_FoxitMgr* pMgr, void* old_p, void* new_p, size_t size, int flags);
468  void (*OnReallocDebug)(FXMEM_FoxitMgr* pMgr, void* old_p, void* new_p, size_t size, int flags, FX_LPCSTR file, int line);
481  void (*OnFree)(FXMEM_FoxitMgr* pMgr, void* p, int flags);
493  void (*OnTag)(FXMEM_FoxitMgr* pMgr, FX_LPCSTR tag);
495 
504 void FXMEM_UseDebugger(FXMEM_FoxitMgr* pFoxitMgr, FXMEM_Debugger* pDebugger);
505 
514 void FXMEM_OutputDebugTag(FXMEM_FoxitMgr* pFoxitMgr, FX_LPCSTR tag);
515 
524 typedef void (*FPDF_OOM_Handler)(FXMEM_FoxitMgr* pFoxitMgr, void* param);
525 
535 void FXMEM_SetOOMHandler(FXMEM_FoxitMgr* pFoxitMgr, FPDF_OOM_Handler pOOMReportFunc, void* param);
536 
541 
550 void* FXMEM_DefaultAlloc(size_t byte_size, int flags);
560 void* FXMEM_DefaultAlloc2(size_t units, size_t unit_size, int flags);
571 void* FXMEM_DefaultAllocDebug(size_t size, int flags, FX_LPCSTR file, int line);
583 void* FXMEM_DefaultAllocDebug2(size_t units, size_t unit_size, int flags, FX_LPCSTR file, int line);
593 void* FXMEM_DefaultRealloc(void* pointer, size_t new_size, int flags);
604 void* FXMEM_DefaultRealloc2(void* pointer, size_t units, size_t unit_size, int flags);
616 void* FXMEM_DefaultReallocDebug(void* pointer, size_t new_size, int flags, FX_LPCSTR file, int line);
629 void* FXMEM_DefaultReallocDebug2(void* pointer, size_t units, size_t unit_size, int flags, FX_LPCSTR file, int line);
638 void FXMEM_DefaultFree(void* pointer, int flags);
639 
642 /* FPDFAPI applications should use the FX_Alloc macro for non-class data types */
643 #ifdef _DEBUG
644  #define FX_Alloc(type, size) (type*)FXMEM_DefaultAllocDebug2(size, sizeof(type), 0, __FILE__, __LINE__)
645  #define FX_Realloc(type, ptr, new_size) (type*)FXMEM_DefaultReallocDebug2(ptr, new_size, sizeof(type), 0, __FILE__, __LINE__)
646 #else
647 
651  #define FX_Alloc(type, size) (type*)FXMEM_DefaultAlloc2(size, sizeof(type), 0)
652 
656  #define FX_Realloc(type, ptr, size) (type*)FXMEM_DefaultRealloc2(ptr, size, sizeof(type), 0)
657 #endif
658 
659 #ifdef _DEBUG
660  #define FX_AllocNL(type, size) (type*)FXMEM_DefaultAllocDebug2(size, sizeof(type), FXMEM_NONLEAVE, __FILE__, __LINE__)
661  #define FX_ReallocNL(type, ptr, new_size) (type*)FXMEM_DefaultReallocDebug2(ptr, new_size, sizeof(type), FXMEM_NONLEAVE, __FILE__, __LINE__)
662 #else
663 
667  #define FX_AllocNL(type, size) (type*)FXMEM_DefaultAlloc2(size, sizeof(type), FXMEM_NONLEAVE)
668 
672  #define FX_ReallocNL(type, ptr, size) (type*)FXMEM_DefaultRealloc2(ptr, size, sizeof(type), FXMEM_NONLEAVE)
673 #endif
674 
676 #define FX_Free(pointer) FXMEM_DefaultFree(pointer, 0)
677 
678 #ifdef __cplusplus
679 }
680 #endif
681 
682 #ifdef __cplusplus
683 
684 #include <memory>
685 #include <algorithm>
686 #include <utility>
687 
688 #if __cplusplus >= 201103L
689 #define FX_EQDELETE = delete
690 #define FX_NOEXCEPT noexcept
691 #define FX_EXPLICIT_OPERATOR explicit
692 #else
693 #define FX_EQDELETE //= delete
694 #define FX_NOEXCEPT //noexcept
695 #define FX_EXPLICIT_OPERATOR //explicit
696 #endif
697 
698 // A template that can hold either owned or unowned references, and cleans up
699 // appropriately. Possibly the most pernicious anti-pattern imaginable, but
700 // it crops up throughout the codebase due to a desire to avoid copying-in
701 // objects or data.
702 template <typename T, typename D = std::default_delete<T>>
703 class CFX_MaybeOwned {
704  public:
705  CFX_MaybeOwned() : m_pObj(nullptr) {}
706  explicit CFX_MaybeOwned(T* ptr) : m_pObj(ptr) {}
707  explicit CFX_MaybeOwned(std::unique_ptr<T, D> ptr)
708  : m_pOwnedObj(std::move(ptr)), m_pObj(m_pOwnedObj.get()) {}
709 
710  CFX_MaybeOwned(CFX_MaybeOwned&& that) FX_NOEXCEPT
711  : m_pOwnedObj(that.m_pOwnedObj.release()), m_pObj(that.m_pObj) {
712  that.m_pObj = nullptr;
713  }
714 
715  void Reset(std::unique_ptr<T, D> ptr) {
716  m_pOwnedObj = std::move(ptr);
717  m_pObj = m_pOwnedObj.get();
718  }
719  void Reset(T* ptr = nullptr) {
720  m_pOwnedObj.reset();
721  m_pObj = ptr;
722  }
723 
724  bool IsOwned() const { return !!m_pOwnedObj; }
725  T* Get() const { return m_pObj; }
726  std::unique_ptr<T, D> Release() {
727  ASSERT(IsOwned());
728  return std::move(m_pOwnedObj);
729  }
730 
731  CFX_MaybeOwned& operator=(CFX_MaybeOwned&& that) {
732  m_pOwnedObj = std::move(that.m_pOwnedObj);
733  m_pObj = that.m_pObj;
734  that.m_pObj = nullptr;
735  return *this;
736  }
737  CFX_MaybeOwned& operator=(T* ptr) {
738  Reset(ptr);
739  return *this;
740  }
741  CFX_MaybeOwned& operator=(std::unique_ptr<T, D> ptr) {
742  Reset(std::move(ptr));
743  return *this;
744  }
745 
746  bool operator==(const CFX_MaybeOwned& that) const {
747  return Get() == that.Get();
748  }
749  bool operator==(const std::unique_ptr<T, D>& ptr) const {
750  return Get() == ptr.get();
751  }
752  bool operator==(T* ptr) const { return Get() == ptr; }
753 
754  bool operator!=(const CFX_MaybeOwned& that) const { return !(*this == that); }
755  bool operator!=(const std::unique_ptr<T, D> ptr) const {
756  return !(*this == ptr);
757  }
758  bool operator!=(T* ptr) const { return !(*this == ptr); }
759 
760  FX_EXPLICIT_OPERATOR operator bool() const { return !!m_pObj; }
761  T& operator*() const { return *m_pObj; }
762  T* operator->() const { return m_pObj; }
763  private:
764  CFX_MaybeOwned(const CFX_MaybeOwned& that) FX_EQDELETE;
765  CFX_MaybeOwned& operator=(const CFX_MaybeOwned& that) FX_EQDELETE;
766 
767  private:
768  std::unique_ptr<T, D> m_pOwnedObj;
769  T* m_pObj;
770 };
771 
772 // Used with std::unique_ptr to FX_Free raw memory.
773 struct CFX_FreeDeleter {
774  inline void operator()(void* ptr) const { FX_Free(ptr); }
775 };
783 class CFX_Object
784 {
785  public:
795  void* operator new (size_t size, FX_LPCSTR file, int line);
796 #ifndef _FX_NO_EXCEPTION_
797 
806  void operator delete (void* p, FX_LPCSTR file, int line);
807 #endif
808 
816  void* operator new (size_t size);
824  void operator delete (void* p);
825 
835  void* operator new[] (size_t size, FX_LPCSTR file, int line);
836 #ifndef _FX_NO_EXCEPTION_
837 
846  void operator delete[] (void* p, FX_LPCSTR file, int line);
847 #endif
848 
856  void* operator new[] (size_t size);
864  void operator delete[] (void* p);
865 
869  void* operator new (size_t, void* buf) { return buf; }
870 #ifndef _FX_NO_EXCEPTION_
871 
874  void operator delete (void*, void*) {}
875 #endif
876  protected:
878  ~CFX_Object() {}
879 };
880 
885 #if (_FX_OS_ == _FX_WIN32_DESKTOP_ || _FX_OS_ == _FX_WIN32_MOBILE_ || _FX_OS_ == _FX_WIN64_) && !defined(__PLACEMENT_NEW_INLINE) && !defined(_MFC_VER) && !defined(_NEW)
886  #define __PLACEMENT_NEW_INLINE
887 
888  inline void* operator new(size_t size, void* pos)
889  {
890  return pos;
891  }
892 
893  inline void operator delete(void* ptr, void* pos)
894  {
895  }
896 #endif //__PLACEMENT_NEW_INLINE
897 
898 #endif //__cplusplus
899 
900 //<<<+++OPENSOURCE_MUST_END
901 
902 //<<<+++OPENSOURCE_MUST_BEGIN
903 #ifdef __cplusplus
904 
905 #if defined(_DEBUG)
906  #define FX_NEW new(__FILE__, __LINE__)
907 #else
908 
912  #define FX_NEW new
913 #endif
914 //<<<+++OPENSOURCE_MUST_END
915 
916 #ifndef _FPDFAPI_MINI_
917  //<<<+++OPENSOURCE_BEGIN LIC==FOXIT
919  #define FX_NEW_VECTOR(Pointer, Class, Count) Pointer = FX_NEW Class[Count]
920 
921  #define FX_DELETE_VECTOR(Pointer, Class, Count) delete[] Pointer
922  //<<<+++OPENSOURCE_END
923 #else
924  //<<<+++OPENSOURCE_BEGIN LIC==GOOGLE
925  #define FX_NEW_VECTOR(Pointer, Class, Count) \
926  { \
927  Pointer = FX_Alloc(Class, Count); \
928  if (Pointer) { \
929  for (int i = 0; i < (Count); i ++) new (Pointer + i) Class; \
930  } \
931  }
932  #define FX_DELETE_VECTOR(Pointer, Class, Count) \
933  { \
934  for (int i = 0; i < (Count); i ++) Pointer[i].~Class(); \
935  FX_Free(Pointer); \
936  }
937  //<<<+++OPENSOURCE_END
938 #endif
939 
940 //<<<+++OPENSOURCE_MUST_BEGIN
945 class CFX_DestructObject : public CFX_Object
946 {
947  public:
949  virtual ~CFX_DestructObject() {}
950 };
951 //<<<+++OPENSOURCE_MUST_END
952 #endif //__cplusplus
953 
954 #ifdef __cplusplus
955 extern "C" {
956 #endif
957 
961 typedef struct _IFX_Allocator
962 {
976  void* (*m_AllocDebug)(struct _IFX_Allocator* pAllocator, size_t size, FX_LPCSTR file, int line);
988  void* (*m_Alloc)(struct _IFX_Allocator* pAllocator, size_t size);
1003  void* (*m_ReallocDebug)(struct _IFX_Allocator* pAllocator, void* p, size_t size, FX_LPCSTR file, int line);
1016  void* (*m_Realloc)(struct _IFX_Allocator* pAllocator, void* p, size_t size);
1028  void (*m_Free)(struct _IFX_Allocator* pAllocator, void* p);
1029 } IFX_Allocator;
1030 
1037 
1038 #ifdef __cplusplus
1039 }
1040 #endif
1041 
1042 #ifdef _DEBUG
1043 
1044  #define FX_Allocator_Alloc(fxAllocator, type, size) \
1045  ((fxAllocator) ? (type*)(fxAllocator)->m_AllocDebug((fxAllocator), (size) * sizeof(type), __FILE__, __LINE__) : (FX_Alloc(type, size)))
1046 
1047  #define FX_Allocator_Realloc(fxAllocator, type, ptr, new_size) \
1048  ((fxAllocator) ? (type*)(fxAllocator)->m_ReallocDebug((fxAllocator), (ptr), (new_size) * sizeof(type), __FILE__, __LINE__) : (FX_Realloc(type, ptr, new_size)))
1049 #else
1050 
1051  #define FX_Allocator_Alloc(fxAllocator, type, size) \
1052  ((fxAllocator) ? (type*)(fxAllocator)->m_Alloc((fxAllocator), (size) * sizeof(type)) : (FX_Alloc(type, size)))
1053 
1054  #define FX_Allocator_Realloc(fxAllocator, type, ptr, new_size) \
1055  ((fxAllocator) ? (type*)(fxAllocator)->m_Realloc((fxAllocator), (ptr), (new_size) * sizeof(type)) : (FX_Realloc(type, ptr, new_size)))
1056 #endif
1057 
1058 #define FX_Allocator_Free(fxAllocator, ptr) \
1059  ((fxAllocator) ? (fxAllocator)->m_Free((fxAllocator), (ptr)) : (FX_Free(ptr)))
1060 //<<<+++OPENSOURCE_MUST_END
1061 
1062 #ifdef __cplusplus
1063 
1065 inline void* operator new(size_t size, IFX_Allocator* fxAllocator)
1066 {
1067  return (void*)FX_Allocator_Alloc(fxAllocator, FX_BYTE, size);
1068 }
1069 
1070 inline void operator delete(void* ptr, IFX_Allocator* fxAllocator)
1071 {
1072  FX_Allocator_Free(fxAllocator, ptr);
1073 }
1074 
1076 #define FX_NewAtAllocator(fxAllocator) \
1077  ::new(static_cast<IFX_Allocator*>(fxAllocator))
1078 
1079 #define FX_DeleteAtAllocator(pointer, fxAllocator, __class__) \
1080  do { if (!(pointer)) break; (pointer)->~__class__(); ::operator delete(static_cast<void*>(pointer), static_cast<IFX_Allocator*>(fxAllocator)); } while(false)
1081 
1087 class CFX_AllocObject
1088 {
1089  public:
1100  void* operator new (size_t size, IFX_Allocator* pAllocator, FX_LPCSTR file, int line);
1101 #ifndef _FX_NO_EXCEPTION_
1102 
1112  void operator delete (void* p, IFX_Allocator* pAllocator, FX_LPCSTR file, int line);
1113 #endif
1114 
1123  void* operator new (size_t size, IFX_Allocator* pAllocator);
1131  void operator delete (void* p);
1132 #ifndef _FX_NO_EXCEPTION_
1133 
1141  void operator delete (void* p, IFX_Allocator* pAllocator);
1142 #endif
1143 
1147  void* operator new (size_t, void* buf) { return buf; }
1148 #ifndef _FX_NO_EXCEPTION_
1149 
1152  void operator delete (void*, void*) {}
1153 #endif
1154 
1160  IFX_Allocator* GetAllocator() const { return m_pAllocator; }
1161 
1162 private: /* all vector operators are disabled */
1173  void* operator new[] (size_t size, IFX_Allocator* pAllocator, FX_LPCSTR file, int line) { return operator new(size, pAllocator, file, line); }
1174 #ifndef _FX_NO_EXCEPTION_
1175 
1185  void operator delete[] (void* p, IFX_Allocator* pAllocator, FX_LPCSTR file, int line) {}
1186 #endif
1187 
1196  void* operator new[] (size_t size, IFX_Allocator* pAllocator) { return operator new(size, pAllocator); }
1204  void operator delete[] (void* p) {}
1205 #ifndef _FX_NO_EXCEPTION_
1206 
1214  void operator delete[] (void* p, IFX_Allocator* pAllocator) {}
1215 #endif
1216 
1217  protected:
1218  /*
1219  * The memory allocator.
1220  */
1221  IFX_Allocator* m_pAllocator;
1222 };
1223 
1224 #if defined(_DEBUG)
1225  #define FX_NEWAT(pAllocator) new(pAllocator, __FILE__, __LINE__)
1226 #else
1227 
1231  #define FX_NEWAT(pAllocator) new(pAllocator)
1232 #endif
1233 
1235 //Cocurrency controls
1237 
1238 #if !defined(_FPDFAPI_MT_NONE_) && !defined(_FPDFAPI_MT_)
1239  // We enable multi-threading by default on all systems. However, MT can be disabled by compilation macro
1240  #define _FPDFAPI_MT_
1241 #endif
1242 
1243 #ifdef _FPDFAPI_MT_
1244 
1247  class CFX_LockObject : public CFX_Object
1248  {
1249  public:
1251  CFX_LockObject() {FX_InitializeCriticalSection(&m_Lock);}
1253  ~CFX_LockObject() {FX_DeleteCriticalSection(&m_Lock);}
1254 
1256  FX_BOOL TryLock() {return FX_TryEnterCriticalSection(&m_Lock);}
1258  void Lock() {FX_EnterCriticalSection(&m_Lock);}
1260  void Unlock() {FX_LeaveCriticalSection(&m_Lock);}
1261 
1262  protected:
1263  /* Critical section. */
1264  FX_CRITICAL_SECTION m_Lock;
1265  friend class CFX_CSLock;
1266  };
1267 
1271  class CFX_CSLock
1272  {
1273  public:
1275  CFX_CSLock() : m_pCS(NULL) {}
1277  CFX_CSLock(FX_CRITICAL_SECTION* pCS) : m_pCS(pCS) {if (m_pCS) FX_EnterCriticalSection(m_pCS);}
1279  CFX_CSLock(CFX_LockObject* pObj) {m_pCS = &pObj->m_Lock; FX_EnterCriticalSection(m_pCS);}
1281  ~CFX_CSLock() {if (m_pCS) FX_LeaveCriticalSection(m_pCS);}
1282 
1284  FX_CRITICAL_SECTION* m_pCS;
1285  };
1286 
1288  #define FXMT_CSLOCK_THIS CFX_CSLock _fx_lock((CFX_LockObject*)this)
1289 
1290  #define FXMT_CSLOCK_OBJ(lock) CFX_CSLock _fx_lock((CFX_LockObject*)lock)
1291 
1292  #define FXMT_CSLOCK_DEFINEOBJ(csLock, lock) CFX_CSLock csLock((CFX_LockObject*)lock)
1293 
1295  #define FXMT_LOCKOBJECT_DEFINE(lockObj) CFX_LockObject lockObj
1296 
1297  #define FXMT_LOCKOBJECT_TRYLOCK(lockObj) (lockObj)->TryLock()
1298 
1299  #define FXMT_LOCKOBJECT_TRYLOCK_IF(lockObj) if ((lockObj)->TryLock())
1300 
1301  #define FXMT_LOCKOBJECT_LOCK(lockObj) (lockObj)->Lock()
1302 
1303  #define FXMT_LOCKOBJECT_UNLOCK(lockObj) (lockObj)->Unlock()
1304 #else
1305  class CFX_LockObject : public CFX_Object {};
1306  #define FXMT_CSLOCK_THIS
1307  #define FXMT_CSLOCK_OBJ(lock)
1308  #define FXMT_CSLOCK_DEFINEOBJ(csLock, lock)
1309 
1310  #define FXMT_LOCKOBJECT_DEFINE(lockObj)
1311  #define FXMT_LOCKOBJECT_TRYLOCK(lockObj)
1312  #define FXMT_LOCKOBJECT_TRYLOCK_IF(lockObj) if (1)
1313  #define FXMT_LOCKOBJECT_LOCK(lockObj)
1314  #define FXMT_LOCKOBJECT_UNLOCK(lockObj)
1315 #endif
1316 //<<<+++OPENSOURCE_MUST_END
1317 
1325 class CFX_GrowOnlyPool : public IFX_Allocator, public CFX_Object
1326 
1327 {
1328  public:
1337  CFX_GrowOnlyPool(IFX_Allocator* pAllocator = NULL, size_t trunk_size = 16384);
1338 
1342  ~CFX_GrowOnlyPool();
1343 
1351  void SetAllocator(IFX_Allocator* pAllocator);
1352 
1360  void SetTrunkSize(size_t trunk_size) { m_TrunkSize = trunk_size; }
1361 
1371  void* AllocDebug(size_t size, FX_LPCSTR file, int line) { return Alloc(size); }
1379  void* Alloc(size_t size);
1387  void* ReallocDebug(void* p, size_t new_size, FX_LPCSTR file, int line) { return NULL; }
1395  void* Realloc(void* p, size_t new_size) { return NULL; }
1401  void Free(void* mem) {}
1402 
1408  void FreeAll();
1409 
1410  private:
1411  /* Trunk size. */
1412  size_t m_TrunkSize;
1413  /* Pointer to the first trunk. */
1414  void* m_pFirstTrunk;
1415 
1416  /* Memory allocator. */
1417  IFX_Allocator* m_pAllocator;
1418  /* Critical section used for synchronization. */
1419  FX_CRITICAL_SECTION m_Lock;
1420 
1421 };
1422 
1423 // A Proxy class for allocate memory.
1424 
1425 template <class T> class AllocProxy : public CFX_Object {
1426  public:
1427  AllocProxy(size_t size) { buffer = (T *)FX_Alloc(T, size); }
1428  ~AllocProxy() { if (buffer) FX_Free(buffer); }
1429  T &operator[](int index) { return buffer[index]; }
1430  operator T *() { return buffer; }
1431  T *operator +(int offset) { return buffer + offset; }
1432  operator bool() const { return !!buffer; }
1433  T *operator->() const { return buffer; }
1434 
1435  private:
1436  AllocProxy(const AllocProxy &) {}
1437  AllocProxy &operator=(const AllocProxy &) {}
1438  T* buffer;
1439 };
1440 
1441 // Used with std::unique_ptr to Release() objects that can't be deleted.
1442 template <class T>
1443 struct CFX_ReleaseDeleter {
1444  inline void operator()(T* ptr) const { ptr->Release(); }
1445 };
1446 
1447 //<<<+++OPENSOURCE_MUST_END
1448 
1449 //<<<+++OPENSOURCE_MUST_BEGIN
1450 #endif //__cplusplus
1451 //<<<+++OPENSOURCE_MUST_END
1452 
1453 #ifdef __cplusplus
1454 extern "C" {
1455 #endif
1456 
1457 //*****************************************************************************
1458 //* Fixed memory management
1459 //*****************************************************************************
1460 #define _FXMEM_NO64_
1461 //#define _FXMEM_LIT_
1462 
1464 #define FX_FIXEDMEM_PAGESIZE (4096 * 16)
1465 
1466 #define FX_FIXEDMEM_MIDBLOCKSIZE (4096)
1467 
1469 typedef struct _FX_MEMCONFIG
1470 {
1477  #if !defined(_FXMEM_NO64_)
1478  size_t nPageNum_Init64;
1479  #endif
1480 
1484  #if !defined(_FXMEM_NO64_)
1485  size_t nPageNum_More64;
1486  #endif
1487  #if defined(_FXMEM_LIT_)
1488  size_t nPageSize_Lit;
1489  size_t nPageNum_InitLit;
1490  size_t nPageNum_MoreLit;
1491  size_t nPageNum_ReservedLit;
1492  #endif
1493 
1503 }FX_MEMCONFIG;
1504 
1515 void FXMEM_SetConfig(const FX_MEMCONFIG* memConfig);
1516 
1518 //#define _FX_MEMSTATE_
1519 #if defined(_FX_MEMSTATE_)
1520 
1521  typedef struct _FX_MEMPAGESTATE_
1522  {
1523  size_t nCurMemSize;
1524  size_t nMinMemSize;
1525  size_t nMaxMemSize;
1526  size_t nCurAvailSize;
1527  size_t nMinAvailSize;
1528  size_t nMaxAvailSize;
1529  size_t nCurUsedSize;
1530  size_t nMinUsedSize;
1531  size_t nMaxUsedSize;
1532  size_t nCurUsedRate;
1533  size_t nMinUsedRate;
1534  size_t nMaxUsedRate;
1535  size_t bValid;
1536  }FX_MEMPAGESTATE;
1537  typedef struct _FX_MEMINFO_
1538  {
1539  size_t memBlockCount[64];
1540  FX_MEMPAGESTATE pageState8;
1541  FX_MEMPAGESTATE pageState16;
1542  FX_MEMPAGESTATE pageState32;
1543  FX_MEMPAGESTATE pageStateMid;
1544  FX_MEMPAGESTATE pageStateLarge;
1545  FX_MEMPAGESTATE totalState;
1546  }FX_MEMINFO;
1547 
1548  #define FX_MEMSTATE_RATEFRACTION 100000
1549 
1550  void FX_MemState_MergeInfo(FX_MEMINFO *mi1, const FX_MEMINFO *mi2);
1551  FX_MEMINFO* FX_MemState_GetInfo();
1552  void FX_MemState_ResetInfo();
1553 
1554 #endif //_FX_MEMSTATE_
1555 
1556 #ifdef __cplusplus
1557 }
1558 #endif
1559 
1560 //<<<+++OPENSOURCE_MUST_END
1561 
1562 //<<<+++OPENSOURCE_MUST_BEGIN
1563 #endif //_FX_MEMORY_H_
1564 //<<<+++OPENSOURCE_MUST_END
1565 
1568 //<<<+++OPENSOURCE_END
size_t nPageSize_Mid
Size of memory pages for middle data range (> 32-bytes & <= FX_FIXEDMEM_MIDBLOCKSIZE). 32 for desktop platforms, 8 for limited memory environments.
Definition: fx_memory.h:1494
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.
size_t FXMEM_GetBlockSizeInFixedMgr(FXMEM_FoxitMgr *pFoxitMgr, void *ptr)
Get the size of a memory block to which ptr points.
void * FXMEM_DefaultAlloc2(size_t units, size_t unit_size, int flags)
Default allocation function using default Foxit memory manager for current module.
#define FX_Allocator_Alloc(fxAllocator, type, size)
Release-mode allocation on an allocator.
Definition: fx_memory.h:1051
void FXMEM_Free(FXMEM_FoxitMgr *pFoxitMgr, void *pointer, int flags)
Foxit basic memory free function.
void FXMEM_PurgeMgr(FXMEM_FoxitMgr *pFoxitMgr)
Release all excessive memory without touching any used memory. This is useful for extensible fixed me...
#define ASSERT(a)
Assertion for debug mode, do nothing for release mode.
Definition: fx_system.h:800
void * FXMEM_Alloc(FXMEM_FoxitMgr *pFoxitMgr, size_t size, int flags)
Foxit basic memory allocation function.
size_t nPageNum_Init8
Initial number of memory pages for 8-bytes fixed data size. 1 for desktop platforms, 1 for limited memory environments.
Definition: fx_memory.h:1472
void * FXMEM_Realloc(FXMEM_FoxitMgr *pFoxitMgr, void *pointer, size_t new_size, int flags)
Foxit basic memory reallocation function.
void FXMEM_DefaultFree(void *pointer, int flags)
Default free function using default Foxit memory manager for current module.
IFX_Allocator * FXMEM_GetDefAllocator()
Get default allocator used by the library.
size_t nPageSize_Large
Minimum size of memory page for large data (> FX_FIXEDMEM_MIDBLOCKSIZE). 128 for desktop platforms...
Definition: fx_memory.h:1500
#define FX_Alloc(type, size)
A macro for Foxit memory allocation operation.
Definition: fx_memory.h:651
Definition: fs_basictypes.h:105
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:1498
FXMEM_FoxitMgr * FXMEM_CreateFoxitMgr(FXMEM_SystemMgr *pSystemMgr)
Create a Foxit manager. A system manager must be provided for actual allocation.
void * user
A generic typeless pointer for user data.
Definition: fx_memory.h:193
void FXMEM_SetDefaultMgr(FXMEM_FoxitMgr *pFoxitMgr)
Set default Foxit manager for current compile module (EXE, DLL, etc.).
Memory debugger interface. All functions must be implemented.
Definition: fx_memory.h:406
void FXMEM_CollectAll(FXMEM_FoxitMgr *pFoxitMgr)
Release all memory blocks allocated by a Foxit manager. This function is only supported on embedded s...
bool operator==(const char *str1, const CFX_ByteString &str2)
Check if two byte strings are equal.
Definition: fs_basictypes.h:128
int FX_BOOL
Boolean variable (should be TRUE or FALSE).
Definition: fx_system.h:670
void FXMEM_SetConfig(const FX_MEMCONFIG *memConfig)
Set configuration of fixed memory.
void * FXMEM_DefaultRealloc(void *pointer, size_t new_size, int flags)
Default reallocation function using default Foxit memory manager for current module.
char const * FX_LPCSTR
Pointer to constant 8-bit Windows (ANSI) characters.
Definition: fx_system.h:684
FXMEM_FoxitMgr * FXMEM_GetDefaultMgr()
Get default memory manager for current module.
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...
Fixed memory manager.
Definition: fx_memory.h:220
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...
void FXMEM_DestroyFoxitMgr(FXMEM_FoxitMgr *pFoxitMgr)
Destroy a Foxit manager instance. If the platform supports auto-collection, then all allocated memory...
void FXMEM_OutputDebugTag(FXMEM_FoxitMgr *pFoxitMgr, FX_LPCSTR tag)
Output a memory debug tag.
size_t nPageNum_Init32
Initial number of memory pages for 32-bytes fixed data size. 24 for desktop platforms, 8 for limited memory environments.
Definition: fx_memory.h:1476
size_t nPageNum_More16
More number of memory pages for 16-bytes fixed data size. 8 for desktop platforms, 4 for limited memory environments.
Definition: fx_memory.h:1481
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...
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:989
Foxit allocator interface.
Definition: fx_memory.h:961
void * FXMEM_DefaultAlloc(size_t byte_size, int flags)
Default allocation function using default Foxit memory manager for current module.
#define FX_Free(pointer)
A macro for Foxit memory free operation.
Definition: fx_memory.h:676
FXMEM_FoxitMgr * FXMEM_CreateFixedMgr(void *pMemory, size_t size, FXMEM_SystemMgr2 *pExtender)
Create a Foxit manager from a pre-allocated, fixed memory buffer.
System level memory manager. Application can implement their own system memory manager.
Definition: fx_memory.h:72
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:1496
void(* FPDF_OOM_Handler)(FXMEM_FoxitMgr *pFoxitMgr, void *param)
the prototype of the Out-Of-Memory handler.
Definition: fx_memory.h:524
size_t nPageNum_Init16
Initial number of memory pages for 16-bytes fixed data size. 8 for desktop platforms, 5 for limited memory environments.
Definition: fx_memory.h:1474
void * FXMEM_AllocDebug(FXMEM_FoxitMgr *pFoxitMgr, size_t size, int flags, FX_LPCSTR file, int line)
Foxit basic memory allocation function in debug-mode.
void FXMEM_ReportOOM(FXMEM_FoxitMgr *pFoxitMgr)
Report Out-of-memory (OOM).
void FXMEM_SetOOMHandler(FXMEM_FoxitMgr *pFoxitMgr, FPDF_OOM_Handler pOOMReportFunc, void *param)
Setup A Out-Of-Memory handler for a Foxit memory manager.
size_t nPageNum_More32
More number of memry pages for 32-bytes fixed data size. 24 for desktop platforms, 12 for limited memory environments.
Definition: fx_memory.h:1483
FXMEM_FoxitMgr * FXMEM_CreateMemoryMgr(size_t size, FX_BOOL extensible)
Create a fixed memory manager as default implementation.
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...
Structure of fixed memory configuration.
Definition: fx_memory.h:1469
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.
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:1502
#define NULL
The null-pointer value.
Definition: fx_system.h:773
#define FX_DEFINEHANDLE(name)
Macro to define a handle type.
Definition: fx_system.h:753
bool operator!=(const char *str1, const CFX_ByteString &str2)
Check if two byte strings are not equal.
Definition: fs_basictypes.h:140
#define FX_Allocator_Free(fxAllocator, ptr)
Free memory block on an allocator.
Definition: fx_memory.h:1058
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...
unsigned char FX_BYTE
Byte (8 bits).
Definition: fx_system.h:644
Header file for system related definitions.

Foxit Software Corporation Logo
@2019 Foxit Software Incorporated. All rights reserved.