MSVC workaround for pending PA non-support.

Merely shim these calls back into malloc to keep the build working.

Change-Id: I2534404328c9878b06df653874b8fbecbf98b95f
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/96630
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 b9895f2..748b9eb 100644
--- a/core/fxcrt/fx_memory.cpp
+++ b/core/fxcrt/fx_memory.cpp
@@ -21,6 +21,12 @@
 #include <windows.h>
 #endif
 
+// TODO(tsepez): remove if/when PartitionAlloc supports MSVC.
+#if !defined(COMPILER_MSVC)
+#define FX_MEMORY_USE_PA
+#endif
+
+#if defined(FX_MEMORY_USE_PA)
 namespace {
 
 pdfium::base::PartitionAllocatorGeneric& GetArrayBufferPartitionAllocator() {
@@ -42,8 +48,10 @@
 }
 
 }  // namespace
+#endif  // defined(FX_MEMORY_USE_PA)
 
 void FX_InitializeMemoryAllocators() {
+#if defined(FX_MEMORY_USE_PA)
   static bool s_partition_allocators_initialized = false;
   if (!s_partition_allocators_initialized) {
     pdfium::base::PartitionAllocGlobalInit(FX_OutOfMemoryTerminate);
@@ -52,6 +60,7 @@
     GetStringPartitionAllocator().init();
     s_partition_allocators_initialized = true;
   }
+#endif  // defined(FX_MEMORY_USE_PA)
 }
 
 void* FXMEM_DefaultAlloc(size_t byte_size) {
@@ -96,11 +105,14 @@
   total *= num_members;
   if (!total.IsValid())
     return nullptr;
-
+#if defined(FX_MEMORY_USE_PA)
   constexpr int kFlags = pdfium::base::PartitionAllocReturnNull;
   return pdfium::base::PartitionAllocGenericFlags(
       GetGeneralPartitionAllocator().root(), kFlags, total.ValueOrDie(),
       "GeneralPartition");
+#else
+  return malloc(total.ValueOrDie());
+#endif
 }
 
 void* AllocOrDie(size_t num_members, size_t member_size) {
@@ -119,6 +131,7 @@
 }
 
 void* Calloc(size_t num_members, size_t member_size) {
+#if defined(FX_MEMORY_USE_PA)
   FX_SAFE_SIZE_T total = member_size;
   total *= num_members;
   if (!total.IsValid())
@@ -129,6 +142,9 @@
   return pdfium::base::PartitionAllocGenericFlags(
       GetGeneralPartitionAllocator().root(), kFlags, total.ValueOrDie(),
       "GeneralPartition");
+#else
+  return calloc(num_members, member_size);
+#endif
 }
 
 void* Realloc(void* ptr, size_t num_members, size_t member_size) {
@@ -137,10 +153,14 @@
   if (!size.IsValid())
     return nullptr;
 
+#if defined(FX_MEMORY_USE_PA)
   return pdfium::base::PartitionReallocGenericFlags(
       GetGeneralPartitionAllocator().root(),
       pdfium::base::PartitionAllocReturnNull, ptr, size.ValueOrDie(),
       "GeneralPartition");
+#else
+  return realloc(ptr, size.ValueOrDie());
+#endif
 }
 
 void* CallocOrDie(size_t num_members, size_t member_size) {
@@ -180,30 +200,53 @@
   if (!total.IsValid())
     return nullptr;
 
+#if defined(FX_MEMORY_USE_PA)
   constexpr int kFlags = pdfium::base::PartitionAllocReturnNull;
   return pdfium::base::PartitionAllocGenericFlags(
       GetStringPartitionAllocator().root(), kFlags, total.ValueOrDie(),
       "StringPartition");
+#else
+  return malloc(total.ValueOrDie());
+#endif
 }
 
 }  // namespace internal
 }  // namespace pdfium
 
 void* FX_ArrayBufferAllocate(size_t length) {
+#if defined(FX_MEMORY_USE_PA)
   return GetArrayBufferPartitionAllocator().root()->AllocFlags(
       pdfium::base::PartitionAllocZeroFill, length, "FXArrayBuffer");
+#else
+  void* result = calloc(length, 1);
+  if (!result)
+    FX_OutOfMemoryTerminate(length);
+  return result;
+#endif
 }
 
 void* FX_ArrayBufferAllocateUninitialized(size_t length) {
+#if defined(FX_MEMORY_USE_PA)
   return GetArrayBufferPartitionAllocator().root()->Alloc(length,
                                                           "FXArrayBuffer");
+#else
+  void* result = malloc(length);
+  if (!result)
+    FX_OutOfMemoryTerminate(length);
+  return result;
+#endif
 }
 
 void FX_ArrayBufferFree(void* data) {
+#if defined(FX_MEMORY_USE_PA)
   GetArrayBufferPartitionAllocator().root()->Free(data);
+#else
+  free(data);
+#endif
 }
 
 void FX_Free(void* ptr) {
+#if defined(FX_MEMORY_USE_PA)
   // TODO(palmer): Removing this check exposes crashes when PDFium callers
   // attempt to free |nullptr|. Although libc's |free| allows freeing |NULL|, no
   // other Partition Alloc callers need this tolerant behavior. Additionally,
@@ -214,4 +257,7 @@
   // fix them. https://bugs.chromium.org/p/pdfium/issues/detail?id=690
   if (ptr)
     pdfium::base::PartitionFree(ptr);
+#else
+  free(ptr);
+#endif
 }