1. From 03bfedcdfb4399878cfd8fec110f4b07db0a895e Mon Sep 17 00:00:00 2001
  2. From: =?UTF-8?q?Samuel=20R=C3=B8dal?= <samuel.rodal@nokia.com>
  3. Date: Thu, 8 Sep 2011 13:10:47 +0200
  4. Subject: [PATCH] Allocate 16-byte aligned memory independent of platform for raster pool.
  5.  
  6. ---
  7. src/gui/painting/qpaintengine_raster.cpp |   54 +++++++++--------------------
  8.  1 files changed, 17 insertions(+), 37 deletions(-)
  9.  
  10. diff --git a/src/gui/painting/qpaintengine_raster.cpp b/src/gui/painting/qpaintengine_raster.cpp
  11. index 9826689..4e1cb5c 100644
  12. --- a/src/gui/painting/qpaintengine_raster.cpp
  13. +++ b/src/gui/painting/qpaintengine_raster.cpp
  14. @@ -3764,6 +3764,11 @@ extern "C" {
  15.      int q_gray_rendered_spans(QT_FT_Raster raster);
  16.  }
  17.  
  18. +static inline uchar *alignAddress(uchar *address, quintptr alignmentMask)
  19. +{
  20. +    return (uchar *)(((quintptr)address + alignmentMask) & ~alignmentMask);
  21. +}
  22. +
  23.  void QRasterPaintEnginePrivate::rasterize(QT_FT_Outline *outline,
  24.                                            ProcessSpans callback,
  25.                                            void *userData, QRasterBuffer *)
  26. @@ -3791,19 +3796,10 @@ void QRasterPaintEnginePrivate::rasterize(QT_FT_Outline *outline,
  27.      // minimize memory reallocations. However if initial size for
  28.      // raster pool is changed for lower value, reallocations will
  29.      // occur normally.
  30. -    const int rasterPoolInitialSize = MINIMUM_POOL_SIZE;
  31. -    int rasterPoolSize = rasterPoolInitialSize;
  32. -    unsigned char *rasterPoolBase;
  33. -#if defined(Q_WS_WIN64)
  34. -    rasterPoolBase =
  35. -        // We make use of setjmp and longjmp in qgrayraster.c which requires
  36. -        // 16-byte alignment, hence we hardcode this requirement here..
  37. -        (unsigned char *) _aligned_malloc(rasterPoolSize, sizeof(void*) * 2);
  38. -#else
  39. -    unsigned char rasterPoolOnStack[rasterPoolInitialSize];
  40. -    rasterPoolBase = rasterPoolOnStack;
  41. -#endif
  42. -    Q_CHECK_PTR(rasterPoolBase);
  43. +    int rasterPoolSize = MINIMUM_POOL_SIZE;
  44. +    uchar rasterPoolOnStack[MINIMUM_POOL_SIZE + 0xf];
  45. +    uchar *rasterPoolBase = alignAddress(rasterPoolOnStack, 0xf);
  46. +    uchar *rasterPoolOnHeap = 0;
  47.  
  48.      qt_ft_grays_raster.raster_reset(*grayRaster.data(), rasterPoolBase, rasterPoolSize);
  49.  
  50. @@ -3839,31 +3835,20 @@ void QRasterPaintEnginePrivate::rasterize(QT_FT_Outline *outline,
  51.  
  52.          // Out of memory, reallocate some more and try again...
  53.          if (error == -6) { // ErrRaster_OutOfMemory from qgrayraster.c
  54. -            int new_size = rasterPoolSize * 2;
  55. -            if (new_size > 1024 * 1024) {
  56. +            rasterPoolSize *= 2;
  57. +            if (rasterPoolSize > 1024 * 1024) {
  58.                  qWarning("QPainter: Rasterization of primitive failed");
  59.                  break;
  60.              }
  61.  
  62.              rendered_spans += q_gray_rendered_spans(*grayRaster.data());
  63.  
  64. -#if defined(Q_WS_WIN64)
  65. -            _aligned_free(rasterPoolBase);
  66. -#else
  67. -            if (rasterPoolBase != rasterPoolOnStack) // initially on the stack
  68. -                free(rasterPoolBase);
  69. -#endif
  70. +            free(rasterPoolOnHeap);
  71. +            rasterPoolOnHeap = (uchar *)malloc(rasterPoolSize + 0xf);
  72.  
  73. -            rasterPoolSize = new_size;
  74. -            rasterPoolBase =
  75. -#if defined(Q_WS_WIN64)
  76. -                // We make use of setjmp and longjmp in qgrayraster.c which requires
  77. -                // 16-byte alignment, hence we hardcode this requirement here..
  78. -                (unsigned char *) _aligned_malloc(rasterPoolSize, sizeof(void*) * 2);
  79. -#else
  80. -                (unsigned char *) malloc(rasterPoolSize);
  81. -#endif
  82. -            Q_CHECK_PTR(rasterPoolBase); // note: we just freed the old rasterPoolBase. I hope it's not fatal.
  83. +            Q_CHECK_PTR(rasterPoolOnHeap); // note: we just freed the old rasterPoolBase. I hope it's not fatal.
  84. +
  85. +            rasterPoolBase = alignAddress(rasterPoolOnHeap, 0xf);
  86.  
  87.              qt_ft_grays_raster.raster_done(*grayRaster.data());
  88.              qt_ft_grays_raster.raster_new(grayRaster.data());
  89. @@ -3873,12 +3858,7 @@ void QRasterPaintEnginePrivate::rasterize(QT_FT_Outline *outline,
  90.          }
  91.      }
  92.  
  93. -#if defined(Q_WS_WIN64)
  94. -    _aligned_free(rasterPoolBase);
  95. -#else
  96. -    if (rasterPoolBase != rasterPoolOnStack) // initially on the stack
  97. -        free(rasterPoolBase);
  98. -#endif
  99. +    free(rasterPoolOnHeap);
  100.  }
  101.  
  102.  void QRasterPaintEnginePrivate::recalculateFastImages()
  103. --
  104. 1.7.0.4