small fixes

This commit is contained in:
Daan Leijen 2023-03-14 18:24:38 -07:00
parent 10f62eb5a1
commit 4348a05d0f
5 changed files with 32 additions and 20 deletions

View file

@ -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 "mimalloc-atomic.h"
#include "prim/prim.h" // mi_prim_out_stderr #include "prim/prim.h" // mi_prim_out_stderr
#include <stdio.h> #include <stdio.h> // FILE
#include <stdlib.h> // abort
#include <stdarg.h> #include <stdarg.h>

View file

@ -504,7 +504,13 @@ mi_msecs_t _mi_prim_clock_now(void) {
// low resolution timer // low resolution timer
mi_msecs_t _mi_prim_clock_now(void) { 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 #endif

View file

@ -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. // 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) { 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; *is_large = false;
return mi_prim_mem_grow(size, try_alignment); return mi_prim_mem_grow(size, try_alignment);
} }
@ -176,7 +176,13 @@ mi_msecs_t _mi_prim_clock_now(void) {
// low resolution timer // low resolution timer
mi_msecs_t _mi_prim_clock_now(void) { 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 #endif

View file

@ -408,6 +408,10 @@ mi_msecs_t _mi_prim_clock_now(void) {
} }
//----------------------------------------------------------------
// Process Info
//----------------------------------------------------------------
#include <windows.h> #include <windows.h>
#include <psapi.h> #include <psapi.h>
@ -470,7 +474,7 @@ void _mi_prim_out_stderr( const char* msg )
hcon = GetStdHandle(STD_ERROR_HANDLE); hcon = GetStdHandle(STD_ERROR_HANDLE);
hconIsConsole = ((hcon != INVALID_HANDLE_VALUE) && GetConsoleScreenBufferInfo(hcon, &sbi)); 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) { if (len > 0 && len < UINT32_MAX) {
DWORD written = 0; DWORD written = 0;
if (hconIsConsole) { if (hconIsConsole) {

View file

@ -11,7 +11,7 @@ terms of the MIT license. A copy of the license can be found in the file
// note: on all primitive functions, we always get: // note: on all primitive functions, we always get:
// addr != NULL and page aligned // addr != NULL and page aligned
// size > 0 and page aligned // size > 0 and page aligned
//
// OS memory configuration // OS memory configuration
typedef struct mi_os_mem_config_s { 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 ); void _mi_prim_mem_init( mi_os_mem_config_t* config );
// Free OS memory // Free OS memory
// pre: addr != NULL, size > 0
void _mi_prim_free(void* addr, size_t size ); 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. // 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 // pre: !commit => !allow_large
// try_alignment >= _mi_os_page_size() and a power of 2 // 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); 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 // Return the number of logical NUMA nodes
size_t _mi_prim_numa_node_count(void); size_t _mi_prim_numa_node_count(void);
// High resolution clock // Clock ticks
mi_msecs_t _mi_prim_clock_now(void); 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, void _mi_prim_process_info(mi_msecs_t* utime, mi_msecs_t* stime,
size_t* current_rss, size_t* peak_rss, size_t* current_rss, size_t* peak_rss,
size_t* current_commit, size_t* peak_commit, size_t* page_faults); size_t* current_commit, size_t* peak_commit, size_t* page_faults);
// Default stderr output. // Default stderr output. (only for warnings etc. with verbose enabled)
// msg != NULL && strlen(msg) > 0 // msg != NULL && _mi_strlen(msg) > 0
void _mi_prim_out_stderr( const char* msg ); 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 // name != NULL, result != NULL, result_size >= 64
bool _mi_prim_getenv(const char* name, char* result, size_t result_size); bool _mi_prim_getenv(const char* name, char* result, size_t result_size);
#endif // MIMALLOC_PRIM_H #endif // MIMALLOC_PRIM_H