mirror of
https://github.com/microsoft/mimalloc.git
synced 2025-05-05 06:59:32 +03:00
review is_zero flag
This commit is contained in:
parent
b845be241a
commit
4d976270eb
6 changed files with 17 additions and 15 deletions
|
@ -300,7 +300,7 @@ static inline uintptr_t _mi_divide_up(uintptr_t size, size_t divider) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Is memory zero initialized?
|
// Is memory zero initialized?
|
||||||
static inline bool mi_mem_is_zero(void* p, size_t size) {
|
static inline bool mi_mem_is_zero(const void* p, size_t size) {
|
||||||
for (size_t i = 0; i < size; i++) {
|
for (size_t i = 0; i < size; i++) {
|
||||||
if (((uint8_t*)p)[i] != 0) return false;
|
if (((uint8_t*)p)[i] != 0) return false;
|
||||||
}
|
}
|
||||||
|
|
|
@ -285,7 +285,7 @@ typedef struct mi_page_s {
|
||||||
uint8_t segment_idx; // index in the segment `pages` array, `page == &segment->pages[page->segment_idx]`
|
uint8_t segment_idx; // index in the segment `pages` array, `page == &segment->pages[page->segment_idx]`
|
||||||
uint8_t segment_in_use:1; // `true` if the segment allocated this page
|
uint8_t segment_in_use:1; // `true` if the segment allocated this page
|
||||||
uint8_t is_committed:1; // `true` if the page virtual memory is committed
|
uint8_t is_committed:1; // `true` if the page virtual memory is committed
|
||||||
uint8_t is_zero_init:1; // `true` if the page was zero initialized
|
uint8_t is_zero_init:1; // `true` if the page was initially zero initialized
|
||||||
|
|
||||||
// layout like this to optimize access in `mi_malloc` and `mi_free`
|
// layout like this to optimize access in `mi_malloc` and `mi_free`
|
||||||
uint16_t capacity; // number of blocks committed, must be the first field, see `segment.c:page_clear`
|
uint16_t capacity; // number of blocks committed, must be the first field, see `segment.c:page_clear`
|
||||||
|
|
11
src/alloc.c
11
src/alloc.c
|
@ -46,12 +46,17 @@ extern inline void* _mi_page_malloc(mi_heap_t* heap, mi_page_t* page, size_t siz
|
||||||
// zero the block? note: we need to zero the full block size (issue #63)
|
// zero the block? note: we need to zero the full block size (issue #63)
|
||||||
if mi_unlikely(zero) {
|
if mi_unlikely(zero) {
|
||||||
mi_assert_internal(page->xblock_size != 0); // do not call with zero'ing for huge blocks (see _mi_malloc_generic)
|
mi_assert_internal(page->xblock_size != 0); // do not call with zero'ing for huge blocks (see _mi_malloc_generic)
|
||||||
const size_t zsize = (page->is_zero ? sizeof(block->next) + MI_PADDING_SIZE : page->xblock_size);
|
if (page->is_zero) {
|
||||||
_mi_memzero_aligned(block, zsize - MI_PADDING_SIZE);
|
block->next = 0;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
mi_assert_internal(page->xblock_size >= MI_PADDING_SIZE);
|
||||||
|
_mi_memzero_aligned(block, page->xblock_size - MI_PADDING_SIZE);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#if (MI_DEBUG>0) && !MI_TRACK_ENABLED && !MI_TSAN
|
#if (MI_DEBUG>0) && !MI_TRACK_ENABLED && !MI_TSAN
|
||||||
if (!page->is_zero && !zero && !mi_page_is_huge(page)) {
|
if (!zero && !mi_page_is_huge(page)) {
|
||||||
memset(block, MI_DEBUG_UNINIT, mi_page_usable_block_size(page));
|
memset(block, MI_DEBUG_UNINIT, mi_page_usable_block_size(page));
|
||||||
}
|
}
|
||||||
#elif (MI_SECURE!=0)
|
#elif (MI_SECURE!=0)
|
||||||
|
|
13
src/page.c
13
src/page.c
|
@ -639,11 +639,6 @@ static void mi_page_extend_free(mi_heap_t* heap, mi_page_t* page, mi_tld_t* tld)
|
||||||
// enable the new free list
|
// enable the new free list
|
||||||
page->capacity += (uint16_t)extend;
|
page->capacity += (uint16_t)extend;
|
||||||
mi_stat_increase(tld->stats.page_committed, extend * bsize);
|
mi_stat_increase(tld->stats.page_committed, extend * bsize);
|
||||||
|
|
||||||
// extension into zero initialized memory preserves the zero'd free list
|
|
||||||
if (!page->is_zero_init) {
|
|
||||||
page->is_zero = false;
|
|
||||||
}
|
|
||||||
mi_assert_expensive(mi_page_is_valid_init(page));
|
mi_assert_expensive(mi_page_is_valid_init(page));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -667,12 +662,12 @@ static void mi_page_init(mi_heap_t* heap, mi_page_t* page, size_t block_size, mi
|
||||||
page->keys[0] = _mi_heap_random_next(heap);
|
page->keys[0] = _mi_heap_random_next(heap);
|
||||||
page->keys[1] = _mi_heap_random_next(heap);
|
page->keys[1] = _mi_heap_random_next(heap);
|
||||||
#endif
|
#endif
|
||||||
#if MI_DEBUG > 0
|
|
||||||
page->is_zero = false; // ensure in debug mode we initialize with MI_DEBUG_UNINIT, see issue #501
|
|
||||||
#else
|
|
||||||
page->is_zero = page->is_zero_init;
|
page->is_zero = page->is_zero_init;
|
||||||
|
#if MI_DEBUG>1
|
||||||
|
if (page->is_zero_init) {
|
||||||
|
mi_mem_is_zero(page_start, page_size);
|
||||||
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
mi_assert_internal(page->capacity == 0);
|
mi_assert_internal(page->capacity == 0);
|
||||||
mi_assert_internal(page->free == NULL);
|
mi_assert_internal(page->free == NULL);
|
||||||
mi_assert_internal(page->used == 0);
|
mi_assert_internal(page->used == 0);
|
||||||
|
|
|
@ -441,7 +441,7 @@ int _mi_prim_alloc_huge_os_pages(void* hint_addr, size_t size, int numa_node, bo
|
||||||
|
|
||||||
int _mi_prim_alloc_huge_os_pages(void* hint_addr, size_t size, int numa_node, bool* is_zero, void** addr) {
|
int _mi_prim_alloc_huge_os_pages(void* hint_addr, size_t size, int numa_node, bool* is_zero, void** addr) {
|
||||||
MI_UNUSED(hint_addr); MI_UNUSED(size); MI_UNUSED(numa_node);
|
MI_UNUSED(hint_addr); MI_UNUSED(size); MI_UNUSED(numa_node);
|
||||||
*is_zero = true;
|
*is_zero = false;
|
||||||
*addr = NULL;
|
*addr = NULL;
|
||||||
return ENOMEM;
|
return ENOMEM;
|
||||||
}
|
}
|
||||||
|
|
|
@ -20,6 +20,7 @@ terms of the MIT license.
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <assert.h>
|
||||||
|
|
||||||
// > mimalloc-test-stress [THREADS] [SCALE] [ITER]
|
// > mimalloc-test-stress [THREADS] [SCALE] [ITER]
|
||||||
//
|
//
|
||||||
|
@ -106,6 +107,7 @@ static void* alloc_items(size_t items, random_t r) {
|
||||||
uintptr_t* p = (uintptr_t*)custom_calloc(items,sizeof(uintptr_t));
|
uintptr_t* p = (uintptr_t*)custom_calloc(items,sizeof(uintptr_t));
|
||||||
if (p != NULL) {
|
if (p != NULL) {
|
||||||
for (uintptr_t i = 0; i < items; i++) {
|
for (uintptr_t i = 0; i < items; i++) {
|
||||||
|
assert(p[i] == 0);
|
||||||
p[i] = (items - i) ^ cookie;
|
p[i] = (items - i) ^ cookie;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue