merge and improve to upper case in options

This commit is contained in:
daan 2019-07-22 10:06:45 -07:00
commit 959d9289b1
6 changed files with 34 additions and 19 deletions

View file

@ -808,7 +808,7 @@ library so all calls to the standard `malloc` interface are
resolved to the _mimalloc_ library. resolved to the _mimalloc_ library.
- `env LD_PRELOAD=/usr/lib/libmimalloc.so myprogram` (on Linux, BSD, etc.) - `env LD_PRELOAD=/usr/lib/libmimalloc.so myprogram` (on Linux, BSD, etc.)
- `env DYLD_INSERT_LIBRARIES=usr/lib/libmimalloc.dylib myprogram` (On macOS) - `env DYLD_INSERT_LIBRARIES=/usr/lib/libmimalloc.dylib myprogram` (On macOS)
Note certain security restrictions may apply when doing this from Note certain security restrictions may apply when doing this from
the [shell](https://stackoverflow.com/questions/43941322/dyld-insert-libraries-ignored-when-calling-application-through-bash). the [shell](https://stackoverflow.com/questions/43941322/dyld-insert-libraries-ignored-when-calling-application-through-bash).

View file

@ -109,7 +109,7 @@ $(document).ready(function(){initNavTree('overrides.html','');});
<p>On these systems we preload the mimalloc shared library so all calls to the standard <code>malloc</code> interface are resolved to the <em>mimalloc</em> library.</p> <p>On these systems we preload the mimalloc shared library so all calls to the standard <code>malloc</code> interface are resolved to the <em>mimalloc</em> library.</p>
<ul> <ul>
<li><code>env LD_PRELOAD=/usr/lib/libmimalloc.so myprogram</code> (on Linux, BSD, etc.)</li> <li><code>env LD_PRELOAD=/usr/lib/libmimalloc.so myprogram</code> (on Linux, BSD, etc.)</li>
<li><p class="startli"><code>env DYLD_INSERT_LIBRARIES=usr/lib/libmimalloc.dylib myprogram</code> (On macOS)</p> <li><p class="startli"><code>env DYLD_INSERT_LIBRARIES=/usr/lib/libmimalloc.dylib myprogram</code> (On macOS)</p>
<p class="startli">Note certain security restrictions may apply when doing this from the <a href="https://stackoverflow.com/questions/43941322/dyld-insert-libraries-ignored-when-calling-application-through-bash">shell</a>.</p> <p class="startli">Note certain security restrictions may apply when doing this from the <a href="https://stackoverflow.com/questions/43941322/dyld-insert-libraries-ignored-when-calling-application-through-bash">shell</a>.</p>
</li> </li>
</ul> </ul>

View file

@ -117,6 +117,9 @@ bool _mi_page_is_valid(mi_page_t* page);
#define mi_likely(x) (x) #define mi_likely(x) (x)
#endif #endif
#ifndef __has_builtin
#define __has_builtin(x) 0
#endif
#if defined(_MSC_VER) #if defined(_MSC_VER)
#define mi_decl_noinline __declspec(noinline) #define mi_decl_noinline __declspec(noinline)
@ -149,9 +152,17 @@ bool _mi_page_is_valid(mi_page_t* page);
// Overflow detecting multiply // Overflow detecting multiply
#define MI_MUL_NO_OVERFLOW ((size_t)1 << (4*sizeof(size_t))) // sqrt(SIZE_MAX) #define MI_MUL_NO_OVERFLOW ((size_t)1 << (4*sizeof(size_t))) // sqrt(SIZE_MAX)
static inline bool mi_mul_overflow(size_t size, size_t count, size_t* total) { static inline bool mi_mul_overflow(size_t size, size_t count, size_t* total) {
#if __has_builtin(__builtin_umul_overflow) || __GNUC__ >= 5
#if (MI_INTPTR_SIZE == 4)
return __builtin_umul_overflow(size, count, total);
#else
return __builtin_umull_overflow(size, count, total);
#endif
#else /* __builtin_umul_overflow is unavailable */
*total = size * count; *total = size * count;
return ((size >= MI_MUL_NO_OVERFLOW || count >= MI_MUL_NO_OVERFLOW) return ((size >= MI_MUL_NO_OVERFLOW || count >= MI_MUL_NO_OVERFLOW)
&& size > 0 && (SIZE_MAX / size) < count); && size > 0 && (SIZE_MAX / size) < count);
#endif
} }
// Align a byte size to a size in _machine words_, // Align a byte size to a size in _machine words_,

View file

@ -191,7 +191,7 @@ library so all calls to the standard `malloc` interface are
resolved to the _mimalloc_ library. resolved to the _mimalloc_ library.
- `env LD_PRELOAD=/usr/lib/libmimalloc.so myprogram` (on Linux, BSD, etc.) - `env LD_PRELOAD=/usr/lib/libmimalloc.so myprogram` (on Linux, BSD, etc.)
- `env DYLD_INSERT_LIBRARIES=usr/lib/libmimalloc.dylib myprogram` (On macOS) - `env DYLD_INSERT_LIBRARIES=/usr/lib/libmimalloc.dylib myprogram` (On macOS)
Note certain security restrictions may apply when doing this from Note certain security restrictions may apply when doing this from
the [shell](https://stackoverflow.com/questions/43941322/dyld-insert-libraries-ignored-when-calling-application-through-bash). the [shell](https://stackoverflow.com/questions/43941322/dyld-insert-libraries-ignored-when-calling-application-through-bash).

View file

@ -59,7 +59,7 @@ static mi_option_desc_t options[_mi_option_last] =
{ 0, UNINIT, "large_os_pages" }, // use large OS pages, use only with eager commit to prevent fragmentation of VMA's { 0, UNINIT, "large_os_pages" }, // use large OS pages, use only with eager commit to prevent fragmentation of VMA's
{ 0, UNINIT, "page_reset" }, { 0, UNINIT, "page_reset" },
{ 0, UNINIT, "cache_reset" }, { 0, UNINIT, "cache_reset" },
{ 0, UNINIT, "reset_decommits" }, // note: cannot enable this if secure is on { 0, UNINIT, "reset_decommits" }, // note: cannot enable this if secure is on
{ 0, UNINIT, "reset_discards" } // note: cannot enable this if secure is on { 0, UNINIT, "reset_discards" } // note: cannot enable this if secure is on
}; };
@ -68,7 +68,7 @@ static void mi_option_init(mi_option_desc_t* desc);
long mi_option_get(mi_option_t option) { long mi_option_get(mi_option_t option) {
mi_assert(option >= 0 && option < _mi_option_last); mi_assert(option >= 0 && option < _mi_option_last);
mi_option_desc_t* desc = &options[option]; mi_option_desc_t* desc = &options[option];
if (desc->init == UNINIT) { if (mi_unlikely(desc->init == UNINIT)) {
mi_option_init(desc); mi_option_init(desc);
if (option != mi_option_verbose) { if (option != mi_option_verbose) {
_mi_verbose_message("option '%s': %ld\n", desc->name, desc->value); _mi_verbose_message("option '%s': %ld\n", desc->name, desc->value);
@ -116,15 +116,15 @@ static void mi_vfprintf( FILE* out, const char* prefix, const char* fmt, va_list
char buf[256]; char buf[256];
if (fmt==NULL) return; if (fmt==NULL) return;
if (out==NULL) out = stdout; if (out==NULL) out = stdout;
if (_mi_preloading()) return; if (_mi_preloading()) return;
vsnprintf(buf,sizeof(buf)-1,fmt,args); vsnprintf(buf,sizeof(buf)-1,fmt,args);
#ifdef _WIN32 #ifdef _WIN32
// on windows with redirection, the C runtime uses us and we cannot call `fputs` // on windows with redirection, the C runtime uses us and we cannot call `fputs`
// while called from the C runtime itself, so use a non-locking option // while called from the C runtime itself, so use a non-locking option
if (out==stderr) { if (out==stderr) {
if (prefix != NULL) _cputs(prefix); if (prefix != NULL) _cputs(prefix);
_cputs(buf); _cputs(buf);
return; return;
} }
#endif #endif
if (prefix != NULL) fputs(prefix,out); if (prefix != NULL) fputs(prefix,out);
@ -204,10 +204,12 @@ static const char* mi_getenv(const char* name) {
const char* s = getenv(name); const char* s = getenv(name);
if (s == NULL) { if (s == NULL) {
char buf[64+1]; char buf[64+1];
mi_strlcpy(buf,name,64); size_t len = strlen(name);
for (size_t i = 0; i < strlen(buf); i++) { if (len >= sizeof(buf)) len = sizeof(buf) - 1;
for (size_t i = 0; i < len; i++) {
buf[i] = toupper(name[i]); buf[i] = toupper(name[i]);
} }
buf[len] = 0;
#pragma warning(suppress:4996) #pragma warning(suppress:4996)
s = getenv(buf); s = getenv(buf);
} }
@ -217,15 +219,17 @@ static const char* mi_getenv(const char* name) {
static void mi_option_init(mi_option_desc_t* desc) { static void mi_option_init(mi_option_desc_t* desc) {
if (!_mi_preloading()) desc->init = DEFAULTED; if (!_mi_preloading()) desc->init = DEFAULTED;
// Read option value from the environment // Read option value from the environment
char buf[64]; char buf[64+1];
mi_strlcpy(buf, "mimalloc_", sizeof(buf)); mi_strlcpy(buf, "mimalloc_", sizeof(buf));
mi_strlcat(buf, desc->name, sizeof(buf)); mi_strlcat(buf, desc->name, sizeof(buf));
const char* s = mi_getenv(buf); const char* s = mi_getenv(buf);
if (s != NULL) { if (s != NULL) {
mi_strlcpy(buf, s, sizeof(buf)); size_t len = strlen(s);
for (size_t i = 0; i < strlen(buf); i++) { if (len >= sizeof(buf)) len = sizeof(buf) - 1;
buf[i] = toupper(buf[i]); for (size_t i = 0; i < len; i++) {
buf[i] = toupper(s[i]);
} }
buf[len] = 0;
if (buf[0]==0 || strstr("1;TRUE;YES;ON", buf) != NULL) { if (buf[0]==0 || strstr("1;TRUE;YES;ON", buf) != NULL) {
desc->value = 1; desc->value = 1;
desc->init = INITIALIZED; desc->init = INITIALIZED;

View file

@ -209,7 +209,7 @@ static void* mi_win_virtual_alloc(void* addr, size_t size, size_t try_alignment,
void* p = NULL; void* p = NULL;
if (use_large_os_page(size, try_alignment)) { if (use_large_os_page(size, try_alignment)) {
if (large_page_try_ok > 0) { if (large_page_try_ok > 0) {
// if a large page page allocation fails, it seems the calls to VirtualAlloc get very expensive. // if a large page allocation fails, it seems the calls to VirtualAlloc get very expensive.
// therefore, once a large page allocation failed, we don't try again for `large_page_try_ok` times. // therefore, once a large page allocation failed, we don't try again for `large_page_try_ok` times.
large_page_try_ok--; large_page_try_ok--;
} }