Merge branch 'dev' into dev-debug

This commit is contained in:
daan 2020-05-19 10:43:58 -07:00
commit bb6e8616ff
6 changed files with 77 additions and 21 deletions

View File

@ -282,7 +282,7 @@ static inline bool mi_count_size_overflow(size_t count, size_t size, size_t* tot
return false; return false;
} }
else if (mi_unlikely(mi_mul_overflow(count, size, total))) { else if (mi_unlikely(mi_mul_overflow(count, size, total))) {
_mi_error_message(EOVERFLOW, "allocation request too large (%zu * %zu bytes)\n", count, size); _mi_error_message(EOVERFLOW, "allocation request is too large (%zu * %zu bytes)\n", count, size);
*total = SIZE_MAX; *total = SIZE_MAX;
return true; return true;
} }

View File

@ -17,8 +17,7 @@ terms of the MIT license. A copy of the license can be found in the file
static mi_decl_restrict void* mi_base_malloc_zero_aligned_at(mi_heap_t* const heap, const size_t size, const size_t alignment, const size_t offset, const bool zero MI_SOURCE_XPARAM) mi_attr_noexcept { static mi_decl_restrict void* mi_base_malloc_zero_aligned_at(mi_heap_t* const heap, const size_t size, const size_t alignment, const size_t offset, const bool zero MI_SOURCE_XPARAM) mi_attr_noexcept {
// note: we don't require `size > offset`, we just guarantee that // note: we don't require `size > offset`, we just guarantee that
// the address at offset is aligned regardless of the allocated size. // the address at offset is aligned regardless of the allocated size.
mi_assert(alignment > 0 && alignment % sizeof(void*) == 0); mi_assert(alignment > 0);
if (mi_unlikely(size > PTRDIFF_MAX)) return NULL; // we don't allocate more than PTRDIFF_MAX (see <https://sourceware.org/ml/libc-announce/2019/msg00001.html>) if (mi_unlikely(size > PTRDIFF_MAX)) return NULL; // we don't allocate more than PTRDIFF_MAX (see <https://sourceware.org/ml/libc-announce/2019/msg00001.html>)
if (mi_unlikely(alignment==0 || !_mi_is_power_of_two(alignment))) return NULL; // require power-of-two (see <https://en.cppreference.com/w/c/memory/aligned_alloc>) if (mi_unlikely(alignment==0 || !_mi_is_power_of_two(alignment))) return NULL; // require power-of-two (see <https://en.cppreference.com/w/c/memory/aligned_alloc>)
const uintptr_t align_mask = alignment-1; // for any x, `(x & align_mask) == (x % alignment)` const uintptr_t align_mask = alignment-1; // for any x, `(x & align_mask) == (x % alignment)`
@ -55,7 +54,7 @@ static mi_decl_restrict void* mi_base_malloc_zero_aligned_at(mi_heap_t* const he
// .. and align within the allocation // .. and align within the allocation
uintptr_t adjust = alignment - (((uintptr_t)p + offset) & align_mask); uintptr_t adjust = alignment - (((uintptr_t)p + offset) & align_mask);
mi_assert_internal(adjust % sizeof(uintptr_t) == 0); mi_assert_internal(adjust <= alignment);
void* aligned_p = (adjust == alignment ? p : (void*)((uintptr_t)p + adjust)); void* aligned_p = (adjust == alignment ? p : (void*)((uintptr_t)p + adjust));
if (aligned_p != p) mi_page_set_has_aligned(_mi_ptr_page(p), true); if (aligned_p != p) mi_page_set_has_aligned(_mi_ptr_page(p), true);
mi_assert_internal(((uintptr_t)aligned_p + offset) % alignment == 0); mi_assert_internal(((uintptr_t)aligned_p + offset) % alignment == 0);

View File

@ -522,6 +522,14 @@ static void mi_strlcat(char* dest, const char* src, size_t dest_size) {
dest[dest_size - 1] = 0; dest[dest_size - 1] = 0;
} }
static inline int mi_strnicmp(const char* s, const char* t, size_t n) {
if (n==0) return 0;
for (; *s != 0 && *t != 0 && n > 0; s++, t++, n--) {
if (toupper(*s) != toupper(*t)) break;
}
return (n==0 ? 0 : *s - *t);
}
#if defined _WIN32 #if defined _WIN32
// On Windows use GetEnvironmentVariable instead of getenv to work // On Windows use GetEnvironmentVariable instead of getenv to work
// reliably even when this is invoked before the C runtime is initialized. // reliably even when this is invoked before the C runtime is initialized.
@ -533,11 +541,45 @@ static bool mi_getenv(const char* name, char* result, size_t result_size) {
size_t len = GetEnvironmentVariableA(name, result, (DWORD)result_size); size_t len = GetEnvironmentVariableA(name, result, (DWORD)result_size);
return (len > 0 && len < result_size); return (len > 0 && len < result_size);
} }
#else #elif !defined(MI_USE_ENVIRON) || (MI_USE_ENVIRON!=0)
// On Posix systemsr use `environ` to acces environment variables
// even before the C runtime is initialized.
#if defined(__APPLE__)
#include <crt_externs.h>
static char** mi_get_environ(void) {
return (*_NSGetEnviron());
}
#else
extern char** environ;
static char** mi_get_environ(void) {
return environ;
}
#endif
static bool mi_getenv(const char* name, char* result, size_t result_size) { static bool mi_getenv(const char* name, char* result, size_t result_size) {
if (name==NULL) return false;
const size_t len = strlen(name);
if (len == 0) return false;
char** env = mi_get_environ();
if (env == NULL) return false;
// compare up to 256 entries
for (int i = 0; i < 256 && env[i] != NULL; i++) {
const char* s = env[i];
if (mi_strnicmp(name, s, len) == 0 && s[len] == '=') { // case insensitive
// found it
mi_strlcpy(result, s + len + 1, result_size);
return true;
}
}
return false;
}
#else
// fallback: use standard C `getenv` but this cannot be used while initializing the C runtime
static bool mi_getenv(const char* name, char* result, size_t result_size) {
// cannot call getenv() when still initializing the C runtime.
if (_mi_preloading()) return false;
const char* s = getenv(name); const char* s = getenv(name);
if (s == NULL) { if (s == NULL) {
// in unix environments we check the upper case name too. // we check the upper case name too.
char buf[64+1]; char buf[64+1];
size_t len = strlen(name); size_t len = strlen(name);
if (len >= sizeof(buf)) len = sizeof(buf) - 1; if (len >= sizeof(buf)) len = sizeof(buf) - 1;
@ -556,11 +598,8 @@ static bool mi_getenv(const char* name, char* result, size_t result_size) {
} }
} }
#endif #endif
static void mi_option_init(mi_option_desc_t* desc) {
#ifndef _WIN32 static void mi_option_init(mi_option_desc_t* desc) {
// cannot call getenv() when still initializing the C runtime.
if (_mi_preloading()) return;
#endif
// Read option value from the environment // Read option value from the environment
char buf[64+1]; char buf[64+1];
mi_strlcpy(buf, "mimalloc_", sizeof(buf)); mi_strlcpy(buf, "mimalloc_", sizeof(buf));
@ -593,9 +632,9 @@ static void mi_option_init(mi_option_desc_t* desc) {
desc->init = DEFAULTED; desc->init = DEFAULTED;
} }
} }
mi_assert_internal(desc->init != UNINIT);
} }
else { else if (!_mi_preloading()) {
desc->init = DEFAULTED; desc->init = DEFAULTED;
} }
mi_assert_internal(desc->init != UNINIT);
} }

View File

@ -26,9 +26,12 @@ terms of the MIT license. A copy of the license can be found in the file
#include <linux/mman.h> // linux mmap flags #include <linux/mman.h> // linux mmap flags
#endif #endif
#if defined(__APPLE__) #if defined(__APPLE__)
#include <TargetConditionals.h>
#if !TARGET_IOS_IPHONE && !TARGET_IOS_SIMULATOR
#include <mach/vm_statistics.h> #include <mach/vm_statistics.h>
#endif #endif
#endif #endif
#endif
/* ----------------------------------------------------------- /* -----------------------------------------------------------
Initialization. Initialization.
@ -262,7 +265,7 @@ static void* mi_win_virtual_alloc(void* addr, size_t size, size_t try_alignment,
p = mi_win_virtual_allocx(addr, size, try_alignment, flags); p = mi_win_virtual_allocx(addr, size, try_alignment, flags);
} }
if (p == NULL) { if (p == NULL) {
_mi_warning_message("unable to allocate memory: error code: %i, addr: %p, size: 0x%x, large only: %d, allow_large: %d\n", GetLastError(), addr, size, large_only, allow_large); _mi_warning_message("unable to allocate OS memory (%zu bytes, error code: %i, address: %p, large only: %d, allow large: %d)\n", size, GetLastError(), addr, large_only, allow_large);
} }
return p; return p;
} }
@ -399,6 +402,9 @@ static void* mi_unix_mmap(void* addr, size_t size, size_t try_alignment, int pro
} }
#endif #endif
} }
if (p == NULL) {
_mi_warning_message("unable to allocate OS memory (%zu bytes, error code: %i, address: %p, large only: %d, allow large: %d)\n", size, errno, addr, large_only, allow_large);
}
return p; return p;
} }
#endif #endif

View File

@ -792,7 +792,7 @@ static mi_page_t* mi_find_page(mi_heap_t* heap, size_t size) mi_attr_noexcept {
const size_t req_size = size - mi_extra_padding(heap); // correct for padding_size in case of an overflow on `size` const size_t req_size = size - mi_extra_padding(heap); // correct for padding_size in case of an overflow on `size`
if (mi_unlikely(req_size > (MI_LARGE_OBJ_SIZE_MAX - mi_extra_padding(heap)))) { if (mi_unlikely(req_size > (MI_LARGE_OBJ_SIZE_MAX - mi_extra_padding(heap)))) {
if (mi_unlikely(req_size > PTRDIFF_MAX)) { // we don't allocate more than PTRDIFF_MAX (see <https://sourceware.org/ml/libc-announce/2019/msg00001.html>) if (mi_unlikely(req_size > PTRDIFF_MAX)) { // we don't allocate more than PTRDIFF_MAX (see <https://sourceware.org/ml/libc-announce/2019/msg00001.html>)
_mi_error_message(EOVERFLOW, "allocation request is too large (%zu b requested)\n", req_size); _mi_error_message(EOVERFLOW, "allocation request is too large (%zu bytes)\n", req_size);
return NULL; return NULL;
} }
else { else {
@ -833,7 +833,8 @@ void* _mi_malloc_generic(mi_heap_t* heap, size_t size MI_SOURCE_XPARAM) mi_attr
} }
if (mi_unlikely(page == NULL)) { // out of memory if (mi_unlikely(page == NULL)) { // out of memory
_mi_error_message(ENOMEM, "cannot allocate memory (%zu bytes requested)\n", size); const size_t req_size = size - MI_PADDING_SIZE; // correct for padding_size in case of an overflow on `size`
_mi_error_message(ENOMEM, "unable to allocate memory (%zu bytes)\n", req_size);
return NULL; return NULL;
} }

View File

@ -240,18 +240,23 @@ static void mi_page_reset(mi_segment_t* segment, mi_page_t* page, size_t size, m
if (reset_size > 0) _mi_mem_reset(start, reset_size, tld->os); if (reset_size > 0) _mi_mem_reset(start, reset_size, tld->os);
} }
static void mi_page_unreset(mi_segment_t* segment, mi_page_t* page, size_t size, mi_segments_tld_t* tld) static bool mi_page_unreset(mi_segment_t* segment, mi_page_t* page, size_t size, mi_segments_tld_t* tld)
{ {
mi_assert_internal(page->is_reset); mi_assert_internal(page->is_reset);
mi_assert_internal(page->is_committed); mi_assert_internal(page->is_committed);
mi_assert_internal(!segment->mem_is_fixed); mi_assert_internal(!segment->mem_is_fixed);
if (segment->mem_is_fixed || !page->is_committed || !page->is_reset) return;
page->is_reset = false; page->is_reset = false;
size_t psize; size_t psize;
uint8_t* start = mi_segment_raw_page_start(segment, page, &psize); uint8_t* start = mi_segment_raw_page_start(segment, page, &psize);
size_t unreset_size = (size == 0 || size > psize ? psize : size); size_t unreset_size = (size == 0 || size > psize ? psize : size);
bool is_zero = false; bool is_zero = false;
if (unreset_size > 0) _mi_mem_unreset(start, unreset_size, &is_zero, tld->os); bool ok = true;
if (unreset_size > 0) {
ok = _mi_mem_unreset(start, unreset_size, &is_zero, tld->os);
}
if (is_zero) page->is_zero_init = true; if (is_zero) page->is_zero_init = true;
return ok;
} }
@ -427,7 +432,7 @@ static size_t mi_segment_size(size_t capacity, size_t required, size_t* pre_size
guardsize = page_size; guardsize = page_size;
required = _mi_align_up(required, page_size); required = _mi_align_up(required, page_size);
} }
;
if (info_size != NULL) *info_size = isize; if (info_size != NULL) *info_size = isize;
if (pre_size != NULL) *pre_size = isize + guardsize; if (pre_size != NULL) *pre_size = isize + guardsize;
return (required==0 ? MI_SEGMENT_SIZE : _mi_align_up( required + isize + 2*guardsize, MI_PAGE_HUGE_ALIGN) ); return (required==0 ? MI_SEGMENT_SIZE : _mi_align_up( required + isize + 2*guardsize, MI_PAGE_HUGE_ALIGN) );
@ -630,7 +635,7 @@ static mi_segment_t* mi_segment_init(mi_segment_t* segment, size_t required, mi_
mi_segments_track_size((long)segment_size, tld); mi_segments_track_size((long)segment_size, tld);
} }
mi_assert_internal(segment != NULL && (uintptr_t)segment % MI_SEGMENT_SIZE == 0); mi_assert_internal(segment != NULL && (uintptr_t)segment % MI_SEGMENT_SIZE == 0);
mi_assert_internal(segment->mem_is_fixed ? segment->mem_is_committed : true);
if (!pages_still_good) { if (!pages_still_good) {
// zero the segment info (but not the `mem` fields) // zero the segment info (but not the `mem` fields)
ptrdiff_t ofs = offsetof(mi_segment_t, next); ptrdiff_t ofs = offsetof(mi_segment_t, next);
@ -731,7 +736,13 @@ static bool mi_segment_page_claim(mi_segment_t* segment, mi_page_t* page, mi_seg
segment->used++; segment->used++;
// check reset // check reset
if (page->is_reset) { if (page->is_reset) {
mi_page_unreset(segment, page, 0, tld); // todo: only unreset the part that was reset? mi_assert_internal(!segment->mem_is_fixed);
bool ok = mi_page_unreset(segment, page, 0, tld);
if (!ok) {
page->segment_in_use = false;
segment->used--;
return false;
}
} }
mi_assert_internal(page->segment_in_use); mi_assert_internal(page->segment_in_use);
mi_assert_internal(segment->used <= segment->capacity); mi_assert_internal(segment->used <= segment->capacity);