• 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
8 #define _FX_MEMORY
9 
10 #include "core/include/fxcrt/fx_memory.h"  // For FX_Alloc().
11 
12 class IFX_MEMAllocator;
13 class CFX_Target;
14 enum FX_ALLOCTYPE {
15   FX_ALLOCTYPE_Default = 0,
16   FX_ALLOCTYPE_Static,
17   FX_ALLOCTYPE_Fixed,
18   FX_ALLOCTYPE_Dynamic,
19 };
20 
21 class IFX_MEMAllocator {
22  public:
~IFX_MEMAllocator()23   virtual ~IFX_MEMAllocator() {}
24   virtual void Release() = 0;
25   virtual void* Alloc(size_t size) = 0;
26   virtual void Free(void* pBlock) = 0;
27   virtual size_t GetBlockSize() const = 0;
28   virtual size_t GetDefChunkSize() const = 0;
29   virtual size_t SetDefChunkSize(size_t size) = 0;
30   virtual size_t GetCurrentDataSize() const = 0;
31 };
32 
33 IFX_MEMAllocator* FX_CreateAllocator(FX_ALLOCTYPE eType,
34                                      size_t chunkSize,
35                                      size_t blockSize);
36 class CFX_Target {
37  public:
~CFX_Target()38   virtual ~CFX_Target() {}
new(size_t size)39   void* operator new(size_t size) { return FX_Alloc(uint8_t, size); }
delete(void * p)40   void operator delete(void* p) { FX_Free(p); }
new(size_t size,IFX_MEMAllocator * pAllocator)41   void* operator new(size_t size, IFX_MEMAllocator* pAllocator) {
42     return pAllocator->Alloc(size);
43   }
delete(void * p,IFX_MEMAllocator * pAllocator)44   void operator delete(void* p, IFX_MEMAllocator* pAllocator) {
45     pAllocator->Free(p);
46   }
new(size_t size,void * place)47   void* operator new(size_t size, void* place) { return place; }
delete(void * p,void * place)48   void operator delete(void* p, void* place) {}
49 };
50 #define FXTARGET_NewWith(__allocator__) new (__allocator__)
51 #define FXTARGET_DeleteWith(__class__, __allocator__, pointer) \
52   {                                                            \
53     (pointer)->~__class__();                                   \
54     (pointer)->operator delete((pointer), (__allocator__));    \
55   }
56 #endif
57