diff options
author | arseny.kapoulkine <arseny.kapoulkine@99668b35-9821-0410-8761-19e4c4f06640> | 2010-07-19 09:57:32 +0000 |
---|---|---|
committer | arseny.kapoulkine <arseny.kapoulkine@99668b35-9821-0410-8761-19e4c4f06640> | 2010-07-19 09:57:32 +0000 |
commit | 7d24b9b5655d584b6dc8b89df7cbd58d2e940a81 (patch) | |
tree | 0301baa043cd688068f6ffa11ad56d284031e664 /tests/allocator.cpp | |
parent | 86ac39edb09647b83784c078f9ea3bd3b7a7d4e8 (diff) |
Set svn:eol-style to native for all text files
git-svn-id: http://pugixml.googlecode.com/svn/trunk@607 99668b35-9821-0410-8761-19e4c4f06640
Diffstat (limited to 'tests/allocator.cpp')
-rw-r--r-- | tests/allocator.cpp | 188 |
1 files changed, 94 insertions, 94 deletions
diff --git a/tests/allocator.cpp b/tests/allocator.cpp index e0efeef..234f95c 100644 --- a/tests/allocator.cpp +++ b/tests/allocator.cpp @@ -1,94 +1,94 @@ -#include "allocator.hpp"
-
-#include <string.h>
-
-// Low-level allocation functions
-#if defined(_WIN32) || defined(_WIN64)
-# ifdef __MWERKS__
-# pragma ANSI_strict off // disable ANSI strictness to include windows.h
-# pragma cpp_extensions on // enable some extensions to include windows.h
-# endif
-
-# ifdef _XBOX_VER
-# define NOD3D
-# include <xtl.h>
-# else
-# include <windows.h>
-# endif
-
-namespace
-{
- const size_t PAGE_SIZE = 4096;
-
- void* allocate(size_t size)
- {
- size_t aligned_size = (size + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1);
-
- void* ptr = VirtualAlloc(0, aligned_size + PAGE_SIZE, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
- if (!ptr) return 0;
-
- void* end = (char*)ptr + aligned_size;
-
- DWORD old_flags;
- VirtualProtect(end, PAGE_SIZE, PAGE_NOACCESS, &old_flags);
-
- return (char*)end - size;
- }
-
- void deallocate(void* ptr, size_t size)
- {
- size_t aligned_size = (size + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1);
-
- void* rptr = (char*)ptr + size - aligned_size;
-
- DWORD old_flags;
- VirtualProtect(rptr, aligned_size + PAGE_SIZE, PAGE_NOACCESS, &old_flags);
- }
-}
-#else
-# include <stdlib.h>
-
-namespace
-{
- void* allocate(size_t size)
- {
- return malloc(size);
- }
-
- void deallocate(void* ptr, size_t size)
- {
- (void)size;
-
- free(ptr);
- }
-}
-#endif
-
-// High-level allocation functions
-void* memory_allocate(size_t size)
-{
- void* result = allocate(size + sizeof(size_t));
- if (!result) return 0;
-
- memcpy(result, &size, sizeof(size_t));
-
- return (size_t*)result + 1;
-}
-
-size_t memory_size(void* ptr)
-{
- size_t result;
- memcpy(&result, (size_t*)ptr - 1, sizeof(size_t));
-
- return result;
-}
-
-void memory_deallocate(void* ptr)
-{
- if (!ptr) return;
-
- size_t size = memory_size(ptr);
-
- deallocate((size_t*)ptr - 1, size + sizeof(size_t));
-}
-
+#include "allocator.hpp" + +#include <string.h> + +// Low-level allocation functions +#if defined(_WIN32) || defined(_WIN64) +# ifdef __MWERKS__ +# pragma ANSI_strict off // disable ANSI strictness to include windows.h +# pragma cpp_extensions on // enable some extensions to include windows.h +# endif + +# ifdef _XBOX_VER +# define NOD3D +# include <xtl.h> +# else +# include <windows.h> +# endif + +namespace +{ + const size_t PAGE_SIZE = 4096; + + void* allocate(size_t size) + { + size_t aligned_size = (size + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1); + + void* ptr = VirtualAlloc(0, aligned_size + PAGE_SIZE, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE); + if (!ptr) return 0; + + void* end = (char*)ptr + aligned_size; + + DWORD old_flags; + VirtualProtect(end, PAGE_SIZE, PAGE_NOACCESS, &old_flags); + + return (char*)end - size; + } + + void deallocate(void* ptr, size_t size) + { + size_t aligned_size = (size + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1); + + void* rptr = (char*)ptr + size - aligned_size; + + DWORD old_flags; + VirtualProtect(rptr, aligned_size + PAGE_SIZE, PAGE_NOACCESS, &old_flags); + } +} +#else +# include <stdlib.h> + +namespace +{ + void* allocate(size_t size) + { + return malloc(size); + } + + void deallocate(void* ptr, size_t size) + { + (void)size; + + free(ptr); + } +} +#endif + +// High-level allocation functions +void* memory_allocate(size_t size) +{ + void* result = allocate(size + sizeof(size_t)); + if (!result) return 0; + + memcpy(result, &size, sizeof(size_t)); + + return (size_t*)result + 1; +} + +size_t memory_size(void* ptr) +{ + size_t result; + memcpy(&result, (size_t*)ptr - 1, sizeof(size_t)); + + return result; +} + +void memory_deallocate(void* ptr) +{ + if (!ptr) return; + + size_t size = memory_size(ptr); + + deallocate((size_t*)ptr - 1, size + sizeof(size_t)); +} + |