lean cpp library
A lean C++ library providing efficient utility classes for high-performance C++ applications.
|
00001 /*****************************************************/ 00002 /* lean Memory (c) Tobias Zirr 2011 */ 00003 /*****************************************************/ 00004 00005 #ifndef LEAN_MEMORY_WIN_HEAP 00006 #define LEAN_MEMORY_WIN_HEAP 00007 00008 #include "../lean.h" 00009 #include "alignment.h" 00010 00011 #ifndef LEAN_ASSUME_WIN_ALIGNMENT 00012 // MONITOR: Windows heap aligns memory to 8 byte (16 on x64) boundaries by default 00013 // see http://stackoverflow.com/questions/2805896/what-alignment-does-heapalloc-use 00014 #ifdef LEAN_64_BIT 00015 00016 00017 #define LEAN_ASSUME_WIN_ALIGNMENT 16 00018 #else 00019 00020 00021 #define LEAN_ASSUME_WIN_ALIGNMENT 8 00022 #endif 00023 #endif 00024 00025 namespace lean 00026 { 00027 namespace memory 00028 { 00029 00031 struct win_heap 00032 { 00034 typedef size_t size_type; 00036 static const size_type default_alignment = LEAN_ASSUME_WIN_ALIGNMENT; 00037 00039 LEAN_MAYBE_EXPORT static void* allocate(size_type size); 00041 LEAN_MAYBE_EXPORT static void free(void *memory); 00042 00044 template <size_t Alignment> 00045 static LEAN_INLINE void* allocate(size_type size) 00046 { 00047 if (Alignment <= default_alignment && check_alignment<Alignment>::valid) 00048 return allocate(size); 00049 else 00050 { 00051 LEAN_STATIC_ASSERT_MSG_ALT(Alignment < static_cast<unsigned char>(-1), 00052 "Alignment > max unsigned char unsupported.", 00053 Alignment_bigger_than_max_unsigned_char_unsupported); 00054 00055 unsigned char *unaligned = reinterpret_cast<unsigned char*>( allocate(size + Alignment) ); 00056 unsigned char *aligned = upper_align<Alignment>(unaligned); 00057 aligned[-1] = static_cast<unsigned char>(aligned - unaligned); 00058 return aligned; 00059 } 00060 } 00062 template <size_t Alignment> 00063 static LEAN_INLINE void free(void *memory) 00064 { 00065 if (Alignment <= default_alignment && check_alignment<Alignment>::valid) 00066 free(memory); 00067 else 00068 { 00069 if (memory) 00070 free(reinterpret_cast<unsigned char*>(memory) - reinterpret_cast<unsigned char*>(memory)[-1]); 00071 } 00072 } 00074 static LEAN_INLINE void free(void *memory, size_t alignment) 00075 { 00076 if (alignment <= default_alignment) 00077 free(memory); 00078 else 00079 { 00080 if (memory) 00081 free(reinterpret_cast<unsigned char*>(memory) - reinterpret_cast<unsigned char*>(memory)[-1]); 00082 } 00083 } 00084 }; 00085 00086 } // namespace 00087 00088 using memory::win_heap; 00089 00090 } // namespace 00091 00092 #ifdef LEAN_INCLUDE_LINKED 00093 #include "source/win_heap.cpp" 00094 #endif 00095 00096 #endif