| 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> | |
| 14 extern "C" { | 13 extern "C" { |
| 15 #endif | 14 #endif |
| 16 #define FX_Alloc(type, size)» » » » » » (type*)c
alloc(size, sizeof(type)) | 15 // For external C libraries to malloc through PDFium. These may return NULL. |
| 17 #define FX_Realloc(type, ptr, size)» » » » » (type*)r
ealloc(ptr, sizeof(type) * (size)) | |
| 18 #define FX_AllocNL(type, size)» » » » » » FX_Alloc
(type, size) | |
| 19 #define FX_ReallocNL(type, ptr, size)» » » » FX_Realloc(type,
ptr, size) | |
| 20 #define FX_Free(ptr)» » » » » » » »
free(ptr) | |
| 21 void* FXMEM_DefaultAlloc(size_t byte_size, int flags); | 16 void* FXMEM_DefaultAlloc(size_t byte_size, int flags); |
| 22 void* FXMEM_DefaultRealloc(void* pointer, size_t new_size, int flags); | 17 void* FXMEM_DefaultRealloc(void* pointer, size_t new_size, int flags); |
| 23 void FXMEM_DefaultFree(void* pointer, int flags); | 18 void FXMEM_DefaultFree(void* pointer, int flags); |
| 24 #ifdef __cplusplus | 19 #ifdef __cplusplus |
| 20 } // extern "C" |
| 21 |
| 22 #include <stdlib.h> |
| 23 #include <limits> |
| 24 #include <new> |
| 25 |
| 26 #define FX_NEW new |
| 27 |
| 28 NEVER_INLINE void FX_OutOfMemoryTerminate(); |
| 29 |
| 30 inline void* FX_SafeRealloc(void* ptr, size_t num_members, size_t member_size) { |
| 31 if (num_members < std::numeric_limits<size_t>::max() / member_size) { |
| 32 return realloc(ptr, num_members * member_size); |
| 33 } |
| 34 return nullptr; |
| 25 } | 35 } |
| 26 | 36 |
| 27 #define FX_NEW new | 37 inline void* FX_AllocOrDie(size_t num_members, size_t member_size) { |
| 38 // TODO(tsepez): See if we can avoid the implicit memset(0). |
| 39 if (void* result = calloc(num_members, member_size)) { |
| 40 return result; |
| 41 } |
| 42 FX_OutOfMemoryTerminate(); // Never returns. |
| 43 return nullptr; // Suppress compiler warning. |
| 44 } |
| 45 |
| 46 inline void* FX_ReallocOrDie(void* ptr, size_t num_members, size_t member_size)
{ |
| 47 if (void* result = FX_SafeRealloc(ptr, num_members, member_size)) { |
| 48 return result; |
| 49 } |
| 50 FX_OutOfMemoryTerminate(); // Never returns. |
| 51 return nullptr; // Suppress compiler warning. |
| 52 } |
| 53 |
| 54 // Never returns NULL. |
| 55 #define FX_Alloc(type, size) (type*)FX_AllocOrDie(size, sizeof(type)) |
| 56 #define FX_Realloc(type, ptr, size) \ |
| 57 (type*)FX_ReallocOrDie(ptr, size, sizeof(type)) |
| 58 |
| 59 // May return NULL. |
| 60 #define FX_TryAlloc(type, size) (type*)calloc(size, sizeof(type)) |
| 61 #define FX_TryRealloc(type, ptr, size) \ |
| 62 (type*)FX_SafeRealloc(ptr, size, sizeof(type)) |
| 63 |
| 64 #define FX_Free(ptr) free(ptr) |
| 28 | 65 |
| 29 class CFX_DestructObject | 66 class CFX_DestructObject |
| 30 { | 67 { |
| 31 public: | 68 public: |
| 32 | 69 |
| 33 virtual ~CFX_DestructObject() {} | 70 virtual ~CFX_DestructObject() {} |
| 34 }; | 71 }; |
| 35 class CFX_GrowOnlyPool | 72 class CFX_GrowOnlyPool |
| 36 { | 73 { |
| 37 public: | 74 public: |
| (...skipping 26 matching lines...) Expand all Loading... |
| 64 | 101 |
| 65 void Free(void*) {} | 102 void Free(void*) {} |
| 66 | 103 |
| 67 void FreeAll(); | 104 void FreeAll(); |
| 68 private: | 105 private: |
| 69 | 106 |
| 70 size_t m_TrunkSize; | 107 size_t m_TrunkSize; |
| 71 | 108 |
| 72 void* m_pFirstTrunk; | 109 void* m_pFirstTrunk; |
| 73 }; | 110 }; |
| 74 #endif | 111 #endif // __cplusplust |
| 75 #endif | 112 #endif // _FX_MEMORY_H_ |
| OLD | NEW |