7#include "core/fxcrt/fx_memory.h"
9#include "core/fxcrt/fx_safe_types.h"
10#include "partition_alloc/partition_alloc.h"
11#include "third_party/base/no_destructor.h"
13#if !defined(PDF_USE_PARTITION_ALLOC)
14#error "File compiled under wrong build option."
19constexpr partition_alloc::PartitionOptions kOptions = {};
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;
29partition_alloc::PartitionAllocator& GetGeneralPartitionAllocator() {
30 static pdfium::base::NoDestructor<partition_alloc::PartitionAllocator>
31 s_general_allocator(kOptions);
32 return *s_general_allocator;
35partition_alloc::PartitionAllocator& GetStringPartitionAllocator() {
36 static pdfium::base::NoDestructor<partition_alloc::PartitionAllocator>
37 s_string_allocator(kOptions);
38 return *s_string_allocator;
45void*
Alloc(size_t num_members, size_t member_size) {
46 FX_SAFE_SIZE_T total = member_size;
51 return GetGeneralPartitionAllocator()
53 ->AllocInline<partition_alloc::AllocFlags::kReturnNull>(
54 total.ValueOrDie(),
"GeneralPartition");
57void*
Calloc(size_t num_members, size_t member_size) {
58 FX_SAFE_SIZE_T total = member_size;
63 return GetGeneralPartitionAllocator()
65 ->AllocInline<partition_alloc::AllocFlags::kReturnNull |
66 partition_alloc::AllocFlags::kZeroFill>(total.ValueOrDie(),
70void*
Realloc(
void* ptr, size_t num_members, size_t member_size) {
71 FX_SAFE_SIZE_T size = num_members;
76 return GetGeneralPartitionAllocator()
78 ->Realloc<partition_alloc::AllocFlags::kReturnNull>(
79 ptr, size.ValueOrDie(),
"GeneralPartition");
92 GetGeneralPartitionAllocator().root()->Free(ptr);
96void*
StringAlloc(size_t num_members, size_t member_size) {
97 FX_SAFE_SIZE_T total = member_size;
102 return GetStringPartitionAllocator()
104 ->AllocInline<partition_alloc::AllocFlags::kReturnNull>(
105 total.ValueOrDie(),
"StringPartition");
118 GetStringPartitionAllocator().root()->Free(ptr);
125 static bool s_partition_allocators_initialized =
false;
126 if (!s_partition_allocators_initialized) {
127 partition_alloc::PartitionAllocGlobalInit(FX_OutOfMemoryTerminate);
130#ifndef V8_ENABLE_SANDBOX
131 GetArrayBufferPartitionAllocator();
133 GetGeneralPartitionAllocator();
134 GetStringPartitionAllocator();
135 s_partition_allocators_initialized =
true;
139#ifndef V8_ENABLE_SANDBOX
141 return GetArrayBufferPartitionAllocator()
143 ->AllocInline<partition_alloc::AllocFlags::kZeroFill>(length,
148 return GetArrayBufferPartitionAllocator().root()->Alloc(length,
153 GetArrayBufferPartitionAllocator().root()->Free(data);
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)