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 CORE_INCLUDE_FXCRT_FX_MEMORY_H_ | 7 #ifndef CORE_INCLUDE_FXCRT_FX_MEMORY_H_ |
8 #define CORE_INCLUDE_FXCRT_FX_MEMORY_H_ | 8 #define CORE_INCLUDE_FXCRT_FX_MEMORY_H_ |
9 | 9 |
10 #include "fx_system.h" | 10 #include "fx_system.h" |
11 | 11 |
12 #ifdef __cplusplus | 12 #ifdef __cplusplus |
13 extern "C" { | 13 extern "C" { |
14 #endif | 14 #endif |
15 // For external C libraries to malloc through PDFium. These may return NULL. | 15 // For external C libraries to malloc through PDFium. These may return NULL. |
16 void*» FXMEM_DefaultAlloc(size_t byte_size, int flags); | 16 void* FXMEM_DefaultAlloc(size_t byte_size, int flags); |
17 void*» FXMEM_DefaultRealloc(void* pointer, size_t new_size, int flags); | 17 void* FXMEM_DefaultRealloc(void* pointer, size_t new_size, int flags); |
18 void» FXMEM_DefaultFree(void* pointer, int flags); | 18 void FXMEM_DefaultFree(void* pointer, int flags); |
19 #ifdef __cplusplus | 19 #ifdef __cplusplus |
20 } // extern "C" | 20 } // extern "C" |
21 | 21 |
22 #include <stdlib.h> | 22 #include <stdlib.h> |
23 #include <limits> | 23 #include <limits> |
24 #include <new> | 24 #include <new> |
25 | 25 |
26 NEVER_INLINE void FX_OutOfMemoryTerminate(); | 26 NEVER_INLINE void FX_OutOfMemoryTerminate(); |
27 | 27 |
28 inline void* FX_SafeRealloc(void* ptr, size_t num_members, size_t member_size) { | 28 inline void* FX_SafeRealloc(void* ptr, size_t num_members, size_t member_size) { |
29 if (num_members < std::numeric_limits<size_t>::max() / member_size) { | 29 if (num_members < std::numeric_limits<size_t>::max() / member_size) { |
30 return realloc(ptr, num_members * member_size); | 30 return realloc(ptr, num_members * member_size); |
31 } | 31 } |
32 return nullptr; | 32 return nullptr; |
33 } | 33 } |
34 | 34 |
35 inline void* FX_AllocOrDie(size_t num_members, size_t member_size) { | 35 inline void* FX_AllocOrDie(size_t num_members, size_t member_size) { |
36 // TODO(tsepez): See if we can avoid the implicit memset(0). | 36 // TODO(tsepez): See if we can avoid the implicit memset(0). |
37 if (void* result = calloc(num_members, member_size)) { | 37 if (void* result = calloc(num_members, member_size)) { |
38 return result; | 38 return result; |
39 } | 39 } |
40 FX_OutOfMemoryTerminate(); // Never returns. | 40 FX_OutOfMemoryTerminate(); // Never returns. |
41 return nullptr; // Suppress compiler warning. | 41 return nullptr; // Suppress compiler warning. |
42 } | 42 } |
43 | 43 |
44 inline void* FX_AllocOrDie2D(size_t w, size_t h, size_t member_size) { | 44 inline void* FX_AllocOrDie2D(size_t w, size_t h, size_t member_size) { |
45 if (w < std::numeric_limits<size_t>::max() / h) { | 45 if (w < std::numeric_limits<size_t>::max() / h) { |
46 return FX_AllocOrDie(w * h, member_size); | 46 return FX_AllocOrDie(w * h, member_size); |
47 } | 47 } |
48 FX_OutOfMemoryTerminate(); // Never returns. | 48 FX_OutOfMemoryTerminate(); // Never returns. |
49 return nullptr; // Suppress compiler warning. | 49 return nullptr; // Suppress compiler warning. |
50 } | 50 } |
51 | 51 |
52 inline void* FX_ReallocOrDie(void* ptr, size_t num_members, size_t member_size)
{ | 52 inline void* FX_ReallocOrDie(void* ptr, |
53 if (void* result = FX_SafeRealloc(ptr, num_members, member_size)) { | 53 size_t num_members, |
54 return result; | 54 size_t member_size) { |
55 } | 55 if (void* result = FX_SafeRealloc(ptr, num_members, member_size)) { |
56 FX_OutOfMemoryTerminate(); // Never returns. | 56 return result; |
57 return nullptr; // Suppress compiler warning. | 57 } |
| 58 FX_OutOfMemoryTerminate(); // Never returns. |
| 59 return nullptr; // Suppress compiler warning. |
58 } | 60 } |
59 | 61 |
60 // Never returns NULL. | 62 // Never returns NULL. |
61 #define FX_Alloc(type, size) (type*)FX_AllocOrDie(size, sizeof(type)) | 63 #define FX_Alloc(type, size) (type*) FX_AllocOrDie(size, sizeof(type)) |
62 #define FX_Alloc2D(type, w, h) (type*)FX_AllocOrDie2D(w, h, sizeof(type)) | 64 #define FX_Alloc2D(type, w, h) (type*) FX_AllocOrDie2D(w, h, sizeof(type)) |
63 #define FX_Realloc(type, ptr, size) \ | 65 #define FX_Realloc(type, ptr, size) \ |
64 (type*)FX_ReallocOrDie(ptr, size, sizeof(type)) | 66 (type*) FX_ReallocOrDie(ptr, size, sizeof(type)) |
65 | 67 |
66 // May return NULL. | 68 // May return NULL. |
67 #define FX_TryAlloc(type, size) (type*)calloc(size, sizeof(type)) | 69 #define FX_TryAlloc(type, size) (type*) calloc(size, sizeof(type)) |
68 #define FX_TryRealloc(type, ptr, size) \ | 70 #define FX_TryRealloc(type, ptr, size) \ |
69 (type*)FX_SafeRealloc(ptr, size, sizeof(type)) | 71 (type*) FX_SafeRealloc(ptr, size, sizeof(type)) |
70 | 72 |
71 #define FX_Free(ptr) free(ptr) | 73 #define FX_Free(ptr) free(ptr) |
72 | 74 |
73 class CFX_DestructObject | 75 class CFX_DestructObject { |
74 { | 76 public: |
75 public: | 77 virtual ~CFX_DestructObject() {} |
| 78 }; |
| 79 class CFX_GrowOnlyPool { |
| 80 public: |
| 81 CFX_GrowOnlyPool(size_t trunk_size = 16384); |
76 | 82 |
77 virtual ~CFX_DestructObject() {} | 83 ~CFX_GrowOnlyPool(); |
78 }; | |
79 class CFX_GrowOnlyPool | |
80 { | |
81 public: | |
82 | 84 |
83 CFX_GrowOnlyPool(size_t trunk_size = 16384); | 85 void SetTrunkSize(size_t trunk_size) { m_TrunkSize = trunk_size; } |
84 | 86 |
85 ~CFX_GrowOnlyPool(); | 87 void* AllocDebug(size_t size, const FX_CHAR* file, int line) { |
| 88 return Alloc(size); |
| 89 } |
86 | 90 |
87 void» SetTrunkSize(size_t trunk_size) | 91 void* Alloc(size_t size); |
88 { | |
89 m_TrunkSize = trunk_size; | |
90 } | |
91 | 92 |
92 void*» AllocDebug(size_t size, const FX_CHAR* file, int line) | 93 void* ReallocDebug(void* p, size_t new_size, const FX_CHAR* file, int line) { |
93 { | 94 return NULL; |
94 return Alloc(size); | 95 } |
95 } | |
96 | 96 |
97 void*» Alloc(size_t size); | 97 void* Realloc(void* p, size_t new_size) { return NULL; } |
98 | 98 |
99 void*» ReallocDebug(void* p, size_t new_size, const FX_CHAR* file, int
line) | 99 void Free(void*) {} |
100 { | |
101 return NULL; | |
102 } | |
103 | 100 |
104 void*» Realloc(void* p, size_t new_size) | 101 void FreeAll(); |
105 { | |
106 return NULL; | |
107 } | |
108 | 102 |
109 void» Free(void*) {} | 103 private: |
| 104 size_t m_TrunkSize; |
110 | 105 |
111 void» FreeAll(); | 106 void* m_pFirstTrunk; |
112 private: | |
113 | |
114 size_t» m_TrunkSize; | |
115 | |
116 void*» m_pFirstTrunk; | |
117 }; | 107 }; |
118 #endif // __cplusplus | 108 #endif // __cplusplus |
119 | 109 |
120 #endif // CORE_INCLUDE_FXCRT_FX_MEMORY_H_ | 110 #endif // CORE_INCLUDE_FXCRT_FX_MEMORY_H_ |
OLD | NEW |