| 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_FXCRT_INCLUDE_FX_MEMORY_H_ | 7 #ifndef CORE_FXCRT_FX_MEMORY_H_ |
| 8 #define CORE_FXCRT_INCLUDE_FX_MEMORY_H_ | 8 #define CORE_FXCRT_FX_MEMORY_H_ |
| 9 | 9 |
| 10 #include "core/fxcrt/include/fx_system.h" | 10 #include "core/fxcrt/fx_system.h" |
| 11 | 11 |
| 12 #ifdef __cplusplus | 12 #ifdef __cplusplus |
| 13 extern "C" { | 13 extern "C" { |
| 14 #endif | 14 #endif |
| 15 | 15 |
| 16 // For external C libraries to malloc through PDFium. These may return nullptr. | 16 // For external C libraries to malloc through PDFium. These may return nullptr. |
| 17 void* FXMEM_DefaultAlloc(size_t byte_size, int flags); | 17 void* FXMEM_DefaultAlloc(size_t byte_size, int flags); |
| 18 void* FXMEM_DefaultRealloc(void* pointer, size_t new_size, int flags); | 18 void* FXMEM_DefaultRealloc(void* pointer, size_t new_size, int flags); |
| 19 void FXMEM_DefaultFree(void* pointer, int flags); | 19 void FXMEM_DefaultFree(void* pointer, int flags); |
| 20 | 20 |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 56 size_t num_members, | 56 size_t num_members, |
| 57 size_t member_size) { | 57 size_t member_size) { |
| 58 if (void* result = FX_SafeRealloc(ptr, num_members, member_size)) { | 58 if (void* result = FX_SafeRealloc(ptr, num_members, member_size)) { |
| 59 return result; | 59 return result; |
| 60 } | 60 } |
| 61 FX_OutOfMemoryTerminate(); // Never returns. | 61 FX_OutOfMemoryTerminate(); // Never returns. |
| 62 return nullptr; // Suppress compiler warning. | 62 return nullptr; // Suppress compiler warning. |
| 63 } | 63 } |
| 64 | 64 |
| 65 // Never returns nullptr. | 65 // Never returns nullptr. |
| 66 #define FX_Alloc(type, size) (type*) FX_AllocOrDie(size, sizeof(type)) | 66 #define FX_Alloc(type, size) (type*)FX_AllocOrDie(size, sizeof(type)) |
| 67 #define FX_Alloc2D(type, w, h) (type*) FX_AllocOrDie2D(w, h, sizeof(type)) | 67 #define FX_Alloc2D(type, w, h) (type*)FX_AllocOrDie2D(w, h, sizeof(type)) |
| 68 #define FX_Realloc(type, ptr, size) \ | 68 #define FX_Realloc(type, ptr, size) \ |
| 69 (type*) FX_ReallocOrDie(ptr, size, sizeof(type)) | 69 (type*)FX_ReallocOrDie(ptr, size, sizeof(type)) |
| 70 | 70 |
| 71 // May return nullptr. | 71 // May return nullptr. |
| 72 #define FX_TryAlloc(type, size) (type*) calloc(size, sizeof(type)) | 72 #define FX_TryAlloc(type, size) (type*)calloc(size, sizeof(type)) |
| 73 #define FX_TryRealloc(type, ptr, size) \ | 73 #define FX_TryRealloc(type, ptr, size) \ |
| 74 (type*) FX_SafeRealloc(ptr, size, sizeof(type)) | 74 (type*)FX_SafeRealloc(ptr, size, sizeof(type)) |
| 75 | 75 |
| 76 #define FX_Free(ptr) free(ptr) | 76 #define FX_Free(ptr) free(ptr) |
| 77 | 77 |
| 78 // The FX_ArraySize(arr) macro returns the # of elements in an array arr. | 78 // The FX_ArraySize(arr) macro returns the # of elements in an array arr. |
| 79 // The expression is a compile-time constant, and therefore can be | 79 // The expression is a compile-time constant, and therefore can be |
| 80 // used in defining new arrays, for example. If you use FX_ArraySize on | 80 // used in defining new arrays, for example. If you use FX_ArraySize on |
| 81 // a pointer by mistake, you will get a compile-time error. | 81 // a pointer by mistake, you will get a compile-time error. |
| 82 // | 82 // |
| 83 // One caveat is that FX_ArraySize() doesn't accept any array of an | 83 // One caveat is that FX_ArraySize() doesn't accept any array of an |
| 84 // anonymous type or a type defined inside a function. | 84 // anonymous type or a type defined inside a function. |
| 85 #define FX_ArraySize(array) (sizeof(ArraySizeHelper(array))) | 85 #define FX_ArraySize(array) (sizeof(ArraySizeHelper(array))) |
| 86 | 86 |
| 87 // This template function declaration is used in defining FX_ArraySize. | 87 // This template function declaration is used in defining FX_ArraySize. |
| 88 // Note that the function doesn't need an implementation, as we only | 88 // Note that the function doesn't need an implementation, as we only |
| 89 // use its type. | 89 // use its type. |
| 90 template <typename T, size_t N> | 90 template <typename T, size_t N> |
| 91 char(&ArraySizeHelper(T(&array)[N]))[N]; | 91 char (&ArraySizeHelper(T (&array)[N]))[N]; |
| 92 | 92 |
| 93 // Used with std::unique_ptr to FX_Free raw memory. | 93 // Used with std::unique_ptr to FX_Free raw memory. |
| 94 struct FxFreeDeleter { | 94 struct FxFreeDeleter { |
| 95 inline void operator()(void* ptr) const { FX_Free(ptr); } | 95 inline void operator()(void* ptr) const { FX_Free(ptr); } |
| 96 }; | 96 }; |
| 97 | 97 |
| 98 // Used with std::unique_ptr to Release() objects that can't be deleted. | 98 // Used with std::unique_ptr to Release() objects that can't be deleted. |
| 99 template <class T> | 99 template <class T> |
| 100 struct ReleaseDeleter { | 100 struct ReleaseDeleter { |
| 101 inline void operator()(T* ptr) const { ptr->Release(); } | 101 inline void operator()(T* ptr) const { ptr->Release(); } |
| 102 }; | 102 }; |
| 103 | 103 |
| 104 // Used to help transfer ownership of a raw pointer to std::unique_ptr. | 104 // Used to help transfer ownership of a raw pointer to std::unique_ptr. |
| 105 template <typename T> | 105 template <typename T> |
| 106 std::unique_ptr<T> WrapUnique(T* ptr) { | 106 std::unique_ptr<T> WrapUnique(T* ptr) { |
| 107 return std::unique_ptr<T>(ptr); | 107 return std::unique_ptr<T>(ptr); |
| 108 } | 108 } |
| 109 | 109 |
| 110 #endif // __cplusplus | 110 #endif // __cplusplus |
| 111 | 111 |
| 112 #endif // CORE_FXCRT_INCLUDE_FX_MEMORY_H_ | 112 #endif // CORE_FXCRT_FX_MEMORY_H_ |
| OLD | NEW |