Contents
1. 참고링크 [Bottom] [Top]
User-Defined Allocator http://www.josuttis.com/cppcode/allocator.html
The Standard Librarian: What Are Allocators Good For? http://www.ddj.com/cpp/184403759
2. 커스텀 할당자 [Bottom] [Top]
2.1. 할당자의 필수 정의/구현 사항 [Bottom] [Top]
2.1.1. 타입 정의 (Typedefs) [Bottom] [Top]
value_type
A type that is managed by the allocator.
pointer
A type that provides a pointer to the type of object managed by the allocator.
const_pointer
A type that provides a constant pointer to the type of object managed by the allocator.
reference
A type that provides a reference to the type of object managed by the allocator.
const_reference
A type that provides a constant reference to type of object managed by the allocator.
size_type
An unsigned integral type that can represent the length of any sequence that an object of template class allocator can allocate.
difference_type
A signed integral type that can represent the difference between values of pointers to the type of object managed by the allocator.
template <typename T> class CustomAllocator { public: typedef T value_type; typedef value_type * pointer; typedef const value_type * const_pointer; typedef value_type & reference; typedef const value_type & const_reference; typedef std::size_t size_type; typedef std::ptrdiff_t difference_type; ... }
2.1.2. 멤버 함수 구현 (Member Functions) [Bottom] [Top]
allocator
Constructors used to create allocator objects.
address
Finds the address of an object whose value is specified.
allocate
Allocates a block of memory large enough to store at least some specified number of elements.
deallocate
Frees a specified number of objects from storage beginning at a specified position.
max_size
Returns the number of elements of type Type that could be allocated by an object of class allocator before the free memory is used up.
construct
Constructs a specific type of object at a specified address that is initialized with a specified value.
destroy
Calls an objects destructor without deallocating the memory where the object was stored.
rebind
A structure that enables an allocator for objects of one type to allocate storage for objects of an other type.
template <typename T> class CustomAllocator { ... public: template <class U> struct rebind { typedef CustomAllocator<U> other; }; CustomAllocator() {...} CustomAllocator( const CustomAllocator& a ) {...} template <typename U> CustomAllocator( const CustomAllocator<U>& a ) {...} ~CustomAllocator() {...} pointer address( reference x ) const {...} const_pointer address( const_reference x ) const {...} pointer allocate( size_type n, const_pointer p = 0 ) {...} void deallocate( pointer p, size_type n ) {...} size_type max_size() const {...} void construct( pointer p, const value_type & x ) {...} void destroy( pointer p ) {...} ... }
2.1.3. 연산자 재정의 (Operators) [Bottom] [Top]
operator=
Assigns one allocator object to another allocator object.
template <typename T> class CustomAllocator { ... public: CustomAllocator & operator=( const CustomAllocator & x ) {...} }
2.2. 사용 예 [Bottom] [Top]
1 // File: STL/malloc_allocator.h 2 3 // malloc_allocator.h : A sample allocator based on malloc 4 // 5 // Reference 6 // The Standard Librarian: What Are Allocators Good For? 7 // - http://www.ddj.com/cpp/184403759 8 // 9 10 template <class T> 11 class malloc_allocator 12 { 13 public: 14 typedef T value_type; 15 typedef value_type* pointer; 16 typedef const value_type* const_pointer; 17 typedef value_type& reference; 18 typedef const value_type& const_reference; 19 typedef std::size_t size_type; 20 typedef std::ptrdiff_t difference_type; 21 22 template <class U> 23 struct rebind { 24 typedef malloc_allocator<U> other; 25 }; 26 27 malloc_allocator() {} 28 malloc_allocator(const malloc_allocator&) {} 29 template <class U> 30 malloc_allocator(const malloc_allocator<U>&) {} 31 ~malloc_allocator() {} 32 33 pointer address(reference x) const { return &x; } 34 const_pointer address(const_reference x) const { return &x; } 35 36 pointer allocate(size_type n, const_pointer = 0) { 37 void* p = std::malloc(n * sizeof(T)); 38 if(!p) 39 throw std::bad_alloc(); 40 return static_cast<pointer>(p); 41 } 42 43 void deallocate(pointer p, size_type) { std::free(p); } 44 45 size_type max_size() const { 46 return static_cast<size_type>(-1) / sizeof(T); 47 } 48 49 void construct(pointer p, const value_type& x) { 50 new(p) value_type(x); 51 } 52 53 void destroy(pointer p) { p->~value_type(); } 54 55 private: 56 void operator=(const malloc_allocator&); 57 }; 58 59 template<> 60 class malloc_allocator<void> 61 { 62 typedef void value_type; 63 typedef void* pointer; 64 typedef const void* const_pointer; 65 66 template <class U> 67 struct rebind { 68 typedef malloc_allocator<U> other; 69 }; 70 }; 71 72 template <class T> 73 inline bool operator==(const malloc_allocator<T>&, const malloc_allocator<T>&) { 74 return true; 75 } 76 77 template <class T> 78 inline bool operator!=(const malloc_allocator<T>&, const malloc_allocator<T>&) { 79 return false; 80 }
1 // File: STL/myalloc.hpp 2 3 /* The following code example is taken from the book 4 * "The C++ Standard Library - A Tutorial and Reference" 5 * by Nicolai M. Josuttis, Addison-Wesley, 1999 6 * 7 * (C) Copyright Nicolai M. Josuttis 1999. 8 * Permission to copy, use, modify, sell and distribute this software 9 * is granted provided this copyright notice appears in all copies. 10 * This software is provided "as is" without express or implied 11 * warranty, and with no claim as to its suitability for any purpose. 12 */ 13 #include <limits> 14 #include <iostream> 15 16 namespace MyLib { 17 template <class T> 18 class MyAlloc { 19 public: 20 // type definitions 21 typedef T value_type; 22 typedef T* pointer; 23 typedef const T* const_pointer; 24 typedef T& reference; 25 typedef const T& const_reference; 26 typedef std::size_t size_type; 27 typedef std::ptrdiff_t difference_type; 28 29 // rebind allocator to type U 30 template <class U> 31 struct rebind { 32 typedef MyAlloc<U> other; 33 }; 34 35 // return address of values 36 pointer address (reference value) const { 37 return &value; 38 } 39 const_pointer address (const_reference value) const { 40 return &value; 41 } 42 43 /* constructors and destructor 44 * - nothing to do because the allocator has no state 45 */ 46 MyAlloc() throw() { 47 } 48 MyAlloc(const MyAlloc&) throw() { 49 } 50 template <class U> 51 MyAlloc (const MyAlloc<U>&) throw() { 52 } 53 ~MyAlloc() throw() { 54 } 55 56 // return maximum number of elements that can be allocated 57 size_type max_size () const throw() { 58 return std::numeric_limits<std::size_t>::max() / sizeof(T); 59 } 60 61 // allocate but don't initialize num elements of type T 62 pointer allocate (size_type num, const void* = 0) { 63 // print message and allocate memory with global new 64 std::cerr << "allocate " << num << " element(s)" 65 << " of size " << sizeof(T) << std::endl; 66 pointer ret = (pointer)(::operator new(num*sizeof(T))); 67 std::cerr << " allocated at: " << (void*)ret << std::endl; 68 return ret; 69 } 70 71 // initialize elements of allocated storage p with value value 72 void construct (pointer p, const T& value) { 73 // initialize memory with placement new 74 new((void*)p)T(value); 75 } 76 77 // destroy elements of initialized storage p 78 void destroy (pointer p) { 79 // destroy objects by calling their destructor 80 p->~T(); 81 } 82 83 // deallocate storage p of deleted elements 84 void deallocate (pointer p, size_type num) { 85 // print message and deallocate memory with global delete 86 std::cerr << "deallocate " << num << " element(s)" 87 << " of size " << sizeof(T) 88 << " at: " << (void*)p << std::endl; 89 ::operator delete((void*)p); 90 } 91 }; 92 93 // return that all specializations of this allocator are interchangeable 94 template <class T1, class T2> 95 bool operator== (const MyAlloc<T1>&, 96 const MyAlloc<T2>&) throw() { 97 return true; 98 } 99 template <class T1, class T2> 100 bool operator!= (const MyAlloc<T1>&, 101 const MyAlloc<T2>&) throw() { 102 return false; 103 } 104 }
3. 고려사항 [Bottom] [Top]
3.1. 할당 전략 (Allocation Strategies) [Bottom] [Top]
참고> Game Programming Gems 3 - 1.6 커스텀 STL 할당자
ISBN: 9788956740812
- 유용한 할당 전략
- 고정 크기 풀 (Fixed-Size Pools) - 동일한 크기(객체 단위)의 메모리 할당.
- 스택 기반 (Stack Based) - 스택을 사용, 수명이 짧은 컨테이너에서 사용.
- 단일 스레드 (Single Threaded) - Lock 에 대한 부담이 줄인 메모리 할당.
- 정적 메모리 (Static Memory) - 정적 메모리 사용, 고정된 메모리에서 사용.
- 다중 힙 (Multiple Heaps) - 할당 크기나 종류에 따라 서로 다른 힙에 할당.
- 디버깅 (Debugging) - 할당 기록, 메모리 누수, 할당 크기 조회, 메모리 덮어쓰기 점검.
- 정렬식 (Aligned) - 특정 조건에 맞게 정렬된 메모리 사용(예를들어, SSE 명령 사용).
- 기타 할당 전략
- 공유 메모리 (Shared Memory)
- 가비지 수집 (Garbage Collected)
- 삭제 금지 (Never Delete) - 응용 프로그램이 종료될 때 메모리 해제.
- 한번에 삭제 (One-Time Delete) - 커스텀 함수를 사용하여 메모리 해제.
