\doxysection{Huge\+Page\+Allocator.\+hpp}
\hypertarget{HugePageAllocator_8hpp_source}{}\label{HugePageAllocator_8hpp_source}\index{/home/chr/devel/PhD/Environment/pub/PCBEnv/pcbenv/cxx/Util/HugePageAllocator.hpp@{/home/chr/devel/PhD/Environment/pub/PCBEnv/pcbenv/cxx/Util/HugePageAllocator.hpp}}

\begin{DoxyCode}{0}
\DoxyCodeLine{00001\ \textcolor{preprocessor}{\#pragma\ once}}
\DoxyCodeLine{00002\ }
\DoxyCodeLine{00003\ \textcolor{preprocessor}{\#include\ <cstddef>}}
\DoxyCodeLine{00004\ \textcolor{preprocessor}{\#include\ <cstdlib>}}
\DoxyCodeLine{00005\ \textcolor{preprocessor}{\#include\ <memory>}}
\DoxyCodeLine{00006\ }
\DoxyCodeLine{00007\ \textcolor{preprocessor}{\#if\ defined(\_WIN32)}}
\DoxyCodeLine{00008\ }
\DoxyCodeLine{00009\ \textcolor{keyword}{template}<\textcolor{keyword}{typename}\ T>\ \textcolor{keyword}{using\ }\mbox{\hyperlink{structAlignedTHPAllocator}{AlignedTHPAllocator}}\ =\ std::allocator<T>;}
\DoxyCodeLine{00010\ }
\DoxyCodeLine{00011\ \textcolor{preprocessor}{\#else}}
\DoxyCodeLine{00012\ }
\DoxyCodeLine{00013\ \textcolor{preprocessor}{\#include\ <cstring>}}
\DoxyCodeLine{00014\ \textcolor{preprocessor}{\#include\ <new>}}
\DoxyCodeLine{00015\ \textcolor{preprocessor}{\#include\ <stdexcept>}}
\DoxyCodeLine{00016\ \textcolor{preprocessor}{\#include\ <sys/mman.h>}}
\DoxyCodeLine{00017\ \textcolor{preprocessor}{\#include\ <unistd.h>}}
\DoxyCodeLine{00018\ \textcolor{preprocessor}{\#include\ <vector>}}
\DoxyCodeLine{00019\ }
\DoxyCodeLine{00020\ \textcolor{keyword}{constexpr}\ std::size\_t\ hugepage\_size\ =\ 2\ *\ 1024\ *\ 1024;}
\DoxyCodeLine{00021\ }
\DoxyCodeLine{00022\ \textcolor{keyword}{template}<\textcolor{keyword}{typename}\ T>\ \textcolor{keyword}{struct\ }AlignedTHPAllocator}
\DoxyCodeLine{00023\ \{}
\DoxyCodeLine{00024\ \ \ \ \ \textcolor{keyword}{using\ }value\_type\ =\ T;}
\DoxyCodeLine{00025\ }
\DoxyCodeLine{00026\ \ \ \ \ AlignedTHPAllocator()\ =\ \textcolor{keywordflow}{default};}
\DoxyCodeLine{00027\ }
\DoxyCodeLine{00028\ \ \ \ \ \textcolor{keyword}{template}<\textcolor{keyword}{class}\ U>\ \textcolor{keyword}{constexpr}\ AlignedTHPAllocator(\textcolor{keyword}{const}\ AlignedTHPAllocator<U>\&)\ \textcolor{keyword}{noexcept}\ \{\ \}}
\DoxyCodeLine{00029\ }
\DoxyCodeLine{00030\ \ \ \ \ T\ *allocate(std::size\_t\ n)}
\DoxyCodeLine{00031\ \ \ \ \ \{}
\DoxyCodeLine{00032\ \ \ \ \ \ \ \ \ std::size\_t\ bytes\ =\ n\ *\ \textcolor{keyword}{sizeof}(T);}
\DoxyCodeLine{00033\ \ \ \ \ \ \ \ \ \textcolor{keywordtype}{void}\ *ptr\ =\ \textcolor{keyword}{nullptr};}
\DoxyCodeLine{00034\ \ \ \ \ \ \ \ \ \textcolor{keywordtype}{int}\ res\ =\ posix\_memalign(\&ptr,\ hugepage\_size,\ bytes);}
\DoxyCodeLine{00035\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ (res\ !=\ 0\ ||\ ptr\ ==\ \textcolor{keyword}{nullptr})}
\DoxyCodeLine{00036\ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{throw}\ std::bad\_alloc();}
\DoxyCodeLine{00037\ \ \ \ \ \ \ \ \ madvise(ptr,\ bytes,\ MADV\_HUGEPAGE);}
\DoxyCodeLine{00038\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ \textcolor{keyword}{static\_cast<}T\ *\textcolor{keyword}{>}(ptr);}
\DoxyCodeLine{00039\ \ \ \ \ \}}
\DoxyCodeLine{00040\ }
\DoxyCodeLine{00041\ \ \ \ \ \textcolor{keywordtype}{void}\ deallocate(T\ *p,\ std::size\_t\ n)\ \textcolor{keyword}{noexcept}}
\DoxyCodeLine{00042\ \ \ \ \ \{}
\DoxyCodeLine{00043\ \ \ \ \ \ \ \ \ std::free(p);}
\DoxyCodeLine{00044\ \ \ \ \ \}}
\DoxyCodeLine{00045\ \};}
\DoxyCodeLine{00046\ }
\DoxyCodeLine{00047\ \textcolor{keyword}{template}<\textcolor{keyword}{typename}\ T,\ \textcolor{keyword}{typename}\ U>\ \textcolor{keywordtype}{bool}\ operator==(\textcolor{keyword}{const}\ \mbox{\hyperlink{structAlignedTHPAllocator}{AlignedTHPAllocator<T>}}\&,\ \textcolor{keyword}{const}\ \mbox{\hyperlink{structAlignedTHPAllocator}{AlignedTHPAllocator<U>}}\&)\ \{\ \textcolor{keywordflow}{return}\ \textcolor{keyword}{true};\ \}}
\DoxyCodeLine{00048\ \textcolor{keyword}{template}<\textcolor{keyword}{typename}\ T,\ \textcolor{keyword}{typename}\ U>\ \textcolor{keywordtype}{bool}\ operator!=(\textcolor{keyword}{const}\ \mbox{\hyperlink{structAlignedTHPAllocator}{AlignedTHPAllocator<T>}}\&,\ \textcolor{keyword}{const}\ \mbox{\hyperlink{structAlignedTHPAllocator}{AlignedTHPAllocator<U>}}\&)\ \{\ \textcolor{keywordflow}{return}\ \textcolor{keyword}{false};\ \}}
\DoxyCodeLine{00049\ }
\DoxyCodeLine{00050\ \textcolor{preprocessor}{\#endif\ }\textcolor{comment}{//\ \_WIN32}}

\end{DoxyCode}
