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 "third_party/base/compiler_specific.h"
26
27 void FX_InitializeMemoryAllocators();
28 NOINLINE void FX_OutOfMemoryTerminate(size_t size);
29
30 // General Partition Allocators.
31
32 // These never return nullptr, and must return cleared memory.
33 #define FX_Alloc(type, size) \
34 static_cast<type*>(pdfium::internal::CallocOrDie(size, sizeof(type)))
35 #define FX_Alloc2D(type, w, h) \
36 static_cast<type*>(pdfium::internal::CallocOrDie2D(w, h, sizeof(type)))
37 #define FX_Realloc(type, ptr, size) \
38 static_cast<type*>(pdfium::internal::ReallocOrDie(ptr, size, sizeof(type)))
39
40 // May return nullptr, but returns cleared memory otherwise.
41 #define FX_TryAlloc(type, size) \
42 static_cast<type*>(pdfium::internal::Calloc(size, sizeof(type)))
43 #define FX_TryRealloc(type, ptr, size) \
44 static_cast<type*>(pdfium::internal::Realloc(ptr, size, sizeof(type)))
45
46 // These never return nullptr, but return uninitialized memory.
47 // TODO(thestig): Add FX_TryAllocUninit() if there is a use case.
48 #define FX_AllocUninit(type, size) \
49 static_cast<type*>(pdfium::internal::AllocOrDie(size, sizeof(type)))
50 #define FX_AllocUninit2D(type, w, h) \
51 static_cast<type*>(pdfium::internal::AllocOrDie2D(w, h, sizeof(type)))
52
53 // FX_Free frees memory from the above.
54 #define FX_Free(ptr) pdfium::internal::Dealloc(ptr)
55
56 // String Partition Allocators.
57
58 // This never returns nullptr, but returns uninitialized memory.
59 #define FX_StringAlloc(type, size) \
60 static_cast<type*>(pdfium::internal::StringAllocOrDie(size, sizeof(type)))
61
62 // FX_StringFree frees memory from FX_StringAlloc.
63 #define FX_StringFree(ptr) pdfium::internal::StringDealloc(ptr)
64
65 #ifndef V8_ENABLE_SANDBOX
66 // V8 Array Buffer Partition Allocators.
67
68 // This never returns nullptr, and returns zeroed memory.
69 void* FX_ArrayBufferAllocate(size_t length);
70
71 // This never returns nullptr, but returns uninitialized memory.
72 void* FX_ArrayBufferAllocateUninitialized(size_t length);
73
74 // FX_ArrayBufferFree accepts memory from both of the above.
75 void FX_ArrayBufferFree(void* data);
76 #endif // V8_ENABLE_SANDBOX
77
78 namespace pdfium {
79 namespace internal {
80
81 // General partition.
82 void* Alloc(size_t num_members, size_t member_size);
83 void* AllocOrDie(size_t num_members, size_t member_size);
84 void* AllocOrDie2D(size_t w, size_t h, size_t member_size);
85 void* Calloc(size_t num_members, size_t member_size);
86 void* Realloc(void* ptr, size_t num_members, size_t member_size);
87 void* CallocOrDie(size_t num_members, size_t member_size);
88 void* CallocOrDie2D(size_t w, size_t h, size_t member_size);
89 void* ReallocOrDie(void* ptr, size_t num_members, size_t member_size);
90 void Dealloc(void* ptr);
91
92 // String partition.
93 void* StringAlloc(size_t num_members, size_t member_size);
94 void* StringAllocOrDie(size_t num_members, size_t member_size);
95 void StringDealloc(void* ptr);
96
97 } // namespace internal
98 } // namespace pdfium
99
100 // Force stack allocation of a class. Classes that do complex work in a
101 // destructor, such as the flushing of buffers, should be declared as
102 // stack-allocated as possible, since future memory allocation schemes
103 // may not run destructors in a predictable manner if an instance is
104 // heap-allocated.
105 #define FX_STACK_ALLOCATED() \
106 void* operator new(size_t) = delete; \
107 void* operator new(size_t, void*) = delete
108
109 // Round up to the power-of-two boundary N.
110 template <int N, typename T>
FxAlignToBoundary(T size)111 inline T FxAlignToBoundary(T size) {
112 static_assert(N > 0 && (N & (N - 1)) == 0, "Not non-zero power of two");
113 return (size + (N - 1)) & ~(N - 1);
114 }
115
116 #endif // __cplusplus
117
118 #endif // CORE_FXCRT_FX_MEMORY_H_
119