Fully encapsulate Partition Alloc in fx_memory.cpp

Avoid changes to fxjs and/or header files when changing allocators.

Change-Id: Ia6f801ce15d02ad5fb4f442e976a209f52dd3939
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/96550
Reviewed-by: Lei Zhang <thestig@chromium.org>
Commit-Queue: Tom Sepez <tsepez@chromium.org>
diff --git a/core/fxcrt/fx_memory.cpp b/core/fxcrt/fx_memory.cpp
index 7dfc251..ab1a774 100644
--- a/core/fxcrt/fx_memory.cpp
+++ b/core/fxcrt/fx_memory.cpp
@@ -21,6 +21,8 @@
 #include <windows.h>
 #endif
 
+namespace {
+
 pdfium::base::PartitionAllocatorGeneric& GetArrayBufferPartitionAllocator() {
   static pdfium::base::NoDestructor<pdfium::base::PartitionAllocatorGeneric>
       s_array_buffer_allocator;
@@ -39,6 +41,8 @@
   return *s_string_allocator;
 }
 
+}  // namespace
+
 void FXMEM_InitializePartitionAlloc() {
   static bool s_partition_allocators_initialized = false;
   if (!s_partition_allocators_initialized) {
@@ -190,6 +194,20 @@
 }  // namespace internal
 }  // namespace pdfium
 
+void* FX_ArrayBufferAllocate(size_t length) {
+  return GetArrayBufferPartitionAllocator().root()->AllocFlags(
+      pdfium::base::PartitionAllocZeroFill, length, "FXArrayBuffer");
+}
+
+void* FX_ArrayBufferAllocateUninitialized(size_t length) {
+  return GetArrayBufferPartitionAllocator().root()->Alloc(length,
+                                                          "FXArrayBuffer");
+}
+
+void FX_ArrayBufferFree(void* data) {
+  GetArrayBufferPartitionAllocator().root()->Free(data);
+}
+
 void FX_Free(void* ptr) {
   // TODO(palmer): Removing this check exposes crashes when PDFium callers
   // attempt to free |nullptr|. Although libc's |free| allows freeing |NULL|, no
diff --git a/core/fxcrt/fx_memory.h b/core/fxcrt/fx_memory.h
index d9bf9c9..4d298c7 100644
--- a/core/fxcrt/fx_memory.h
+++ b/core/fxcrt/fx_memory.h
@@ -24,16 +24,6 @@
 
 #include "third_party/base/compiler_specific.h"
 
-namespace pdfium {
-namespace base {
-class PartitionAllocatorGeneric;
-}  // namespace base
-}  // namespace pdfium
-
-pdfium::base::PartitionAllocatorGeneric& GetArrayBufferPartitionAllocator();
-pdfium::base::PartitionAllocatorGeneric& GetGeneralPartitionAllocator();
-pdfium::base::PartitionAllocatorGeneric& GetStringPartitionAllocator();
-
 void FXMEM_InitializePartitionAlloc();
 NOINLINE void FX_OutOfMemoryTerminate(size_t size);
 
@@ -66,9 +56,20 @@
 #define FX_StringAlloc(type, size) \
   static_cast<type*>(pdfium::internal::StringAllocOrDie(size, sizeof(type)))
 
-// Free accepts memory from all of the above.
+// FX_Free accepts memory from all of the above.
 void FX_Free(void* ptr);
 
+// V8 Array Buffer Partition Allocators.
+
+// This never returns nullptr, and returns zeroed memory.
+void* FX_ArrayBufferAllocate(size_t length);
+
+// This never returns nullptr, but returns uninitialized memory.
+void* FX_ArrayBufferAllocateUninitialized(size_t length);
+
+// FX_ArrayBufferFree accepts memory from both of the above.
+void FX_ArrayBufferFree(void* data);
+
 namespace pdfium {
 namespace internal {
 
diff --git a/fxjs/cfx_v8_array_buffer_allocator.cpp b/fxjs/cfx_v8_array_buffer_allocator.cpp
index e7ebbc1..4c0eb2d 100644
--- a/fxjs/cfx_v8_array_buffer_allocator.cpp
+++ b/fxjs/cfx_v8_array_buffer_allocator.cpp
@@ -7,22 +7,19 @@
 #include "fxjs/cfx_v8_array_buffer_allocator.h"
 
 #include "core/fxcrt/fx_memory.h"
-#include "third_party/base/allocator/partition_allocator/partition_alloc.h"
 
 void* CFX_V8ArrayBufferAllocator::Allocate(size_t length) {
   if (length > kMaxAllowedBytes)
     return nullptr;
-  return GetArrayBufferPartitionAllocator().root()->AllocFlags(
-      pdfium::base::PartitionAllocZeroFill, length, "CFX_V8ArrayBuffer");
+  return FX_ArrayBufferAllocate(length);
 }
 
 void* CFX_V8ArrayBufferAllocator::AllocateUninitialized(size_t length) {
   if (length > kMaxAllowedBytes)
     return nullptr;
-  return GetArrayBufferPartitionAllocator().root()->Alloc(length,
-                                                          "CFX_V8ArrayBuffer");
+  return FX_ArrayBufferAllocateUninitialized(length);
 }
 
 void CFX_V8ArrayBufferAllocator::Free(void* data, size_t length) {
-  GetArrayBufferPartitionAllocator().root()->Free(data);
+  FX_ArrayBufferFree(data);
 }