| 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 NEVER_INLINE void FX_OutOfMemoryTerminate(); |
| 27 |
| 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) { |
| 30 return realloc(ptr, num_members * member_size); |
| 31 } |
| 32 return nullptr; |
| 25 } | 33 } |
| 26 | 34 |
| 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). |
| 37 if (void* result = calloc(num_members, member_size)) { |
| 38 return result; |
| 39 } |
| 40 FX_OutOfMemoryTerminate(); // Never returns. |
| 41 return nullptr; // Suppress compiler warning. |
| 42 } |
| 43 |
| 44 inline void* FX_ReallocOrDie(void* ptr, size_t num_members, size_t member_size)
{ |
| 45 if (void* result = FX_SafeRealloc(ptr, num_members, member_size)) { |
| 46 return result; |
| 47 } |
| 48 FX_OutOfMemoryTerminate(); // Never returns. |
| 49 return nullptr; // Suppress compiler warning. |
| 50 } |
| 51 |
| 52 // Never returns NULL. |
| 53 #define FX_Alloc(type, size) (type*)FX_AllocOrDie(size, sizeof(type)) |
| 54 #define FX_Realloc(type, ptr, size) \ |
| 55 (type*)FX_ReallocOrDie(ptr, size, sizeof(type)) |
| 56 |
| 57 // May return NULL. |
| 58 #define FX_TryAlloc(type, size) (type*)calloc(size, sizeof(type)) |
| 59 #define FX_TryRealloc(type, ptr, size) \ |
| 60 (type*)FX_SafeRealloc(ptr, size, sizeof(type)) |
| 61 |
| 62 #define FX_Free(ptr) free(ptr) |
| 63 |
| 27 class CFX_DestructObject | 64 class CFX_DestructObject |
| 28 { | 65 { |
| 29 public: | 66 public: |
| 30 | 67 |
| 31 virtual ~CFX_DestructObject() {} | 68 virtual ~CFX_DestructObject() {} |
| 32 }; | 69 }; |
| 33 class CFX_GrowOnlyPool | 70 class CFX_GrowOnlyPool |
| 34 { | 71 { |
| 35 public: | 72 public: |
| 36 | 73 |
| (...skipping 25 matching lines...) Expand all Loading... |
| 62 | 99 |
| 63 void Free(void*) {} | 100 void Free(void*) {} |
| 64 | 101 |
| 65 void FreeAll(); | 102 void FreeAll(); |
| 66 private: | 103 private: |
| 67 | 104 |
| 68 size_t m_TrunkSize; | 105 size_t m_TrunkSize; |
| 69 | 106 |
| 70 void* m_pFirstTrunk; | 107 void* m_pFirstTrunk; |
| 71 }; | 108 }; |
| 72 #endif | 109 #endif // __cplusplust |
| 73 #endif | 110 #endif // _FX_MEMORY_H_ |
| OLD | NEW |