• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2014 The PDFium Authors
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 <stddef.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);
18 void* FXMEM_DefaultCalloc(size_t num_elems, size_t byte_size);
19 void* FXMEM_DefaultRealloc(void* pointer, size_t new_size);
20 void FXMEM_DefaultFree(void* pointer);
21 
22 #ifdef __cplusplus
23 }  // extern "C"
24 
25 #include "core/fxcrt/compiler_specific.h"
26 
27 #if defined(COMPILER_MSVC)
28 #include <malloc.h>
29 #else
30 #include <stdlib.h>
31 #endif
32 
33 void FX_InitializeMemoryAllocators();
34 void FX_DestroyMemoryAllocators();
35 NOINLINE void FX_OutOfMemoryTerminate(size_t size);
36 
37 // General Partition Allocators.
38 
39 // These never return nullptr, and must return cleared memory.
40 #define FX_Alloc(type, size) \
41   static_cast<type*>(pdfium::internal::CallocOrDie(size, sizeof(type)))
42 #define FX_Alloc2D(type, w, h) \
43   static_cast<type*>(pdfium::internal::CallocOrDie2D(w, h, sizeof(type)))
44 #define FX_Realloc(type, ptr, size) \
45   static_cast<type*>(pdfium::internal::ReallocOrDie(ptr, size, sizeof(type)))
46 
47 // May return nullptr, but returns cleared memory otherwise.
48 #define FX_TryAlloc(type, size) \
49   static_cast<type*>(pdfium::internal::Calloc(size, sizeof(type)))
50 #define FX_TryRealloc(type, ptr, size) \
51   static_cast<type*>(pdfium::internal::Realloc(ptr, size, sizeof(type)))
52 
53 // These never return nullptr, but return uninitialized memory.
54 #define FX_AllocUninit(type, size) \
55   static_cast<type*>(pdfium::internal::AllocOrDie(size, sizeof(type)))
56 #define FX_AllocUninit2D(type, w, h) \
57   static_cast<type*>(pdfium::internal::AllocOrDie2D(w, h, sizeof(type)))
58 
59 // May return nullptr, but returns uninitialized memory otherwise.
60 #define FX_TryAllocUninit(type, size) \
61   static_cast<type*>(pdfium::internal::Alloc(size, sizeof(type)))
62 #define FX_TryAllocUninit2D(type, w, h) \
63   static_cast<type*>(pdfium::internal::Alloc2D(w, h, sizeof(type)))
64 
65 // FX_Free frees memory from the above.
66 #define FX_Free(ptr) pdfium::internal::Dealloc(ptr)
67 
68 // String Partition Allocators.
69 
70 // This never returns nullptr, but returns uninitialized memory.
71 #define FX_StringAlloc(type, size) \
72   static_cast<type*>(pdfium::internal::StringAllocOrDie(size, sizeof(type)))
73 
74 // FX_StringFree frees memory from FX_StringAlloc.
75 #define FX_StringFree(ptr) pdfium::internal::StringDealloc(ptr)
76 
77 #ifndef V8_ENABLE_SANDBOX
78 // V8 Array Buffer Partition Allocators.
79 
80 // This never returns nullptr, and returns zeroed memory.
81 void* FX_ArrayBufferAllocate(size_t length);
82 
83 // This never returns nullptr, but returns uninitialized memory.
84 void* FX_ArrayBufferAllocateUninitialized(size_t length);
85 
86 // FX_ArrayBufferFree accepts memory from both of the above.
87 void FX_ArrayBufferFree(void* data);
88 #endif  // V8_ENABLE_SANDBOX
89 
90 // Aligned allocators.
91 
92 // This can be replaced with std::aligned_alloc when we have C++17.
93 // Caveat: std::aligned_alloc requires the size parameter be an integral
94 // multiple of alignment.
95 void* FX_AlignedAlloc(size_t size, size_t alignment);
96 
FX_AlignedFree(void * ptr)97 inline void FX_AlignedFree(void* ptr) {
98 #if defined(COMPILER_MSVC)
99   _aligned_free(ptr);
100 #else
101   free(ptr);
102 #endif
103 }
104 
105 namespace pdfium {
106 namespace internal {
107 
108 // General partition.
109 void* Alloc(size_t num_members, size_t member_size);
110 void* Alloc2D(size_t w, size_t h, size_t member_size);
111 void* AllocOrDie(size_t num_members, size_t member_size);
112 void* AllocOrDie2D(size_t w, size_t h, size_t member_size);
113 void* Calloc(size_t num_members, size_t member_size);
114 void* Realloc(void* ptr, size_t num_members, size_t member_size);
115 void* CallocOrDie(size_t num_members, size_t member_size);
116 void* CallocOrDie2D(size_t w, size_t h, size_t member_size);
117 void* ReallocOrDie(void* ptr, size_t num_members, size_t member_size);
118 void Dealloc(void* ptr);
119 
120 // String partition.
121 void* StringAlloc(size_t num_members, size_t member_size);
122 void* StringAllocOrDie(size_t num_members, size_t member_size);
123 void StringDealloc(void* ptr);
124 
125 }  // namespace internal
126 }  // namespace pdfium
127 
128 // Force stack allocation of a class. Classes that do complex work in a
129 // destructor, such as the flushing of buffers, should be declared as
130 // stack-allocated as possible, since future memory allocation schemes
131 // may not run destructors in a predictable manner if an instance is
132 // heap-allocated.
133 #define FX_STACK_ALLOCATED()           \
134   void* operator new(size_t) = delete; \
135   void* operator new(size_t, void*) = delete
136 
137 // Round up to the power-of-two boundary N.
138 template <int N, typename T>
FxAlignToBoundary(T size)139 inline T FxAlignToBoundary(T size) {
140   static_assert(N > 0 && (N & (N - 1)) == 0, "Not non-zero power of two");
141   return (size + (N - 1)) & ~(N - 1);
142 }
143 
144 #endif  // __cplusplus
145 
146 #endif  // CORE_FXCRT_FX_MEMORY_H_
147