Merge pull request #1357 from microsoft/dev-main

release v3.4.5
diff --git a/.github/workflows/release.yaml b/.github/workflows/release.yaml
index 3855e62..dd60b12 100644
--- a/.github/workflows/release.yaml
+++ b/.github/workflows/release.yaml
@@ -9,7 +9,7 @@
   contents: write
 
 env:
-  RELEASE: Release v3.4.4
+  RELEASE: Release v3.4.5
   FORCE_JAVASCRIPT_ACTIONS_TO_NODE24: true
   
 name: Release
@@ -19,7 +19,7 @@
     runs-on: ${{ matrix.os }}
     strategy:
       matrix:
-        branch: [v3.4.4, v2.4.4, v1.9.14]  # [dev,dev2,dev3]
+        branch: [v3.4.5, v2.4.5, v1.9.15]  # [dev,dev2,dev3]
         # we build on the oldest ubuntu version for better binary compatibility.
         os: [windows-latest, macOS-latest, macos-15-intel, ubuntu-22.04, ubuntu-22.04-arm]  
 
diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml
index df85b16..6a685eb 100644
--- a/.github/workflows/test.yaml
+++ b/.github/workflows/test.yaml
@@ -22,6 +22,9 @@
         exclude:
           - os: macos-14
             tests: extra          
+        include:
+          - os: windows-latest
+            tests: mingw-ucrt64
         
     steps:
       # Checkout: https://github.com/actions/checkout
@@ -161,3 +164,33 @@
           ctest --test-dir out/release-guarded --verbose --timeout 240 -C Release
         env:
           MIMALLOC_GUARDED_SAMPLE_RATE: 100
+
+      # MSYS2 mingw64 UCRT64
+      - name: mingw-ucrt64 x64, Setup
+        if: matrix.tests == 'mingw-ucrt64'
+        uses: msys2/setup-msys2@v2       
+        with: 
+          msystem: UCRT64
+          install: |
+            mingw-w64-ucrt-x86_64-gcc
+            mingw-w64-ucrt-x86_64-git
+            mingw-w64-ucrt-x86_64-make
+            mingw-w64-ucrt-x86_64-cmake
+
+      - name: mingw-ucrt64 x64, Debug
+        if: matrix.tests == 'mingw-ucrt64'
+        shell: msys2 {0}
+        run: |
+          env
+          cmake . -B out/debug-mingw-ucrt64-x64 -DCMAKE_BUILD_TYPE=Debug -DMI_DEBUG_FULL=ON
+          cmake --build out/debug-mingw-ucrt64-x64 --parallel 4 --config Debug
+          ctest --test-dir out/debug-mingw-ucrt64-x64 --verbose --timeout 240 -C Debug
+
+      - name: mingw-ucrt64 x64, Release
+        if: matrix.tests == 'mingw-ucrt64'
+        shell: msys2 {0}
+        run: |
+          cmake . -B out/release-mingw-ucrt64-x64 -DCMAKE_BUILD_TYPE=Release  -DMI_OPT_ARCH=ON
+          cmake --build out/release-mingw-ucrt64-x64 --parallel 4 --config Release
+          ctest --test-dir out/release-mingw-ucrt64-x64 --verbose --timeout 240 -C Release
+
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 2294ad9..b2a65b4 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -406,6 +406,12 @@
   list(APPEND mi_defines MI_LIBC_MUSL=1)
 endif()
 
+if("$ENV{MSYSTEM}" STREQUAL "UCRT64")
+  message(STATUS "Detected MSYS2 UCRT64 environment")
+  set(MI_MINGW_UCRT64 "ON")
+  list(APPEND mi_defines MI_WINGW_UCRT64=1)
+endif()
+
 if(MI_WIN_USE_FIXED_TLS)
   message(STATUS "Use fixed TLS slot on Windows to avoid extra tests in the malloc fast path (MI_WIN_USE_FIXED_TLS=ON)")
   list(APPEND mi_defines MI_WIN_USE_FIXED_TLS=1)
@@ -580,6 +586,12 @@
 else()
   set(mi_libname "${mi_libname}-${CMAKE_BUILD_TYPE_LC}") #append build type (e.g. -debug) if not a release version
 endif()
+string(REGEX REPLACE "^mimalloc[-]?" "" mi_libsuffix "${mi_libname}")
+
+set(mi_libname "mimalloc")
+if(NOT mi_libsuffix STREQUAL "")
+set(mi_libname "${mi_libname}-${mi_libsuffix}")
+endif()
 
 if(MI_BUILD_SHARED)
   list(APPEND mi_build_targets "shared")
@@ -643,16 +655,20 @@
     # On windows, link and copy the mimalloc redirection dll too.
     if(CMAKE_GENERATOR_PLATFORM STREQUAL "arm64ec")
       set(MIMALLOC_REDIRECT_SUFFIX "-arm64ec")
+      set(MINJECT_SUFFIX "")
     elseif(MI_ARCH STREQUAL "x64")
       set(MIMALLOC_REDIRECT_SUFFIX "")
+      set(MINJECT_SUFFIX "")
       if(CMAKE_SYSTEM_PROCESSOR STREQUAL "ARM64")
         message(STATUS "Note: x64 code emulated on Windows for arm64 should use an arm64ec build of 'mimalloc.dll'")
         message(STATUS "      together with 'mimalloc-redirect-arm64ec.dll'. See the 'bin\\readme.md' for more information.")
       endif()
     elseif(MI_ARCH STREQUAL "x86")
       set(MIMALLOC_REDIRECT_SUFFIX "32")
+      set(MINJECT_SUFFIX "32")
     else()
       set(MIMALLOC_REDIRECT_SUFFIX "-${MI_ARCH}")  # -arm64 etc.
+      set(MINJECT_SUFFIX "-${MI_ARCH}")
     endif()
 
     target_link_libraries(mimalloc PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/bin/mimalloc-redirect${MIMALLOC_REDIRECT_SUFFIX}.lib)  # the DLL import library
@@ -774,6 +790,11 @@
     if(WIN32)
       target_compile_definitions(mimalloc-test-stress-dynamic PRIVATE "MI_LINK_VERSION=1")  # link mi_version
       target_link_libraries(mimalloc-test-stress-dynamic PRIVATE mimalloc ${mi_libraries})  # link mi_version
+      if(MINGW AND MI_MINGW_UCRT64)
+        # mingw always links mimalloc after system libraries.
+        # post-process with `minject` to make sure mimalloc is the first dll in the load order.
+        add_custom_command(TARGET mimalloc-test-stress-dynamic POST_BUILD COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/bin/minject${MINJECT_SUFFIX} --postfix=${mi_libsuffix} -f -i $<TARGET_FILE:mimalloc-test-stress-dynamic>)
+      endif()
       add_test(NAME test-stress-dynamic COMMAND ${CMAKE_COMMAND} -E env MIMALLOC_VERBOSE=1 $<TARGET_FILE:mimalloc-test-stress-dynamic>)
     else()
       target_link_libraries(mimalloc-test-stress-dynamic PRIVATE ${mi_libraries}) # pthreads, issue 1158
diff --git a/cmake/mimalloc-config-version.cmake b/cmake/mimalloc-config-version.cmake
index acbea62..045dcf7 100644
--- a/cmake/mimalloc-config-version.cmake
+++ b/cmake/mimalloc-config-version.cmake
@@ -1,6 +1,6 @@
 set(mi_version_major 2)
 set(mi_version_minor 4)
-set(mi_version_patch 4)
+set(mi_version_patch 5)
 set(mi_version ${mi_version_major}.${mi_version_minor})
 
 set(PACKAGE_VERSION ${mi_version})
diff --git a/include/mimalloc.h b/include/mimalloc.h
index ce29b3d..601e7da 100644
--- a/include/mimalloc.h
+++ b/include/mimalloc.h
@@ -8,7 +8,7 @@
 #ifndef MIMALLOC_H
 #define MIMALLOC_H
 
-#define MI_MALLOC_VERSION 20404  // major + 2 digits minor + 2 digits patch
+#define MI_MALLOC_VERSION 20405  // major + 2 digits minor + 2 digits patch
 
 // ------------------------------------------------------
 // Compiler specific attributes
diff --git a/include/mimalloc/internal.h b/include/mimalloc/internal.h
index 9b1e673..67399dc 100644
--- a/include/mimalloc/internal.h
+++ b/include/mimalloc/internal.h
@@ -45,7 +45,11 @@
 #define mi_decl_align(a)        __attribute__((aligned(a)))
 #define mi_decl_noreturn        __attribute__((noreturn))
 #define mi_decl_weak            __attribute__((weak))
+#if defined(__MINGW32__)
+#define mi_decl_hidden
+#else
 #define mi_decl_hidden          __attribute__((visibility("hidden")))
+#endif
 #if (__GNUC__ >= 4) || defined(__clang__)
 #define mi_decl_cold            __attribute__((cold))
 #else
@@ -80,6 +84,14 @@
 #define mi_likely(x)       (x)
 #endif
 
+#if (defined(__GNUC__) && (__GNUC__ >= 7)) || defined(__clang__) // includes clang and icc
+#define mi_decl_maybe_unused    __attribute__((unused))
+#elif __cplusplus >= 201703L    // c++17
+#define mi_decl_maybe_unused    [[maybe_unused]]
+#else
+#define mi_decl_maybe_unused
+#endif
+
 #ifndef __has_builtin
 #define __has_builtin(x)    0
 #endif
@@ -239,7 +251,7 @@
 bool       _mi_segment_visit_blocks(mi_segment_t* segment, int heap_tag, bool visit_blocks, mi_block_visit_fun* visitor, void* arg);
 
 // "page.c"
-void*       _mi_malloc_generic(mi_heap_t* heap, size_t size, bool zero, size_t huge_alignment, size_t* usable)  mi_attr_noexcept mi_attr_malloc;
+void*       _mi_malloc_generic(mi_heap_t* heap, size_t size, bool zero, size_t huge_alignment, mi_page_t** ppage)  mi_attr_noexcept mi_attr_malloc;
 
 void        _mi_page_retire(mi_page_t* page) mi_attr_noexcept;                  // free the page if there are no other pages with many free blocks
 void        _mi_page_unfull(mi_page_t* page);
@@ -282,17 +294,17 @@
 mi_msecs_t  _mi_clock_start(void);
 
 // "alloc.c"
-void*       _mi_page_malloc_zero(mi_heap_t* heap, mi_page_t* page, size_t size, bool zero, size_t* usable) mi_attr_noexcept;  // called from `_mi_malloc_generic`
+void*       _mi_page_malloc_zero(mi_heap_t* heap, mi_page_t* page, size_t size, bool zero, mi_page_t** ppage) mi_attr_noexcept;  // called from `_mi_malloc_generic`
 void*       _mi_page_malloc(mi_heap_t* heap, mi_page_t* page, size_t size) mi_attr_noexcept;                  // called from `_mi_heap_malloc_aligned`
 void*       _mi_page_malloc_zeroed(mi_heap_t* heap, mi_page_t* page, size_t size) mi_attr_noexcept;           // called from `_mi_heap_malloc_aligned`
 void*       _mi_heap_malloc_zero(mi_heap_t* heap, size_t size, bool zero) mi_attr_noexcept;
-void*       _mi_heap_malloc_zero_ex(mi_heap_t* heap, size_t size, bool zero, size_t huge_alignment, size_t* usable) mi_attr_noexcept;     // called from `_mi_heap_malloc_aligned`
-void*       _mi_heap_realloc_zero(mi_heap_t* heap, void* p, size_t newsize, bool zero, size_t* usable_pre, size_t* usable_post) mi_attr_noexcept;
+void*       _mi_heap_malloc_zero_ex(mi_heap_t* heap, size_t size, bool zero, size_t huge_alignment, mi_page_t** ppage) mi_attr_noexcept;     // called from `_mi_heap_malloc_aligned`
+void*       _mi_heap_realloc_zero(mi_heap_t* heap, void* p, size_t newsize, bool zero, size_t* pblock_size_pre, size_t* pblock_size_post) mi_attr_noexcept;
 mi_block_t* _mi_page_ptr_unalign(const mi_page_t* page, const void* p);
 bool        _mi_free_delayed_block(mi_block_t* block);
 void        _mi_free_generic(mi_segment_t* segment, mi_page_t* page, bool is_local, void* p) mi_attr_noexcept;  // for runtime integration
 void        _mi_padding_shrink(const mi_page_t* page, const mi_block_t* block, const size_t min_size);
-
+size_t      _mi_page_usable_size(const mi_page_t* page, const void* p) mi_attr_noexcept;
 #if MI_DEBUG>1
 bool        _mi_page_is_valid(mi_page_t* page);
 #endif
@@ -381,7 +393,7 @@
 
 // valid alignment values are as posix memalign: <https://en.cppreference.com/c/memory/aligned_alloc#Notes>
 static inline bool mi_alignment_is_valid(size_t alignment) {
-  return ((alignment!=0) && _mi_is_power_of_two(alignment)); 
+  return ((alignment!=0) && _mi_is_power_of_two(alignment));
 }
 
 // Is a pointer aligned?
@@ -392,7 +404,7 @@
 // Align upwards
 static inline uintptr_t _mi_align_up(uintptr_t sz, size_t alignment) {
   mi_assert_internal(alignment != 0);
-  uintptr_t mask = alignment - 1;
+  const uintptr_t mask = alignment - 1;
   if ((alignment & mask) == 0) {  // power of two?
     return ((sz + mask) & ~mask);
   }
@@ -401,28 +413,27 @@
   }
 }
 
-// Align downwards
-static inline uintptr_t _mi_align_down(uintptr_t sz, size_t alignment) {
-  mi_assert_internal(alignment != 0);
-  uintptr_t mask = alignment - 1;
-  if ((alignment & mask) == 0) { // power of two?
-    return (sz & ~mask);
-  }
-  else {
-    return ((sz / alignment) * alignment);
-  }
-}
-
 // Align a pointer upwards
 static inline void* _mi_align_up_ptr(const void* p, size_t alignment) {
   return (void*)_mi_align_up((uintptr_t)p, alignment);
 }
 
-// Align a pointer downwards
-static inline void* _mi_align_down_ptr(void* p, size_t alignment) {
-  return (void*)_mi_align_down((uintptr_t)p, alignment);
+// Align down
+static inline uintptr_t _mi_align_down(uintptr_t sz, size_t alignment) {
+  mi_assert_internal(alignment != 0);
+  const uintptr_t mask = alignment - 1;
+  if ((alignment & mask) == 0) {  // power of two?
+    return (sz & ~mask);
+  }
+  else {
+    return ((sz/alignment)*alignment);
+  }
 }
 
+// Align a pointer downwards
+static inline void* _mi_align_down_ptr(const void* p, size_t alignment) {
+  return (void*)_mi_align_down((uintptr_t)p, alignment);
+}
 
 // Divide upwards: `s <= _mi_divide_up(s,d)*d < s+d`.
 static inline uintptr_t _mi_divide_up(uintptr_t size, size_t divider) {
@@ -738,15 +749,15 @@
     // no sample
     heap->guarded_sample_count = count;
     return false;
-  }  
-  else { 
+  }
+  else {
     // count == 0
     const size_t rate = heap->guarded_sample_rate;
     if (rate == 0) {
       return false; // don't write to an empty theap
     }
     else if (size >= heap->guarded_size_min && size <= heap->guarded_size_max) {
-      // use guarded allocation        
+      // use guarded allocation
       heap->guarded_sample_count = rate;  // reset
       return true;
     }
@@ -758,7 +769,7 @@
   }
 }
 
-mi_decl_restrict void* _mi_heap_malloc_guarded(mi_heap_t* heap, size_t size, bool zero, size_t* usable) mi_attr_noexcept;
+mi_decl_restrict void* _mi_heap_malloc_guarded(mi_heap_t* heap, size_t size, bool zero, mi_page_t** ppage) mi_attr_noexcept;
 
 #endif
 
diff --git a/include/mimalloc/types.h b/include/mimalloc/types.h
index 6f4c704..70bc0e9 100644
--- a/include/mimalloc/types.h
+++ b/include/mimalloc/types.h
@@ -543,7 +543,7 @@
 #if (MI_PADDING)
 typedef struct mi_padding_s {
   uint32_t canary; // encoded block value to check validity of the padding (in case of overflow)
-  uint32_t delta;  // padding bytes before the block. (mi_usable_size(p) - delta == exact allocated bytes)
+  uint32_t delta;  // padding bytes before the block. (mi_full_usable_size(p) - delta == exact allocated bytes)
 } mi_padding_t;
 #define MI_PADDING_SIZE   (sizeof(mi_padding_t))
 #define MI_PADDING_WSIZE  ((MI_PADDING_SIZE + MI_INTPTR_SIZE - 1) / MI_INTPTR_SIZE)
diff --git a/readme.md b/readme.md
index ccf0e64..ddb5571 100644
--- a/readme.md
+++ b/readme.md
@@ -15,9 +15,9 @@
 Initially developed by Daan Leijen for the runtime systems of the
 [Koka](https://koka-lang.github.io) and [Lean](https://github.com/leanprover/lean) languages.
 
-Latest release   : `v3.4.4`  (2026-08-01) recommended.  
-Latest v2 release: `v2.4.4`  (2026-08-01) stable.  
-Latest v1 release: `v1.9.14` (2026-08-01) legacy.
+Latest release   : `v3.4.5`  (2026-08-05) recommended.  
+Latest v2 release: `v2.4.5`  (2026-08-05) stable.  
+Latest v1 release: `v1.9.15` (2026-08-05) legacy.
 
 mimalloc is a drop-in replacement for `malloc` and can be used in other programs
 without code changes, for example, on dynamically linked ELF-based systems (Linux, BSD, etc.) you can use it as:
@@ -89,6 +89,10 @@
         __Send PR's against this version if possible.__
 
 ### Releases
+
+* 2026-08-05, `v1.9.15`, `v2.4.5`, `v3.4.5`: Fix compilation with xmalloc (#1353), make the build deterministic (#1355),
+  mi_zalloc_aligned fix (#763), improve support for mingw (ucrt64), fix fputs fallback on windows (#1354), (v3): use proper 
+  lock backoff for first-class heap deletion, improved riscv64 codegen.
 * 2026-08-01, `v1.9.14`, `v2.4.4`, `v3.4.4`: various bug and security fixes through Opus 5 LLM audit (issue #1271, by @Zoxc). 
   (v3): use pthreads by default on macOS (issue #1333, issue #1327), fix glibc 2.44 crash (issue #1341), fix alignment check for realloc_aligned. (v1,v2,v3): Add initial mingw support,
   set `errno` on allocation errors, improved double-free checks and size checks in secure mode, enable 
@@ -127,25 +131,6 @@
   v3 uses faster TLS access on Windows, and has improved performance for `mi_calloc` and aligned allocations.
   Fixed rare race condition on older v3, fixed potential buffer overflow in debug statistics, add API for returning
   allocated sizes on allocation and free.
-* 2025-06-13, `v3.1.5`: Bug fix release where memory was not always correctly committed (issue #1098).
-* 2025-06-09, `v1.9.4`, `v2.2.4`, `v3.1.4` (beta) : Some important bug fixes, including a case where OS memory
-  was not always fully released. Improved v3 performance, build on XBox, fix build on Android, support interpose 
-  for older macOS versions, use MADV_FREE_REUSABLE on macOS, always check commit success, better support for Windows 
-  fixed TLS offset, etc.
-* 2025-03-28, `v1.9.3`, `v2.2.3`, `v3.0.3` (beta) : Various small bug and build fixes, including:
-  fix arm32 pre v7 builds, fix mingw build, get runtime statistics, improve statistic commit counts, 
-  fix execution on non BMI1 x64 systems. 
-* 2025-03-06, `v1.9.2`, `v2.2.2`, `v3.0.2-beta`: Various small bug and build fixes. 
-  Add `mi_options_print`, `mi_arenas_print`, and the experimental `mi_stat_get` and `mi_stat_get_json`. 
-  Add `mi_thread_set_in_threadpool` and `mi_heap_set_numa_affinity` (v3 only). Add vcpkg portfile. 
-  Upgrade mimalloc-redirect to v1.3.2. `MI_OPT_ARCH` is off by default now but still assumes armv8.1-a on arm64
-  for fast atomic operations. Add QNX support.
-* 2025-01-03, `v1.8.9`, `v2.1.9`, `v3.0.1-alpha`: Interim release. Support Windows arm64. New [guarded](#guarded) build that can place OS 
-  guard pages behind objects to catch buffer overflows as they occur. 
-  Many small fixes: build on Windows arm64, cygwin, riscV, and dragonfly; fix Windows static library initialization to account for
-  thread local destructors (in Rust/C++); macOS tag change; macOS TLS slot fix; improve stats; 
-  consistent `mimalloc.dll` on Windows (instead of `mimalloc-override.dll`); fix mimalloc-redirect on Win11 H2; 
-  add 0-byte to canary; upstream CPython fixes; reduce .bss size; allow fixed TLS slot on Windows for improved performance.
 
 * [Older release notes](#older-release-notes)
 
@@ -930,6 +915,25 @@
 
 # Older Release Notes
 
+* 2025-06-09, `v1.9.4`, `v2.2.4`, `v3.1.4` (beta) : Some important bug fixes, including a case where OS memory
+  was not always fully released. Improved v3 performance, build on XBox, fix build on Android, support interpose 
+  for older macOS versions, use MADV_FREE_REUSABLE on macOS, always check commit success, better support for Windows 
+  fixed TLS offset, etc.
+* 2025-03-28, `v1.9.3`, `v2.2.3`, `v3.0.3` (beta) : Various small bug and build fixes, including:
+  fix arm32 pre v7 builds, fix mingw build, get runtime statistics, improve statistic commit counts, 
+  fix execution on non BMI1 x64 systems. 
+* 2025-03-06, `v1.9.2`, `v2.2.2`, `v3.0.2-beta`: Various small bug and build fixes. 
+  Add `mi_options_print`, `mi_arenas_print`, and the experimental `mi_stat_get` and `mi_stat_get_json`. 
+  Add `mi_thread_set_in_threadpool` and `mi_heap_set_numa_affinity` (v3 only). Add vcpkg portfile. 
+  Upgrade mimalloc-redirect to v1.3.2. `MI_OPT_ARCH` is off by default now but still assumes armv8.1-a on arm64
+  for fast atomic operations. Add QNX support.
+* 2025-01-03, `v1.8.9`, `v2.1.9`, `v3.0.1-alpha`: Interim release. Support Windows arm64. New [guarded](#guarded) build that can place OS 
+  guard pages behind objects to catch buffer overflows as they occur. 
+  Many small fixes: build on Windows arm64, cygwin, riscV, and dragonfly; fix Windows static library initialization to account for
+  thread local destructors (in Rust/C++); macOS tag change; macOS TLS slot fix; improve stats; 
+  consistent `mimalloc.dll` on Windows (instead of `mimalloc-override.dll`); fix mimalloc-redirect on Win11 H2; 
+  add 0-byte to canary; upstream CPython fixes; reduce .bss size; allow fixed TLS slot on Windows for improved performance.
+
 * 2024-05-21, `v1.8.7`, `v2.1.7`: Fix build issues on less common platforms. Started upstreaming patches
   from the CPython [integration](https://github.com/python/cpython/issues/113141#issuecomment-2119255217). Upstream `vcpkg` patches.
 * 2024-05-13, `v1.8.6`, `v2.1.6`: Fix build errors on various (older) platforms. Refactored aligned allocation.
diff --git a/src/alloc-aligned.c b/src/alloc-aligned.c
index 679eda4..33f271a 100644
--- a/src/alloc-aligned.c
+++ b/src/alloc-aligned.c
@@ -25,7 +25,7 @@
 }
 
 #if MI_GUARDED
-static mi_decl_noinline mi_decl_restrict void* mi_heap_malloc_guarded_aligned(mi_heap_t* heap, size_t size, size_t alignment, bool zero, size_t* usable) mi_attr_noexcept {
+static mi_decl_noinline mi_decl_restrict void* mi_heap_malloc_guarded_aligned(mi_heap_t* heap, size_t size, size_t alignment, bool zero, mi_page_t** ppage) mi_attr_noexcept {
   // use over allocation for guarded blocksl
   mi_assert_internal(alignment > 0 && alignment < MI_BLOCK_ALIGNMENT_MAX);
   if mi_unlikely(alignment >= MI_BLOCK_ALIGNMENT_MAX || size > (MI_MAX_ALLOC_SIZE - MI_PADDING_SIZE - alignment)) {
@@ -33,7 +33,7 @@
     return NULL;
   }
   const size_t oversize = size + alignment - 1;
-  void* const base = _mi_heap_malloc_guarded(heap, oversize, zero, usable);
+  void* const base = _mi_heap_malloc_guarded(heap, oversize, zero, ppage);
   if (base==NULL) return NULL;
   void* const p = _mi_align_up_ptr(base, alignment);
   mi_track_align(base, p, (uint8_t*)p - (uint8_t*)base, size);
@@ -42,28 +42,29 @@
   return p;
 }
 
-static void* mi_heap_malloc_zero_no_guarded(mi_heap_t* heap, size_t size, bool zero, size_t* usable) {
+static void* mi_heap_malloc_zero_no_guarded(mi_heap_t* heap, size_t size, bool zero, mi_page_t** ppage) {
   const size_t rate = heap->guarded_sample_rate;
   // only write if `rate!=0` so we don't write to the constant `_mi_heap_empty`
   if (rate != 0) { heap->guarded_sample_rate = 0; }
-  void* p = _mi_heap_malloc_zero_ex(heap, size, zero, 0, usable);
+  void* p = _mi_heap_malloc_zero_ex(heap, size, zero, 0, ppage);
   if (rate != 0) { heap->guarded_sample_rate = rate; }
   return p;
 }
 #else
-static void* mi_heap_malloc_zero_no_guarded(mi_heap_t* heap, size_t size, bool zero, size_t* usable) {
-  return _mi_heap_malloc_zero_ex(heap, size, zero, 0, usable);
+static void* mi_heap_malloc_zero_no_guarded(mi_heap_t* heap, size_t size, bool zero, mi_page_t** ppage) {
+  return _mi_heap_malloc_zero_ex(heap, size, zero, 0, ppage);
 }
 #endif
 
 // Fallback aligned allocation that over-allocates -- split out for better codegen
-static mi_decl_noinline void* mi_heap_malloc_zero_aligned_at_overalloc(mi_heap_t* const heap, const size_t size, const size_t alignment, const size_t offset, const bool zero, size_t* usable) mi_attr_noexcept
+static mi_decl_noinline void* mi_heap_malloc_zero_aligned_at_overalloc(mi_heap_t* const heap, const size_t size, const size_t alignment, const size_t offset, const bool zero, mi_page_t** ppage) mi_attr_noexcept
 {
   mi_assert_internal(size <= (MI_MAX_ALLOC_SIZE - MI_PADDING_SIZE));
   mi_assert_internal(mi_alignment_is_valid(alignment));
 
   void* p;
   size_t oversize;
+  mi_page_t* page;
   if mi_unlikely(alignment > MI_BLOCK_ALIGNMENT_MAX) {
     // use OS allocation for very large alignment and allocate inside a huge page (dedicated segment with 1 page)
     // This can support alignments >= MI_SEGMENT_SIZE by ensuring the object can be aligned at a point in the
@@ -75,7 +76,7 @@
     }
     oversize = (size <= MI_SMALL_SIZE_MAX ? MI_SMALL_SIZE_MAX + 1 /* ensure we use generic malloc path */ : size);
     // note: no guarded as alignment > 0
-    p = _mi_heap_malloc_zero_ex(heap, oversize, false, alignment, usable); // the page block size should be large enough to align in the single huge page block
+    p = _mi_heap_malloc_zero_ex(heap, oversize, false, alignment, &page); // the page block size should be large enough to align in the single huge page block
     // zero afterwards as only the area from the aligned_p may be committed!
     if (p == NULL) return NULL;
   }
@@ -84,10 +85,11 @@
     mi_assert_internal(size <= (MI_MAX_ALLOC_SIZE - MI_PADDING_SIZE) && alignment <= MI_BLOCK_ALIGNMENT_MAX);
     mi_assert_internal(size < SIZE_MAX - alignment); // `oversize` cannot overflow
     oversize = (size < MI_MAX_ALIGN_SIZE ? MI_MAX_ALIGN_SIZE : size) + alignment - 1;  // adjust for size <= 16; with size 0 and alignment 64k, we would allocate a 64k block and pointing just beyond that.
-    p = mi_heap_malloc_zero_no_guarded(heap, oversize, zero, usable);
+    p = mi_heap_malloc_zero_no_guarded(heap, oversize, zero, &page);
     if (p == NULL) return NULL;
   }
-  mi_page_t* page = _mi_ptr_page(p);
+  mi_assert_internal(page == _mi_ptr_page(p));
+  if (ppage!=NULL) { *ppage = page; }
 
   // .. and align within the allocation
   const uintptr_t align_mask = alignment - 1;  // for any x, `(x & align_mask) == (x % alignment)`
@@ -97,11 +99,11 @@
   void* aligned_p = (void*)((uintptr_t)p + adjust);
   if (aligned_p != p) {
     mi_page_set_has_aligned(page, true);
-    if (usable!=NULL) { 
-      mi_assert_internal(*usable > adjust);
-      if (*usable > adjust) { *usable = *usable - adjust; }
-      mi_assert_internal(*usable >= size);
-    }
+    // if (pblock_size!=NULL) { 
+    //   mi_assert_internal(*pblock_size > adjust);
+    //   if (*pblock_size > adjust) { *pblock_size = *pblock_size - adjust; }
+    //   mi_assert_internal(*pblock_size >= size);
+    // }
     #if MI_GUARDED
     // set tag to aligned so mi_usable_size works with guard pages
     if (adjust >= sizeof(mi_block_t)) {
@@ -142,7 +144,7 @@
 }
 
 // Generic primitive aligned allocation -- split out for better codegen
-static mi_decl_noinline void* mi_heap_malloc_zero_aligned_at_generic(mi_heap_t* const heap, const size_t size, const size_t alignment, const size_t offset, const bool zero, size_t* usable) mi_attr_noexcept
+static mi_decl_noinline void* mi_heap_malloc_zero_aligned_at_generic(mi_heap_t* const heap, const size_t size, const size_t alignment, const size_t offset, const bool zero, mi_page_t** ppage) mi_attr_noexcept
 {
   mi_assert_internal(mi_alignment_is_valid(alignment));
   // we don't allocate more than MI_MAX_ALLOC_SIZE (see <https://sourceware.org/ml/libc-announce/2019/msg00001.html>)
@@ -155,7 +157,7 @@
   // this is important to try as the fast path in `mi_heap_malloc_zero_aligned` only works when there exist
   // a page with the right block size, and if we always use the over-alloc fallback that would never happen.
   if (offset == 0 && mi_malloc_is_naturally_aligned(size,alignment)) {
-    void* p = mi_heap_malloc_zero_no_guarded(heap, size, zero, usable);
+    void* p = mi_heap_malloc_zero_no_guarded(heap, size, zero, ppage);
     mi_assert_internal(p == NULL || ((uintptr_t)p % alignment) == 0);
     const bool is_aligned_or_null = (((uintptr_t)p) & (alignment-1))==0;
     if mi_likely(is_aligned_or_null) {
@@ -169,7 +171,7 @@
   }
 
   // fall back to over-allocation
-  return mi_heap_malloc_zero_aligned_at_overalloc(heap,size,alignment,offset,zero,usable);
+  return mi_heap_malloc_zero_aligned_at_overalloc(heap,size,alignment,offset,zero,ppage);
 }
 
 
@@ -181,7 +183,7 @@
 // Primitive aligned allocation
 static void* mi_heap_malloc_zero_aligned_at(mi_heap_t* const heap, const size_t size, 
                                             const size_t alignment, const size_t offset, const bool zero,
-                                            size_t* usable) mi_attr_noexcept
+                                            mi_page_t** ppage) mi_attr_noexcept
 {
   // note: we don't require `size > offset`, we just guarantee that the address at offset is aligned regardless of the allocated size.
   if mi_unlikely(!mi_alignment_is_valid(alignment)) { // require power-of-two and multiple of void* (see <https://en.cppreference.com/w/c/memory/aligned_alloc#Notes>)
@@ -190,7 +192,7 @@
 
   #if MI_GUARDED
   if (offset==0 && alignment < MI_BLOCK_ALIGNMENT_MAX && mi_heap_malloc_use_guarded(heap,size)) {
-    return mi_heap_malloc_guarded_aligned(heap, size, alignment, zero, usable);
+    return mi_heap_malloc_guarded_aligned(heap, size, alignment, zero, ppage);
   }
   #endif
 
@@ -203,7 +205,7 @@
       const bool is_aligned = (((uintptr_t)page->free + offset) & align_mask)==0;
       if mi_likely(is_aligned)
       {
-        if (usable!=NULL) { *usable = mi_page_usable_block_size(page); }
+        if (ppage!=NULL) { *ppage = page; }
         void* p = (zero ? _mi_page_malloc_zeroed(heap,page,padsize) : _mi_page_malloc(heap,page,padsize)); // call specific page malloc for better codegen
         mi_assert_internal(p != NULL);
         mi_assert_internal(((uintptr_t)p + offset) % alignment == 0);
@@ -214,7 +216,7 @@
   }
 
   // fallback to generic aligned allocation
-  return mi_heap_malloc_zero_aligned_at_generic(heap, size, alignment, offset, zero, usable);
+  return mi_heap_malloc_zero_aligned_at_generic(heap, size, alignment, offset, zero, ppage);
 }
 
 
@@ -265,8 +267,11 @@
   return mi_heap_malloc_aligned(mi_prim_get_default_heap(), size, alignment);
 }
 
-mi_decl_nodiscard mi_decl_restrict void* mi_umalloc_aligned(size_t size, size_t alignment, size_t* block_size) mi_attr_noexcept {
-  return mi_heap_malloc_zero_aligned_at(mi_prim_get_default_heap(), size, alignment, 0, false, block_size);
+mi_decl_nodiscard mi_decl_restrict void* mi_umalloc_aligned(size_t size, size_t alignment, size_t* pblock_size) mi_attr_noexcept {
+  mi_page_t* page;
+  void* p = mi_heap_malloc_zero_aligned_at(mi_prim_get_default_heap(), size, alignment, 0, false, &page);
+  if (p!=NULL && pblock_size!=NULL) { *pblock_size = mi_page_block_size(page); }
+  return p;
 }
 
 mi_decl_nodiscard mi_decl_restrict void* mi_zalloc_aligned_at(size_t size, size_t alignment, size_t offset) mi_attr_noexcept {
@@ -277,8 +282,11 @@
   return mi_heap_zalloc_aligned(mi_prim_get_default_heap(), size, alignment);
 }
 
-mi_decl_nodiscard mi_decl_restrict void* mi_uzalloc_aligned(size_t size, size_t alignment, size_t* block_size) mi_attr_noexcept {
-  return mi_heap_malloc_zero_aligned_at(mi_prim_get_default_heap(), size, alignment, 0, true, block_size);
+mi_decl_nodiscard mi_decl_restrict void* mi_uzalloc_aligned(size_t size, size_t alignment, size_t* pblock_size) mi_attr_noexcept {
+  mi_page_t* page;
+  void* p = mi_heap_malloc_zero_aligned_at(mi_prim_get_default_heap(), size, alignment, 0, true, &page);
+  if (p!=NULL && pblock_size!=NULL) { *pblock_size = mi_page_block_size(page); }
+  return p;
 }
 
 mi_decl_nodiscard mi_decl_restrict void* mi_calloc_aligned_at(size_t count, size_t size, size_t alignment, size_t offset) mi_attr_noexcept {
@@ -301,20 +309,24 @@
   }
   if (alignment <= sizeof(uintptr_t) && offset==0) return _mi_heap_realloc_zero(heap,p,newsize,zero,NULL,NULL); 
   if (p == NULL) return mi_heap_malloc_zero_aligned_at(heap,newsize,alignment,offset,zero,NULL);
-  size_t size = mi_usable_size(p);
+  const size_t size = mi_usable_size(p);
   if (newsize <= size && newsize >= (size - (size / 2)) && (((uintptr_t)p + offset) & (alignment-1)) == 0) {
     return p;  // reallocation still fits, is aligned and not more than 50% waste
   }
   else {
-    // note: we don't zero allocate upfront so we only zero initialize the expanded part
-    void* newp = mi_heap_malloc_aligned_at(heap,newsize,alignment,offset);
-    if (newp != NULL) {
-      if (zero && newsize > size) {
+    // note: we don't zero allocate upfront so we only zero initialize the expanded part (at the cost of calling mi_usable_size)
+    mi_page_t* newpage;
+    void* const newp = mi_heap_malloc_zero_aligned_at(heap,newsize,alignment,offset,false /* no zero*/, &newpage);
+    if (newp != NULL) {      
+      const size_t copy_size  = (newsize > size ? size : newsize);
+      const size_t zero_start = (copy_size >= sizeof(intptr_t) ? copy_size - sizeof(intptr_t) : 0); // also set last word in the previous allocation to zero to ensure any padding is zero-initialized    
+      const size_t usable     = _mi_page_usable_size(newpage,newp);
+      mi_assert_internal(usable >= newsize); // use usable for zero'ing, issue #763
+      if (zero && usable > zero_start) {
         // also set last word in the previous allocation to zero to ensure any padding is zero-initialized
-        size_t start = (size >= sizeof(intptr_t) ? size - sizeof(intptr_t) : 0);
-        _mi_memzero((uint8_t*)newp + start, newsize - start);
+        _mi_memzero((uint8_t*)newp + zero_start, usable - zero_start);
       }
-      _mi_memcpy(newp, p, (newsize > size ? size : newsize)); // cannot be aligned due to abitrary offset... (todo: require offset to be a multiple of sizeof(void*)?)
+      _mi_memcpy(newp, p, copy_size); // cannot be aligned due to abitrary offset... (todo: require offset to be a multiple of sizeof(void*)?)
       mi_free(p); // only free if successful
     }
     return newp;
diff --git a/src/alloc.c b/src/alloc.c
index 038ecfb..b165523 100644
--- a/src/alloc.c
+++ b/src/alloc.c
@@ -28,7 +28,7 @@
 // Fast allocation in a page: just pop from the free list.
 // Fall back to generic allocation only if the list is empty.
 // Note: in release mode the (inlined) routine is about 7 instructions with a single test.
-extern inline void* _mi_page_malloc_zero(mi_heap_t* heap, mi_page_t* page, size_t size, bool zero, size_t* usable) mi_attr_noexcept
+extern inline void* _mi_page_malloc_zero(mi_heap_t* heap, mi_page_t* page, size_t size, bool zero, mi_page_t** ppage) mi_attr_noexcept
 {
   mi_assert_internal(size >= MI_PADDING_SIZE);
   mi_assert_internal(page->block_size == 0 /* empty heap */ || mi_page_block_size(page) >= size);
@@ -36,10 +36,10 @@
   // check the free list
   mi_block_t* const block = page->free;
   if mi_unlikely(block == NULL) {
-    return _mi_malloc_generic(heap, size, zero, 0, usable);
+    return _mi_malloc_generic(heap, size, zero, 0, ppage);
   }
   mi_assert_internal(block != NULL && _mi_ptr_page(block) == page);
-  if (usable != NULL) { *usable = mi_page_usable_block_size(page); };
+  if (ppage != NULL) { *ppage = page; };
   // pop from the free list
   page->free = mi_block_next(page, block);
   page->used++;
@@ -122,7 +122,7 @@
   return _mi_page_malloc_zero(heap,page,size,true,NULL);
 }
 
-static inline mi_decl_restrict void* mi_heap_malloc_small_zero(mi_heap_t* heap, size_t size, bool zero, size_t* usable) mi_attr_noexcept {
+static inline mi_decl_restrict void* mi_heap_malloc_small_zero(mi_heap_t* heap, size_t size, bool zero, mi_page_t** ppage) mi_attr_noexcept {
   mi_assert(heap != NULL);
   mi_assert(size <= MI_SMALL_SIZE_MAX);
   #if MI_DEBUG
@@ -134,13 +134,13 @@
   #endif
   #if MI_GUARDED
   if (mi_heap_malloc_use_guarded(heap,size)) {
-    return _mi_heap_malloc_guarded(heap, size, zero, usable);
+    return _mi_heap_malloc_guarded(heap, size, zero, ppage);
   }
   #endif
 
   // get page in constant time, and allocate from it
   mi_page_t* page = _mi_heap_get_free_small_page(heap, size + MI_PADDING_SIZE);
-  void* const p = _mi_page_malloc_zero(heap, page, size + MI_PADDING_SIZE, zero, usable);
+  void* const p = _mi_page_malloc_zero(heap, page, size + MI_PADDING_SIZE, zero, ppage);
   mi_track_malloc(p,size,zero);
 
   #if MI_DEBUG>3
@@ -165,22 +165,22 @@
 }
 
 // The main allocation function
-extern inline void* _mi_heap_malloc_zero_ex(mi_heap_t* heap, size_t size, bool zero, size_t huge_alignment, size_t* usable) mi_attr_noexcept {
+extern inline void* _mi_heap_malloc_zero_ex(mi_heap_t* heap, size_t size, bool zero, size_t huge_alignment, mi_page_t** ppage) mi_attr_noexcept {
   // fast path for small objects
   if mi_likely(size <= MI_SMALL_SIZE_MAX) {
     mi_assert_internal(huge_alignment == 0);
-    return mi_heap_malloc_small_zero(heap, size, zero, usable);
+    return mi_heap_malloc_small_zero(heap, size, zero, ppage);
   }
   #if MI_GUARDED
   else if (huge_alignment==0 && mi_heap_malloc_use_guarded(heap,size)) {
-    return _mi_heap_malloc_guarded(heap, size, zero, usable);
+    return _mi_heap_malloc_guarded(heap, size, zero, ppage);
   }
   #endif
   else {
     // regular allocation
     mi_assert(heap!=NULL);
     mi_assert(heap->thread_id == 0 || heap->thread_id == _mi_thread_id());   // heaps are thread local
-    void* const p = _mi_malloc_generic(heap, size + MI_PADDING_SIZE, zero, huge_alignment, usable);  // note: size can overflow but it is detected in malloc_generic
+    void* const p = _mi_malloc_generic(heap, size + MI_PADDING_SIZE, zero, huge_alignment, ppage);  // note: size can overflow but it is detected in malloc_generic
     mi_track_malloc(p,size,zero);
 
     #if MI_DEBUG>3
@@ -228,31 +228,47 @@
   return mi_heap_calloc(mi_prim_get_default_heap(),count,size);
 }
 
-// Return usable size
-mi_decl_nodiscard mi_decl_restrict void* mi_umalloc_small(size_t size, size_t* usable) mi_attr_noexcept {
-  return mi_heap_malloc_small_zero(mi_prim_get_default_heap(), size, false, usable);
+static void* mi_ublock_size( void* p, mi_page_t* page, size_t* pblock_size ) {
+  mi_assert_internal(page == _mi_ptr_page(p));
+  if (pblock_size!=NULL) {
+    if (p!=NULL) { *pblock_size = mi_page_block_size(page); }
+  }
+  return p;
 }
 
-mi_decl_nodiscard mi_decl_restrict void* mi_uzalloc_small(size_t size, size_t* usable) mi_attr_noexcept {
-  return mi_heap_malloc_small_zero(mi_prim_get_default_heap(), size, true, usable);
+// Return the block size
+mi_decl_nodiscard mi_decl_restrict void* mi_umalloc_small(size_t size, size_t* pblock_size) mi_attr_noexcept {
+  mi_page_t* page;
+  void* p = mi_heap_malloc_small_zero(mi_prim_get_default_heap(), size, false, &page);
+  return mi_ublock_size(p,page,pblock_size);
 }
 
-mi_decl_nodiscard mi_decl_restrict void* mi_heap_umalloc(mi_heap_t* heap, size_t size, size_t* usable) mi_attr_noexcept {
-  return _mi_heap_malloc_zero_ex(heap, size, false, 0, usable);
+mi_decl_nodiscard mi_decl_restrict void* mi_uzalloc_small(size_t size, size_t* pblock_size) mi_attr_noexcept {
+  mi_page_t* page;
+  void* p = mi_heap_malloc_small_zero(mi_prim_get_default_heap(), size, true, &page);
+  return mi_ublock_size(p,page,pblock_size);
 }
 
-mi_decl_nodiscard mi_decl_restrict void* mi_umalloc(size_t size, size_t* usable) mi_attr_noexcept {
-  return mi_heap_umalloc(mi_prim_get_default_heap(), size, usable);
+mi_decl_nodiscard mi_decl_restrict void* mi_heap_umalloc(mi_heap_t* heap, size_t size, size_t* pblock_size) mi_attr_noexcept {
+  mi_page_t* page;
+  void* p = _mi_heap_malloc_zero_ex(heap, size, false, 0, &page);
+  return mi_ublock_size(p,page,pblock_size);
 }
 
-mi_decl_nodiscard mi_decl_restrict void* mi_uzalloc(size_t size, size_t* usable) mi_attr_noexcept {
-  return _mi_heap_malloc_zero_ex(mi_prim_get_default_heap(), size, true, 0, usable);
+mi_decl_nodiscard mi_decl_restrict void* mi_umalloc(size_t size, size_t* pblock_size) mi_attr_noexcept {
+  return mi_heap_umalloc(mi_prim_get_default_heap(), size, pblock_size);
 }
 
-mi_decl_nodiscard mi_decl_restrict void* mi_ucalloc(size_t count, size_t size, size_t* usable) mi_attr_noexcept {
+mi_decl_nodiscard mi_decl_restrict void* mi_uzalloc(size_t size, size_t* pblock_size) mi_attr_noexcept {
+  mi_page_t* page;
+  void* p = _mi_heap_malloc_zero_ex(mi_prim_get_default_heap(), size, true, 0, &page);
+  return mi_ublock_size(p,page,pblock_size);
+}
+
+mi_decl_nodiscard mi_decl_restrict void* mi_ucalloc(size_t count, size_t size, size_t* pblock_size) mi_attr_noexcept {
   size_t total;
   if (mi_count_size_overflow(count,size,&total)) return NULL;
-  return mi_uzalloc(total, usable);
+  return mi_uzalloc(total, pblock_size);
 }
 
 // Uninitialized `calloc`
@@ -275,13 +291,13 @@
   #else
   if (p == NULL) return NULL;
   const mi_page_t* const page = mi_validate_ptr_page(p,"mi_expand");  
-  const size_t size = _mi_usable_size(p,page);
+  const size_t size = _mi_page_usable_size(page,p);
   if (newsize > size) return NULL;
   return p; // it fits
   #endif
 }
 
-void* _mi_heap_realloc_zero(mi_heap_t* heap, void* p, size_t newsize, bool zero, size_t* usable_pre, size_t* usable_post) mi_attr_noexcept {
+void* _mi_heap_realloc_zero(mi_heap_t* heap, void* p, size_t newsize, bool zero, size_t* pblock_size_pre, size_t* pblock_size_post) mi_attr_noexcept {
   // if p == NULL then behave as malloc.
   // else if size == 0 then reallocate to a zero-sized block (and don't return NULL, just as mi_malloc(0)).
   // (this means that returning NULL always indicates an error, and `p` will not have been freed in that case.)
@@ -290,40 +306,44 @@
   if (p==NULL) {
     page = NULL;
     size = 0;
-    if (usable_pre!=NULL) { *usable_pre = 0; }
+    if (pblock_size_pre!=NULL) { *pblock_size_pre = 0; }
   }
   else {    
     page = mi_validate_ptr_page(p,"mi_realloc"); 
     if mi_unlikely(page==NULL) {  // invalid pointer
-      if (usable_pre!=NULL) { *usable_pre = 0; }
-      if (usable_post!=NULL) { *usable_post = 0; }  
+      if (pblock_size_pre!=NULL) { *pblock_size_pre = 0; }
+      if (pblock_size_post!=NULL) { *pblock_size_post = 0; }  
       return NULL;
     } 
-    size = _mi_usable_size(p,page);
-    if (usable_pre!=NULL) { *usable_pre = mi_page_usable_block_size(page); }    
+    size = _mi_page_usable_size(page,p);
+    if (pblock_size_pre!=NULL) { *pblock_size_pre = mi_page_block_size(page); }    
   }
   if mi_unlikely(newsize <= size && newsize >= (size / 2) && newsize > 0) {  // note: newsize must be > 0 or otherwise we return NULL for realloc(NULL,0)
     mi_assert_internal(p!=NULL);
     // todo: do not track as the usable size is still the same in the free; adjust potential padding?
     // mi_track_resize(p,size,newsize)
     // if (newsize < size) { mi_track_mem_noaccess((uint8_t*)p + newsize, size - newsize); }
-    if (usable_post!=NULL) { *usable_post = mi_page_usable_block_size(page); }
+    if (pblock_size_post!=NULL) { *pblock_size_post = mi_page_block_size(page); }
     return p;  // reallocation still fits and not more than 50% waste
   }
-  void* newp = mi_heap_umalloc(heap,newsize,usable_post);
+  // note: we don't zero allocate upfront so we only zero initialize the expanded part 
+  mi_page_t* newpage; // use block_size for zero-ing, issue #763
+  void* const newp = _mi_heap_malloc_zero_ex(heap,newsize,false /* no zero */,0,&newpage);
   if mi_likely(newp != NULL) {
-    if (zero && newsize > size) {
-      // also set last word in the previous allocation to zero to ensure any padding is zero-initialized
-      const size_t start = (size >= sizeof(intptr_t) ? size - sizeof(intptr_t) : 0);
-      _mi_memzero((uint8_t*)newp + start, newsize - start);
+    if (pblock_size_post!=NULL) { *pblock_size_post = mi_page_block_size(newpage); }  
+    const size_t copy_size  = (newsize > size ? size : newsize);
+    const size_t zero_start = _mi_align_down( (copy_size >= sizeof(intptr_t) ? copy_size - sizeof(intptr_t) : 0), sizeof(intptr_t)); // also set last word in the previous allocation to zero to ensure any padding is zero-initialized
+    const size_t usable = _mi_page_usable_size(newpage,newp); 
+    mi_assert_internal(usable >= newsize);     
+    if (zero && usable > zero_start) {      
+      _mi_memzero_aligned((uint8_t*)newp + zero_start, usable - zero_start);
     }
     else if (newsize == 0) {
       ((uint8_t*)newp)[0] = 0; // work around for applications that expect zero-reallocation to be zero initialized (issue #725)
     }
     if mi_likely(p != NULL) {
-      const size_t copysize = (newsize > size ? size : newsize);
-      mi_track_mem_defined(p,copysize);  // _mi_useable_size may be too large for byte precise memory tracking..
-      _mi_memcpy(newp, p, copysize);
+      mi_track_mem_defined(p,copy_size);  // _mi_useable_size may be too large for byte precise memory tracking..
+      _mi_memcpy_aligned(newp, p, copy_size);
       mi_free(p); // only free the original pointer if successful
     }
   }
@@ -367,8 +387,8 @@
   return mi_heap_reallocn(mi_prim_get_default_heap(),p,count,size);
 }
 
-mi_decl_nodiscard void* mi_urealloc(void* p, size_t newsize, size_t* usable_pre, size_t* usable_post) mi_attr_noexcept {
-  return _mi_heap_realloc_zero(mi_prim_get_default_heap(),p,newsize, false, usable_pre, usable_post);
+mi_decl_nodiscard void* mi_urealloc(void* p, size_t newsize, size_t* pblock_size_pre, size_t* pblock_size_post) mi_attr_noexcept {
+  return _mi_heap_realloc_zero(mi_prim_get_default_heap(),p,newsize, false, pblock_size_pre, pblock_size_post);
 }
 
 // Reallocate but free `p` on errors
@@ -736,7 +756,7 @@
   return p;
 }
 
-mi_decl_restrict void* _mi_heap_malloc_guarded(mi_heap_t* heap, size_t size, bool zero, size_t* usable) mi_attr_noexcept
+mi_decl_restrict void* _mi_heap_malloc_guarded(mi_heap_t* heap, size_t size, bool zero, mi_page_t** ppage) mi_attr_noexcept
 {
   // allocate multiple of page size ending in a guard page
   // ensure minimal alignment requirement?
@@ -748,11 +768,11 @@
   const size_t obj_size = (mi_option_is_enabled(mi_option_guarded_precise) ? size : _mi_align_up(size, MI_MAX_ALIGN_SIZE));
   const size_t bsize    = _mi_align_up(_mi_align_up(obj_size, MI_MAX_ALIGN_SIZE) + sizeof(mi_block_t), MI_MAX_ALIGN_SIZE);
   const size_t req_size = _mi_align_up(bsize + os_page_size, os_page_size);
-  mi_block_t* const block = (mi_block_t*)_mi_malloc_generic(heap, req_size, false /* don't zero */, 0 /* huge_alignment */, usable);
+  mi_block_t* const block = (mi_block_t*)_mi_malloc_generic(heap, req_size, false /* don't zero */, 0 /* huge_alignment */, ppage);
   if (block==NULL) return NULL;
-  size_t usable_size;
-  void* const p   = mi_block_ptr_set_guarded(block, obj_size, &usable_size);
-  if (p == NULL) return NULL;    
+  size_t usable_size = 0;
+  void* const p = mi_block_ptr_set_guarded(block, obj_size, &usable_size);
+  if (p == NULL) return NULL;
   if (zero) {
     _mi_memzero(p,obj_size);  // we have to zero afterwards as padding might have written inside the block (if the `blocksize > reqsize + os_page_size`)
   }
diff --git a/src/free.c b/src/free.c
index 73cbd5e..9f9350b 100644
--- a/src/free.c
+++ b/src/free.c
@@ -174,17 +174,17 @@
 
 // Free a block
 // Fast path written carefully to prevent register spilling on the stack
-static inline void mi_free_ex(void* p, size_t* usable) mi_attr_noexcept
+static inline void mi_free_ex(void* p, size_t* pblock_size) mi_attr_noexcept
 {
   mi_segment_t* const segment = mi_checked_ptr_segment(p,"mi_free");
   if mi_unlikely(segment==NULL) {
-    if (usable!=NULL) { *usable = 0; }
+    if (pblock_size!=NULL) { *pblock_size = 0; }
     return;
   }
 
   const bool is_local = (_mi_prim_thread_id() == mi_atomic_load_relaxed(&segment->thread_id));
   mi_page_t* const page = _mi_segment_page_of(segment, p);
-  if (usable!=NULL) { *usable = mi_page_usable_block_size(page); }
+  if (pblock_size!=NULL) { *pblock_size = mi_page_block_size(page); }
   
   if mi_likely(is_local) {                        // thread-local free?
     if mi_likely(page->flags.full_aligned == 0) { // and it is not a full page (full pages need to move from the full bin), nor has aligned blocks (aligned blocks need to be unaligned)
@@ -207,8 +207,8 @@
   mi_free_ex(p,NULL);
 }
 
-void mi_ufree(void* p, size_t* usable) mi_attr_noexcept {
-  mi_free_ex(p,usable);
+void mi_ufree(void* p, size_t* pblock_size) mi_attr_noexcept {
+  mi_free_ex(p,pblock_size);
 }
 
 void mi_free_small(void* p) mi_attr_noexcept {
@@ -373,8 +373,9 @@
   return page;
 }
 
-static inline size_t _mi_usable_size(const void* p, const mi_page_t* page) mi_attr_noexcept {
+size_t _mi_page_usable_size(const mi_page_t* page, const void* p) mi_attr_noexcept {
   if mi_unlikely(page==NULL) return 0;
+  mi_assert_internal(mi_validate_ptr_page(p,"_mi_page_usable_size") == page);
   if mi_likely(!mi_page_has_aligned(page)) {
     const mi_block_t* block = mi_validate_block_from_ptr(page,p);
     return mi_page_usable_size_of(page, block, false /* is guarded */);
@@ -387,7 +388,7 @@
 
 mi_decl_nodiscard size_t mi_usable_size(const void* p) mi_attr_noexcept {
   const mi_page_t* const page = mi_validate_ptr_page(p,"mi_usable_size");
-  return _mi_usable_size(p,page);
+  return _mi_page_usable_size(page,p);
 }
 
 
@@ -399,7 +400,7 @@
   MI_UNUSED_RELEASE(size);
   #if MI_DEBUG
   const mi_page_t* const page = mi_validate_ptr_page(p,"mi_free_size");  
-  const size_t available = _mi_usable_size(p,page);
+  const size_t available = _mi_page_usable_size(page,p);
   mi_assert(p == NULL || size <= available || available == 0 /* invalid pointer */ );
   #endif
   mi_free(p);
diff --git a/src/options.c b/src/options.c
index 1ed752b..d139e57 100644
--- a/src/options.c
+++ b/src/options.c
@@ -207,7 +207,7 @@
   const int vermajor = MI_MALLOC_VERSION/10000;
   const int verminor = (MI_MALLOC_VERSION%10000)/100;
   const int verpatch = (MI_MALLOC_VERSION%100);
-  _mi_message("v%i.%i.%i%s%s (built on %s, %s)\n", vermajor, verminor, verpatch,
+  _mi_message("v%i.%i.%i%s%s\n", vermajor, verminor, verpatch,
       #if defined(MI_CMAKE_BUILD_TYPE)
       ", " mi_stringify(MI_CMAKE_BUILD_TYPE)
       #else
@@ -219,7 +219,7 @@
       #else
       ""
       #endif
-      , __DATE__, __TIME__);
+      );
 
   // show options
   for (int i = 0; i < _mi_option_last; i++) {
@@ -570,7 +570,7 @@
     }
   #endif
   #if defined(MI_XMALLOC)
-    if (err==ENOMEM || err==EOVERFLOW || err=EINVAL) { // abort on memory allocation fails in xmalloc mode
+    if (err==ENOMEM || err==EOVERFLOW || err==EINVAL) { // abort on memory allocation fails in xmalloc mode
       abort();
     }
   #endif
diff --git a/src/os.c b/src/os.c
index 3156538..c876bd7 100644
--- a/src/os.c
+++ b/src/os.c
@@ -91,7 +91,6 @@
 bool _mi_os_decommit(void* addr, size_t size);
 bool _mi_os_commit(void* addr, size_t size, bool* is_zero);
 
-
 /* -----------------------------------------------------------
   aligned hinting
 -------------------------------------------------------------- */
diff --git a/src/page.c b/src/page.c
index f7b5780..d188f06 100644
--- a/src/page.c
+++ b/src/page.c
@@ -1019,7 +1019,7 @@
 // Note: in debug mode the size includes MI_PADDING_SIZE and might have overflowed.
 // The `huge_alignment` is normally 0 but is set to a multiple of MI_SLICE_SIZE for
 // very large requested alignments in which case we use a huge singleton page.
-void* _mi_malloc_generic(mi_heap_t* heap, size_t size, bool zero, size_t huge_alignment, size_t* usable) mi_attr_noexcept
+void* _mi_malloc_generic(mi_heap_t* heap, size_t size, bool zero, size_t huge_alignment, mi_page_t** ppage) mi_attr_noexcept
 {
   mi_assert_internal(heap != NULL);
 
@@ -1065,7 +1065,7 @@
   mi_assert_internal(mi_page_block_size(page) >= size);
 
   // and try again, this time succeeding! (i.e. this should never recurse through _mi_page_malloc)
-  void* const p = _mi_page_malloc_zero(heap, page, size, zero, usable);
+  void* const p = _mi_page_malloc_zero(heap, page, size, zero, ppage);
   mi_assert_internal(p != NULL);
 
   // move singleton pages to the full queue
diff --git a/src/prim/unix/prim.c b/src/prim/unix/prim.c
index 65c66cd..6cbdddd 100644
--- a/src/prim/unix/prim.c
+++ b/src/prim/unix/prim.c
@@ -231,7 +231,7 @@
 
 // return errno on failure
 static int unix_madvise(void* addr, size_t size, int advice) {
-  #if defined(__sun)
+  #if defined(__sun) || defined(_AIX)
   const int res = madvise((caddr_t)addr, size, advice);  // Solaris needs cast (issue #520)
   return (res==0 ? 0 : errno);
   #elif defined(__QNX__)
diff --git a/src/prim/windows/prim.c b/src/prim/windows/prim.c
index 7f5c6be..d63412c 100644
--- a/src/prim/windows/prim.c
+++ b/src/prim/windows/prim.c
@@ -79,7 +79,7 @@
 typedef BOOL (__stdcall *PGetPhysicallyInstalledSystemMemory)( PULONGLONG TotalMemoryInKilobytes );
 
 // Load a library
-static HMODULE mi_win_loadlibrary(const TCHAR* library) {  
+static HMODULE mi_win_loadlibrary(const TCHAR* library) {
   #if MI_WIN_DESKTOP
     return LoadLibrary(library);
   #else
@@ -91,7 +91,7 @@
 static HMODULE mi_win_getlibrary(const TCHAR* library, bool* should_free) {
   #if MI_WIN_DESKTOP
   // avoid calling LoadLibrary for "kernel32", "ntdll", and "kernelbase" (also to avoid hitting the loader lock)
-  HMODULE mod = GetModuleHandle(library); 
+  HMODULE mod = GetModuleHandle(library);
   if (mod!=NULL) {
     *should_free = false;
     return mod;
@@ -102,7 +102,7 @@
 }
 
 static void mi_win_freelibrary(HMODULE mod, bool should_free) {
-  if (should_free) { 
+  if (should_free) {
     FreeLibrary(mod);
   }
 }
@@ -612,12 +612,17 @@
   // on windows with redirection, the C runtime cannot handle locale dependent output
   // after the main thread closes so we use direct console output.
   if (!_mi_preloading()) {
-    // _cputs(msg);  // _cputs cannot be used as it aborts when failing to lock the console
     static HANDLE hcon = INVALID_HANDLE_VALUE;
+    #if MI_WIN_DESKTOP
     static bool hconIsConsole = false;
+    #endif
     if (hcon == INVALID_HANDLE_VALUE) {
-      hcon = GetStdHandle(STD_ERROR_HANDLE); // returns NULL on error
+      hcon = GetStdHandle(STD_ERROR_HANDLE);   // returns NULL if no stderr is available
       #if MI_WIN_DESKTOP
+      if (hcon==NULL) {
+        AttachConsole(ATTACH_PARENT_PROCESS);  // if started from a parent console, try to attach to that
+        hcon = GetStdHandle(STD_ERROR_HANDLE);
+      }
       CONSOLE_SCREEN_BUFFER_INFO sbi;
       hconIsConsole = ((hcon != NULL && hcon != INVALID_HANDLE_VALUE) && GetConsoleScreenBufferInfo(hcon, &sbi));
       #endif
@@ -625,19 +630,19 @@
     const size_t len = _mi_strlen(msg);
     if (len > 0 && len < UINT32_MAX) {
       DWORD written = 0;
-      if (hconIsConsole) {
+      if (hcon != NULL && hcon != INVALID_HANDLE_VALUE) {
         #if MI_WIN_DESKTOP
-        WriteConsoleA(hcon, msg, (DWORD)len, &written, NULL);
+        if (hconIsConsole) {
+          WriteConsoleA(hcon, msg, (DWORD)len, &written, NULL);
+        }
+        else
         #endif
+        {
+          // use direct write in case stderr was redirected
+          WriteFile(hcon, msg, (DWORD)len, &written, NULL);
+        }
       }
-      else if (hcon != NULL && hcon != INVALID_HANDLE_VALUE) {
-        // use direct write if stderr was redirected
-        WriteFile(hcon, msg, (DWORD)len, &written, NULL);
-      }
-      else {
-        // finally fall back to fputs after all
-        fputs(msg, stderr);
-      }
+      // don't fall back to fputs or _cputs as the crt can have it locked
     }
   }
 }
@@ -739,6 +744,7 @@
   }
 }
 
+mi_decl_maybe_unused
 static void NTAPI mi_win_main(PVOID module, DWORD reason, LPVOID reserved) {
   MI_UNUSED(reserved);
   MI_UNUSED(module);
@@ -761,17 +767,18 @@
    both static and dynamic linkage (`MI_WIN_INIT_USE_CRT_TLS`).
 ------------------------------------------------------------------------- */
 #if !defined(MI_WIN_INIT_USE_CRT_TLS) && !defined(MI_WIN_INIT_USE_RAW_DLLMAIN) && !defined(MI_WIN_INIT_USE_TLS_DLLMAIN) && !defined(MI_WIN_INIT_USE_FLS)
-  #if defined(__GNUC__) && !defined(_MSC_VER)  /* mingw */
-    #define MI_WIN_INIT_USE_FLS          1     /* needed for v1/v2, see <https://github.com/zackees/mimalloc-pprof/pull/48> */
-  #elif defined(__INTEL_LLVM_COMPILER) || defined(__INTEL_COMPILER)
-    #define MI_WIN_INIT_USE_TLS_DLLMAIN  1     /* needed for Intel ICX, see issue #1268 */    
+  #if defined(__INTEL_LLVM_COMPILER) || defined(__INTEL_COMPILER)
+    #define MI_WIN_INIT_USE_TLS_DLLMAIN  1     /* needed for Intel ICX, see issue #1268 */
   #else
     #define MI_WIN_INIT_USE_CRT_TLS      1     /* default */
   #endif
 #endif
 
 #if defined(MI_WIN_INIT_USE_CRT_TLS)
-  #define MI_PRIM_HAS_PROCESS_ATTACH  1
+  #if !defined(__MINGW32__) || !defined(MI_MINGW_UCRT64)  // on mingw without UCRT use the constructor attribute (in `src/prim/prim.c`)
+  #define MI_PRIM_HAS_PROCESS_ATTACH  1   
+  #endif
+
   // nothing to do since `_mi_thread_done` is handled through the DLL_THREAD_DETACH event.
   void _mi_prim_thread_init_auto_done(void) {}
   void _mi_prim_thread_done_auto_done(void) {}
@@ -876,12 +883,12 @@
     #pragma data_seg(".CRT$XIB")
       mi_crt_callback_t _mi_crt_callback_init[] = { &mi_crt_init };
     #pragma data_seg()
-  #elif defined(__GCC__)  // mingw
+  #elif defined(__MINGW32__)
     extern const IMAGE_TLS_DIRECTORY _tls_used;
     __attribute__((used)) static const void* const mi_tls_used_ref = &_tls_used; // pull in the CRT tls
     __attribute__((used, section(".CRT$XLB"))) PIMAGE_TLS_CALLBACK _mi_tls_callback_pre = &mi_tls_attach;
     __attribute__((used, section(".CRT$XLY"))) PIMAGE_TLS_CALLBACK _mi_tls_callback_post = &mi_tls_detach;
-    __attribute__((used, section(".CRT$XIB"))) mi_crt_callback_t   _mi_crt_callback_init = &mi_crt_init;  
+    __attribute__((used, section(".CRT$XIB"))) mi_crt_callback_t   _mi_crt_callback_init = &mi_crt_init;
   #endif
 
   #if defined(__cplusplus)
@@ -973,11 +980,11 @@
     #pragma data_seg(".CRT$XLY")
       PIMAGE_TLS_CALLBACK _mi_tls_callback_post[] = { &mi_tls_detach };
     #pragma data_seg()
-  #elif defined(__GCC__)  // mingw
+  #elif defined(__MINGW32__)
     extern const IMAGE_TLS_DIRECTORY _tls_used;
     __attribute__((used)) static const void* const mi_tls_used_ref = &_tls_used; // pull in the CRT tls
     __attribute__((used, section(".CRT$XLB"))) PIMAGE_TLS_CALLBACK _mi_tls_callback_pre  = &mi_tls_attach;
-    __attribute__((used, section(".CRT$XLY"))) PIMAGE_TLS_CALLBACK _mi_tls_callback_post = &mi_tls_detach;      
+    __attribute__((used, section(".CRT$XLY"))) PIMAGE_TLS_CALLBACK _mi_tls_callback_post = &mi_tls_detach;
   #endif
 
   #if defined(__cplusplus)
@@ -1047,11 +1054,11 @@
     #pragma data_seg(".CRT$XLY")
     PIMAGE_TLS_CALLBACK _mi_tls_callback_post[] = { &mi_win_main_detach };
     #pragma data_seg()
-  #elif defined(__GCC__)  // mingw
+  #elif defined(__MINGW32__)
     extern const IMAGE_TLS_DIRECTORY _tls_used;
     __attribute__((used)) static const void* const mi_tls_used_ref = &_tls_used; // pull in the CRT tls
     __attribute__((used, section(".CRT$XLB"))) PIMAGE_TLS_CALLBACK _mi_tls_callback_pre  = &mi_tls_attach;
-    __attribute__((used, section(".CRT$XLY"))) PIMAGE_TLS_CALLBACK _mi_tls_callback_post = &mi_tls_detach;    
+    __attribute__((used, section(".CRT$XLY"))) PIMAGE_TLS_CALLBACK _mi_tls_callback_post = &mi_tls_detach;
   #endif
 
   #if defined(__cplusplus)
@@ -1092,8 +1099,8 @@
       #pragma comment(linker, "/INCLUDE:__mi_crt_callback_init")
       #pragma data_seg(".CRT$XIU")
       mi_crt_callback_t _mi_crt_callback_init[] = { &mi_crt_init };
-      #pragma data_seg()    
-    #endif  
+      #pragma data_seg()
+    #endif
     #if defined(__cplusplus)
     }
     #endif
diff --git a/test/test-api.c b/test/test-api.c
index 566d8b3..5814c20 100644
--- a/test/test-api.c
+++ b/test/test-api.c
@@ -54,13 +54,16 @@
 
 static bool test_zero_aligned_first(void);
 
-bool mem_is_zero(uint8_t* p, size_t size) {
+static bool mem_has_vals(const uint8_t* p, size_t size, uint8_t val) {
   if (p==NULL) return false;
   for (size_t i = 0; i < size; ++i) {
-    if (p[i] != 0) return false;
+    if (p[i] != val) return false;
   }
   return true;
 }
+static bool mem_is_zero(const void* p, size_t size) {
+  return mem_has_vals((const uint8_t*)p,size,0);
+}
 
 // ---------------------------------------------------------------------------
 // Main testing
@@ -335,6 +338,20 @@
     mi_free(p);
   };
 
+  CHECK_BODY("rezalloc_aligned_zeros") {  // issue #763
+    size_t alignment = 1024;
+    size_t n = 1024 * 6;
+    void* ptr = mi_zalloc_aligned(n, alignment);
+    assert(mem_is_zero(ptr,n));
+    memset(ptr,123,n/2);
+    
+    ptr = mi_rezalloc_aligned(ptr, n/2, alignment);
+    assert(mem_has_vals((uint8_t*)ptr,n/2,123));
+    
+    ptr = mi_rezalloc_aligned(ptr, n, alignment);
+    assert(mem_has_vals((uint8_t*)ptr,n/2,123));
+    result = mem_is_zero((uint8_t*)ptr + n/2, n/2);    
+  }
   // ---------------------------------------------------
   // Reallocation
   // ---------------------------------------------------
diff --git a/test/test-stress.c b/test/test-stress.c
index 4f5a3d5..8012b39 100644
--- a/test/test-stress.c
+++ b/test/test-stress.c
@@ -344,7 +344,7 @@
   }
   #endif
   mi_collect(true);
-  mi_stats_print(NULL);  
+  mi_stats_print(NULL);
 #endif
   //bench_end_program();
   return 0;