mirror of
https://github.com/microsoft/mimalloc.git
synced 2025-05-06 23:39:31 +03:00
merge from dev
This commit is contained in:
commit
211f11218e
2 changed files with 44 additions and 6 deletions
13
src/arena.c
13
src/arena.c
|
@ -1149,14 +1149,19 @@ static bool mi_manage_os_memory_ex2(mi_subproc_t* subproc, void* start, size_t s
|
||||||
{
|
{
|
||||||
mi_assert(_mi_is_aligned(start,MI_ARENA_SLICE_SIZE));
|
mi_assert(_mi_is_aligned(start,MI_ARENA_SLICE_SIZE));
|
||||||
mi_assert(start!=NULL);
|
mi_assert(start!=NULL);
|
||||||
|
if (arena_id != NULL) { *arena_id = _mi_arena_id_none(); }
|
||||||
if (start==NULL) return false;
|
if (start==NULL) return false;
|
||||||
if (!_mi_is_aligned(start,MI_ARENA_SLICE_SIZE)) {
|
if (!_mi_is_aligned(start,MI_ARENA_SLICE_SIZE)) {
|
||||||
// todo: use alignment in memid to align to slice size first?
|
// we can align the start since the memid tracks the real base of the memory.
|
||||||
_mi_warning_message("cannot use OS memory since it is not aligned to %zu KiB (address %p)", MI_ARENA_SLICE_SIZE/MI_KiB, start);
|
void* const aligned_start = _mi_align_up_ptr(start, MI_ARENA_SLICE_SIZE);
|
||||||
|
const size_t diff = (uint8_t*)aligned_start - (uint8_t*)start;
|
||||||
|
if (diff >= size || (size - diff) < MI_ARENA_SLICE_SIZE) {
|
||||||
|
_mi_warning_message("after alignment, the size of the arena becomes too small (memory at %p with size %zu)\n", start, size);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
start = aligned_start;
|
||||||
if (arena_id != NULL) { *arena_id = _mi_arena_id_none(); }
|
size = size - diff;
|
||||||
|
}
|
||||||
|
|
||||||
const size_t slice_count = _mi_align_down(size / MI_ARENA_SLICE_SIZE, MI_BCHUNK_BITS);
|
const size_t slice_count = _mi_align_down(size / MI_ARENA_SLICE_SIZE, MI_BCHUNK_BITS);
|
||||||
if (slice_count > MI_BITMAP_MAX_BIT_COUNT) { // 16 GiB for now
|
if (slice_count > MI_BITMAP_MAX_BIT_COUNT) { // 16 GiB for now
|
||||||
|
|
|
@ -1,3 +1,6 @@
|
||||||
|
#if _WIN32
|
||||||
|
#include <windows.h>
|
||||||
|
#endif
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
|
@ -22,12 +25,14 @@ static void negative_stat(void);
|
||||||
static void alloc_huge(void);
|
static void alloc_huge(void);
|
||||||
static void test_heap_walk(void);
|
static void test_heap_walk(void);
|
||||||
static void test_canary_leak(void);
|
static void test_canary_leak(void);
|
||||||
|
static void test_manage_os_memory(void);
|
||||||
// static void test_large_pages(void);
|
// static void test_large_pages(void);
|
||||||
|
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
mi_version();
|
mi_version();
|
||||||
mi_stats_reset();
|
mi_stats_reset();
|
||||||
|
test_manage_os_memory();
|
||||||
// test_large_pages();
|
// test_large_pages();
|
||||||
// detect double frees and heap corruption
|
// detect double frees and heap corruption
|
||||||
// double_free1();
|
// double_free1();
|
||||||
|
@ -241,6 +246,34 @@ static void test_canary_leak(void) {
|
||||||
free(p);
|
free(p);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#if _WIN32
|
||||||
|
static void test_manage_os_memory(void) {
|
||||||
|
size_t size = 256 * 1024 * 1024;
|
||||||
|
void* ptr = VirtualAlloc(NULL, size, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
|
||||||
|
mi_arena_id_t arena_id;
|
||||||
|
mi_manage_os_memory_ex(ptr, size, true /* committed */, true /* pinned */, false /* is zero */, -1 /* numa node */, true /* exclusive */, &arena_id);
|
||||||
|
mi_heap_t* cuda_heap = mi_heap_new_in_arena(arena_id); // you can do this in any thread
|
||||||
|
|
||||||
|
// now allocate only in the cuda arena
|
||||||
|
void* p1 = mi_heap_malloc(cuda_heap, 8);
|
||||||
|
int* p2 = mi_heap_malloc_tp(cuda_heap, int);
|
||||||
|
*p2 = 42;
|
||||||
|
|
||||||
|
// and maybe set the cuda heap as the default heap? (but careful as now `malloc` will allocate in the cuda heap as well)
|
||||||
|
{
|
||||||
|
mi_heap_t* prev_default_heap = mi_heap_set_default(cuda_heap);
|
||||||
|
void* p3 = mi_malloc(8); // allocate in the cuda heap
|
||||||
|
mi_free(p3);
|
||||||
|
}
|
||||||
|
mi_free(p1);
|
||||||
|
mi_free(p2);
|
||||||
|
}
|
||||||
|
#else
|
||||||
|
static void test_manage_os_memory(void) {
|
||||||
|
// empty
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
// Experiment with huge OS pages
|
// Experiment with huge OS pages
|
||||||
#if 0
|
#if 0
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue