Skip to content

Commit a3fb949

Browse files
committed
merge from dev
2 parents 26799d8 + 86a15cc commit a3fb949

7 files changed

Lines changed: 38 additions & 15 deletions

File tree

include/mimalloc/internal.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ void _mi_strlcat(char* dest, const char* src, size_t dest_size);
131131
size_t _mi_strlen(const char* s);
132132
size_t _mi_strnlen(const char* s, size_t max_len);
133133
char* _mi_strnstr(char* s, size_t max_len, const char* pat);
134+
bool _mi_streq(const char* s, const char* t);
134135
bool _mi_getenv(const char* name, char* result, size_t result_size);
135136

136137
// "options.c"

src/libc.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,23 @@ char _mi_toupper(char c) {
2222
}
2323

2424
int _mi_strnicmp(const char* s, const char* t, size_t n) {
25+
mi_assert_internal(s!=NULL && t!=NULL);
2526
if (n == 0) return 0;
2627
for (; *s != 0 && *t != 0 && n > 0; s++, t++, n--) {
2728
if (_mi_toupper(*s) != _mi_toupper(*t)) break;
2829
}
2930
return (n == 0 ? 0 : *s - *t);
3031
}
3132

33+
bool _mi_streq(const char* s, const char* t) {
34+
if (s==NULL && t==NULL) return true;
35+
if (s==NULL || t==NULL) return false;
36+
for (; *s != 0 && *t != 0; s++, t++) {
37+
if (*s != *t) break;
38+
}
39+
return (*s == *t);
40+
}
41+
3242
void _mi_strlcpy(char* dest, const char* src, size_t dest_size) {
3343
if (dest==NULL || src==NULL || dest_size == 0) return;
3444
// copy until end of src, or when dest is (almost) full

src/options.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -647,11 +647,11 @@ static void mi_option_init(mi_option_desc_t* desc) {
647647
buf[i] = _mi_toupper(s[i]);
648648
}
649649
buf[len] = 0;
650-
if (buf[0] == 0 || strstr("1;TRUE;YES;ON", buf) != NULL) {
650+
if (buf[0] == 0 || _mi_streq(buf,"1") || _mi_streq(buf,"TRUE") || _mi_streq(buf,"YES") || _mi_streq(buf,"ON")) {
651651
desc->value = 1;
652652
desc->init = MI_OPTION_INITIALIZED;
653653
}
654-
else if (strstr("0;FALSE;NO;OFF", buf) != NULL) {
654+
else if (_mi_streq(buf,"0") || _mi_streq(buf,"FALSE") || _mi_streq(buf,"NO") || _mi_streq(buf,"OFF")) {
655655
desc->value = 0;
656656
desc->init = MI_OPTION_INITIALIZED;
657657
}

src/page-queue.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,9 +115,12 @@ mi_decl_nodiscard mi_decl_export size_t mi_good_size(size_t size) mi_attr_noexce
115115
if (size <= MI_LARGE_MAX_OBJ_SIZE) {
116116
return _mi_bin_size(mi_bin(size + MI_PADDING_SIZE));
117117
}
118-
else {
118+
else if (size <= MI_MAX_ALLOC_SIZE) {
119119
return _mi_align_up(size + MI_PADDING_SIZE,_mi_os_page_size());
120120
}
121+
else {
122+
return size;
123+
}
121124
}
122125

123126
#if (MI_DEBUG>1)

src/prim/osx/alloc-override-zone.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -256,9 +256,8 @@ static malloc_zone_t mi_malloc_zone = {
256256

257257
static inline malloc_zone_t* mi_get_default_zone(void)
258258
{
259-
static bool init;
260-
if mi_unlikely(!init) {
261-
init = true;
259+
static mi_atomic_once_t init;
260+
if (mi_atomic_once(&init)) {
262261
malloc_zone_register(&mi_malloc_zone); // by calling register we avoid a zone error on free (see <http://eatmyrandom.blogspot.com/2010/03/mallocfree-interception-on-mac-os-x.html>)
263262
}
264263
return &mi_malloc_zone;
@@ -329,7 +328,7 @@ static bool zone_check(malloc_zone_t* zone) {
329328

330329
static malloc_zone_t* zone_from_ptr(const void* p) {
331330
MI_UNUSED(p);
332-
return mi_get_default_zone();
331+
return (mi_is_in_heap_region(p) ? mi_get_default_zone() : NULL);
333332
}
334333

335334
static void zone_log(malloc_zone_t* zone, void* p) {

src/prim/unix/prim.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,8 +185,16 @@ static void unix_detect_physical_memory( size_t page_size, size_t* physical_memo
185185
MI_UNUSED(page_size);
186186
struct sysinfo info; _mi_memzero_var(info);
187187
const int err = sysinfo(&info);
188-
if (err==0 && info.totalram > 0 && info.totalram <= SIZE_MAX) {
189-
*physical_memory_in_kib = (size_t)info.totalram / MI_KiB;
188+
if (err==0 && info.mem_unit > 0 && info.totalram <= SIZE_MAX) {
189+
if (info.mem_unit==MI_KiB) {
190+
*physical_memory_in_kib = (size_t)info.totalram;
191+
}
192+
else {
193+
size_t total = 0;
194+
if (!mi_mul_overflow((size_t)info.totalram, (size_t)info.mem_unit, &total)) {
195+
*physical_memory_in_kib = (total / MI_KiB);
196+
}
197+
}
190198
}
191199
#elif defined(_SC_PHYS_PAGES) // do not use by default as it might cause allocation (by using `fopen` to parse /proc/meminfo) (issue #1100)
192200
const long pphys = sysconf(_SC_PHYS_PAGES);

src/prim/windows/prim.c

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,11 @@ typedef BOOL (__stdcall* PGetVersionExW)(LPOSVERSIONINFOW lpVersionInformation);
9292

9393
static bool win_enable_large_os_pages(size_t* large_page_size)
9494
{
95-
static bool large_initialized = false;
96-
if (large_initialized) return (_mi_os_large_page_size() > 0);
97-
large_initialized = true;
95+
static mi_atomic_once_t large_initialized;
96+
if (!mi_atomic_once(&large_initialized)) {
97+
return (_mi_os_large_page_size() > 0);
98+
}
99+
98100
if (pGetLargePageMinimum==NULL) return false; // no large page support (xbox etc.)
99101

100102
// Try to see if large OS pages are supported
@@ -417,8 +419,8 @@ static void* _mi_prim_alloc_huge_os_pagesx(void* hint_addr, size_t size, int num
417419

418420
MI_MEM_EXTENDED_PARAMETER params[3] = { {{0,0},{0}},{{0,0},{0}},{{0,0},{0}} };
419421
// on modern Windows try use NtAllocateVirtualMemoryEx for 1GiB huge pages
420-
static bool mi_huge_pages_available = true;
421-
if (pNtAllocateVirtualMemoryEx != NULL && mi_huge_pages_available) {
422+
static _Atomic(size_t) mi_huge_pages_available = ATOMIC_VAR_INIT(1);
423+
if (pNtAllocateVirtualMemoryEx != NULL && mi_atomic_load_acquire(&mi_huge_pages_available) != 0) {
422424
params[0].Type.Type = MiMemExtendedParameterAttributeFlags;
423425
params[0].Arg.ULong64 = MI_MEM_EXTENDED_PARAMETER_NONPAGED_HUGE;
424426
ULONG param_count = 1;
@@ -435,7 +437,7 @@ static void* _mi_prim_alloc_huge_os_pagesx(void* hint_addr, size_t size, int num
435437
}
436438
else {
437439
// fall back to regular large pages
438-
mi_huge_pages_available = false; // don't try further huge pages
440+
mi_atomic_store_release(&mi_huge_pages_available,0); // don't try further huge pages
439441
_mi_warning_message("unable to allocate using huge (1GiB) pages, trying large (2MiB) pages instead (status 0x%lx)\n", err);
440442
}
441443
}

0 commit comments

Comments
 (0)