728x90
이 글은 [인프런-게임서버 만들기 by rookiss]를 내용을 토대로 개인적으로 학습한 내용을 종합하여 정리 했습니다.
이번 단계는 앞서 만든 메모리 할당기 stomp allocator를 컨테이너에도 적용되게 하는 실습이다.

컨테이너는 기본적으로 할당자를 템플릿 변수로 받는다. 이를 채워 줄 StlAllocator를 정의 해보자.
template<typename T>
class StlAllocator
{
public:
using value_type = T;
StlAllocator() {}
template<typename other>
StlAllocator(const StlAllocator<other>&) {}
T* allocate(size_t count)
{
const int32 size = count * sizeof(T);
return static_cast<T*>(StompAllocator::Alloc(size));
}
void deallocate(T* ptr, size_t count)
{
StompAllocator::Release(ptr);
}
};
할당자로 쓰기 위해 정의 해야하는 부분은 vector 라이브러 버전에 따라 달라 질 수 있으니 참고하자. 중요한 건 allocate, deallocate 함수의 구현부다.
커스텀 할당기를 편하게 사용하기 위해 template using 문을 작성 해보자.
template<typename T>
using Vector = vector < T, StlAllocator<T>>;
결과
int main()
{
Vector<Knight> v(100);
}
이로서 일단 메모리를 xnew를 사용하는 버전, 컨테이너를 생성하는 버전 모두 stomp allocator를 통해서 메모리를 할당 받을 수 있다!
728x90
'Programming Language > C++' 카테고리의 다른 글
[인강/코드없는 프로그래밍] false sharing (0) | 2022.02.20 |
---|---|
CustomAllocator] 5단계_StompAllocator_2 (0) | 2022.02.19 |
CustomAllocator] 4단계_StompAllocator_1 (0) | 2022.02.14 |
CustomAllocator] 3단계_메모리 페이징 (0) | 2022.02.14 |
CustomAllocator] 2단계_BaseAllocator (0) | 2022.02.14 |