スタックアロケータの実装方法を調べていると、下記のようなコードを紹介しているサイトがありました。このコードを打ち込むと、確かにスタックアロケータとして機能したのですがBASE_ALIGNの働きがわかりません。BASE_ALIGNがどのような働きをするのか教えていただきたいです。
コードを紹介していたサイト
http://aaa568.blog76.fc2.com/blog-entry-31.html
該当のソースコード
使用している言語:c++
ソースコード
class StackAllocator
{
private:
char* m_pBuff;
char* m_pCurrent;
char* m_pEnd;
enum { BASE_ALIGN = 8, };
public:
StackAllocator(void* pBuff, size_t buffSize)
: m_pBuff((char*)(((size_t)pBuff + (BASE_ALIGN - 1))&~(BASE_ALIGN)))
, m_pEnd((char*)pBuff + buffSize)
, m_pCurrent(m_pBuff)
{
}
void* Allocate(size_t size) { size = (size + (BASE_ALIGN - 1)) & ~(BASE_ALIGN - 1); char* pBuff = m_pCurrent; char* pNext = pBuff + size; if (pNext <= m_pEnd) { m_pCurrent = pNext; return pBuff; } return NULL; } void* AlignedAlloc(size_t size, size_t align) { size = (size + (BASE_ALIGN - 1)) & ~(BASE_ALIGN - 1); char* pBuff = (char*)(((size_t)m_pCurrent + (align - 1)) & ~(align - 1)); char* pNext = pBuff + size; if (pNext <= m_pEnd) { m_pCurrent = pNext; return pBuff; } return NULL; } void Clear() { m_pCurrent = m_pBuff; } void* GetMarker() { return (void*)m_pCurrent; } void FreeToMarker(void* pMarker) { m_pCurrent = (char*)pMarker; }
};
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/07/08 05:28
2018/07/08 07:06
2018/07/08 09:00