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_INCLUDE_FX_MEMORY_H_ |
8 #define CORE_FXCRT_INCLUDE_FX_MEMORY_H_ | 8 #define CORE_FXCRT_INCLUDE_FX_MEMORY_H_ |
9 | 9 |
10 #include "core/fxcrt/include/fx_system.h" | 10 #include "core/fxcrt/include/fx_system.h" |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
67 #define FX_Realloc(type, ptr, size) \ | 67 #define FX_Realloc(type, ptr, size) \ |
68 (type*) FX_ReallocOrDie(ptr, size, sizeof(type)) | 68 (type*) FX_ReallocOrDie(ptr, size, sizeof(type)) |
69 | 69 |
70 // May return NULL. | 70 // May return NULL. |
71 #define FX_TryAlloc(type, size) (type*) calloc(size, sizeof(type)) | 71 #define FX_TryAlloc(type, size) (type*) calloc(size, sizeof(type)) |
72 #define FX_TryRealloc(type, ptr, size) \ | 72 #define FX_TryRealloc(type, ptr, size) \ |
73 (type*) FX_SafeRealloc(ptr, size, sizeof(type)) | 73 (type*) FX_SafeRealloc(ptr, size, sizeof(type)) |
74 | 74 |
75 #define FX_Free(ptr) free(ptr) | 75 #define FX_Free(ptr) free(ptr) |
76 | 76 |
| 77 // The FX_ArraySize(arr) macro returns the # of elements in an array arr. |
| 78 // The expression is a compile-time constant, and therefore can be |
| 79 // used in defining new arrays, for example. If you use FX_ArraySize on |
| 80 // a pointer by mistake, you will get a compile-time error. |
| 81 // |
| 82 // One caveat is that FX_ArraySize() doesn't accept any array of an |
| 83 // anonymous type or a type defined inside a function. |
| 84 #define FX_ArraySize(array) (sizeof(ArraySizeHelper(array))) |
| 85 |
| 86 // This template function declaration is used in defining FX_ArraySize. |
| 87 // Note that the function doesn't need an implementation, as we only |
| 88 // use its type. |
| 89 template <typename T, size_t N> |
| 90 char(&ArraySizeHelper(T(&array)[N]))[N]; |
| 91 |
| 92 // Used with std::unique_ptr to FX_Free raw memory. |
| 93 struct FxFreeDeleter { |
| 94 inline void operator()(void* ptr) const { FX_Free(ptr); } |
| 95 }; |
| 96 |
| 97 // Used with std::unique_ptr to Release() objects that can't be deleted. |
| 98 template <class T> |
| 99 struct ReleaseDeleter { |
| 100 inline void operator()(T* ptr) const { ptr->Release(); } |
| 101 }; |
| 102 |
77 class CFX_DestructObject { | 103 class CFX_DestructObject { |
78 public: | 104 public: |
79 virtual ~CFX_DestructObject() {} | 105 virtual ~CFX_DestructObject() {} |
80 }; | 106 }; |
81 | 107 |
82 #endif // __cplusplus | 108 #endif // __cplusplus |
83 | 109 |
84 #endif // CORE_FXCRT_INCLUDE_FX_MEMORY_H_ | 110 #endif // CORE_FXCRT_INCLUDE_FX_MEMORY_H_ |
OLD | NEW |