From 1b4123af9e105f430b45bca09749cb5fb998e929 Mon Sep 17 00:00:00 2001 From: "arseny.kapoulkine" Date: Tue, 3 Aug 2010 12:18:27 +0000 Subject: tests: Reduce address space pressure of test allocator git-svn-id: http://pugixml.googlecode.com/svn/trunk@623 99668b35-9821-0410-8761-19e4c4f06640 --- tests/allocator.cpp | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) (limited to 'tests') diff --git a/tests/allocator.cpp b/tests/allocator.cpp index 234f95c..a1a0351 100644 --- a/tests/allocator.cpp +++ b/tests/allocator.cpp @@ -20,11 +20,24 @@ namespace { const size_t PAGE_SIZE = 4096; + size_t align_to_page(size_t value) + { + return (value + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1); + } + + void* allocate_page_aligned(size_t size) + { + // We can't use VirtualAlloc because it has 64Kb granularity so we run out of address space quickly + void* result = malloc(size + PAGE_SIZE); + + return (void*)align_to_page((size_t)result); + } + void* allocate(size_t size) { - size_t aligned_size = (size + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1); + size_t aligned_size = align_to_page(size); - void* ptr = VirtualAlloc(0, aligned_size + PAGE_SIZE, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE); + void* ptr = allocate_page_aligned(aligned_size + PAGE_SIZE); if (!ptr) return 0; void* end = (char*)ptr + aligned_size; @@ -37,12 +50,12 @@ namespace void deallocate(void* ptr, size_t size) { - size_t aligned_size = (size + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1); + size_t aligned_size = align_to_page(size); void* rptr = (char*)ptr + size - aligned_size; - DWORD old_flags; - VirtualProtect(rptr, aligned_size + PAGE_SIZE, PAGE_NOACCESS, &old_flags); + DWORD old_flags; + VirtualProtect(rptr, aligned_size + PAGE_SIZE, PAGE_NOACCESS, &old_flags); } } #else -- cgit v1.2.3