On Mon, 7 Apr 2025, Pali Rohár wrote:
On Sunday 06 April 2025 14:10:35 Pali Rohár wrote:
It mimics msvc _aligned_msize() function.
---
mingw-w64-crt/misc/mingw-aligned-malloc.c | 23 +++++++++++++++++++++++
mingw-w64-headers/crt/malloc.h | 1 +
2 files changed, 24 insertions(+)
diff --git a/mingw-w64-crt/misc/mingw-aligned-malloc.c
b/mingw-w64-crt/misc/mingw-aligned-malloc.c
index 5029b6007842..d376a103a55f 100644
--- a/mingw-w64-crt/misc/mingw-aligned-malloc.c
+++ b/mingw-w64-crt/misc/mingw-aligned-malloc.c
@@ -118,3 +118,26 @@ __mingw_aligned_realloc (void *memblock, size_t size,
size_t alignment)
{
return __mingw_aligned_offset_realloc (memblock, size, alignment, 0);
}
+
+size_t
+__mingw_aligned_msize (void *memblock, size_t alignment, size_t offset)
+{
+ void *p0;
+
+ if (!memblock || NOT_POWER_OF_TWO (alignment))
+ {
+ errno = EINVAL;
+ return (size_t)-1;
+ }
+
+ p0 = ORIG_PTR (memblock);
+
+ /* It is an error if the alignment or offset does not match. */
+ if (memblock != PTR_ALIGN (p0, alignment, offset))
+ {
+ errno = EINVAL;
+ return (size_t)-1;
+ }
+
+ return _msize (p0) - (alignment + sizeof (void *));
+}
diff --git a/mingw-w64-headers/crt/malloc.h b/mingw-w64-headers/crt/malloc.h
index 78d67dd2eeb4..52918782bf8c 100644
--- a/mingw-w64-headers/crt/malloc.h
+++ b/mingw-w64-headers/crt/malloc.h
@@ -133,6 +133,7 @@ void __mingw_aligned_free (void *_Memory);
void * __mingw_aligned_offset_realloc (void *_Memory, size_t _Size, size_t
_Alignment, size_t _Offset);
void * __mingw_aligned_offset_malloc (size_t, size_t, size_t);
void * __mingw_aligned_realloc (void *_Memory, size_t _Size, size_t _Offset);
+size_t __mingw_aligned_msize (void *memblock, size_t alignment, size_t offset);
#if defined(__x86_64__) || defined(__i386__)
/* Get the compiler's definition of _mm_malloc and _mm_free. */
--
2.20.1
I was doing another round of testing and I have figured out that this
__mingw_aligned_msize is not compatible with msvcrt.dll. There are two
problems: off-by-one error and incorrect handling of alignment less than
pointer size.
One I fix these problems, I will send a new version.
Sorry for troubles.
No problem - thanks for testing it thoroughly!
// Martin
_______________________________________________
Mingw-w64-public mailing list
Mingw-w64-public@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/mingw-w64-public