• 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 "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 // String Partition Allocators.
54 
55 // This never returns nullptr, but returns uninitialized memory.
56 #define FX_StringAlloc(type, size) \
57   static_cast<type*>(pdfium::internal::StringAllocOrDie(size, sizeof(type)))
58 
59 // FX_Free accepts memory from all of the above.
60 void FX_Free(void* ptr);
61 
62 #ifndef V8_ENABLE_SANDBOX
63 // V8 Array Buffer Partition Allocators.
64 
65 // This never returns nullptr, and returns zeroed memory.
66 void* FX_ArrayBufferAllocate(size_t length);
67 
68 // This never returns nullptr, but returns uninitialized memory.
69 void* FX_ArrayBufferAllocateUninitialized(size_t length);
70 
71 // FX_ArrayBufferFree accepts memory from both of the above.
72 void FX_ArrayBufferFree(void* data);
73 #endif  // V8_ENABLE_SANDBOX
74 
75 namespace pdfium {
76 namespace internal {
77 
78 // General partition.
79 void* Alloc(size_t num_members, size_t member_size);
80 void* AllocOrDie(size_t num_members, size_t member_size);
81 void* AllocOrDie2D(size_t w, size_t h, size_t member_size);
82 void* Calloc(size_t num_members, size_t member_size);
83 void* Realloc(void* ptr, size_t num_members, size_t member_size);
84 void* CallocOrDie(size_t num_members, size_t member_size);
85 void* CallocOrDie2D(size_t w, size_t h, size_t member_size);
86 void* ReallocOrDie(void* ptr, size_t num_members, size_t member_size);
87 
88 // String partition.
89 void* StringAlloc(size_t num_members, size_t member_size);
90 void* StringAllocOrDie(size_t num_members, size_t member_size);
91 
92 }  // namespace internal
93 }  // namespace pdfium
94 
95 // Force stack allocation of a class. Classes that do complex work in a
96 // destructor, such as the flushing of buffers, should be declared as
97 // stack-allocated as possible, since future memory allocation schemes
98 // may not run destructors in a predictable manner if an instance is
99 // heap-allocated.
100 #define FX_STACK_ALLOCATED()           \
101   void* operator new(size_t) = delete; \
102   void* operator new(size_t, void*) = delete
103 
104 // Round up to the power-of-two boundary N.
105 template <int N, typename T>
FxAlignToBoundary(T size)106 inline T FxAlignToBoundary(T size) {
107   static_assert(N > 0 && (N & (N - 1)) == 0, "Not non-zero power of two");
108   return (size + (N - 1)) & ~(N - 1);
109 }
110 
111 #endif  // __cplusplus
112 
113 #endif  // CORE_FXCRT_FX_MEMORY_H_
114