Remove CFX_FixedBufGrow

This Cl replaces the CFX_FixedBufGrow class with std::vector.

Change-Id: I85c85b7a8de4794840b561e09841bb464cfa9dfe
Reviewed-on: https://pdfium-review.googlesource.com/3138
Reviewed-by: Tom Sepez <tsepez@chromium.org>
Commit-Queue: dsinclair <dsinclair@chromium.org>
diff --git a/core/fpdfapi/page/cpdf_colorspace.cpp b/core/fpdfapi/page/cpdf_colorspace.cpp
index 3244319..428ab0e 100644
--- a/core/fpdfapi/page/cpdf_colorspace.cpp
+++ b/core/fpdfapi/page/cpdf_colorspace.cpp
@@ -10,6 +10,7 @@
 #include <limits>
 #include <memory>
 #include <utility>
+#include <vector>
 
 #include "core/fpdfapi/cpdf_modulemgr.h"
 #include "core/fpdfapi/page/cpdf_docpagedata.h"
@@ -540,16 +541,16 @@
                                          int image_width,
                                          int image_height,
                                          bool bTransMask) const {
-  CFX_FixedBufGrow<float, 16> srcbuf(m_nComponents);
-  float* src = srcbuf;
-  float R;
-  float G;
-  float B;
+  std::vector<float> src(std::max(16U, m_nComponents));
   const int divisor = m_Family != PDFCS_INDEXED ? 255 : 1;
   for (int i = 0; i < pixels; i++) {
     for (uint32_t j = 0; j < m_nComponents; j++)
       src[j] = static_cast<float>(*src_buf++) / divisor;
-    GetRGB(src, &R, &G, &B);
+
+    float R;
+    float G;
+    float B;
+    GetRGB(src.data(), &R, &G, &B);
     *dest_buf++ = static_cast<int32_t>(B * 255);
     *dest_buf++ = static_cast<int32_t>(G * 255);
     *dest_buf++ = static_cast<int32_t>(R * 255);
@@ -1100,15 +1101,15 @@
       return false;
     }
   }
-  CFX_FixedBufGrow<float, 16> Comps(m_nBaseComponents);
-  float* comps = Comps;
+
+  std::vector<float> comps(std::max(16, m_nBaseComponents));
   const uint8_t* pTable = m_Table.raw_str();
   for (int i = 0; i < m_nBaseComponents; i++) {
     comps[i] =
         m_pCompMinMax[i * 2] +
         m_pCompMinMax[i * 2 + 1] * pTable[index * m_nBaseComponents + i] / 255;
   }
-  return m_pBaseCS->GetRGB(comps, R, G, B);
+  return m_pBaseCS->GetRGB(comps.data(), R, G, B);
 }
 
 CPDF_ColorSpace* CPDF_IndexedCS::GetBaseCS() const {
@@ -1221,20 +1222,20 @@
       return false;
 
     int nComps = m_pAltCS->CountComponents();
-    CFX_FixedBufGrow<float, 16> results(nComps);
+    std::vector<float> results(std::max(16, nComps));
     for (int i = 0; i < nComps; i++)
       results[i] = *pBuf;
-    return m_pAltCS->GetRGB(results, R, G, B);
+    return m_pAltCS->GetRGB(results.data(), R, G, B);
   }
 
-  CFX_FixedBufGrow<float, 16> results(m_pFunc->CountOutputs());
+  std::vector<float> results(std::max(16U, m_pFunc->CountOutputs()));
   int nresults = 0;
-  m_pFunc->Call(pBuf, 1, results, &nresults);
+  m_pFunc->Call(pBuf, 1, results.data(), &nresults);
   if (nresults == 0)
     return false;
 
   if (m_pAltCS)
-    return m_pAltCS->GetRGB(results, R, G, B);
+    return m_pAltCS->GetRGB(results.data(), R, G, B);
 
   R = 0;
   G = 0;
@@ -1284,13 +1285,13 @@
   if (!m_pFunc)
     return false;
 
-  CFX_FixedBufGrow<float, 16> results(m_pFunc->CountOutputs());
+  std::vector<float> results(std::max(16U, m_pFunc->CountOutputs()));
   int nresults = 0;
-  m_pFunc->Call(pBuf, m_nComponents, results, &nresults);
+  m_pFunc->Call(pBuf, m_nComponents, results.data(), &nresults);
   if (nresults == 0)
     return false;
 
-  return m_pAltCS->GetRGB(results, R, G, B);
+  return m_pAltCS->GetRGB(results.data(), R, G, B);
 }
 
 void CPDF_DeviceNCS::EnableStdConversion(bool bEnabled) {
diff --git a/core/fpdfapi/page/fpdf_page_func.cpp b/core/fpdfapi/page/fpdf_page_func.cpp
index 30f3b89..b12b286 100644
--- a/core/fpdfapi/page/fpdf_page_func.cpp
+++ b/core/fpdfapi/page/fpdf_page_func.cpp
@@ -529,11 +529,9 @@
 
 bool CPDF_SampledFunc::v_Call(float* inputs, float* results) const {
   int pos = 0;
-  CFX_FixedBufGrow<float, 16> encoded_input_buf(m_nInputs);
-  float* encoded_input = encoded_input_buf;
-  CFX_FixedBufGrow<uint32_t, 32> int_buf(m_nInputs * 2);
-  uint32_t* index = int_buf;
-  uint32_t* blocksize = index + m_nInputs;
+  std::vector<float> encoded_input(std::max(16U, m_nInputs));
+  std::vector<uint32_t> index(std::max(32U, m_nInputs * 2));
+  uint32_t* blocksize = index.data() + m_nInputs;
   for (uint32_t i = 0; i < m_nInputs; i++) {
     if (i == 0)
       blocksize[i] = 1;
diff --git a/core/fpdfapi/render/cpdf_dibsource.cpp b/core/fpdfapi/render/cpdf_dibsource.cpp
index 899a783..27f59c0 100644
--- a/core/fpdfapi/render/cpdf_dibsource.cpp
+++ b/core/fpdfapi/render/cpdf_dibsource.cpp
@@ -795,8 +795,7 @@
       m_bpc == 8 && m_bDefaultDecode) {
   } else {
     int palette_count = 1 << (m_bpc * m_nComponents);
-    CFX_FixedBufGrow<float, 16> color_values(m_nComponents);
-    float* color_value = color_values;
+    std::vector<float> color_value(std::max(16U, m_nComponents));
     for (int i = 0; i < palette_count; i++) {
       int color_data = i;
       for (uint32_t j = 0; j < m_nComponents; j++) {
@@ -811,11 +810,11 @@
         int nComponents = m_pColorSpace->CountComponents();
         std::vector<float> temp_buf(nComponents);
         for (int k = 0; k < nComponents; k++) {
-          temp_buf[k] = *color_value;
+          temp_buf[k] = color_value[0];
         }
         m_pColorSpace->GetRGB(temp_buf.data(), &R, &G, &B);
       } else {
-        m_pColorSpace->GetRGB(color_value, &R, &G, &B);
+        m_pColorSpace->GetRGB(color_value.data(), &R, &G, &B);
       }
       SetPaletteArgb(i, ArgbEncode(255, FXSYS_round(R * 255),
                                    FXSYS_round(G * 255), FXSYS_round(B * 255)));
@@ -915,8 +914,7 @@
     }
   }
 
-  CFX_FixedBufGrow<float, 16> color_values1(m_nComponents);
-  float* color_values = color_values1;
+  std::vector<float> color_values(std::max(16U, m_nComponents));
   float R = 0.0f;
   float G = 0.0f;
   float B = 0.0f;
@@ -935,7 +933,7 @@
         G = (1.0f - color_values[1]) * k;
         B = (1.0f - color_values[2]) * k;
       } else {
-        m_pColorSpace->GetRGB(color_values, &R, &G, &B);
+        m_pColorSpace->GetRGB(color_values.data(), &R, &G, &B);
       }
       R = ClampValue(R, 1.0f);
       G = ClampValue(G, 1.0f);
@@ -961,7 +959,7 @@
         G = (1.0f - color_values[1]) * k;
         B = (1.0f - color_values[2]) * k;
       } else {
-        m_pColorSpace->GetRGB(color_values, &R, &G, &B);
+        m_pColorSpace->GetRGB(color_values.data(), &R, &G, &B);
       }
       R = ClampValue(R, 1.0f);
       G = ClampValue(G, 1.0f);
@@ -1344,7 +1342,7 @@
     if (src_x == last_src_x) {
       argb = last_argb;
     } else {
-      CFX_FixedBufGrow<uint8_t, 128> extracted_components(m_nComponents);
+      std::vector<uint8_t> extracted_components(std::max(128U, m_nComponents));
       const uint8_t* pSrcPixel = nullptr;
       if (m_bpc % 8 != 0) {
         // No need to check for 32-bit overflow, as |src_x| is bounded by
@@ -1358,13 +1356,13 @@
               GetBits8(pSrcPixel, src_bit_pos, m_bpc) * unit_To8Bpc);
           src_bit_pos += m_bpc;
         }
-        pSrcPixel = extracted_components;
+        pSrcPixel = extracted_components.data();
       } else {
         pSrcPixel = pSrcLine + src_x * orig_Bpp;
         if (m_bpc == 16) {
           for (uint32_t j = 0; j < m_nComponents; ++j)
             extracted_components[j] = pSrcPixel[j * 2];
-          pSrcPixel = extracted_components;
+          pSrcPixel = extracted_components.data();
         }
       }
 
@@ -1385,8 +1383,8 @@
             extracted_components[j] =
                 color_value > 255 ? 255 : (color_value < 0 ? 0 : color_value);
           }
-          m_pColorSpace->TranslateImageLine(color, extracted_components, 1, 0,
-                                            0, bTransMask);
+          m_pColorSpace->TranslateImageLine(color, extracted_components.data(),
+                                            1, 0, 0, bTransMask);
         }
         argb = FXARGB_MAKE(0xFF, color[2], color[1], color[0]);
       } else {
diff --git a/core/fpdfapi/render/cpdf_renderstatus.cpp b/core/fpdfapi/render/cpdf_renderstatus.cpp
index 17576fc..a40a25c 100644
--- a/core/fpdfapi/render/cpdf_renderstatus.cpp
+++ b/core/fpdfapi/render/cpdf_renderstatus.cpp
@@ -135,9 +135,8 @@
   matrix.SetReverse(*pObject2Bitmap);
   uint32_t total_results =
       std::max(CountOutputs(funcs), pCS->CountComponents());
-  CFX_FixedBufGrow<float, 16> result_array(total_results);
-  float* pResults = result_array;
-  FXSYS_memset(pResults, 0, total_results * sizeof(float));
+  std::vector<float> pResults(std::max(16U, total_results));
+  FXSYS_memset(pResults.data(), 0, total_results * sizeof(float));
   uint32_t rgb_array[SHADING_STEPS];
   for (int i = 0; i < SHADING_STEPS; i++) {
     float input = (t_max - t_min) * i / SHADING_STEPS + t_min;
@@ -145,14 +144,14 @@
     for (const auto& func : funcs) {
       if (func) {
         int nresults = 0;
-        if (func->Call(&input, 1, pResults + offset, &nresults))
+        if (func->Call(&input, 1, pResults.data() + offset, &nresults))
           offset += nresults;
       }
     }
     float R = 0.0f;
     float G = 0.0f;
     float B = 0.0f;
-    pCS->GetRGB(pResults, &R, &G, &B);
+    pCS->GetRGB(pResults.data(), &R, &G, &B);
     rgb_array[i] =
         FXARGB_TODIB(FXARGB_MAKE(alpha, FXSYS_round(R * 255),
                                  FXSYS_round(G * 255), FXSYS_round(B * 255)));
@@ -218,9 +217,8 @@
   }
   uint32_t total_results =
       std::max(CountOutputs(funcs), pCS->CountComponents());
-  CFX_FixedBufGrow<float, 16> result_array(total_results);
-  float* pResults = result_array;
-  FXSYS_memset(pResults, 0, total_results * sizeof(float));
+  std::vector<float> pResults(std::max(16U, total_results));
+  FXSYS_memset(pResults.data(), 0, total_results * sizeof(float));
   uint32_t rgb_array[SHADING_STEPS];
   for (int i = 0; i < SHADING_STEPS; i++) {
     float input = (t_max - t_min) * i / SHADING_STEPS + t_min;
@@ -228,14 +226,14 @@
     for (const auto& func : funcs) {
       if (func) {
         int nresults;
-        if (func->Call(&input, 1, pResults + offset, &nresults))
+        if (func->Call(&input, 1, pResults.data() + offset, &nresults))
           offset += nresults;
       }
     }
     float R = 0.0f;
     float G = 0.0f;
     float B = 0.0f;
-    pCS->GetRGB(pResults, &R, &G, &B);
+    pCS->GetRGB(pResults.data(), &R, &G, &B);
     rgb_array[i] =
         FXARGB_TODIB(FXARGB_MAKE(alpha, FXSYS_round(R * 255),
                                  FXSYS_round(G * 255), FXSYS_round(B * 255)));
@@ -343,9 +341,8 @@
   int pitch = pBitmap->GetPitch();
   uint32_t total_results =
       std::max(CountOutputs(funcs), pCS->CountComponents());
-  CFX_FixedBufGrow<float, 16> result_array(total_results);
-  float* pResults = result_array;
-  FXSYS_memset(pResults, 0, total_results * sizeof(float));
+  std::vector<float> pResults(std::max(16U, total_results));
+  FXSYS_memset(pResults.data(), 0, total_results * sizeof(float));
   for (int row = 0; row < height; row++) {
     uint32_t* dib_buf = (uint32_t*)(pBitmap->GetBuffer() + row * pitch);
     for (int column = 0; column < width; column++) {
@@ -359,7 +356,7 @@
       for (const auto& func : funcs) {
         if (func) {
           int nresults;
-          if (func->Call(input, 2, pResults + offset, &nresults))
+          if (func->Call(input, 2, pResults.data() + offset, &nresults))
             offset += nresults;
         }
       }
@@ -367,7 +364,7 @@
       float R = 0.0f;
       float G = 0.0f;
       float B = 0.0f;
-      pCS->GetRGB(pResults, &R, &G, &B);
+      pCS->GetRGB(pResults.data(), &R, &G, &B);
       dib_buf[column] = FXARGB_TODIB(FXARGB_MAKE(
           alpha, (int32_t)(R * 255), (int32_t)(G * 255), (int32_t)(B * 255)));
     }
@@ -2042,13 +2039,13 @@
     CPDF_Array* pBackColor = pDict->GetArrayFor("Background");
     if (pBackColor &&
         pBackColor->GetCount() >= pColorSpace->CountComponents()) {
-      CFX_FixedBufGrow<float, 16> comps(pColorSpace->CountComponents());
+      std::vector<float> comps(std::max(16U, pColorSpace->CountComponents()));
       for (uint32_t i = 0; i < pColorSpace->CountComponents(); i++)
         comps[i] = pBackColor->GetNumberAt(i);
       float R = 0.0f;
       float G = 0.0f;
       float B = 0.0f;
-      pColorSpace->GetRGB(comps, &R, &G, &B);
+      pColorSpace->GetRGB(comps.data(), &R, &G, &B);
       background = ArgbEncode(255, (int32_t)(R * 255), (int32_t)(G * 255),
                               (int32_t)(B * 255));
     }
@@ -2576,22 +2573,21 @@
 
         float R, G, B;
         uint32_t comps = 8;
-        if (pCS->CountComponents() > comps) {
+        if (pCS->CountComponents() > comps)
           comps = pCS->CountComponents();
-        }
-        CFX_FixedBufGrow<float, 8> float_array(comps);
-        float* pFloats = float_array;
+
+        std::vector<float> pFloats(std::max(8U, comps));
         FX_SAFE_UINT32 num_floats = comps;
         num_floats *= sizeof(float);
-        if (!num_floats.IsValid()) {
+        if (!num_floats.IsValid())
           return nullptr;
-        }
-        FXSYS_memset(pFloats, 0, num_floats.ValueOrDie());
+
+        FXSYS_memset(pFloats.data(), 0, num_floats.ValueOrDie());
         size_t count = pBC->GetCount() > 8 ? 8 : pBC->GetCount();
         for (size_t i = 0; i < count; i++) {
           pFloats[i] = pBC->GetNumberAt(i);
         }
-        pCS->GetRGB(pFloats, &R, &G, &B);
+        pCS->GetRGB(pFloats.data(), &R, &G, &B);
         back_color = 0xff000000 | ((int32_t)(R * 255) << 16) |
                      ((int32_t)(G * 255) << 8) | (int32_t)(B * 255);
         m_pContext->GetDocument()->GetPageData()->ReleaseColorSpace(pCSObj);
@@ -2623,11 +2619,11 @@
   int src_pitch = bitmap.GetPitch();
   std::vector<uint8_t> transfers(256);
   if (pFunc) {
-    CFX_FixedBufGrow<float, 16> results(pFunc->CountOutputs());
+    std::vector<float> results(std::max(16U, pFunc->CountOutputs()));
     for (int i = 0; i < 256; i++) {
       float input = (float)i / 255.0f;
       int nresult;
-      pFunc->Call(&input, 1, results, &nresult);
+      pFunc->Call(&input, 1, results.data(), &nresult);
       transfers[i] = FXSYS_round(results[0] * 255);
     }
   } else {
diff --git a/core/fxcodec/codec/fx_codec_icc.cpp b/core/fxcodec/codec/fx_codec_icc.cpp
index b143dcc..cb89584 100644
--- a/core/fxcodec/codec/fx_codec_icc.cpp
+++ b/core/fxcodec/codec/fx_codec_icc.cpp
@@ -4,6 +4,9 @@
 
 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
 
+#include <algorithm>
+#include <vector>
+
 #include "core/fxcodec/codec/codec_int.h"
 #include "core/fxcodec/fx_codec.h"
 #include "third_party/lcms2-2.6/include/lcms2.h"
@@ -165,14 +168,12 @@
   CLcmsCmm* p = (CLcmsCmm*)pTransform;
   uint8_t output[4];
   if (p->m_bLab) {
-    CFX_FixedBufGrow<double, 16> inputs(nSrcComponents);
-    double* input = inputs;
+    std::vector<double> input(std::max(16U, nSrcComponents));
     for (uint32_t i = 0; i < nSrcComponents; i++)
       input[i] = pSrcValues[i];
-    cmsDoTransform(p->m_hTransform, input, output, 1);
+    cmsDoTransform(p->m_hTransform, input.data(), output, 1);
   } else {
-    CFX_FixedBufGrow<uint8_t, 16> inputs(nSrcComponents);
-    uint8_t* input = inputs;
+    std::vector<uint8_t> input(std::max(16U, nSrcComponents));
     for (uint32_t i = 0; i < nSrcComponents; i++) {
       if (pSrcValues[i] > 1.0f)
         input[i] = 255;
@@ -181,7 +182,7 @@
       else
         input[i] = static_cast<int>(pSrcValues[i] * 255.0f);
     }
-    cmsDoTransform(p->m_hTransform, input, output, 1);
+    cmsDoTransform(p->m_hTransform, input.data(), output, 1);
   }
   switch (p->m_nDstComponents) {
     case 1:
diff --git a/core/fxcrt/fx_basic.h b/core/fxcrt/fx_basic.h
index 076766a..2786918 100644
--- a/core/fxcrt/fx_basic.h
+++ b/core/fxcrt/fx_basic.h
@@ -317,23 +317,6 @@
   }
 };
 
-template <class DataType, int FixedSize>
-class CFX_FixedBufGrow {
- public:
-  explicit CFX_FixedBufGrow(int data_size) {
-    if (data_size > FixedSize) {
-      m_pGrowData.reset(FX_Alloc(DataType, data_size));
-      return;
-    }
-    FXSYS_memset(m_FixedData, 0, sizeof(DataType) * FixedSize);
-  }
-  operator DataType*() { return m_pGrowData ? m_pGrowData.get() : m_FixedData; }
-
- private:
-  DataType m_FixedData[FixedSize];
-  std::unique_ptr<DataType, FxFreeDeleter> m_pGrowData;
-};
-
 class CFX_BitStream {
  public:
   void Init(const uint8_t* pData, uint32_t dwSize);
diff --git a/core/fxge/apple/fx_apple_platform.cpp b/core/fxge/apple/fx_apple_platform.cpp
index 20e86ed..33e675c 100644
--- a/core/fxge/apple/fx_apple_platform.cpp
+++ b/core/fxge/apple/fx_apple_platform.cpp
@@ -4,6 +4,9 @@
 
 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
 
+#include <algorithm>
+#include <vector>
+
 #include "core/fxcrt/fx_system.h"
 
 #ifndef _SKIA_SUPPORT_
@@ -55,8 +58,8 @@
     if (!pFont->GetPlatformFont())
       return false;
   }
-  CFX_FixedBufGrow<uint16_t, 32> glyph_indices(nChars);
-  CFX_FixedBufGrow<CGPoint, 32> glyph_positions(nChars);
+  std::vector<uint16_t> glyph_indices(std::max(32, nChars));
+  std::vector<CGPoint> glyph_positions(std::max(32, nChars));
   for (int i = 0; i < nChars; i++) {
     glyph_indices[i] =
         pCharPos[i].m_ExtGID ? pCharPos[i].m_ExtGID : pCharPos[i].m_GlyphIndex;
@@ -74,9 +77,9 @@
     new_matrix.d = -new_matrix.d;
   }
   quartz2d.setGraphicsTextMatrix(pContext, &new_matrix);
-  return quartz2d.drawGraphicsString(pContext, pFont->GetPlatformFont(),
-                                     font_size, glyph_indices, glyph_positions,
-                                     nChars, argb, nullptr);
+  return quartz2d.drawGraphicsString(
+      pContext, pFont->GetPlatformFont(), font_size, glyph_indices.data(),
+      glyph_positions.data(), nChars, argb, nullptr);
 }
 
 }  // namespace