[PATCH v2 2/6] mfplat: Properly align memory allocated by IMFMediaBuffer.

Giovanni Mascellani gmascellani at codeweavers.com
Fri Feb 25 10:37:43 CST 2022


Signed-off-by: Giovanni Mascellani <gmascellani at codeweavers.com>
---
v2:
 * Handle alignments that are not powers of 2 minus 1. According to
   my tests, this result in the alignment being increased to the
   smalles power of 2 minus 1 bigger than the requested alignment.
   I can add tests if it is wanted.
---
 dlls/mfplat/buffer.c       | 14 ++++++++++++--
 dlls/mfplat/tests/mfplat.c |  1 -
 2 files changed, 12 insertions(+), 3 deletions(-)

diff --git a/dlls/mfplat/buffer.c b/dlls/mfplat/buffer.c
index 774a8604ae9..a91a6a561ce 100644
--- a/dlls/mfplat/buffer.c
+++ b/dlls/mfplat/buffer.c
@@ -18,6 +18,8 @@
 
 #define COBJMACROS
 
+#include <malloc.h>
+
 #include "mfplat_private.h"
 #include "rtworkq.h"
 
@@ -171,7 +173,7 @@ static ULONG WINAPI memory_buffer_Release(IMFMediaBuffer *iface)
         }
         DeleteCriticalSection(&buffer->cs);
         free(buffer->_2d.linear_buffer);
-        free(buffer->data);
+        _aligned_free(buffer->data);
         free(buffer);
     }
 
@@ -1254,8 +1256,16 @@ static const IMFDXGIBufferVtbl dxgi_buffer_vtbl =
 static HRESULT memory_buffer_init(struct buffer *buffer, DWORD max_length, DWORD alignment,
         const IMFMediaBufferVtbl *vtbl)
 {
-    if (!(buffer->data = calloc(1, ALIGN_SIZE(max_length, alignment))))
+    unsigned i;
+
+    /* Saturate bits to the right, so that alignment is a power of 2
+     * minus 1. */
+    for (i = 1; i < 8 * sizeof(alignment); i *= 2)
+        alignment |= alignment >> i;
+
+    if (!(buffer->data = _aligned_malloc(ALIGN_SIZE(max_length, alignment), (size_t)alignment + 1)))
         return E_OUTOFMEMORY;
+    memset(buffer->data, 0, ALIGN_SIZE(max_length, alignment));
 
     buffer->IMFMediaBuffer_iface.lpVtbl = vtbl;
     buffer->refcount = 1;
diff --git a/dlls/mfplat/tests/mfplat.c b/dlls/mfplat/tests/mfplat.c
index 9470cf27a90..91f19da850d 100644
--- a/dlls/mfplat/tests/mfplat.c
+++ b/dlls/mfplat/tests/mfplat.c
@@ -2317,7 +2317,6 @@ static void test_system_memory_buffer(void)
     hr = IMFMediaBuffer_Lock(buffer, &data, &max, &length);
     ok(hr == S_OK, "Failed to lock, hr %#x.\n", hr);
     ok(max == 201 && length == 10, "Unexpected length.\n");
-    todo_wine
     ok(((uintptr_t)data & MF_512_BYTE_ALIGNMENT) == 0, "Data at %p is insufficiently aligned.\n", data);
     hr = IMFMediaBuffer_Unlock(buffer);
     ok(hr == S_OK, "Failed to unlock, hr %#x.\n", hr);
-- 
2.35.1




More information about the wine-devel mailing list