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
12class IFX_MEMAllocator;
13class CFX_Target;
14enum FX_ALLOCTYPE {
15  FX_ALLOCTYPE_Default = 0,
16  FX_ALLOCTYPE_Static,
17  FX_ALLOCTYPE_Fixed,
18  FX_ALLOCTYPE_Dynamic,
19};
20
21class IFX_MEMAllocator {
22 public:
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
33IFX_MEMAllocator* FX_CreateAllocator(FX_ALLOCTYPE eType,
34                                     size_t chunkSize,
35                                     size_t blockSize);
36class CFX_Target {
37 public:
38  virtual ~CFX_Target() {}
39  void* operator new(size_t size) { return FX_Alloc(uint8_t, size); }
40  void operator delete(void* p) { FX_Free(p); }
41  void* operator new(size_t size, IFX_MEMAllocator* pAllocator) {
42    return pAllocator->Alloc(size);
43  }
44  void operator delete(void* p, IFX_MEMAllocator* pAllocator) {
45    pAllocator->Free(p);
46  }
47  void* operator new(size_t size, void* place) { return 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