Foxit PDF SDK
fx_memory.h
浏览该文件的文档.
1 
14 //<<<+++OPENSOURCE
15 //<<<+++OPENSOURCE_LICENSE
16 //<<<+++OPENSOURCE_BEGIN LIC==FOXIT||LIC==GOOGLE
17 
23 /*
24  *
25  * FPDFAPI 可能使用与应用程序不同的堆(在 FPDFAPI 的 DLL 版本情况下),
26  * 因此,为了在 API 和应用程序之间交换指针,应用程序应使用以下
27  * 内存分配和释放函数。
28  *
29  *
30  * FOXIT API 中有两层内存管理器:
31  *
32  * 1. 系统级内存管理器。此级别依赖于系统。
33  * API 提供了一个默认的系统内存管理器(使用 CRT malloc/free 函数),
34  * 但应用程序可以实现自己的管理器以适应不同的系统环境。
35  *
36  * 2. Foxit 内存管理器。此级别与系统无关,但依赖于系统级管理器。
37  * API 提供了一个默认的 Foxit 内存管理器,它依赖于默认的系统管理器。
38  * 应用程序无法自定义 Foxit 内存管理器,但它们可以创建不同的
39  * Foxit 内存管理器实例,依赖于不同的底层系统管理器
40  * (例如,每个系统管理器可能使用不同的堆,那么,不同的 Foxit 管理器
41  * 将能够使用单独的堆。)
42  *
43  *
44  * Foxit 内存管理器还提供子堆功能,用于将对象聚合在一起
45  * 以避免内存碎片。
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 // 基础内存例程
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);
178  void (*Purge)(struct _FXMEM_SystemMgr* pMgr);
189  void (*CollectAll)(struct _FXMEM_SystemMgr* pMgr);
190 
192  void* user;
194 
200 FX_DEFINEHANDLE(FXMEM_FoxitMgr)
201 
202 
214 FXMEM_FoxitMgr* FXMEM_CreateFoxitMgr(FXMEM_SystemMgr* pSystemMgr);
215 
227 FXMEM_FoxitMgr* FXMEM_CreatePyMgr();
228 
244 int FXMEM_SetPyConfig(size_t lowByteRange, size_t highByteRange);
245 
249 typedef struct _FXMEM_SystemMgr2
250 {
261  FX_BOOL (*More)(struct _FXMEM_SystemMgr2* pMgr, size_t alloc_size, void** new_memory, size_t* new_size);
270  void (*Free)(struct _FXMEM_SystemMgr2* pMgr, void* memory);
272 
287 FXMEM_FoxitMgr* FXMEM_CreateFixedMgr(void* pMemory, size_t size, FXMEM_SystemMgr2* pExtender);
288 
302 FXMEM_FoxitMgr* FXMEM_CreateMemoryMgr(size_t size, FX_BOOL extensible);
303 
312 size_t FXMEM_GetBlockSizeInFixedMgr(FXMEM_FoxitMgr* pFoxitMgr, void* ptr);
313 
319 FXMEM_FoxitMgr* FXMEM_GetDefaultMgr();
320 
327 void FXMEM_SetDefaultMgr(FXMEM_FoxitMgr* pFoxitMgr);
328 
334 void FXMEM_ResetSystemMgr();
335 
343 void FXMEM_DestroyFoxitMgr(FXMEM_FoxitMgr* pFoxitMgr);
344 
349 
359 void* FXMEM_Alloc(FXMEM_FoxitMgr* pFoxitMgr, size_t size, int flags);
371 void* FXMEM_AllocDebug(FXMEM_FoxitMgr* pFoxitMgr, size_t size, int flags, FX_LPCSTR file, int line);
382 void* FXMEM_Realloc(FXMEM_FoxitMgr* pFoxitMgr, void* pointer, size_t new_size, int flags);
395 void* FXMEM_ReallocDebug(FXMEM_FoxitMgr* pFoxitMgr, void* pointer, size_t new_size, int flags, FX_LPCSTR file, int line);
405 void FXMEM_Free(FXMEM_FoxitMgr* pFoxitMgr, void* pointer, int flags);
406 
416 void FXMEM_CollectAll(FXMEM_FoxitMgr* pFoxitMgr);
417 
427 void FXMEM_PurgeMgr(FXMEM_FoxitMgr* pFoxitMgr);
428 
436 void FXMEM_ReportOOM(FXMEM_FoxitMgr* pFoxitMgr);
437 
441 typedef struct {
455  void (*OnAlloc)(FXMEM_FoxitMgr* pMgr, void* p, size_t size, int flags);
471  void (*OnAllocDebug)(FXMEM_FoxitMgr* pMgr, void* p, size_t size, int flags, FX_LPCSTR file, int line);
486  void (*OnRealloc)(FXMEM_FoxitMgr* pMgr, void* old_p, void* new_p, size_t size, int flags);
503  void (*OnReallocDebug)(FXMEM_FoxitMgr* pMgr, void* old_p, void* new_p, size_t size, int flags, FX_LPCSTR file, int line);
516  void (*OnFree)(FXMEM_FoxitMgr* pMgr, void* p, int flags);
528  void (*OnTag)(FXMEM_FoxitMgr* pMgr, FX_LPCSTR tag);
530 
539 void FXMEM_UseDebugger(FXMEM_FoxitMgr* pFoxitMgr, FXMEM_Debugger* pDebugger);
540 
549 void FXMEM_OutputDebugTag(FXMEM_FoxitMgr* pFoxitMgr, FX_LPCSTR tag);
550 
559 typedef void (*FPDF_OOM_Handler)(FXMEM_FoxitMgr* pFoxitMgr, void* param);
560 
570 void FXMEM_SetOOMHandler(FXMEM_FoxitMgr* pFoxitMgr, FPDF_OOM_Handler pOOMReportFunc, void* param);
571 
576 
585 void* FXMEM_DefaultAlloc(size_t byte_size, int flags);
595 void* FXMEM_DefaultAlloc2(size_t units, size_t unit_size, int flags);
606 void* FXMEM_DefaultAllocDebug(size_t size, int flags, FX_LPCSTR file, int line);
618 void* FXMEM_DefaultAllocDebug2(size_t units, size_t unit_size, int flags, FX_LPCSTR file, int line);
628 void* FXMEM_DefaultRealloc(void* pointer, size_t new_size, int flags);
639 void* FXMEM_DefaultRealloc2(void* pointer, size_t units, size_t unit_size, int flags);
651 void* FXMEM_DefaultReallocDebug(void* pointer, size_t new_size, int flags, FX_LPCSTR file, int line);
664 void* FXMEM_DefaultReallocDebug2(void* pointer, size_t units, size_t unit_size, int flags, FX_LPCSTR file, int line);
673 void FXMEM_DefaultFree(void* pointer, int flags);
674 
677 /* FPDFAPI应用程序应该为非类数据类型使用FX_Alloc宏 */
678 #ifdef _DEBUG
679  #define FX_Alloc(type, size) (type*)FXMEM_DefaultAllocDebug2(size, sizeof(type), 0, __FILE__, __LINE__)
680  #define FX_Realloc(type, ptr, new_size) (type*)FXMEM_DefaultReallocDebug2(ptr, new_size, sizeof(type), 0, __FILE__, __LINE__)
681 #else
682 
686  #define FX_Alloc(type, size) (type*)FXMEM_DefaultAlloc2(size, sizeof(type), 0)
687 
691  #define FX_Realloc(type, ptr, size) (type*)FXMEM_DefaultRealloc2(ptr, size, sizeof(type), 0)
692 #endif
693 
694 #ifdef _DEBUG
695  #define FX_AllocNL(type, size) (type*)FXMEM_DefaultAllocDebug2(size, sizeof(type), FXMEM_NONLEAVE, __FILE__, __LINE__)
696  #define FX_ReallocNL(type, ptr, new_size) (type*)FXMEM_DefaultReallocDebug2(ptr, new_size, sizeof(type), FXMEM_NONLEAVE, __FILE__, __LINE__)
697 #else
698 
702  #define FX_AllocNL(type, size) (type*)FXMEM_DefaultAlloc2(size, sizeof(type), FXMEM_NONLEAVE)
703 
707  #define FX_ReallocNL(type, ptr, size) (type*)FXMEM_DefaultRealloc2(ptr, size, sizeof(type), FXMEM_NONLEAVE)
708 #endif
709 
711 #define FX_Free(pointer) FXMEM_DefaultFree(pointer, 0)
712 
713 #ifdef __cplusplus
714 }
715 #endif
716 
717 #ifdef __cplusplus
718 
719 #include <memory>
720 #include <algorithm>
721 #include <utility>
722 
723 #if __cplusplus >= 201103L
724 #define FX_EQDELETE = delete
725 #define FX_NOEXCEPT noexcept
726 #define FX_EXPLICIT_OPERATOR explicit
727 #else
728 #define FX_EQDELETE //= delete
729 #define FX_NOEXCEPT //noexcept
730 #define FX_EXPLICIT_OPERATOR //explicit
731 #endif
732 // 一个可以持有拥有或非拥有引用的模板,并进行适当的清理。
733 // 可能是最恶劣的反模式,但由于避免复制对象或数据的需要,
734 // 在整个代码库中经常出现。
735 template <typename T, typename D = std::default_delete<T>>
736 class CFX_MaybeOwned {
737  public:
738  CFX_MaybeOwned() : m_pObj(nullptr) {}
739  explicit CFX_MaybeOwned(T* ptr) : m_pObj(ptr) {}
740  explicit CFX_MaybeOwned(std::unique_ptr<T, D> ptr)
741  : m_pOwnedObj(std::move(ptr)), m_pObj(m_pOwnedObj.get()) {}
742 
743  CFX_MaybeOwned(CFX_MaybeOwned&& that) FX_NOEXCEPT
744  : m_pOwnedObj(that.m_pOwnedObj.release()), m_pObj(that.m_pObj) {
745  that.m_pObj = nullptr;
746  }
747 
748  void Reset(std::unique_ptr<T, D> ptr) {
749  m_pOwnedObj = std::move(ptr);
750  m_pObj = m_pOwnedObj.get();
751  }
752  void Reset(T* ptr = nullptr) {
753  m_pOwnedObj.reset();
754  m_pObj = ptr;
755  }
756 
757  bool IsOwned() const { return !!m_pOwnedObj; }
758  T* Get() const { return m_pObj; }
759  std::unique_ptr<T, D> Release() {
760  ASSERT(IsOwned());
761  return std::move(m_pOwnedObj);
762  }
763 
764  CFX_MaybeOwned& operator=(CFX_MaybeOwned&& that) {
765  m_pOwnedObj = std::move(that.m_pOwnedObj);
766  m_pObj = that.m_pObj;
767  that.m_pObj = nullptr;
768  return *this;
769  }
770  CFX_MaybeOwned& operator=(T* ptr) {
771  Reset(ptr);
772  return *this;
773  }
774  CFX_MaybeOwned& operator=(std::unique_ptr<T, D> ptr) {
775  Reset(std::move(ptr));
776  return *this;
777  }
778 
779  bool operator==(const CFX_MaybeOwned& that) const {
780  return Get() == that.Get();
781  }
782  bool operator==(const std::unique_ptr<T, D>& ptr) const {
783  return Get() == ptr.get();
784  }
785  bool operator==(T* ptr) const { return Get() == ptr; }
786 
787  bool operator!=(const CFX_MaybeOwned& that) const { return !(*this == that); }
788  bool operator!=(const std::unique_ptr<T, D> ptr) const {
789  return !(*this == ptr);
790  }
791  bool operator!=(T* ptr) const { return !(*this == ptr); }
792 
793  FX_EXPLICIT_OPERATOR operator bool() const { return !!m_pObj; }
794  T& operator*() const { return *m_pObj; }
795  T* operator->() const { return m_pObj; }
796  private:
797  CFX_MaybeOwned(const CFX_MaybeOwned& that) FX_EQDELETE;
798  CFX_MaybeOwned& operator=(const CFX_MaybeOwned& that) FX_EQDELETE;
799 
800  private:
801  std::unique_ptr<T, D> m_pOwnedObj;
802  T* m_pObj;
803 };
804 
805 // 与std::unique_ptr一起使用以FX_Free原始内存。
806 struct CFX_FreeDeleter {
807  inline void operator()(void* ptr) const { FX_Free(ptr); }
808 };
816 class CFX_Object
817 {
818  public:
828  void* operator new (size_t size, FX_LPCSTR file, int line);
829 #ifndef _FX_NO_EXCEPTION_
830 
839  void operator delete (void* p, FX_LPCSTR file, int line);
840 #endif
841 
849  void* operator new (size_t size);
857  void operator delete (void* p);
858 
868  void* operator new[] (size_t size, FX_LPCSTR file, int line);
869 #ifndef _FX_NO_EXCEPTION_
870 
879  void operator delete[] (void* p, FX_LPCSTR file, int line);
880 #endif
881 
889  void* operator new[] (size_t size);
897  void operator delete[] (void* p);
898 
902  void* operator new (size_t, void* buf) { return buf; }
903 #ifndef _FX_NO_EXCEPTION_
904 
907  void operator delete (void*, void*) {}
908 #endif
909  protected:
911  ~CFX_Object() {}
912 };
913 
918 #if (_FX_OS_ == _FX_WIN32_DESKTOP_ || _FX_OS_ == _FX_WIN32_MOBILE_ || _FX_OS_ == _FX_WIN64_) && !defined(__PLACEMENT_NEW_INLINE) && !defined(_MFC_VER) && !defined(_NEW)
919  #define __PLACEMENT_NEW_INLINE
920 
921  inline void* operator new(size_t size, void* pos)
922  {
923  return pos;
924  }
925 
926  inline void operator delete(void* ptr, void* pos)
927  {
928  }
929 #endif //__PLACEMENT_NEW_INLINE
930 
931 #endif //__cplusplus
932 
933 //<<<+++OPENSOURCE_MUST_END
934 
935 //<<<+++OPENSOURCE_MUST_BEGIN
936 #ifdef __cplusplus
937 
938 #if defined(_DEBUG)
939  #define FX_NEW new(__FILE__, __LINE__)
940 #else
941 
945  #define FX_NEW new
946 #endif
947 //<<<+++OPENSOURCE_MUST_END
948 
949 #ifndef _FPDFAPI_MINI_
950  //<<<+++OPENSOURCE_BEGIN LIC==FOXIT
952  #define FX_NEW_VECTOR(Pointer, Class, Count) Pointer = FX_NEW Class[Count]
953 
954  #define FX_DELETE_VECTOR(Pointer, Class, Count) delete[] Pointer
955  //<<<+++OPENSOURCE_END
956 #else
957  //<<<+++OPENSOURCE_BEGIN LIC==GOOGLE
958  #define FX_NEW_VECTOR(Pointer, Class, Count) \
959  { \
960  Pointer = FX_Alloc(Class, Count); \
961  if (Pointer) { \
962  for (int i = 0; i < (Count); i ++) new (Pointer + i) Class; \
963  } \
964  }
965  #define FX_DELETE_VECTOR(Pointer, Class, Count) \
966  { \
967  for (int i = 0; i < (Count); i ++) Pointer[i].~Class(); \
968  FX_Free(Pointer); \
969  }
970  //<<<+++OPENSOURCE_END
971 #endif
972 
973 //<<<+++OPENSOURCE_MUST_BEGIN
978 class CFX_DestructObject : public CFX_Object
979 {
980  public:
982  virtual ~CFX_DestructObject() {}
983 };
984 //<<<+++OPENSOURCE_MUST_END
985 #endif //__cplusplus
986 
987 #ifdef __cplusplus
988 extern "C" {
989 #endif
990 
994 typedef struct _IFX_Allocator
995 {
1009  void* (*m_AllocDebug)(struct _IFX_Allocator* pAllocator, size_t size, FX_LPCSTR file, int line);
1021  void* (*m_Alloc)(struct _IFX_Allocator* pAllocator, size_t size);
1036  void* (*m_ReallocDebug)(struct _IFX_Allocator* pAllocator, void* p, size_t size, FX_LPCSTR file, int line);
1049  void* (*m_Realloc)(struct _IFX_Allocator* pAllocator, void* p, size_t size);
1061  void (*m_Free)(struct _IFX_Allocator* pAllocator, void* p);
1062 } IFX_Allocator;
1063 
1070 
1071 #ifdef __cplusplus
1072 }
1073 #endif
1074 
1075 #ifdef _DEBUG
1076 
1077  #define FX_Allocator_Alloc(fxAllocator, type, size) \
1078  ((fxAllocator) ? (type*)(fxAllocator)->m_AllocDebug((fxAllocator), (size) * sizeof(type), __FILE__, __LINE__) : (FX_Alloc(type, size)))
1079 
1080  #define FX_Allocator_Realloc(fxAllocator, type, ptr, new_size) \
1081  ((fxAllocator) ? (type*)(fxAllocator)->m_ReallocDebug((fxAllocator), (ptr), (new_size) * sizeof(type), __FILE__, __LINE__) : (FX_Realloc(type, ptr, new_size)))
1082 #else
1083 
1084  #define FX_Allocator_Alloc(fxAllocator, type, size) \
1085  ((fxAllocator) ? (type*)(fxAllocator)->m_Alloc((fxAllocator), (size) * sizeof(type)) : (FX_Alloc(type, size)))
1086 
1087  #define FX_Allocator_Realloc(fxAllocator, type, ptr, new_size) \
1088  ((fxAllocator) ? (type*)(fxAllocator)->m_Realloc((fxAllocator), (ptr), (new_size) * sizeof(type)) : (FX_Realloc(type, ptr, new_size)))
1089 #endif
1090 
1091 #define FX_Allocator_Free(fxAllocator, ptr) \
1092  ((fxAllocator) ? (fxAllocator)->m_Free((fxAllocator), (ptr)) : (FX_Free(ptr)))
1093 //<<<+++OPENSOURCE_MUST_END
1094 
1095 #ifdef __cplusplus
1096 
1098 inline void* operator new(size_t size, IFX_Allocator* fxAllocator)
1099 {
1100  return (void*)FX_Allocator_Alloc(fxAllocator, FX_BYTE, size);
1101 }
1102 
1103 inline void operator delete(void* ptr, IFX_Allocator* fxAllocator)
1104 {
1105  FX_Allocator_Free(fxAllocator, ptr);
1106 }
1107 
1109 #define FX_NewAtAllocator(fxAllocator) \
1110  ::new(static_cast<IFX_Allocator*>(fxAllocator))
1111 
1112 #define FX_DeleteAtAllocator(pointer, fxAllocator, __class__) \
1113  do { (pointer)->~__class__(); ::operator delete(static_cast<void*>(pointer), static_cast<IFX_Allocator*>(fxAllocator)); } while(false)
1114 
1120 class CFX_AllocObject
1121 {
1122  public:
1133  void* operator new (size_t size, IFX_Allocator* pAllocator, FX_LPCSTR file, int line);
1134 #ifndef _FX_NO_EXCEPTION_
1135 
1145  void operator delete (void* p, IFX_Allocator* pAllocator, FX_LPCSTR file, int line);
1146 #endif
1147 
1156  void* operator new (size_t size, IFX_Allocator* pAllocator);
1164  void operator delete (void* p);
1165 #ifndef _FX_NO_EXCEPTION_
1166 
1174  void operator delete (void* p, IFX_Allocator* pAllocator);
1175 #endif
1176 
1180  void* operator new (size_t, void* buf) { return buf; }
1181 #ifndef _FX_NO_EXCEPTION_
1182 
1185  void operator delete (void*, void*) {}
1186 #endif
1187 
1193  IFX_Allocator* GetAllocator() const { return m_pAllocator; }
1194 
1195 private: /* 所有向量操作符都被禁用 */
1206  void* operator new[] (size_t size, IFX_Allocator* pAllocator, FX_LPCSTR file, int line) { return operator new(size, pAllocator, file, line); }
1207 #ifndef _FX_NO_EXCEPTION_
1208 
1218  void operator delete[] (void* p, IFX_Allocator* pAllocator, FX_LPCSTR file, int line) {}
1219 #endif
1220 
1229  void* operator new[] (size_t size, IFX_Allocator* pAllocator) { return operator new(size, pAllocator); }
1237  void operator delete[] (void* p) {}
1238 #ifndef _FX_NO_EXCEPTION_
1239 
1247  void operator delete[] (void* p, IFX_Allocator* pAllocator) {}
1248 #endif
1249 
1250  protected:
1251  /*
1252  * 内存分配器。
1253  */
1254  IFX_Allocator* m_pAllocator;
1255 };
1256 
1257 #if defined(_DEBUG)
1258  #define FX_NEWAT(pAllocator) new(pAllocator, __FILE__, __LINE__)
1259 #else
1260 
1264  #define FX_NEWAT(pAllocator) new(pAllocator)
1265 #endif
1266 
1268 //并发控制
1270 
1271 #if !defined(_FPDFAPI_MT_NONE_) && !defined(_FPDFAPI_MT_)
1272  // 我们在所有系统上默认启用多线程。然而,MT可以通过编译宏禁用
1273  #define _FPDFAPI_MT_
1274 #endif
1275 
1276 #ifdef _FPDFAPI_MT_
1277 
1280  class CFX_LockObject : public CFX_Object
1281  {
1282  public:
1284  CFX_LockObject() {FX_InitializeCriticalSection(&m_Lock);}
1286  ~CFX_LockObject() {FX_DeleteCriticalSection(&m_Lock);}
1287 
1289  FX_BOOL TryLock() {return FX_TryEnterCriticalSection(&m_Lock);}
1291  void Lock() {FX_EnterCriticalSection(&m_Lock);}
1293  void Unlock() {FX_LeaveCriticalSection(&m_Lock);}
1294 
1295  protected:
1296  /* 临界区。 */
1297  FX_CRITICAL_SECTION m_Lock;
1298  friend class CFX_CSLock;
1299  };
1300 
1304  class CFX_CSLock
1305  {
1306  public:
1308  CFX_CSLock() : m_pCS(NULL) {}
1310  CFX_CSLock(FX_CRITICAL_SECTION* pCS) : m_pCS(pCS) {if (m_pCS) FX_EnterCriticalSection(m_pCS);}
1312  CFX_CSLock(CFX_LockObject* pObj) {m_pCS = &pObj->m_Lock; FX_EnterCriticalSection(m_pCS);}
1314  ~CFX_CSLock() {if (m_pCS) FX_LeaveCriticalSection(m_pCS);}
1315 
1317  FX_CRITICAL_SECTION* m_pCS;
1318  };
1319 
1321  #define FXMT_CSLOCK_THIS CFX_CSLock _fx_lock((CFX_LockObject*)this)
1322 
1323  #define FXMT_CSLOCK_OBJ(lock) CFX_CSLock _fx_lock((CFX_LockObject*)lock)
1324 
1325  #define FXMT_CSLOCK_DEFINEOBJ(csLock, lock) CFX_CSLock csLock((CFX_LockObject*)lock)
1326 
1328  #define FXMT_LOCKOBJECT_DEFINE(lockObj) CFX_LockObject lockObj
1329 
1330  #define FXMT_LOCKOBJECT_TRYLOCK(lockObj) (lockObj)->TryLock()
1331 
1332  #define FXMT_LOCKOBJECT_TRYLOCK_IF(lockObj) if ((lockObj)->TryLock())
1333 
1334  #define FXMT_LOCKOBJECT_LOCK(lockObj) (lockObj)->Lock()
1335 
1336  #define FXMT_LOCKOBJECT_UNLOCK(lockObj) (lockObj)->Unlock()
1337 #else
1338  class CFX_LockObject : public CFX_Object {};
1339  #define FXMT_CSLOCK_THIS
1340  #define FXMT_CSLOCK_OBJ(lock)
1341  #define FXMT_CSLOCK_DEFINEOBJ(csLock, lock)
1342 
1343  #define FXMT_LOCKOBJECT_DEFINE(lockObj)
1344  #define FXMT_LOCKOBJECT_TRYLOCK(lockObj)
1345  #define FXMT_LOCKOBJECT_TRYLOCK_IF(lockObj) if (1)
1346  #define FXMT_LOCKOBJECT_LOCK(lockObj)
1347  #define FXMT_LOCKOBJECT_UNLOCK(lockObj)
1348 #endif
1349 //<<<+++OPENSOURCE_MUST_END
1350 
1358 class CFX_GrowOnlyPool : public IFX_Allocator, public CFX_Object
1359 
1360 {
1361  public:
1370  CFX_GrowOnlyPool(IFX_Allocator* pAllocator = NULL, size_t trunk_size = 16384);
1371 
1375  ~CFX_GrowOnlyPool();
1376 
1384  void SetAllocator(IFX_Allocator* pAllocator);
1385 
1393  void SetTrunkSize(size_t trunk_size) { m_TrunkSize = trunk_size; }
1394 
1404  void* AllocDebug(size_t size, FX_LPCSTR file, int line) { return Alloc(size); }
1412  void* Alloc(size_t size);
1420  void* ReallocDebug(void* p, size_t new_size, FX_LPCSTR file, int line) { return NULL; }
1428  void* Realloc(void* p, size_t new_size) { return NULL; }
1434  void Free(void* mem) {}
1435 
1441  void FreeAll();
1442 
1443  private:
1444  /* Trunk大小。 */
1445  size_t m_TrunkSize;
1446  /* 指向第一个trunk的指针。 */
1447  void* m_pFirstTrunk;
1448 
1449  /* 内存分配器。 */
1450  IFX_Allocator* m_pAllocator;
1451  /* 用于同步的临界区。 */
1452  FX_CRITICAL_SECTION m_Lock;
1453 
1454 };
1455 
1456 // 用于分配内存的代理类。
1457 
1458 template <class T> class AllocProxy : public CFX_Object {
1459  public:
1460  AllocProxy(size_t size) { buffer = (T *)FX_Alloc(T, size); }
1461  ~AllocProxy() { if (buffer) FX_Free(buffer); }
1462  T &operator[](int index) { return buffer[index]; }
1463  operator T *() { return buffer; }
1464  T *operator +(int offset) { return buffer + offset; }
1465  operator bool() const { return !!buffer; }
1466  T *operator->() const { return buffer; }
1467 
1468  private:
1469  AllocProxy(const AllocProxy &) {}
1470  AllocProxy &operator=(const AllocProxy &) {}
1471  T* buffer;
1472 };
1473 
1474 // 与std::unique_ptr一起使用以Release()无法删除的对象。
1475 template <class T>
1476 struct CFX_ReleaseDeleter {
1477  inline void operator()(T* ptr) const { ptr->Release(); }
1478 };
1479 
1480 //<<<+++OPENSOURCE_MUST_END
1481 
1482 //<<<+++OPENSOURCE_MUST_BEGIN
1483 #endif //__cplusplus
1484 //<<<+++OPENSOURCE_MUST_END
1485 
1486 #ifdef __cplusplus
1487 extern "C" {
1488 #endif
1489 
1490 //*****************************************************************************
1491 //* 固定内存管理
1492 //*****************************************************************************
1493 #define _FXMEM_NO64_
1494 //#define _FXMEM_LIT_
1495 
1497 #define FX_FIXEDMEM_PAGESIZE (4096 * 16)
1498 
1499 #define FX_FIXEDMEM_MIDBLOCKSIZE (4096)
1500 
1502 typedef struct _FX_MEMCONFIG
1503 {
1510  #if !defined(_FXMEM_NO64_)
1511  size_t nPageNum_Init64;
1512  #endif
1513 
1517  #if !defined(_FXMEM_NO64_)
1518  size_t nPageNum_More64;
1519  #endif
1520  #if defined(_FXMEM_LIT_)
1521  size_t nPageSize_Lit;
1522  size_t nPageNum_InitLit;
1523  size_t nPageNum_MoreLit;
1524  size_t nPageNum_ReservedLit;
1525  #endif
1526 
1536 }FX_MEMCONFIG;
1537 
1548 void FXMEM_SetConfig(const FX_MEMCONFIG* memConfig);
1549 
1551 //#define _FX_MEMSTATE_
1552 #if defined(_FX_MEMSTATE_)
1553 
1554  typedef struct _FX_MEMPAGESTATE_
1555  {
1556  size_t nCurMemSize;
1557  size_t nMinMemSize;
1558  size_t nMaxMemSize;
1559  size_t nCurAvailSize;
1560  size_t nMinAvailSize;
1561  size_t nMaxAvailSize;
1562  size_t nCurUsedSize;
1563  size_t nMinUsedSize;
1564  size_t nMaxUsedSize;
1565  size_t nCurUsedRate;
1566  size_t nMinUsedRate;
1567  size_t nMaxUsedRate;
1568  size_t bValid;
1569  }FX_MEMPAGESTATE;
1570  typedef struct _FX_MEMINFO_
1571  {
1572  size_t memBlockCount[64];
1573  FX_MEMPAGESTATE pageState8;
1574  FX_MEMPAGESTATE pageState16;
1575  FX_MEMPAGESTATE pageState32;
1576  FX_MEMPAGESTATE pageStateMid;
1577  FX_MEMPAGESTATE pageStateLarge;
1578  FX_MEMPAGESTATE totalState;
1579  }FX_MEMINFO;
1580 
1581  #define FX_MEMSTATE_RATEFRACTION 100000
1582 
1583  void FX_MemState_MergeInfo(FX_MEMINFO *mi1, const FX_MEMINFO *mi2);
1584  FX_MEMINFO* FX_MemState_GetInfo();
1585  void FX_MemState_ResetInfo();
1586 
1587 #endif //_FX_MEMSTATE_
1588 
1589 #ifdef __cplusplus
1590 }
1591 #endif
1592 
1593 //<<<+++OPENSOURCE_MUST_END
1594 
1595 //<<<+++OPENSOURCE_MUST_BEGIN
1596 #endif //_FX_MEMORY_H_
1597 //<<<+++OPENSOURCE_MUST_END
1598 
1601 //<<<+++OPENSOURCE_END
size_t nPageSize_Mid
中等数据范围(>32字节且<=FX_FIXEDMEM_MIDBLOCKSIZE)的内存页大小。桌面平台为32,有限内存环境为8。
Definition: fx_memory.h:1527
void * FXMEM_ReallocDebug(FXMEM_FoxitMgr *pFoxitMgr, void *pointer, size_t new_size, int flags, FX_LPCSTR file, int line)
Foxit调试模式基本内存重分配函数。
size_t FXMEM_GetBlockSizeInFixedMgr(FXMEM_FoxitMgr *pFoxitMgr, void *ptr)
获取 ptr 指向的内存块的大小。
int FXMEM_SetPyConfig(size_t lowByteRange, size_t highByteRange)
设置Python内存的配置。
void * FXMEM_DefaultAlloc2(size_t units, size_t unit_size, int flags)
使用当前模块默认Foxit内存管理器的默认分配函数。
#define FX_Allocator_Alloc(fxAllocator, type, size)
在分配器上进行发布模式分配。
Definition: fx_memory.h:1084
void FXMEM_Free(FXMEM_FoxitMgr *pFoxitMgr, void *pointer, int flags)
Foxit基本内存释放函数。
void FXMEM_PurgeMgr(FXMEM_FoxitMgr *pFoxitMgr)
释放所有多余的内存而不触及任何使用中的内存。 这对于可扩展固定内存管理器(FXMEM_SystemMgr2)很有用, 因为我们从不释放那些额外的内存池,直到内存管理器被销毁。
#define ASSERT(a)
调试模式的断言,发布模式下不执行任何操作。
Definition: fx_system.h:819
void * FXMEM_Alloc(FXMEM_FoxitMgr *pFoxitMgr, size_t size, int flags)
Foxit基本内存分配函数。
size_t nPageNum_Init8
8字节固定数据大小的初始内存页数。桌面平台为1,有限内存环境为1。
Definition: fx_memory.h:1505
void * FXMEM_Realloc(FXMEM_FoxitMgr *pFoxitMgr, void *pointer, size_t new_size, int flags)
Foxit基本内存重分配函数。
void FXMEM_DefaultFree(void *pointer, int flags)
使用当前模块默认Foxit内存管理器的默认释放函数。
IFX_Allocator * FXMEM_GetDefAllocator()
获取库使用的默认分配器。
size_t nPageSize_Large
大数据(>FX_FIXEDMEM_MIDBLOCKSIZE)的最小内存页大小。桌面平台为128,有限内存环境为32。
Definition: fx_memory.h:1533
#define FX_Alloc(type, size)
Foxit内存分配操作的宏。
Definition: fx_memory.h:686
size_t nPageNum_MoreMid
中等数据范围的更多内存页数。桌面平台为4,有限内存环境为4。
Definition: fx_memory.h:1531
FXMEM_FoxitMgr * FXMEM_CreateFoxitMgr(FXMEM_SystemMgr *pSystemMgr)
创建Foxit管理器。必须提供系统管理器用于实际分配。
void * user
用于用户数据的通用无类型指针。
Definition: fx_memory.h:192
void FXMEM_SetDefaultMgr(FXMEM_FoxitMgr *pFoxitMgr)
为当前编译模块(EXE、DLL等)设置默认Foxit管理器。
内存调试器接口。所有函数都必须实现。
Definition: fx_memory.h:441
void FXMEM_CollectAll(FXMEM_FoxitMgr *pFoxitMgr)
释放由Foxit管理器分配的所有内存块。此函数仅在嵌入式系统上支持。
bool operator==(const char *str1, const CFX_ByteString &str2)
检查两个字节字符串是否相等。
Definition: fs_basictypes.h:128
int FX_BOOL
布尔变量(应为TRUE或FALSE)。
Definition: fx_system.h:691
void FXMEM_SetConfig(const FX_MEMCONFIG *memConfig)
设置固定内存的配置。
void * FXMEM_DefaultRealloc(void *pointer, size_t new_size, int flags)
使用当前模块默认Foxit内存管理器的默认重分配函数。
char const * FX_LPCSTR
指向常量8位Windows (ANSI) 字符的指针。
Definition: fx_system.h:705
void FXMEM_ResetSystemMgr()
重置当前模块的Foxit系统内存管理器。
FXMEM_FoxitMgr * FXMEM_GetDefaultMgr()
获取当前模块的默认内存管理器。
void FXMEM_UseDebugger(FXMEM_FoxitMgr *pFoxitMgr, FXMEM_Debugger *pDebugger)
使用捕获所有内存活动的内存调试器。对参数 pDebugger 使用 NULL 停止调试。
固定内存管理器。
Definition: fx_memory.h:249
void * FXMEM_DefaultAllocDebug2(size_t units, size_t unit_size, int flags, FX_LPCSTR file, int line)
使用当前模块默认Foxit内存管理器的默认调试模式分配函数。
void FXMEM_DestroyFoxitMgr(FXMEM_FoxitMgr *pFoxitMgr)
销毁Foxit管理器实例。如果平台支持自动收集, 那么所有分配的内存块将被释放。
void FXMEM_OutputDebugTag(FXMEM_FoxitMgr *pFoxitMgr, FX_LPCSTR tag)
输出内存调试标签。
size_t nPageNum_Init32
32字节固定数据大小的初始内存页数。桌面平台为24,有限内存环境为8。
Definition: fx_memory.h:1509
size_t nPageNum_More16
16字节固定数据大小的更多内存页数。桌面平台为8,有限内存环境为4。
Definition: fx_memory.h:1514
void * FXMEM_DefaultReallocDebug2(void *pointer, size_t units, size_t unit_size, int flags, FX_LPCSTR file, int line)
使用当前模块默认Foxit内存管理器的默认调试模式重分配函数。
FXMEM_FoxitMgr * FXMEM_CreatePyMgr()
创建Python管理器。一个快速、专用的小块内存分配器,用于 在通用malloc之上使用——基于以前的技术。
CFX_ByteString operator+(FX_BSTR str1, FX_BSTR str2)
连接非缓冲字节字符串和非缓冲字节字符串。
Definition: fx_string.h:977
Foxit分配器接口。
Definition: fx_memory.h:994
void * FXMEM_DefaultAlloc(size_t byte_size, int flags)
使用当前模块默认Foxit内存管理器的默认分配函数。
#define FX_Free(pointer)
Foxit内存释放操作的宏。
Definition: fx_memory.h:711
FXMEM_FoxitMgr * FXMEM_CreateFixedMgr(void *pMemory, size_t size, FXMEM_SystemMgr2 *pExtender)
从预分配的固定内存缓冲区创建Foxit管理器。
系统级内存管理器。应用程序可以实现自己的系统内存管理器。
Definition: fx_memory.h:72
size_t nPageNum_InitMid
中等数据范围的初始内存页数。桌面平台为2,有限内存环境为2。
Definition: fx_memory.h:1529
void(* FPDF_OOM_Handler)(FXMEM_FoxitMgr *pFoxitMgr, void *param)
内存不足处理程序的原型。
Definition: fx_memory.h:559
size_t nPageNum_Init16
16字节固定数据大小的初始内存页数。桌面平台为8,有限内存环境为5。
Definition: fx_memory.h:1507
void * FXMEM_AllocDebug(FXMEM_FoxitMgr *pFoxitMgr, size_t size, int flags, FX_LPCSTR file, int line)
Foxit调试模式基本内存分配函数。
void FXMEM_ReportOOM(FXMEM_FoxitMgr *pFoxitMgr)
报告内存不足(OOM)。
void FXMEM_SetOOMHandler(FXMEM_FoxitMgr *pFoxitMgr, FPDF_OOM_Handler pOOMReportFunc, void *param)
为Foxit内存管理器设置内存不足处理程序。
size_t nPageNum_More32
32字节固定数据大小的更多内存页数。桌面平台为24,有限内存环境为12。
Definition: fx_memory.h:1516
FXMEM_FoxitMgr * FXMEM_CreateMemoryMgr(size_t size, FX_BOOL extensible)
创建固定内存管理器作为默认实现。
void * FXMEM_DefaultAllocDebug(size_t size, int flags, FX_LPCSTR file, int line)
使用当前模块默认Foxit内存管理器的默认调试模式分配函数。
固定内存配置结构。
Definition: fx_memory.h:1502
void * FXMEM_DefaultRealloc2(void *pointer, size_t units, size_t unit_size, int flags)
使用当前模块默认Foxit内存管理器的默认重分配函数。
size_t nPageSize_Alone
大数据独立内存页的最小大小。桌面平台为64,有限内存环境为64。
Definition: fx_memory.h:1535
#define NULL
空指针值。
Definition: fx_system.h:792
#define FX_DEFINEHANDLE(name)
定义句柄类型的宏。
Definition: fx_system.h:772
bool operator!=(const char *str1, const CFX_ByteString &str2)
检查两个字节字符串是否不相等。
Definition: fs_basictypes.h:140
#define FX_Allocator_Free(fxAllocator, ptr)
在分配器上释放内存块。
Definition: fx_memory.h:1091
void * FXMEM_DefaultReallocDebug(void *pointer, size_t new_size, int flags, FX_LPCSTR file, int line)
使用当前模块默认Foxit内存管理器的默认调试模式重分配函数。
unsigned char FX_BYTE
字节(8位)。
Definition: fx_system.h:665
系统相关定义的头文件。