Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
fx_memory_pa.cpp
Go to the documentation of this file.
1// Copyright 2022 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#include "core/fxcrt/fx_memory.h"
8
9#include "core/fxcrt/fx_safe_types.h"
10#include "partition_alloc/partition_alloc.h"
11#include "third_party/base/no_destructor.h"
12
13#if !defined(PDF_USE_PARTITION_ALLOC)
14#error "File compiled under wrong build option."
15#endif
16
17namespace {
18
19constexpr partition_alloc::PartitionOptions kOptions = {};
20
21#ifndef V8_ENABLE_SANDBOX
22partition_alloc::PartitionAllocator& GetArrayBufferPartitionAllocator() {
23 static pdfium::base::NoDestructor<partition_alloc::PartitionAllocator>
24 s_array_buffer_allocator(kOptions);
25 return *s_array_buffer_allocator;
26}
27#endif // V8_ENABLE_SANDBOX
28
29partition_alloc::PartitionAllocator& GetGeneralPartitionAllocator() {
30 static pdfium::base::NoDestructor<partition_alloc::PartitionAllocator>
31 s_general_allocator(kOptions);
32 return *s_general_allocator;
33}
34
35partition_alloc::PartitionAllocator& GetStringPartitionAllocator() {
36 static pdfium::base::NoDestructor<partition_alloc::PartitionAllocator>
37 s_string_allocator(kOptions);
38 return *s_string_allocator;
39}
40
41} // namespace
42
43namespace pdfium::internal {
44
45void* Alloc(size_t num_members, size_t member_size) {
46 FX_SAFE_SIZE_T total = member_size;
47 total *= num_members;
48 if (!total.IsValid())
49 return nullptr;
50
51 return GetGeneralPartitionAllocator()
52 .root()
53 ->AllocInline<partition_alloc::AllocFlags::kReturnNull>(
54 total.ValueOrDie(), "GeneralPartition");
55}
56
57void* Calloc(size_t num_members, size_t member_size) {
58 FX_SAFE_SIZE_T total = member_size;
59 total *= num_members;
60 if (!total.IsValid())
61 return nullptr;
62
63 return GetGeneralPartitionAllocator()
64 .root()
65 ->AllocInline<partition_alloc::AllocFlags::kReturnNull |
66 partition_alloc::AllocFlags::kZeroFill>(total.ValueOrDie(),
67 "GeneralPartition");
68}
69
70void* Realloc(void* ptr, size_t num_members, size_t member_size) {
71 FX_SAFE_SIZE_T size = num_members;
72 size *= member_size;
73 if (!size.IsValid())
74 return nullptr;
75
76 return GetGeneralPartitionAllocator()
77 .root()
78 ->Realloc<partition_alloc::AllocFlags::kReturnNull>(
79 ptr, size.ValueOrDie(), "GeneralPartition");
80}
81
82void Dealloc(void* ptr) {
83 // TODO(palmer): Removing this check exposes crashes when PDFium callers
84 // attempt to free |nullptr|. Although libc's |free| allows freeing |NULL|, no
85 // other Partition Alloc callers need this tolerant behavior. Additionally,
86 // checking for |nullptr| adds a branch to |PartitionFree|, and it's nice to
87 // not have to have that.
88 //
89 // So this check is hiding (what I consider to be) bugs, and we should try to
90 // fix them. https://bugs.chromium.org/p/pdfium/issues/detail?id=690
91 if (ptr) {
92 GetGeneralPartitionAllocator().root()->Free(ptr);
93 }
94}
95
96void* StringAlloc(size_t num_members, size_t member_size) {
97 FX_SAFE_SIZE_T total = member_size;
98 total *= num_members;
99 if (!total.IsValid())
100 return nullptr;
101
102 return GetStringPartitionAllocator()
103 .root()
104 ->AllocInline<partition_alloc::AllocFlags::kReturnNull>(
105 total.ValueOrDie(), "StringPartition");
106}
107
108void StringDealloc(void* ptr) {
109 // TODO(palmer): Removing this check exposes crashes when PDFium callers
110 // attempt to free |nullptr|. Although libc's |free| allows freeing |NULL|, no
111 // other Partition Alloc callers need this tolerant behavior. Additionally,
112 // checking for |nullptr| adds a branch to |PartitionFree|, and it's nice to
113 // not have to have that.
114 //
115 // So this check is hiding (what I consider to be) bugs, and we should try to
116 // fix them. https://bugs.chromium.org/p/pdfium/issues/detail?id=690
117 if (ptr) {
118 GetStringPartitionAllocator().root()->Free(ptr);
119 }
120}
121
122} // namespace pdfium::internal
123
125 static bool s_partition_allocators_initialized = false;
126 if (!s_partition_allocators_initialized) {
127 partition_alloc::PartitionAllocGlobalInit(FX_OutOfMemoryTerminate);
128 // These calls force the allocators to be created and initialized (via magic
129 // of static local variables).
130#ifndef V8_ENABLE_SANDBOX
131 GetArrayBufferPartitionAllocator();
132#endif // V8_ENABLE_SANDBOX
133 GetGeneralPartitionAllocator();
134 GetStringPartitionAllocator();
135 s_partition_allocators_initialized = true;
136 }
137}
138
139#ifndef V8_ENABLE_SANDBOX
140void* FX_ArrayBufferAllocate(size_t length) {
141 return GetArrayBufferPartitionAllocator()
142 .root()
143 ->AllocInline<partition_alloc::AllocFlags::kZeroFill>(length,
144 "FXArrayBuffer");
145}
146
148 return GetArrayBufferPartitionAllocator().root()->Alloc(length,
149 "FXArrayBuffer");
150}
151
152void FX_ArrayBufferFree(void* data) {
153 GetArrayBufferPartitionAllocator().root()->Free(data);
154}
155#endif // V8_ENABLE_SANDBOX
void * FX_ArrayBufferAllocate(size_t length)
void FX_InitializeMemoryAllocators()
void FX_ArrayBufferFree(void *data)
void * FX_ArrayBufferAllocateUninitialized(size_t length)
void * Alloc(size_t num_members, size_t member_size)
void * StringAlloc(size_t num_members, size_t member_size)
void * Realloc(void *ptr, size_t num_members, size_t member_size)
void StringDealloc(void *ptr)
void * Calloc(size_t num_members, size_t member_size)
void Dealloc(void *ptr)