OLD | NEW |
1 // Copyright 2014 PDFium Authors. All rights reserved. | 1 // Copyright 2014 PDFium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com | 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com |
6 | 6 |
7 #include "xfa/fgas/crt/fgas_memory.h" | 7 #include "xfa/fgas/crt/fgas_memory.h" |
8 | 8 |
9 #ifndef MEMORY_TOOL_REPLACES_ALLOCATOR | 9 #ifndef MEMORY_TOOL_REPLACES_ALLOCATOR |
10 #define MEMORY_TOOL_REPLACES_ALLOCATOR // Temporary, for CF testing. | 10 #define MEMORY_TOOL_REPLACES_ALLOCATOR // Temporary, for CF testing. |
11 #endif | 11 #endif |
12 | 12 |
13 #include <algorithm> | 13 #include <algorithm> |
14 | 14 |
15 #ifdef MEMORY_TOOL_REPLACES_ALLOCATOR | 15 #ifdef MEMORY_TOOL_REPLACES_ALLOCATOR |
16 | 16 |
17 namespace { | 17 namespace { |
18 | 18 |
19 class CFX_DefStore : public IFX_MemoryAllocator, public CFX_Target { | 19 class CFX_DefStore : public IFX_MemoryAllocator, public CFX_Target { |
20 public: | 20 public: |
21 CFX_DefStore() {} | 21 CFX_DefStore() {} |
22 ~CFX_DefStore() override {} | 22 ~CFX_DefStore() override {} |
| 23 |
23 void* Alloc(size_t size) override { return FX_Alloc(uint8_t, size); } | 24 void* Alloc(size_t size) override { return FX_Alloc(uint8_t, size); } |
24 void Free(void* pBlock) override { FX_Free(pBlock); } | 25 void Free(void* pBlock) override { FX_Free(pBlock); } |
25 }; | 26 }; |
26 | 27 |
27 } // namespace | 28 } // namespace |
28 | 29 |
29 IFX_MemoryAllocator* IFX_MemoryAllocator::Create(FX_ALLOCTYPE eType, | 30 IFX_MemoryAllocator* IFX_MemoryAllocator::Create(FX_ALLOCTYPE eType, |
30 size_t chunkSize, | 31 size_t chunkSize, |
31 size_t blockSize) { | 32 size_t blockSize) { |
32 return new CFX_DefStore(); | 33 return new CFX_DefStore(); |
33 } | 34 } |
34 | 35 |
35 #else // MEMORY_TOOL_REPLACES_ALLOCATOR | 36 #else // MEMORY_TOOL_REPLACES_ALLOCATOR |
36 | 37 |
37 namespace { | 38 namespace { |
38 | 39 |
39 struct FX_STATICSTORECHUNK { | 40 struct FX_STATICSTORECHUNK { |
40 FX_STATICSTORECHUNK* pNextChunk; | 41 FX_STATICSTORECHUNK* pNextChunk; |
41 size_t iChunkSize; | 42 size_t iChunkSize; |
42 size_t iFreeSize; | 43 size_t iFreeSize; |
43 }; | 44 }; |
44 | 45 |
45 class CFX_StaticStore : public IFX_MemoryAllocator, public CFX_Target { | 46 class CFX_StaticStore : public IFX_MemoryAllocator, public CFX_Target { |
46 public: | 47 public: |
47 CFX_StaticStore(size_t iDefChunkSize = 4096); | 48 CFX_StaticStore(size_t iDefChunkSize); |
48 ~CFX_StaticStore() override; | 49 ~CFX_StaticStore() override; |
| 50 |
49 void* Alloc(size_t size) override; | 51 void* Alloc(size_t size) override; |
50 void Free(void* pBlock) override {} | 52 void Free(void* pBlock) override {} |
51 | 53 |
52 protected: | 54 private: |
53 size_t m_iAllocatedSize; | 55 size_t m_iAllocatedSize; |
54 size_t m_iDefChunkSize; | 56 size_t m_iDefChunkSize; |
55 FX_STATICSTORECHUNK* m_pChunk; | 57 FX_STATICSTORECHUNK* m_pChunk; |
56 FX_STATICSTORECHUNK* m_pLastChunk; | 58 FX_STATICSTORECHUNK* m_pLastChunk; |
57 FX_STATICSTORECHUNK* AllocChunk(size_t size); | 59 FX_STATICSTORECHUNK* AllocChunk(size_t size); |
58 FX_STATICSTORECHUNK* FindChunk(size_t size); | 60 FX_STATICSTORECHUNK* FindChunk(size_t size); |
59 }; | 61 }; |
60 | 62 |
61 struct FX_FIXEDSTORECHUNK { | 63 struct FX_FIXEDSTORECHUNK { |
62 uint8_t* FirstFlag() { return reinterpret_cast<uint8_t*>(this + 1); } | 64 uint8_t* FirstFlag() { return reinterpret_cast<uint8_t*>(this + 1); } |
63 uint8_t* FirstBlock() { return FirstFlag() + iChunkSize; } | 65 uint8_t* FirstBlock() { return FirstFlag() + iChunkSize; } |
64 | 66 |
65 FX_FIXEDSTORECHUNK* pNextChunk; | 67 FX_FIXEDSTORECHUNK* pNextChunk; |
66 size_t iChunkSize; | 68 size_t iChunkSize; |
67 size_t iFreeNum; | 69 size_t iFreeNum; |
68 }; | 70 }; |
69 | 71 |
70 class CFX_FixedStore : public IFX_MemoryAllocator, public CFX_Target { | 72 class CFX_FixedStore : public IFX_MemoryAllocator, public CFX_Target { |
71 public: | 73 public: |
72 CFX_FixedStore(size_t iBlockSize, size_t iBlockNumsInChunk); | 74 CFX_FixedStore(size_t iBlockSize, size_t iBlockNumsInChunk); |
73 ~CFX_FixedStore() override; | 75 ~CFX_FixedStore() override; |
74 void* Alloc(size_t size) override; | 76 void* Alloc(size_t size) override; |
75 void Free(void* pBlock) override; | 77 void Free(void* pBlock) override; |
76 | 78 |
77 protected: | 79 private: |
78 FX_FIXEDSTORECHUNK* AllocChunk(); | 80 FX_FIXEDSTORECHUNK* AllocChunk(); |
79 | 81 |
80 size_t m_iBlockSize; | 82 size_t m_iBlockSize; |
81 size_t m_iDefChunkSize; | 83 size_t m_iDefChunkSize; |
82 FX_FIXEDSTORECHUNK* m_pChunk; | 84 FX_FIXEDSTORECHUNK* m_pChunk; |
83 }; | 85 }; |
84 | 86 |
85 } // namespace | 87 } // namespace |
86 | 88 |
87 #define FX_4BYTEALIGN(size) (((size) + 3) & ~3) | 89 #define FX_4BYTEALIGN(size) (((size) + 3) & ~3) |
(...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
242 } | 244 } |
243 } | 245 } |
244 size_t CFX_FixedStore::SetDefChunkSize(size_t iChunkSize) { | 246 size_t CFX_FixedStore::SetDefChunkSize(size_t iChunkSize) { |
245 ASSERT(iChunkSize != 0); | 247 ASSERT(iChunkSize != 0); |
246 size_t v = m_iDefChunkSize; | 248 size_t v = m_iDefChunkSize; |
247 m_iDefChunkSize = FX_4BYTEALIGN(iChunkSize); | 249 m_iDefChunkSize = FX_4BYTEALIGN(iChunkSize); |
248 return v; | 250 return v; |
249 } | 251 } |
250 | 252 |
251 #endif // MEMORY_TOOL_REPLACES_ALLOCATOR | 253 #endif // MEMORY_TOOL_REPLACES_ALLOCATOR |
OLD | NEW |