diff --git a/src/options.c b/src/options.c index a93ea89e..4c76ae41 100644 --- a/src/options.c +++ b/src/options.c @@ -9,7 +9,8 @@ terms of the MIT license. A copy of the license can be found in the file #include "mimalloc-atomic.h" #include "prim/prim.h" // mi_prim_out_stderr -#include +#include // FILE +#include // abort #include diff --git a/src/prim/prim-unix.c b/src/prim/prim-unix.c index 495b274e..ce24c24a 100644 --- a/src/prim/prim-unix.c +++ b/src/prim/prim-unix.c @@ -504,7 +504,13 @@ mi_msecs_t _mi_prim_clock_now(void) { // low resolution timer mi_msecs_t _mi_prim_clock_now(void) { - return ((mi_msecs_t)clock() / ((mi_msecs_t)CLOCKS_PER_SEC / 1000)); + #if !defined(CLOCKS_PER_SEC) || (CLOCKS_PER_SEC == 1000) || (CLOCKS_PER_SEC == 0) + return (mi_msecs_t)clock(); + #elif (CLOCKS_PER_SEC < 1000) + return (mi_msecs_t)clock() * (1000 / (mi_msecs_t)CLOCKS_PER_SEC); + #else + return (mi_msecs_t)clock() / ((mi_msecs_t)CLOCKS_PER_SEC / 1000); + #endif } #endif diff --git a/src/prim/prim-wasi.c b/src/prim/prim-wasi.c index 7de491e4..c37b0847 100644 --- a/src/prim/prim-wasi.c +++ b/src/prim/prim-wasi.c @@ -110,7 +110,7 @@ static void* mi_prim_mem_grow(size_t size, size_t try_alignment) { // Note: the `try_alignment` is just a hint and the returned pointer is not guaranteed to be aligned. void* _mi_prim_alloc(size_t size, size_t try_alignment, bool commit, bool allow_large, bool* is_large) { - MI_UNUSED(allow_large); + MI_UNUSED(allow_large); MI_UNUSED(commit); *is_large = false; return mi_prim_mem_grow(size, try_alignment); } @@ -176,7 +176,13 @@ mi_msecs_t _mi_prim_clock_now(void) { // low resolution timer mi_msecs_t _mi_prim_clock_now(void) { - return ((mi_msecs_t)clock() / ((mi_msecs_t)CLOCKS_PER_SEC / 1000)); + #if !defined(CLOCKS_PER_SEC) || (CLOCKS_PER_SEC == 1000) || (CLOCKS_PER_SEC == 0) + return (mi_msecs_t)clock(); + #elif (CLOCKS_PER_SEC < 1000) + return (mi_msecs_t)clock() * (1000 / (mi_msecs_t)CLOCKS_PER_SEC); + #else + return (mi_msecs_t)clock() / ((mi_msecs_t)CLOCKS_PER_SEC / 1000); + #endif } #endif diff --git a/src/prim/prim-windows.c b/src/prim/prim-windows.c index 97b1936f..f6f1a9db 100644 --- a/src/prim/prim-windows.c +++ b/src/prim/prim-windows.c @@ -408,6 +408,10 @@ mi_msecs_t _mi_prim_clock_now(void) { } +//---------------------------------------------------------------- +// Process Info +//---------------------------------------------------------------- + #include #include @@ -470,7 +474,7 @@ void _mi_prim_out_stderr( const char* msg ) hcon = GetStdHandle(STD_ERROR_HANDLE); hconIsConsole = ((hcon != INVALID_HANDLE_VALUE) && GetConsoleScreenBufferInfo(hcon, &sbi)); } - const size_t len = strlen(msg); + const size_t len = _mi_strlen(msg); if (len > 0 && len < UINT32_MAX) { DWORD written = 0; if (hconIsConsole) { @@ -501,4 +505,4 @@ bool _mi_prim_getenv(const char* name, char* result, size_t result_size) { result[0] = 0; size_t len = GetEnvironmentVariableA(name, result, (DWORD)result_size); return (len > 0 && len < result_size); -} \ No newline at end of file +} diff --git a/src/prim/prim.h b/src/prim/prim.h index 734d02bf..2cbbc85c 100644 --- a/src/prim/prim.h +++ b/src/prim/prim.h @@ -9,9 +9,9 @@ terms of the MIT license. A copy of the license can be found in the file #define MIMALLOC_PRIM_H // note: on all primitive functions, we always get: -// addr != NULL and page aligned -// size > 0 and page aligned -// +// addr != NULL and page aligned +// size > 0 and page aligned + // OS memory configuration typedef struct mi_os_mem_config_s { @@ -26,12 +26,10 @@ typedef struct mi_os_mem_config_s { void _mi_prim_mem_init( mi_os_mem_config_t* config ); // Free OS memory -// pre: addr != NULL, size > 0 void _mi_prim_free(void* addr, size_t size ); -// Allocate OS memory. +// Allocate OS memory. Return NULL on error. // The `try_alignment` is just a hint and the returned pointer does not have to be aligned. -// return NULL on error. // pre: !commit => !allow_large // try_alignment >= _mi_os_page_size() and a power of 2 void* _mi_prim_alloc(size_t size, size_t try_alignment, bool commit, bool allow_large, bool* is_large); @@ -58,23 +56,20 @@ size_t _mi_prim_numa_node(void); // Return the number of logical NUMA nodes size_t _mi_prim_numa_node_count(void); -// High resolution clock +// Clock ticks mi_msecs_t _mi_prim_clock_now(void); -// Return process information +// Return process information (only for statistics) void _mi_prim_process_info(mi_msecs_t* utime, mi_msecs_t* stime, size_t* current_rss, size_t* peak_rss, size_t* current_commit, size_t* peak_commit, size_t* page_faults); -// Default stderr output. -// msg != NULL && strlen(msg) > 0 +// Default stderr output. (only for warnings etc. with verbose enabled) +// msg != NULL && _mi_strlen(msg) > 0 void _mi_prim_out_stderr( const char* msg ); -// Get an environment variable. +// Get an environment variable. (only for options) // name != NULL, result != NULL, result_size >= 64 bool _mi_prim_getenv(const char* name, char* result, size_t result_size); - #endif // MIMALLOC_PRIM_H - -