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 #ifndef _FX_MEMORY_H_ | 7 #ifndef _FX_MEMORY_H_ |
8 #define _FX_MEMORY_H_ | 8 #define _FX_MEMORY_H_ |
9 | 9 |
10 #include "fx_system.h" | 10 #include "fx_system.h" |
11 | 11 |
12 #ifdef __cplusplus | 12 #ifdef __cplusplus |
| 13 #include <new> |
13 extern "C" { | 14 extern "C" { |
14 #endif | 15 #endif |
15 #define FX_Alloc(type, size) (type*)c
alloc(size, sizeof(type)) | 16 #define FX_Alloc(type, size) (type*)c
alloc(size, sizeof(type)) |
16 #define FX_Realloc(type, ptr, size) (type*)r
ealloc(ptr, sizeof(type) * (size)) | 17 #define FX_Realloc(type, ptr, size) (type*)r
ealloc(ptr, sizeof(type) * (size)) |
17 #define FX_AllocNL(type, size) FX_Alloc
(type, size) | 18 #define FX_AllocNL(type, size) FX_Alloc
(type, size) |
18 #define FX_ReallocNL(type, ptr, size) FX_Realloc(type,
ptr, size) | 19 #define FX_ReallocNL(type, ptr, size) FX_Realloc(type,
ptr, size) |
19 #define FX_Free(ptr)
free(ptr) | 20 #define FX_Free(ptr)
free(ptr) |
20 void* FXMEM_DefaultAlloc(size_t byte_size, int flags); | 21 void* FXMEM_DefaultAlloc(size_t byte_size, int flags); |
21 void* FXMEM_DefaultRealloc(void* pointer, size_t new_size, int flags); | 22 void* FXMEM_DefaultRealloc(void* pointer, size_t new_size, int flags); |
22 void FXMEM_DefaultFree(void* pointer, int flags); | 23 void FXMEM_DefaultFree(void* pointer, int flags); |
23 #ifdef __cplusplus | 24 #ifdef __cplusplus |
24 } | 25 } |
25 #endif | |
26 #ifdef __cplusplus | |
27 class CFX_Object | |
28 { | |
29 public: | |
30 void* operator new (size_t size, FX_LPCSTR file, int l
ine) | |
31 { | |
32 return malloc(size); | |
33 } | |
34 void operator delete (void* p, FX_LPCSTR file, int li
ne) | |
35 { | |
36 free(p); | |
37 } | |
38 void* operator new (size_t size) | |
39 { | |
40 return malloc(size); | |
41 } | |
42 void operator delete (void* p) | |
43 { | |
44 free(p); | |
45 } | |
46 void* operator new[] (size_t size, FX_LPCSTR file, int
line) | |
47 { | |
48 return malloc(size); | |
49 } | |
50 void operator delete[] (void* p, FX_LPCSTR file, int
line) | |
51 { | |
52 free(p); | |
53 } | |
54 void* operator new[] (size_t size) | |
55 { | |
56 return malloc(size); | |
57 } | |
58 void operator delete[] (void* p) | |
59 { | |
60 free(p); | |
61 } | |
62 void* operator new (size_t, void* buf) | |
63 { | |
64 return buf; | |
65 } | |
66 void operator delete (void*, void*)
{} | |
67 }; | |
68 #endif | |
69 #ifdef __cplusplus | |
70 #if defined(_DEBUG) | |
71 #define FX_NEW new(__FILE__, __LINE__) | |
72 #else | |
73 | 26 |
74 #define FX_NEW new | 27 #define FX_NEW new(std::nothrow) |
75 #endif | 28 #define FX_NEW_VECTOR(Pointer, Class, Count) (Pointer = FX_NEW Class[Count]) |
76 #define FX_NEW_VECTOR(Pointer, Class, Count) \ | 29 #define FX_DELETE_VECTOR(Pointer, Class, Count) delete[] Pointer |
77 { \ | 30 |
78 Pointer = FX_Alloc(Class, Count); \ | 31 class CFX_DestructObject |
79 if (Pointer) { \ | |
80 for (int i = 0; i < (Count); i ++) new (Pointer + i) Class; \ | |
81 } \ | |
82 } | |
83 #define FX_DELETE_VECTOR(Pointer, Class, Count) \ | |
84 { \ | |
85 for (int i = 0; i < (Count); i ++) Pointer[i].~Class(); \ | |
86 FX_Free(Pointer); \ | |
87 } | |
88 class CFX_DestructObject : public CFX_Object | |
89 { | 32 { |
90 public: | 33 public: |
91 | 34 |
92 virtual ~CFX_DestructObject() {} | 35 virtual ~CFX_DestructObject() {} |
93 }; | 36 }; |
94 class CFX_GrowOnlyPool : public CFX_Object | 37 class CFX_GrowOnlyPool |
95 { | 38 { |
96 public: | 39 public: |
97 | 40 |
98 CFX_GrowOnlyPool(size_t trunk_size = 16384); | 41 CFX_GrowOnlyPool(size_t trunk_size = 16384); |
99 | 42 |
100 ~CFX_GrowOnlyPool(); | 43 ~CFX_GrowOnlyPool(); |
101 | 44 |
102 void SetTrunkSize(size_t trunk_size) | 45 void SetTrunkSize(size_t trunk_size) |
103 { | 46 { |
104 m_TrunkSize = trunk_size; | 47 m_TrunkSize = trunk_size; |
(...skipping 20 matching lines...) Expand all Loading... |
125 | 68 |
126 void FreeAll(); | 69 void FreeAll(); |
127 private: | 70 private: |
128 | 71 |
129 size_t m_TrunkSize; | 72 size_t m_TrunkSize; |
130 | 73 |
131 void* m_pFirstTrunk; | 74 void* m_pFirstTrunk; |
132 }; | 75 }; |
133 #endif | 76 #endif |
134 #endif | 77 #endif |
OLD | NEW |