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 // A template that can hold either owned or unowned references, and cleans up
698 // appropriately. Possibly the most pernicious anti-pattern imaginable, but
699 // it crops up throughout the codebase due to a desire to avoid copying-in
700 // objects or data.
701 template <typename T, typename D = std::default_delete<T>>
702 class CFX_MaybeOwned {
703  public:
704  CFX_MaybeOwned() : m_pObj(nullptr) {}
705  explicit CFX_MaybeOwned(T* ptr) : m_pObj(ptr) {}
706  explicit CFX_MaybeOwned(std::unique_ptr<T, D> ptr)
707  : m_pOwnedObj(std::move(ptr)), m_pObj(m_pOwnedObj.get()) {}
708 
709  CFX_MaybeOwned(CFX_MaybeOwned&& that) FX_NOEXCEPT
710  : m_pOwnedObj(that.m_pOwnedObj.release()), m_pObj(that.m_pObj) {
711  that.m_pObj = nullptr;
712  }
713 
714  void Reset(std::unique_ptr<T, D> ptr) {
715  m_pOwnedObj = std::move(ptr);
716  m_pObj = m_pOwnedObj.get();
717  }
718  void Reset(T* ptr = nullptr) {
719  m_pOwnedObj.reset();
720  m_pObj = ptr;
721  }
722 
723  bool IsOwned() const { return !!m_pOwnedObj; }
724  T* Get() const { return m_pObj; }
725  std::unique_ptr<T, D> Release() {
726  ASSERT(IsOwned());
727  return std::move(m_pOwnedObj);
728  }
729 
730  CFX_MaybeOwned& operator=(CFX_MaybeOwned&& that) {
731  m_pOwnedObj = std::move(that.m_pOwnedObj);
732  m_pObj = that.m_pObj;
733  that.m_pObj = nullptr;
734  return *this;
735  }
736  CFX_MaybeOwned& operator=(T* ptr) {
737  Reset(ptr);
738  return *this;
739  }
740  CFX_MaybeOwned& operator=(std::unique_ptr<T, D> ptr) {
741  Reset(std::move(ptr));
742  return *this;
743  }
744 
745  bool operator==(const CFX_MaybeOwned& that) const {
746  return Get() == that.Get();
747  }
748  bool operator==(const std::unique_ptr<T, D>& ptr) const {
749  return Get() == ptr.get();
750  }
751  bool operator==(T* ptr) const { return Get() == ptr; }
752 
753  bool operator!=(const CFX_MaybeOwned& that) const { return !(*this == that); }
754  bool operator!=(const std::unique_ptr<T, D> ptr) const {
755  return !(*this == ptr);
756  }
757  bool operator!=(T* ptr) const { return !(*this == ptr); }
758 
759  FX_EXPLICIT_OPERATOR operator bool() const { return !!m_pObj; }
760  T& operator*() const { return *m_pObj; }
761  T* operator->() const { return m_pObj; }
762  private:
763  CFX_MaybeOwned(const CFX_MaybeOwned& that) FX_EQDELETE;
764  CFX_MaybeOwned& operator=(const CFX_MaybeOwned& that) FX_EQDELETE;
765 
766  private:
767  std::unique_ptr<T, D> m_pOwnedObj;
768  T* m_pObj;
769 };
770 
771 // Used with std::unique_ptr to FX_Free raw memory.
772 struct CFX_FreeDeleter {
773  inline void operator()(void* ptr) const { FX_Free(ptr); }
774 };
782 class CFX_Object
783 {
784  public:
794  void* operator new (size_t size, FX_LPCSTR file, int line);
795 #ifndef _FX_NO_EXCEPTION_
796 
805  void operator delete (void* p, FX_LPCSTR file, int line);
806 #endif
807 
815  void* operator new (size_t size);
823  void operator delete (void* p);
824 
834  void* operator new[] (size_t size, FX_LPCSTR file, int line);
835 #ifndef _FX_NO_EXCEPTION_
836 
845  void operator delete[] (void* p, FX_LPCSTR file, int line);
846 #endif
847 
855  void* operator new[] (size_t size);
863  void operator delete[] (void* p);
864 
868  void* operator new (size_t, void* buf) { return buf; }
869 #ifndef _FX_NO_EXCEPTION_
870 
873  void operator delete (void*, void*) {}
874 #endif
875  protected:
877  ~CFX_Object() {}
878 };
879 
884 #if (_FX_OS_ == _FX_WIN32_DESKTOP_ || _FX_OS_ == _FX_WIN32_MOBILE_ || _FX_OS_ == _FX_WIN64_) && !defined(__PLACEMENT_NEW_INLINE) && !defined(_MFC_VER) && !defined(_NEW)
885  #define __PLACEMENT_NEW_INLINE
886 
887  inline void* operator new(size_t size, void* pos)
888  {
889  return pos;
890  }
891 
892  inline void operator delete(void* ptr, void* pos)
893  {
894  }
895 #endif //__PLACEMENT_NEW_INLINE
896 
897 #endif //__cplusplus
898 
899 //<<<+++OPENSOURCE_MUST_END
900 
901 //<<<+++OPENSOURCE_MUST_BEGIN
902 #ifdef __cplusplus
903 
904 #if defined(_DEBUG)
905  #define FX_NEW new(__FILE__, __LINE__)
906 #else
907 
911  #define FX_NEW new
912 #endif
913 //<<<+++OPENSOURCE_MUST_END
914 
915 #ifndef _FPDFAPI_MINI_
916  //<<<+++OPENSOURCE_BEGIN LIC==FOXIT
918  #define FX_NEW_VECTOR(Pointer, Class, Count) Pointer = FX_NEW Class[Count]
919 
920  #define FX_DELETE_VECTOR(Pointer, Class, Count) delete[] Pointer
921  //<<<+++OPENSOURCE_END
922 #else
923  //<<<+++OPENSOURCE_BEGIN LIC==GOOGLE
924  #define FX_NEW_VECTOR(Pointer, Class, Count) \
925  { \
926  Pointer = FX_Alloc(Class, Count); \
927  if (Pointer) { \
928  for (int i = 0; i < (Count); i ++) new (Pointer + i) Class; \
929  } \
930  }
931  #define FX_DELETE_VECTOR(Pointer, Class, Count) \
932  { \
933  for (int i = 0; i < (Count); i ++) Pointer[i].~Class(); \
934  FX_Free(Pointer); \
935  }
936  //<<<+++OPENSOURCE_END
937 #endif
938 
939 //<<<+++OPENSOURCE_MUST_BEGIN
944 class CFX_DestructObject : public CFX_Object
945 {
946  public:
948  virtual ~CFX_DestructObject() {}
949 };
950 //<<<+++OPENSOURCE_MUST_END
951 #endif //__cplusplus
952 
953 #ifdef __cplusplus
954 extern "C" {
955 #endif
956 
960 typedef struct _IFX_Allocator
961 {
975  void* (*m_AllocDebug)(struct _IFX_Allocator* pAllocator, size_t size, FX_LPCSTR file, int line);
987  void* (*m_Alloc)(struct _IFX_Allocator* pAllocator, size_t size);
1002  void* (*m_ReallocDebug)(struct _IFX_Allocator* pAllocator, void* p, size_t size, FX_LPCSTR file, int line);
1015  void* (*m_Realloc)(struct _IFX_Allocator* pAllocator, void* p, size_t size);
1027  void (*m_Free)(struct _IFX_Allocator* pAllocator, void* p);
1028 } IFX_Allocator;
1029 
1036 
1037 #ifdef __cplusplus
1038 }
1039 #endif
1040 
1041 #ifdef _DEBUG
1042 
1043  #define FX_Allocator_Alloc(fxAllocator, type, size) \
1044  ((fxAllocator) ? (type*)(fxAllocator)->m_AllocDebug((fxAllocator), (size) * sizeof(type), __FILE__, __LINE__) : (FX_Alloc(type, size)))
1045 
1046  #define FX_Allocator_Realloc(fxAllocator, type, ptr, new_size) \
1047  ((fxAllocator) ? (type*)(fxAllocator)->m_ReallocDebug((fxAllocator), (ptr), (new_size) * sizeof(type), __FILE__, __LINE__) : (FX_Realloc(type, ptr, new_size)))
1048 #else
1049 
1050  #define FX_Allocator_Alloc(fxAllocator, type, size) \
1051  ((fxAllocator) ? (type*)(fxAllocator)->m_Alloc((fxAllocator), (size) * sizeof(type)) : (FX_Alloc(type, size)))
1052 
1053  #define FX_Allocator_Realloc(fxAllocator, type, ptr, new_size) \
1054  ((fxAllocator) ? (type*)(fxAllocator)->m_Realloc((fxAllocator), (ptr), (new_size) * sizeof(type)) : (FX_Realloc(type, ptr, new_size)))
1055 #endif
1056 
1057 #define FX_Allocator_Free(fxAllocator, ptr) \
1058  ((fxAllocator) ? (fxAllocator)->m_Free((fxAllocator), (ptr)) : (FX_Free(ptr)))
1059 //<<<+++OPENSOURCE_MUST_END
1060 
1061 #ifdef __cplusplus
1062 
1064 inline void* operator new(size_t size, IFX_Allocator* fxAllocator)
1065 {
1066  return (void*)FX_Allocator_Alloc(fxAllocator, FX_BYTE, size);
1067 }
1068 
1069 inline void operator delete(void* ptr, IFX_Allocator* fxAllocator)
1070 {
1071  FX_Allocator_Free(fxAllocator, ptr);
1072 }
1073 
1075 #define FX_NewAtAllocator(fxAllocator) \
1076  ::new(static_cast<IFX_Allocator*>(fxAllocator))
1077 
1078 #define FX_DeleteAtAllocator(pointer, fxAllocator, __class__) \
1079  do { if (!(pointer)) break; (pointer)->~__class__(); ::operator delete(static_cast<void*>(pointer), static_cast<IFX_Allocator*>(fxAllocator)); } while(false)
1080 
1086 class CFX_AllocObject
1087 {
1088  public:
1099  void* operator new (size_t size, IFX_Allocator* pAllocator, FX_LPCSTR file, int line);
1100 #ifndef _FX_NO_EXCEPTION_
1101 
1111  void operator delete (void* p, IFX_Allocator* pAllocator, FX_LPCSTR file, int line);
1112 #endif
1113 
1122  void* operator new (size_t size, IFX_Allocator* pAllocator);
1130  void operator delete (void* p);
1131 #ifndef _FX_NO_EXCEPTION_
1132 
1140  void operator delete (void* p, IFX_Allocator* pAllocator);
1141 #endif
1142 
1146  void* operator new (size_t, void* buf) { return buf; }
1147 #ifndef _FX_NO_EXCEPTION_
1148 
1151  void operator delete (void*, void*) {}
1152 #endif
1153 
1159  IFX_Allocator* GetAllocator() const { return m_pAllocator; }
1160 
1161 private: /* all vector operators are disabled */
1172  void* operator new[] (size_t size, IFX_Allocator* pAllocator, FX_LPCSTR file, int line) { return operator new(size, pAllocator, file, line); }
1173 #ifndef _FX_NO_EXCEPTION_
1174 
1184  void operator delete[] (void* p, IFX_Allocator* pAllocator, FX_LPCSTR file, int line) {}
1185 #endif
1186 
1195  void* operator new[] (size_t size, IFX_Allocator* pAllocator) { return operator new(size, pAllocator); }
1203  void operator delete[] (void* p) {}
1204 #ifndef _FX_NO_EXCEPTION_
1205 
1213  void operator delete[] (void* p, IFX_Allocator* pAllocator) {}
1214 #endif
1215 
1216  protected:
1217  /*
1218  * The memory allocator.
1219  */
1220  IFX_Allocator* m_pAllocator;
1221 };
1222 
1223 #if defined(_DEBUG)
1224  #define FX_NEWAT(pAllocator) new(pAllocator, __FILE__, __LINE__)
1225 #else
1226 
1230  #define FX_NEWAT(pAllocator) new(pAllocator)
1231 #endif
1232 
1234 //Cocurrency controls
1236 
1237 #if !defined(_FPDFAPI_MT_NONE_) && !defined(_FPDFAPI_MT_)
1238  // We enable multi-threading by default on all systems. However, MT can be disabled by compilation macro
1239  #define _FPDFAPI_MT_
1240 #endif
1241 
1242 #ifdef _FPDFAPI_MT_
1243 
1246  class CFX_LockObject : public CFX_Object
1247  {
1248  public:
1250  CFX_LockObject() {FX_InitializeCriticalSection(&m_Lock);}
1252  ~CFX_LockObject() {FX_DeleteCriticalSection(&m_Lock);}
1253 
1255  FX_BOOL TryLock() {return FX_TryEnterCriticalSection(&m_Lock);}
1257  void Lock() {FX_EnterCriticalSection(&m_Lock);}
1259  void Unlock() {FX_LeaveCriticalSection(&m_Lock);}
1260 
1261  protected:
1262  /* Critical section. */
1263  FX_CRITICAL_SECTION m_Lock;
1264  friend class CFX_CSLock;
1265  };
1266 
1270  class CFX_CSLock
1271  {
1272  public:
1274  CFX_CSLock() : m_pCS(NULL) {}
1276  CFX_CSLock(FX_CRITICAL_SECTION* pCS) : m_pCS(pCS) {if (m_pCS) FX_EnterCriticalSection(m_pCS);}
1278  CFX_CSLock(CFX_LockObject* pObj) {m_pCS = &pObj->m_Lock; FX_EnterCriticalSection(m_pCS);}
1280  ~CFX_CSLock() {if (m_pCS) FX_LeaveCriticalSection(m_pCS);}
1281 
1283  FX_CRITICAL_SECTION* m_pCS;
1284  };
1285 
1287  #define FXMT_CSLOCK_THIS CFX_CSLock _fx_lock((CFX_LockObject*)this)
1288 
1289  #define FXMT_CSLOCK_OBJ(lock) CFX_CSLock _fx_lock((CFX_LockObject*)lock)
1290 
1291  #define FXMT_CSLOCK_DEFINEOBJ(csLock, lock) CFX_CSLock csLock((CFX_LockObject*)lock)
1292 
1294  #define FXMT_LOCKOBJECT_DEFINE(lockObj) CFX_LockObject lockObj
1295 
1296  #define FXMT_LOCKOBJECT_TRYLOCK(lockObj) (lockObj)->TryLock()
1297 
1298  #define FXMT_LOCKOBJECT_TRYLOCK_IF(lockObj) if ((lockObj)->TryLock())
1299 
1300  #define FXMT_LOCKOBJECT_LOCK(lockObj) (lockObj)->Lock()
1301 
1302  #define FXMT_LOCKOBJECT_UNLOCK(lockObj) (lockObj)->Unlock()
1303 #else
1304  class CFX_LockObject : public CFX_Object {};
1305  #define FXMT_CSLOCK_THIS
1306  #define FXMT_CSLOCK_OBJ(lock)
1307  #define FXMT_CSLOCK_DEFINEOBJ(csLock, lock)
1308 
1309  #define FXMT_LOCKOBJECT_DEFINE(lockObj)
1310  #define FXMT_LOCKOBJECT_TRYLOCK(lockObj)
1311  #define FXMT_LOCKOBJECT_TRYLOCK_IF(lockObj) if (1)
1312  #define FXMT_LOCKOBJECT_LOCK(lockObj)
1313  #define FXMT_LOCKOBJECT_UNLOCK(lockObj)
1314 #endif
1315 //<<<+++OPENSOURCE_MUST_END
1316 
1324 class CFX_GrowOnlyPool : public IFX_Allocator, public CFX_Object
1325 
1326 {
1327  public:
1336  CFX_GrowOnlyPool(IFX_Allocator* pAllocator = NULL, size_t trunk_size = 16384);
1337 
1341  ~CFX_GrowOnlyPool();
1342 
1350  void SetAllocator(IFX_Allocator* pAllocator);
1351 
1359  void SetTrunkSize(size_t trunk_size) { m_TrunkSize = trunk_size; }
1360 
1370  void* AllocDebug(size_t size, FX_LPCSTR file, int line) { return Alloc(size); }
1378  void* Alloc(size_t size);
1386  void* ReallocDebug(void* p, size_t new_size, FX_LPCSTR file, int line) { return NULL; }
1394  void* Realloc(void* p, size_t new_size) { return NULL; }
1400  void Free(void* mem) {}
1401 
1407  void FreeAll();
1408 
1409  private:
1410  /* Trunk size. */
1411  size_t m_TrunkSize;
1412  /* Pointer to the first trunk. */
1413  void* m_pFirstTrunk;
1414 
1415  /* Memory allocator. */
1416  IFX_Allocator* m_pAllocator;
1417  /* Critical section used for synchronization. */
1418  FX_CRITICAL_SECTION m_Lock;
1419 
1420 };
1421 
1422 // A Proxy class for allocate memory.
1423 
1424 template <class T> class AllocProxy : public CFX_Object {
1425  public:
1426  AllocProxy(size_t size) { buffer = (T *)FX_Alloc(T, size); }
1427  ~AllocProxy() { if (buffer) FX_Free(buffer); }
1428  T &operator[](int index) { return buffer[index]; }
1429  operator T *() { return buffer; }
1430  T *operator +(int offset) { return buffer + offset; }
1431  operator bool() const { return !!buffer; }
1432  T *operator->() const { return buffer; }
1433 
1434  private:
1435  AllocProxy(const AllocProxy &) {}
1436  AllocProxy &operator=(const AllocProxy &) {}
1437  T* buffer;
1438 };
1439 
1440 // Used with std::unique_ptr to Release() objects that can't be deleted.
1441 template <class T>
1442 struct CFX_ReleaseDeleter {
1443  inline void operator()(T* ptr) const { ptr->Release(); }
1444 };
1445 
1446 //<<<+++OPENSOURCE_MUST_END
1447 
1448 //<<<+++OPENSOURCE_MUST_BEGIN
1449 #endif //__cplusplus
1450 //<<<+++OPENSOURCE_MUST_END
1451 
1452 #ifdef __cplusplus
1453 extern "C" {
1454 #endif
1455 
1456 //*****************************************************************************
1457 //* Fixed memory management
1458 //*****************************************************************************
1459 #define _FXMEM_NO64_
1460 //#define _FXMEM_LIT_
1461 
1463 #define FX_FIXEDMEM_PAGESIZE (4096 * 16)
1464 
1465 #define FX_FIXEDMEM_MIDBLOCKSIZE (4096)
1466 
1468 typedef struct _FX_MEMCONFIG
1469 {
1476  #if !defined(_FXMEM_NO64_)
1477  size_t nPageNum_Init64;
1478  #endif
1479 
1483  #if !defined(_FXMEM_NO64_)
1484  size_t nPageNum_More64;
1485  #endif
1486  #if defined(_FXMEM_LIT_)
1487  size_t nPageSize_Lit;
1488  size_t nPageNum_InitLit;
1489  size_t nPageNum_MoreLit;
1490  size_t nPageNum_ReservedLit;
1491  #endif
1492 
1502 }FX_MEMCONFIG;
1503 
1514 void FXMEM_SetConfig(const FX_MEMCONFIG* memConfig);
1515 
1517 //#define _FX_MEMSTATE_
1518 #if defined(_FX_MEMSTATE_)
1519 
1520  typedef struct _FX_MEMPAGESTATE_
1521  {
1522  size_t nCurMemSize;
1523  size_t nMinMemSize;
1524  size_t nMaxMemSize;
1525  size_t nCurAvailSize;
1526  size_t nMinAvailSize;
1527  size_t nMaxAvailSize;
1528  size_t nCurUsedSize;
1529  size_t nMinUsedSize;
1530  size_t nMaxUsedSize;
1531  size_t nCurUsedRate;
1532  size_t nMinUsedRate;
1533  size_t nMaxUsedRate;
1534  size_t bValid;
1535  }FX_MEMPAGESTATE;
1536  typedef struct _FX_MEMINFO_
1537  {
1538  size_t memBlockCount[64];
1539  FX_MEMPAGESTATE pageState8;
1540  FX_MEMPAGESTATE pageState16;
1541  FX_MEMPAGESTATE pageState32;
1542  FX_MEMPAGESTATE pageStateMid;
1543  FX_MEMPAGESTATE pageStateLarge;
1544  FX_MEMPAGESTATE totalState;
1545  }FX_MEMINFO;
1546 
1547  #define FX_MEMSTATE_RATEFRACTION 100000
1548 
1549  void FX_MemState_MergeInfo(FX_MEMINFO *mi1, const FX_MEMINFO *mi2);
1550  FX_MEMINFO* FX_MemState_GetInfo();
1551  void FX_MemState_ResetInfo();
1552 
1553 #endif //_FX_MEMSTATE_
1554 
1555 #ifdef __cplusplus
1556 }
1557 #endif
1558 
1559 //<<<+++OPENSOURCE_MUST_END
1560 
1561 //<<<+++OPENSOURCE_MUST_BEGIN
1562 #endif //_FX_MEMORY_H_
1563 //<<<+++OPENSOURCE_MUST_END
1564 
1567 //<<<+++OPENSOURCE_END
size_t nPageSize_Mid
Size of memory pages for middle data range (> 32-bytes & <= FX_FIXEDMEM_MIDBLOCKSIZE)....
Definition: fx_memory.h:1493
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:1050
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:794
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,...
Definition: fx_memory.h:1471
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:1499
#define FX_Alloc(type, size)
A macro for Foxit memory allocation operation.
Definition: fx_memory.h:651
Definition: fs_basictypes.h:102
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:1497
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:125
int FX_BOOL
Boolean variable (should be TRUE or FALSE).
Definition: fx_system.h:666
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:679
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,...
Definition: fx_memory.h:1475
size_t nPageNum_More16
More number of memory pages for 16-bytes fixed data size. 8 for desktop platforms,...
Definition: fx_memory.h:1480
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:982
Foxit allocator interface.
Definition: fx_memory.h:960
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:1495
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,...
Definition: fx_memory.h:1473
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,...
Definition: fx_memory.h:1482
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:1468
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:1501
#define NULL
The null-pointer value.
Definition: fx_system.h:767
#define FX_DEFINEHANDLE(name)
Macro to define a handle type.
Definition: fx_system.h:749
bool operator!=(const char *str1, const CFX_ByteString &str2)
Check if two byte strings are not equal.
Definition: fs_basictypes.h:137
#define FX_Allocator_Free(fxAllocator, ptr)
Free memory block on an allocator.
Definition: fx_memory.h:1057
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
@2018 Foxit Software Incorporated. All rights reserved.