diff options
author | arseny.kapoulkine <arseny.kapoulkine@99668b35-9821-0410-8761-19e4c4f06640> | 2010-08-03 12:18:27 +0000 |
---|---|---|
committer | arseny.kapoulkine <arseny.kapoulkine@99668b35-9821-0410-8761-19e4c4f06640> | 2010-08-03 12:18:27 +0000 |
commit | 1b4123af9e105f430b45bca09749cb5fb998e929 (patch) | |
tree | 5f12c75b258bc3abadf23baed8f26c84d65d9332 | |
parent | cab68ab3289fd8f8ff2a92a3bf74ed8195ce2b7c (diff) |
tests: Reduce address space pressure of test allocator
git-svn-id: http://pugixml.googlecode.com/svn/trunk@623 99668b35-9821-0410-8761-19e4c4f06640
-rw-r--r-- | tests/allocator.cpp | 23 |
1 files changed, 18 insertions, 5 deletions
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 |