diff options
author | Dan Sinclair <dsinclair@chromium.org> | 2017-01-10 16:38:10 -0500 |
---|---|---|
committer | Chromium commit bot <commit-bot@chromium.org> | 2017-01-10 21:53:32 +0000 |
commit | 0cb9b8cb094532ff868314350680d3fb0ca2fe51 (patch) | |
tree | 60e61f1e9547d5b99c010ee96b8c0ea7c0480e85 /xfa/fgas/crt | |
parent | 1f5d4988dcdac125e3e822d37c9086a5e4a3e224 (diff) | |
download | pdfium-0cb9b8cb094532ff868314350680d3fb0ca2fe51.tar.xz |
Strip out custom allocator code
This Cl replaces the custom IFX_MemoryAllocator code with new/delete as needed.
Change-Id: Ie786f607c9e0b3035ffd87733bc3e29a4b6426d9
Reviewed-on: https://pdfium-review.googlesource.com/2164
Commit-Queue: dsinclair <dsinclair@chromium.org>
Reviewed-by: Tom Sepez <tsepez@chromium.org>
Diffstat (limited to 'xfa/fgas/crt')
-rw-r--r-- | xfa/fgas/crt/fgas_memory.cpp | 254 | ||||
-rw-r--r-- | xfa/fgas/crt/fgas_memory.h | 53 | ||||
-rw-r--r-- | xfa/fgas/crt/fgas_utils.cpp | 4 | ||||
-rw-r--r-- | xfa/fgas/crt/fgas_utils.h | 21 |
4 files changed, 12 insertions, 320 deletions
diff --git a/xfa/fgas/crt/fgas_memory.cpp b/xfa/fgas/crt/fgas_memory.cpp deleted file mode 100644 index 9625f95203..0000000000 --- a/xfa/fgas/crt/fgas_memory.cpp +++ /dev/null @@ -1,254 +0,0 @@ -// Copyright 2014 PDFium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com - -#include "xfa/fgas/crt/fgas_memory.h" - -#ifndef MEMORY_TOOL_REPLACES_ALLOCATOR -// Use CFX_DefStore to replace CFX_FixedStore to simplify memory -// management so that some problems such Use-After-Free can be -// detected by Asan or ClusterFuzz tools. -#define MEMORY_TOOL_REPLACES_ALLOCATOR -#endif - -#include <algorithm> - -namespace { - -struct FX_STATICSTORECHUNK { - FX_STATICSTORECHUNK* pNextChunk; - size_t iChunkSize; - size_t iFreeSize; -}; - -class CFX_StaticStore : public IFX_MemoryAllocator, public CFX_Target { - public: - explicit CFX_StaticStore(size_t iDefChunkSize); - ~CFX_StaticStore() override; - - void* Alloc(size_t size) override; - void Free(void* pBlock) override {} - - private: - size_t m_iAllocatedSize; - size_t m_iDefChunkSize; - FX_STATICSTORECHUNK* m_pChunk; - FX_STATICSTORECHUNK* m_pLastChunk; - FX_STATICSTORECHUNK* AllocChunk(size_t size); - FX_STATICSTORECHUNK* FindChunk(size_t size); -}; - -#ifdef MEMORY_TOOL_REPLACES_ALLOCATOR - -class CFX_DefStore : public IFX_MemoryAllocator, public CFX_Target { - public: - CFX_DefStore() {} - ~CFX_DefStore() override {} - - void* Alloc(size_t size) override { return FX_Alloc(uint8_t, size); } - void Free(void* pBlock) override { FX_Free(pBlock); } -}; - -#else - -struct FX_FIXEDSTORECHUNK { - uint8_t* FirstFlag() { return reinterpret_cast<uint8_t*>(this + 1); } - uint8_t* FirstBlock() { return FirstFlag() + iChunkSize; } - - FX_FIXEDSTORECHUNK* pNextChunk; - size_t iChunkSize; - size_t iFreeNum; -}; - -class CFX_FixedStore : public IFX_MemoryAllocator, public CFX_Target { - public: - CFX_FixedStore(size_t iBlockSize, size_t iBlockNumsInChunk); - ~CFX_FixedStore() override; - void* Alloc(size_t size) override; - void Free(void* pBlock) override; - - private: - FX_FIXEDSTORECHUNK* AllocChunk(); - - size_t m_iBlockSize; - size_t m_iDefChunkSize; - FX_FIXEDSTORECHUNK* m_pChunk; -}; - -#endif // MEMORY_TOOL_REPLACES_ALLOCATOR - -} // namespace - -#define FX_4BYTEALIGN(size) (((size) + 3) & ~3) - -std::unique_ptr<IFX_MemoryAllocator> IFX_MemoryAllocator::Create( - FX_ALLOCTYPE eType, - size_t chunkSize, - size_t blockSize) { - switch (eType) { - case FX_ALLOCTYPE_Static: - return std::unique_ptr<IFX_MemoryAllocator>( - new CFX_StaticStore(chunkSize)); - case FX_ALLOCTYPE_Fixed: -#ifdef MEMORY_TOOL_REPLACES_ALLOCATOR - return std::unique_ptr<IFX_MemoryAllocator>(new CFX_DefStore()); -#else - return std::unique_ptr<IFX_MemoryAllocator>( - new CFX_FixedStore(blockSize, chunkSize)); -#endif // MEMORY_TOOL_REPLACES_ALLOCATOR - default: - ASSERT(0); - return nullptr; - } -} - -CFX_StaticStore::CFX_StaticStore(size_t iDefChunkSize) - : m_iAllocatedSize(0), - m_iDefChunkSize(iDefChunkSize), - m_pChunk(nullptr), - m_pLastChunk(nullptr) { - ASSERT(m_iDefChunkSize != 0); -} - -CFX_StaticStore::~CFX_StaticStore() { - FX_STATICSTORECHUNK* pChunk = m_pChunk; - while (pChunk) { - FX_STATICSTORECHUNK* pNext = pChunk->pNextChunk; - FX_Free(pChunk); - pChunk = pNext; - } -} - -FX_STATICSTORECHUNK* CFX_StaticStore::AllocChunk(size_t size) { - ASSERT(size != 0); - FX_STATICSTORECHUNK* pChunk = (FX_STATICSTORECHUNK*)FX_Alloc( - uint8_t, sizeof(FX_STATICSTORECHUNK) + size); - pChunk->iChunkSize = size; - pChunk->iFreeSize = size; - pChunk->pNextChunk = nullptr; - if (!m_pLastChunk) { - m_pChunk = pChunk; - } else { - m_pLastChunk->pNextChunk = pChunk; - } - m_pLastChunk = pChunk; - return pChunk; -} - -FX_STATICSTORECHUNK* CFX_StaticStore::FindChunk(size_t size) { - ASSERT(size != 0); - if (!m_pLastChunk || m_pLastChunk->iFreeSize < size) { - return AllocChunk(std::max(m_iDefChunkSize, size)); - } - return m_pLastChunk; -} - -void* CFX_StaticStore::Alloc(size_t size) { - size = FX_4BYTEALIGN(size); - ASSERT(size != 0); - FX_STATICSTORECHUNK* pChunk = FindChunk(size); - ASSERT(pChunk->iFreeSize >= size); - uint8_t* p = (uint8_t*)pChunk; - p += sizeof(FX_STATICSTORECHUNK) + pChunk->iChunkSize - pChunk->iFreeSize; - pChunk->iFreeSize -= size; - m_iAllocatedSize += size; - return p; -} - -#ifndef MEMORY_TOOL_REPLACES_ALLOCATOR - -CFX_FixedStore::CFX_FixedStore(size_t iBlockSize, size_t iBlockNumsInChunk) - : m_iBlockSize(FX_4BYTEALIGN(iBlockSize)), - m_iDefChunkSize(FX_4BYTEALIGN(iBlockNumsInChunk)), - m_pChunk(nullptr) { - ASSERT(m_iBlockSize != 0 && m_iDefChunkSize != 0); -} - -CFX_FixedStore::~CFX_FixedStore() { - FX_FIXEDSTORECHUNK* pChunk = m_pChunk; - while (pChunk) { - FX_FIXEDSTORECHUNK* pNext = pChunk->pNextChunk; - FX_Free(pChunk); - pChunk = pNext; - } -} - -FX_FIXEDSTORECHUNK* CFX_FixedStore::AllocChunk() { - int32_t iTotalSize = sizeof(FX_FIXEDSTORECHUNK) + m_iDefChunkSize + - m_iBlockSize * m_iDefChunkSize; - FX_FIXEDSTORECHUNK* pChunk = - (FX_FIXEDSTORECHUNK*)FX_Alloc(uint8_t, iTotalSize); - if (!pChunk) - return nullptr; - - FXSYS_memset(pChunk->FirstFlag(), 0, m_iDefChunkSize); - pChunk->pNextChunk = m_pChunk; - pChunk->iChunkSize = m_iDefChunkSize; - pChunk->iFreeNum = m_iDefChunkSize; - m_pChunk = pChunk; - return pChunk; -} - -void* CFX_FixedStore::Alloc(size_t size) { - if (size > m_iBlockSize) { - return nullptr; - } - FX_FIXEDSTORECHUNK* pChunk = m_pChunk; - while (pChunk) { - if (pChunk->iFreeNum > 0) { - break; - } - pChunk = pChunk->pNextChunk; - } - if (!pChunk) { - pChunk = AllocChunk(); - } - uint8_t* pFlags = pChunk->FirstFlag(); - size_t i = 0; - for (; i < pChunk->iChunkSize; i++) - if (pFlags[i] == 0) { - break; - } - ASSERT(i < pChunk->iChunkSize); - pFlags[i] = 1; - pChunk->iFreeNum--; - return pChunk->FirstBlock() + i * m_iBlockSize; -} - -void CFX_FixedStore::Free(void* pBlock) { - FX_FIXEDSTORECHUNK* pPrior = nullptr; - FX_FIXEDSTORECHUNK* pChunk = m_pChunk; - uint8_t* pStart = nullptr; - uint8_t* pEnd; - while (pChunk) { - pStart = pChunk->FirstBlock(); - if (pBlock >= pStart) { - pEnd = pStart + m_iBlockSize * pChunk->iChunkSize; - if (pBlock < pEnd) { - break; - } - } - pPrior = pChunk, pChunk = pChunk->pNextChunk; - } - ASSERT(pChunk); - size_t iPos = ((uint8_t*)pBlock - pStart) / m_iBlockSize; - ASSERT(iPos < pChunk->iChunkSize); - uint8_t* pFlags = pChunk->FirstFlag(); - if (pFlags[iPos] == 0) { - return; - } - pFlags[iPos] = 0; - pChunk->iFreeNum++; - if (pChunk->iFreeNum == pChunk->iChunkSize) { - if (!pPrior) { - m_pChunk = pChunk->pNextChunk; - } else { - pPrior->pNextChunk = pChunk->pNextChunk; - } - FX_Free(pChunk); - } -} - -#endif // MEMORY_TOOL_REPLACES_ALLOCATOR diff --git a/xfa/fgas/crt/fgas_memory.h b/xfa/fgas/crt/fgas_memory.h deleted file mode 100644 index 4e4e33f7fc..0000000000 --- a/xfa/fgas/crt/fgas_memory.h +++ /dev/null @@ -1,53 +0,0 @@ -// Copyright 2014 PDFium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com - -#ifndef XFA_FGAS_CRT_FGAS_MEMORY_H_ -#define XFA_FGAS_CRT_FGAS_MEMORY_H_ - -#include <memory> - -#include "core/fxcrt/fx_memory.h" -#include "core/fxcrt/fx_system.h" - -enum FX_ALLOCTYPE { - FX_ALLOCTYPE_Static, - FX_ALLOCTYPE_Fixed, -}; - -class IFX_MemoryAllocator { - public: - virtual ~IFX_MemoryAllocator() {} - virtual void* Alloc(size_t size) = 0; - virtual void Free(void* pBlock) = 0; - - static std::unique_ptr<IFX_MemoryAllocator> Create(FX_ALLOCTYPE eType, - size_t chunkSize, - size_t blockSize); -}; - -class CFX_Target { - public: - virtual ~CFX_Target() {} - void* operator new(size_t size) { return FX_Alloc(uint8_t, size); } - void operator delete(void* p) { FX_Free(p); } - void* operator new(size_t size, IFX_MemoryAllocator* pAllocator) { - return pAllocator->Alloc(size); - } - void operator delete(void* p, IFX_MemoryAllocator* pAllocator) { - pAllocator->Free(p); - } - void* operator new(size_t size, void* place) { return place; } - void operator delete(void* p, void* place) {} -}; - -#define FXTARGET_NewWith(__allocator__) new (__allocator__) -#define FXTARGET_DeleteWith(__class__, __allocator__, pointer) \ - { \ - (pointer)->~__class__(); \ - (pointer)->operator delete((pointer), (__allocator__)); \ - } - -#endif // XFA_FGAS_CRT_FGAS_MEMORY_H_ diff --git a/xfa/fgas/crt/fgas_utils.cpp b/xfa/fgas/crt/fgas_utils.cpp index 0cdbf77d31..1e86c6c16a 100644 --- a/xfa/fgas/crt/fgas_utils.cpp +++ b/xfa/fgas/crt/fgas_utils.cpp @@ -10,10 +10,10 @@ #include "core/fxcrt/fx_basic.h" -class FX_BASEARRAYDATA : public CFX_Target { +class FX_BASEARRAYDATA { public: FX_BASEARRAYDATA(int32_t growsize, int32_t blocksize); - ~FX_BASEARRAYDATA() override; + ~FX_BASEARRAYDATA(); int32_t iGrowSize; int32_t iBlockSize; diff --git a/xfa/fgas/crt/fgas_utils.h b/xfa/fgas/crt/fgas_utils.h index c7bc45f73d..465601149b 100644 --- a/xfa/fgas/crt/fgas_utils.h +++ b/xfa/fgas/crt/fgas_utils.h @@ -8,14 +8,13 @@ #define XFA_FGAS_CRT_FGAS_UTILS_H_ #include "core/fxcrt/fx_coordinates.h" -#include "xfa/fgas/crt/fgas_memory.h" class FX_BASEARRAYDATA; -class CFX_BaseArray : public CFX_Target { +class CFX_BaseArray { protected: CFX_BaseArray(int32_t iGrowSize, int32_t iBlockSize); - ~CFX_BaseArray() override; + ~CFX_BaseArray(); int32_t GetSize() const; int32_t GetBlockSize() const; @@ -77,10 +76,10 @@ class CFX_BaseArrayTemplate : public CFX_BaseArray { void RemoveAll(bool bLeaveMemory) { CFX_BaseArray::RemoveAll(bLeaveMemory); } }; -class CFX_BaseMassArrayImp : public CFX_Target { +class CFX_BaseMassArrayImp { public: CFX_BaseMassArrayImp(int32_t iChunkSize, int32_t iBlockSize); - ~CFX_BaseMassArrayImp() override; + ~CFX_BaseMassArrayImp(); uint8_t* AddSpace() { return AddSpaceTo(m_iBlockCount); } uint8_t* AddSpaceTo(int32_t index); @@ -105,10 +104,10 @@ class CFX_BaseMassArrayImp : public CFX_Target { int32_t iSrcCount); }; -class CFX_BaseMassArray : public CFX_Target { +class CFX_BaseMassArray { protected: CFX_BaseMassArray(int32_t iChunkSize, int32_t iBlockSize); - ~CFX_BaseMassArray() override; + ~CFX_BaseMassArray(); int32_t GetSize() const; uint8_t* AddSpaceTo(int32_t index); @@ -251,10 +250,10 @@ class CFX_ObjectMassArrayTemplate : public CFX_BaseMassArray { } }; -class CFX_BaseDiscreteArray : public CFX_Target { +class CFX_BaseDiscreteArray { protected: CFX_BaseDiscreteArray(int32_t iChunkSize, int32_t iBlockSize); - ~CFX_BaseDiscreteArray() override; + ~CFX_BaseDiscreteArray(); uint8_t* AddSpaceTo(int32_t index); uint8_t* GetAt(int32_t index) const; @@ -281,10 +280,10 @@ class CFX_DiscreteArrayTemplate : public CFX_BaseDiscreteArray { void RemoveAll() { CFX_BaseDiscreteArray::RemoveAll(); } }; -class CFX_BaseStack : public CFX_Target { +class CFX_BaseStack { protected: CFX_BaseStack(int32_t iChunkSize, int32_t iBlockSize); - ~CFX_BaseStack() override; + ~CFX_BaseStack(); uint8_t* Push(); void Pop(); |