1 // Copyright 2014 PDFium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6
7 #ifndef CORE_FXCRT_FX_MEMORY_H_
8 #define CORE_FXCRT_FX_MEMORY_H_
9
10 #include "core/fxcrt/fx_system.h"
11
12 #ifdef __cplusplus
13 extern "C" {
14 #endif
15
16 // For external C libraries to malloc through PDFium. These may return nullptr.
17 void* FXMEM_DefaultAlloc(size_t byte_size, int flags);
18 void* FXMEM_DefaultRealloc(void* pointer, size_t new_size, int flags);
19 void FXMEM_DefaultFree(void* pointer, int flags);
20
21 #ifdef __cplusplus
22 } // extern "C"
23
24 #include <stdlib.h>
25 #include <limits>
26 #include <memory>
27 #include <new>
28
29 NEVER_INLINE void FX_OutOfMemoryTerminate();
30
FX_SafeRealloc(void * ptr,size_t num_members,size_t member_size)31 inline void* FX_SafeRealloc(void* ptr, size_t num_members, size_t member_size) {
32 if (num_members < std::numeric_limits<size_t>::max() / member_size) {
33 return realloc(ptr, num_members * member_size);
34 }
35 return nullptr;
36 }
37
FX_AllocOrDie(size_t num_members,size_t member_size)38 inline void* FX_AllocOrDie(size_t num_members, size_t member_size) {
39 // TODO(tsepez): See if we can avoid the implicit memset(0).
40 if (void* result = calloc(num_members, member_size)) {
41 return result;
42 }
43 FX_OutOfMemoryTerminate(); // Never returns.
44 return nullptr; // Suppress compiler warning.
45 }
46
FX_AllocOrDie2D(size_t w,size_t h,size_t member_size)47 inline void* FX_AllocOrDie2D(size_t w, size_t h, size_t member_size) {
48 if (w < std::numeric_limits<size_t>::max() / h) {
49 return FX_AllocOrDie(w * h, member_size);
50 }
51 FX_OutOfMemoryTerminate(); // Never returns.
52 return nullptr; // Suppress compiler warning.
53 }
54
FX_ReallocOrDie(void * ptr,size_t num_members,size_t member_size)55 inline void* FX_ReallocOrDie(void* ptr,
56 size_t num_members,
57 size_t member_size) {
58 if (void* result = FX_SafeRealloc(ptr, num_members, member_size)) {
59 return result;
60 }
61 FX_OutOfMemoryTerminate(); // Never returns.
62 return nullptr; // Suppress compiler warning.
63 }
64
65 // Never returns nullptr.
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))
68 #define FX_Realloc(type, ptr, size) \
69 (type*)FX_ReallocOrDie(ptr, size, sizeof(type))
70
71 // May return nullptr.
72 #define FX_TryAlloc(type, size) (type*)calloc(size, sizeof(type))
73 #define FX_TryRealloc(type, ptr, size) \
74 (type*)FX_SafeRealloc(ptr, size, sizeof(type))
75
76 #define FX_Free(ptr) free(ptr)
77
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
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.
82 //
83 // One caveat is that FX_ArraySize() doesn't accept any array of an
84 // anonymous type or a type defined inside a function.
85 #define FX_ArraySize(array) (sizeof(ArraySizeHelper(array)))
86
87 // This template function declaration is used in defining FX_ArraySize.
88 // Note that the function doesn't need an implementation, as we only
89 // use its type.
90 template <typename T, size_t N>
91 char (&ArraySizeHelper(T (&array)[N]))[N];
92
93 // Used with std::unique_ptr to FX_Free raw memory.
94 struct FxFreeDeleter {
operatorFxFreeDeleter95 inline void operator()(void* ptr) const { FX_Free(ptr); }
96 };
97
98 // Used with std::unique_ptr to Release() objects that can't be deleted.
99 template <class T>
100 struct ReleaseDeleter {
operatorReleaseDeleter101 inline void operator()(T* ptr) const { ptr->Release(); }
102 };
103
104 #endif // __cplusplus
105
106 #endif // CORE_FXCRT_FX_MEMORY_H_
107