• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2014 PDFium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6 
7 #ifndef _FX_MEMORY_IMP
8 #define _FX_MEMORY_IMP
9 class CFX_DefStore;
10 class CFX_StaticStore;
11 class CFX_FixedStore;
12 class CFX_DynamicStore;
13 class CFX_DefStore : public IFX_MEMAllocator, public CFX_Target {
14  public:
CFX_DefStore()15   CFX_DefStore() {}
~CFX_DefStore()16   ~CFX_DefStore() {}
Release()17   virtual void Release() { delete this; }
Alloc(size_t size)18   virtual void* Alloc(size_t size) { return FX_Alloc(uint8_t, size); }
Free(void * pBlock)19   virtual void Free(void* pBlock) { FX_Free(pBlock); }
GetBlockSize()20   virtual size_t GetBlockSize() const { return 0; }
GetDefChunkSize()21   virtual size_t GetDefChunkSize() const { return 0; }
SetDefChunkSize(size_t size)22   virtual size_t SetDefChunkSize(size_t size) { return 0; }
GetCurrentDataSize()23   virtual size_t GetCurrentDataSize() const { return 0; }
24 };
25 #if _FX_OS_ != _FX_ANDROID_
26 #pragma pack(push, 1)
27 #endif
28 typedef struct _FX_STATICSTORECHUNK {
29   _FX_STATICSTORECHUNK* pNextChunk;
30   size_t iChunkSize;
31   size_t iFreeSize;
32 } FX_STATICSTORECHUNK, *FX_LPSTATICSTORECHUNK;
33 typedef FX_STATICSTORECHUNK const* FX_LPCSTATICSTORECHUNK;
34 #if _FX_OS_ != _FX_ANDROID_
35 #pragma pack(pop)
36 #endif
37 class CFX_StaticStore : public IFX_MEMAllocator, public CFX_Target {
38  public:
39   CFX_StaticStore(size_t iDefChunkSize = 4096);
40   ~CFX_StaticStore();
Release()41   virtual void Release() { delete this; }
42   virtual void* Alloc(size_t size);
Free(void * pBlock)43   virtual void Free(void* pBlock) {}
GetBlockSize()44   virtual size_t GetBlockSize() const { return 0; }
GetDefChunkSize()45   virtual size_t GetDefChunkSize() const { return m_iDefChunkSize; }
46   virtual size_t SetDefChunkSize(size_t size);
GetCurrentDataSize()47   virtual size_t GetCurrentDataSize() const { return m_iAllocatedSize; }
48 
49  protected:
50   size_t m_iAllocatedSize;
51   size_t m_iDefChunkSize;
52   FX_LPSTATICSTORECHUNK m_pChunk;
53   FX_LPSTATICSTORECHUNK m_pLastChunk;
54   FX_LPSTATICSTORECHUNK AllocChunk(size_t size);
55   FX_LPSTATICSTORECHUNK FindChunk(size_t size);
56 };
57 #if _FX_OS_ != _FX_ANDROID_
58 #pragma pack(push, 1)
59 #endif
60 typedef struct _FX_FIXEDSTORECHUNK {
FirstFlag_FX_FIXEDSTORECHUNK61   uint8_t* FirstFlag() const {
62     return (uint8_t*)this + sizeof(_FX_FIXEDSTORECHUNK);
63   }
FirstBlock_FX_FIXEDSTORECHUNK64   uint8_t* FirstBlock() const { return FirstFlag() + iChunkSize; }
65   _FX_FIXEDSTORECHUNK* pNextChunk;
66   size_t iChunkSize;
67   size_t iFreeNum;
68 } FX_FIXEDSTORECHUNK, *FX_LPFIXEDSTORECHUNK;
69 typedef FX_FIXEDSTORECHUNK const* FX_LPCFIXEDSTORECHUNK;
70 #if _FX_OS_ != _FX_ANDROID_
71 #pragma pack(pop)
72 #endif
73 class CFX_FixedStore : public IFX_MEMAllocator, public CFX_Target {
74  public:
75   CFX_FixedStore(size_t iBlockSize, size_t iBlockNumsInChunk);
76   virtual ~CFX_FixedStore();
Release()77   virtual void Release() { delete this; }
78   virtual void* Alloc(size_t size);
79   virtual void Free(void* pBlock);
GetBlockSize()80   virtual size_t GetBlockSize() const { return m_iBlockSize; }
GetDefChunkSize()81   virtual size_t GetDefChunkSize() const { return m_iDefChunkSize; }
82   virtual size_t SetDefChunkSize(size_t iChunkSize);
GetCurrentDataSize()83   virtual size_t GetCurrentDataSize() const { return 0; }
84 
85  protected:
86   size_t m_iBlockSize;
87   size_t m_iDefChunkSize;
88   FX_LPFIXEDSTORECHUNK m_pChunk;
89   FX_LPFIXEDSTORECHUNK AllocChunk();
90 };
91 #if _FX_OS_ != _FX_ANDROID_
92 #pragma pack(push, 1)
93 #endif
94 typedef struct _FX_DYNAMICSTOREBLOCK {
NextBlock_FX_DYNAMICSTOREBLOCK95   _FX_DYNAMICSTOREBLOCK* NextBlock() const {
96     return (_FX_DYNAMICSTOREBLOCK*)(Data() + iBlockSize);
97   }
Data_FX_DYNAMICSTOREBLOCK98   uint8_t* Data() const {
99     return (uint8_t*)this + sizeof(_FX_DYNAMICSTOREBLOCK);
100   }
101   size_t iBlockSize;
102   FX_BOOL bUsed;
103 } FX_DYNAMICSTOREBLOCK, *FX_LPDYNAMICSTOREBLOCK;
104 typedef FX_DYNAMICSTOREBLOCK const* FX_LPCDYNAMICSTOREBLOCK;
105 typedef struct _FX_DYNAMICSTORECHUNK {
FirstBlock_FX_DYNAMICSTORECHUNK106   FX_LPDYNAMICSTOREBLOCK FirstBlock() const {
107     return (FX_LPDYNAMICSTOREBLOCK)((uint8_t*)this +
108                                     sizeof(_FX_DYNAMICSTORECHUNK));
109   }
110   _FX_DYNAMICSTORECHUNK* pNextChunk;
111   size_t iChunkSize;
112   size_t iFreeSize;
113 } FX_DYNAMICSTORECHUNK, *FX_LPDYNAMICSTORECHUNK;
114 typedef FX_DYNAMICSTORECHUNK const* FX_LPCDYNAMICSTORECHUNK;
115 #if _FX_OS_ != _FX_ANDROID_
116 #pragma pack(pop)
117 #endif
118 class CFX_DynamicStore : public IFX_MEMAllocator, public CFX_Target {
119  public:
120   CFX_DynamicStore(size_t iDefChunkSize = 4096);
121   virtual ~CFX_DynamicStore();
Release()122   virtual void Release() { delete this; }
123   virtual void* Alloc(size_t size);
124   virtual void Free(void* pBlock);
GetBlockSize()125   virtual size_t GetBlockSize() const { return 0; }
GetDefChunkSize()126   virtual size_t GetDefChunkSize() const { return m_iDefChunkSize; }
127   virtual size_t SetDefChunkSize(size_t size);
GetCurrentDataSize()128   virtual size_t GetCurrentDataSize() const { return 0; }
129 
130  protected:
131   size_t m_iDefChunkSize;
132   FX_LPDYNAMICSTORECHUNK m_pChunk;
133   FX_LPDYNAMICSTORECHUNK AllocChunk(size_t size);
134 };
135 #endif
136