Make FX_Free() a macro

Get consistency with the other FX_* memory macros. Implement
the actual Free() in pdfium::internal namespace. Then avoid
mixing internal/non-internal calls in the memory wrappers,
which was the main motivation for the change.

Change-Id: I6d3966bd6c8509ae958e9c19543989b3951aa312
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/109550
Reviewed-by: Lei Zhang <thestig@chromium.org>
Commit-Queue: Tom Sepez <tsepez@chromium.org>
diff --git a/core/fxcrt/fx_memory.h b/core/fxcrt/fx_memory.h
index 3fb9d12..ac61146 100644
--- a/core/fxcrt/fx_memory.h
+++ b/core/fxcrt/fx_memory.h
@@ -50,8 +50,8 @@
 #define FX_AllocUninit2D(type, w, h) \
   static_cast<type*>(pdfium::internal::AllocOrDie2D(w, h, sizeof(type)))
 
-// FX_Free accepts memory from all of the above.
-void FX_Free(void* ptr);
+// FX_Free frees memory from the above.
+#define FX_Free(ptr) pdfium::internal::Dealloc(ptr)
 
 // String Partition Allocators.
 
@@ -59,7 +59,8 @@
 #define FX_StringAlloc(type, size) \
   static_cast<type*>(pdfium::internal::StringAllocOrDie(size, sizeof(type)))
 
-void FX_StringFree(void* ptr);
+// FX_StringFree frees memory from FX_StringAlloc.
+#define FX_StringFree(ptr) pdfium::internal::StringDealloc(ptr)
 
 #ifndef V8_ENABLE_SANDBOX
 // V8 Array Buffer Partition Allocators.
@@ -86,10 +87,12 @@
 void* CallocOrDie(size_t num_members, size_t member_size);
 void* CallocOrDie2D(size_t w, size_t h, size_t member_size);
 void* ReallocOrDie(void* ptr, size_t num_members, size_t member_size);
+void Dealloc(void* ptr);
 
 // String partition.
 void* StringAlloc(size_t num_members, size_t member_size);
 void* StringAllocOrDie(size_t num_members, size_t member_size);
+void StringDealloc(void* ptr);
 
 }  // namespace internal
 }  // namespace pdfium
diff --git a/core/fxcrt/fx_memory_malloc.cpp b/core/fxcrt/fx_memory_malloc.cpp
index cf6bb7e..e199d42 100644
--- a/core/fxcrt/fx_memory_malloc.cpp
+++ b/core/fxcrt/fx_memory_malloc.cpp
@@ -47,6 +47,10 @@
   return realloc(ptr, total.ValueOrDie());
 }
 
+void Dealloc(void* ptr) {
+  free(ptr);
+}
+
 void* StringAlloc(size_t num_members, size_t member_size) {
   FX_SAFE_SIZE_T total = member_size;
   total *= num_members;
@@ -55,6 +59,10 @@
   return malloc(total.ValueOrDie());
 }
 
+void StringDealloc(void* ptr) {
+  free(ptr);
+}
+
 }  // namespace internal
 }  // namespace pdfium
 
@@ -77,11 +85,3 @@
 void FX_ArrayBufferFree(void* data) {
   free(data);
 }
-
-void FX_Free(void* ptr) {
-  free(ptr);
-}
-
-void FX_StringFree(void* ptr) {
-  free(ptr);
-}
diff --git a/core/fxcrt/fx_memory_pa.cpp b/core/fxcrt/fx_memory_pa.cpp
index 33afece..789f68c 100644
--- a/core/fxcrt/fx_memory_pa.cpp
+++ b/core/fxcrt/fx_memory_pa.cpp
@@ -77,6 +77,20 @@
       "GeneralPartition");
 }
 
+void Dealloc(void* ptr) {
+  // 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,
+  // checking for |nullptr| adds a branch to |PartitionFree|, and it's nice to
+  // not have to have that.
+  //
+  // So this check is hiding (what I consider to be) bugs, and we should try to
+  // fix them. https://bugs.chromium.org/p/pdfium/issues/detail?id=690
+  if (ptr) {
+    GetGeneralPartitionAllocator().root()->Free(ptr);
+  }
+}
+
 void* StringAlloc(size_t num_members, size_t member_size) {
   FX_SAFE_SIZE_T total = member_size;
   total *= num_members;
@@ -88,6 +102,20 @@
       "StringPartition");
 }
 
+void StringDealloc(void* ptr) {
+  // 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,
+  // checking for |nullptr| adds a branch to |PartitionFree|, and it's nice to
+  // not have to have that.
+  //
+  // So this check is hiding (what I consider to be) bugs, and we should try to
+  // fix them. https://bugs.chromium.org/p/pdfium/issues/detail?id=690
+  if (ptr) {
+    GetStringPartitionAllocator().root()->Free(ptr);
+  }
+}
+
 }  // namespace internal
 }  // namespace pdfium
 
@@ -121,31 +149,3 @@
   GetArrayBufferPartitionAllocator().root()->Free(data);
 }
 #endif  // V8_ENABLE_SANDBOX
-
-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
-  // other Partition Alloc callers need this tolerant behavior. Additionally,
-  // checking for |nullptr| adds a branch to |PartitionFree|, and it's nice to
-  // not have to have that.
-  //
-  // So this check is hiding (what I consider to be) bugs, and we should try to
-  // fix them. https://bugs.chromium.org/p/pdfium/issues/detail?id=690
-  if (ptr) {
-    GetGeneralPartitionAllocator().root()->Free(ptr);
-  }
-}
-
-void FX_StringFree(void* ptr) {
-  // 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,
-  // checking for |nullptr| adds a branch to |PartitionFree|, and it's nice to
-  // not have to have that.
-  //
-  // So this check is hiding (what I consider to be) bugs, and we should try to
-  // fix them. https://bugs.chromium.org/p/pdfium/issues/detail?id=690
-  if (ptr) {
-    GetStringPartitionAllocator().root()->Free(ptr);
-  }
-}
diff --git a/core/fxcrt/fx_memory_wrappers.h b/core/fxcrt/fx_memory_wrappers.h
index a1848d8..f4f0b06 100644
--- a/core/fxcrt/fx_memory_wrappers.h
+++ b/core/fxcrt/fx_memory_wrappers.h
@@ -92,8 +92,9 @@
           typename = std::enable_if_t<std::is_arithmetic<T>::value ||
                                       std::is_enum<T>::value ||
                                       IsFXDataPartitionException<T>::value>>
-using FxAllocAllocator =
-    FxPartitionAllocAllocator<T, pdfium::internal::AllocOrDie, FX_Free>;
+using FxAllocAllocator = FxPartitionAllocAllocator<T,
+                                                   pdfium::internal::AllocOrDie,
+                                                   pdfium::internal::Dealloc>;
 
 // Used to put backing store for std::string<> and std::ostringstream<>
 // into the string partition.
@@ -101,6 +102,6 @@
 using FxStringAllocator =
     FxPartitionAllocAllocator<T,
                               pdfium::internal::StringAllocOrDie,
-                              FX_StringFree>;
+                              pdfium::internal::StringDealloc>;
 
 #endif  // CORE_FXCRT_FX_MEMORY_WRAPPERS_H_