Move Fx2DSizeOrDie() to a new fx_2d_size.h header file.

Then make it a template function and try to get inlining.

Change-Id: I3ba409578b6a6a966d6f6446b6794ef2d01b8e7f
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/101691
Commit-Queue: Tom Sepez <tsepez@chromium.org>
Reviewed-by: Lei Zhang <thestig@chromium.org>
diff --git a/core/fpdfapi/page/cpdf_colorspace.cpp b/core/fpdfapi/page/cpdf_colorspace.cpp
index 8104ef3..0b550f5 100644
--- a/core/fpdfapi/page/cpdf_colorspace.cpp
+++ b/core/fpdfapi/page/cpdf_colorspace.cpp
@@ -33,7 +33,7 @@
 #include "core/fpdfapi/parser/fpdf_parser_utility.h"
 #include "core/fxcodec/fx_codec.h"
 #include "core/fxcrt/data_vector.h"
-#include "core/fxcrt/fx_memory.h"
+#include "core/fxcrt/fx_2d_size.h"
 #include "core/fxcrt/fx_memory_wrappers.h"
 #include "core/fxcrt/fx_safe_types.h"
 #include "core/fxcrt/maybe_owned.h"
diff --git a/core/fpdfapi/page/cpdf_expintfunc.cpp b/core/fpdfapi/page/cpdf_expintfunc.cpp
index b12cf43..a8de8b9 100644
--- a/core/fpdfapi/page/cpdf_expintfunc.cpp
+++ b/core/fpdfapi/page/cpdf_expintfunc.cpp
@@ -12,7 +12,7 @@
 #include "core/fpdfapi/parser/cpdf_dictionary.h"
 #include "core/fpdfapi/parser/cpdf_number.h"
 #include "core/fxcrt/data_vector.h"
-#include "core/fxcrt/fx_memory.h"
+#include "core/fxcrt/fx_2d_size.h"
 #include "core/fxcrt/fx_safe_types.h"
 #include "core/fxcrt/stl_util.h"
 
diff --git a/core/fpdfapi/page/cpdf_image.cpp b/core/fpdfapi/page/cpdf_image.cpp
index e2b3fa2..c9be231 100644
--- a/core/fpdfapi/page/cpdf_image.cpp
+++ b/core/fpdfapi/page/cpdf_image.cpp
@@ -27,7 +27,7 @@
 #include "core/fpdfapi/parser/cpdf_string.h"
 #include "core/fxcodec/jpeg/jpegmodule.h"
 #include "core/fxcrt/data_vector.h"
-#include "core/fxcrt/fx_memory.h"
+#include "core/fxcrt/fx_2d_size.h"
 #include "core/fxcrt/fx_memory_wrappers.h"
 #include "core/fxcrt/fx_stream.h"
 #include "core/fxcrt/span_util.h"
diff --git a/core/fpdfapi/page/cpdf_indexedcs.cpp b/core/fpdfapi/page/cpdf_indexedcs.cpp
index 32a36bd..85f694f 100644
--- a/core/fpdfapi/page/cpdf_indexedcs.cpp
+++ b/core/fpdfapi/page/cpdf_indexedcs.cpp
@@ -16,7 +16,7 @@
 #include "core/fpdfapi/parser/cpdf_stream_acc.h"
 #include "core/fpdfapi/parser/cpdf_string.h"
 #include "core/fxcrt/data_vector.h"
-#include "core/fxcrt/fx_memory.h"
+#include "core/fxcrt/fx_2d_size.h"
 #include "core/fxcrt/fx_safe_types.h"
 #include "core/fxcrt/retain_ptr.h"
 #include "third_party/base/check_op.h"
diff --git a/core/fxcodec/fax/faxmodule.cpp b/core/fxcodec/fax/faxmodule.cpp
index 5ec3725..075c72d 100644
--- a/core/fxcodec/fax/faxmodule.cpp
+++ b/core/fxcodec/fax/faxmodule.cpp
@@ -17,6 +17,7 @@
 #include "core/fxcodec/scanlinedecoder.h"
 #include "core/fxcrt/binary_buffer.h"
 #include "core/fxcrt/data_vector.h"
+#include "core/fxcrt/fx_2d_size.h"
 #include "core/fxcrt/fx_memory.h"
 #include "core/fxge/calculate_pitch.h"
 #include "third_party/base/check.h"
diff --git a/core/fxcrt/BUILD.gn b/core/fxcrt/BUILD.gn
index 923afe2..9fab012 100644
--- a/core/fxcrt/BUILD.gn
+++ b/core/fxcrt/BUILD.gn
@@ -44,6 +44,7 @@
     "fixed_try_alloc_zeroed_data_vector.h",
     "fixed_uninit_data_vector.h",
     "fixed_zeroed_data_vector.h",
+    "fx_2d_size.h",
     "fx_bidi.cpp",
     "fx_bidi.h",
     "fx_codepage.cpp",
diff --git a/core/fxcrt/fx_2d_size.h b/core/fxcrt/fx_2d_size.h
new file mode 100644
index 0000000..9b7f5d1
--- /dev/null
+++ b/core/fxcrt/fx_2d_size.h
@@ -0,0 +1,17 @@
+// Copyright 2022 The PDFium Authors
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef CORE_FXCRT_FX_2D_SIZE_H_
+#define CORE_FXCRT_FX_2D_SIZE_H_
+
+#include "core/fxcrt/fx_safe_types.h"
+
+template <typename T, typename U>
+size_t Fx2DSizeOrDie(const T& w, const U& h) {
+  FX_SAFE_SIZE_T safe_size = w;
+  safe_size *= h;
+  return safe_size.ValueOrDie();
+}
+
+#endif  // CORE_FXCRT_FX_2D_SIZE_H_
diff --git a/core/fxcrt/fx_memory.cpp b/core/fxcrt/fx_memory.cpp
index 5c28349..7b6aa34 100644
--- a/core/fxcrt/fx_memory.cpp
+++ b/core/fxcrt/fx_memory.cpp
@@ -8,12 +8,11 @@
 
 #include <stdlib.h>  // For abort().
 
+#include <iterator>
 #include <limits>
 
 #include "build/build_config.h"
-#include "core/fxcrt/fx_safe_types.h"
 #include "third_party/base/debug/alias.h"
-#include "third_party/base/numerics/safe_math.h"
 
 #if BUILDFLAG(IS_WIN)
 #include <windows.h>
@@ -102,9 +101,3 @@
 }
 }  // namespace internal
 }  // namespace pdfium
-
-size_t Fx2DSizeOrDie(size_t w, size_t h) {
-  pdfium::base::CheckedNumeric<size_t> safe_size = w;
-  safe_size *= h;
-  return safe_size.ValueOrDie();
-}
diff --git a/core/fxcrt/fx_memory.h b/core/fxcrt/fx_memory.h
index 9022048..8157eb2 100644
--- a/core/fxcrt/fx_memory.h
+++ b/core/fxcrt/fx_memory.h
@@ -106,8 +106,6 @@
   return (size + (N - 1)) & ~(N - 1);
 }
 
-size_t Fx2DSizeOrDie(size_t w, size_t h);
-
 #endif  // __cplusplus
 
 #endif  // CORE_FXCRT_FX_MEMORY_H_
diff --git a/core/fxge/skia/fx_skia_device.cpp b/core/fxge/skia/fx_skia_device.cpp
index f9a2f78..4187348 100644
--- a/core/fxge/skia/fx_skia_device.cpp
+++ b/core/fxge/skia/fx_skia_device.cpp
@@ -25,7 +25,7 @@
 #include "core/fpdfapi/parser/cpdf_stream_acc.h"
 #include "core/fxcrt/cfx_bitstream.h"
 #include "core/fxcrt/data_vector.h"
-#include "core/fxcrt/fx_memory.h"
+#include "core/fxcrt/fx_2d_size.h"
 #include "core/fxcrt/fx_memory_wrappers.h"
 #include "core/fxcrt/fx_system.h"
 #include "core/fxcrt/stl_util.h"
diff --git a/fxbarcode/datamatrix/BC_DefaultPlacement.cpp b/fxbarcode/datamatrix/BC_DefaultPlacement.cpp
index be97e69..4e772dd 100644
--- a/fxbarcode/datamatrix/BC_DefaultPlacement.cpp
+++ b/fxbarcode/datamatrix/BC_DefaultPlacement.cpp
@@ -27,7 +27,7 @@
 #include <utility>
 
 #include "core/fxcrt/data_vector.h"
-#include "core/fxcrt/fx_memory.h"
+#include "core/fxcrt/fx_2d_size.h"
 #include "fxbarcode/datamatrix/BC_Encoder.h"
 #include "third_party/base/check_op.h"
 
diff --git a/fxbarcode/oned/BC_OnedCodaBarWriter.cpp b/fxbarcode/oned/BC_OnedCodaBarWriter.cpp
index 864e20a..6be75ad 100644
--- a/fxbarcode/oned/BC_OnedCodaBarWriter.cpp
+++ b/fxbarcode/oned/BC_OnedCodaBarWriter.cpp
@@ -24,8 +24,8 @@
 
 #include <iterator>
 
+#include "core/fxcrt/fx_2d_size.h"
 #include "core/fxcrt/fx_extension.h"
-#include "core/fxcrt/fx_memory.h"
 #include "fxbarcode/BC_Writer.h"
 #include "fxbarcode/common/BC_CommonBitMatrix.h"
 #include "fxbarcode/oned/BC_OneDimWriter.h"