|
1 | 1 | #ifndef MYTINYSTL_ALLOCATOR_H_ |
2 | 2 | #define MYTINYSTL_ALLOCATOR_H_ |
3 | 3 |
|
4 | | -// 这个头文件包含一个模板类 allocator,用于管理对象的分配 |
| 4 | +// 这个头文件包含一个模板类 allocator,用于管理内存的分配、释放,对象的构造、析构 |
5 | 5 |
|
6 | | -#include "alloc.h" |
7 | 6 | #include "construct.h" |
8 | 7 | #include "util.h" |
9 | 8 |
|
10 | 9 | namespace mystl |
11 | 10 | { |
12 | 11 |
|
13 | 12 | // 模板类:allocator |
14 | | -// 接受两个参数,参数一表示对象的类型,参数二表示空间配置器的类型,默认使用 mystl::alloc |
15 | | -template <class T, class Alloc = mystl::alloc> |
| 13 | +// 模板函数代表数据类型 |
| 14 | +template <class T> |
16 | 15 | class allocator |
17 | 16 | { |
18 | 17 | public: |
@@ -42,66 +41,69 @@ class allocator |
42 | 41 | static void destroy(T* first, T* last); |
43 | 42 | }; |
44 | 43 |
|
45 | | -template <class T, class Alloc> |
46 | | -T* allocator<T, Alloc>::allocate() |
| 44 | +template <class T> |
| 45 | +T* allocator<T>::allocate() |
47 | 46 | { |
48 | | - return static_cast<T*>(Alloc::allocate(sizeof(T))); |
| 47 | + return static_cast<T*>(::operator new(sizeof(T))); |
49 | 48 | } |
50 | 49 |
|
51 | | -template <class T, class Alloc> |
52 | | -T* allocator<T, Alloc>::allocate(size_type n) |
| 50 | +template <class T> |
| 51 | +T* allocator<T>::allocate(size_type n) |
53 | 52 | { |
54 | | - if (n == 0) return nullptr; |
55 | | - return static_cast<T*>(Alloc::allocate(n * sizeof(T))); |
| 53 | + if (n == 0) |
| 54 | + return nullptr; |
| 55 | + return static_cast<T*>(::operator new(n * sizeof(T))); |
56 | 56 | } |
57 | 57 |
|
58 | | -template <class T, class Alloc> |
59 | | -void allocator<T, Alloc>::deallocate(T* ptr) |
| 58 | +template <class T> |
| 59 | +void allocator<T>::deallocate(T* ptr) |
60 | 60 | { |
61 | | - if (ptr == nullptr) return; |
62 | | - Alloc::deallocate(ptr, sizeof(T)); |
| 61 | + if (ptr == nullptr) |
| 62 | + return; |
| 63 | + ::operator delete(ptr); |
63 | 64 | } |
64 | 65 |
|
65 | | -template <class T, class Alloc> |
66 | | -void allocator<T, Alloc>::deallocate(T* ptr, size_type n) |
| 66 | +template <class T> |
| 67 | +void allocator<T>::deallocate(T* ptr, size_type /*size*/) |
67 | 68 | { |
68 | | - if (ptr == nullptr) return; |
69 | | - Alloc::deallocate(ptr, n * sizeof(T)); |
| 69 | + if (ptr == nullptr) |
| 70 | + return; |
| 71 | + ::operator delete(ptr); |
70 | 72 | } |
71 | 73 |
|
72 | | -template <class T, class Alloc> |
73 | | -void allocator<T, Alloc>::construct(T* ptr) |
| 74 | +template <class T> |
| 75 | +void allocator<T>::construct(T* ptr) |
74 | 76 | { |
75 | 77 | mystl::construct(ptr); |
76 | 78 | } |
77 | 79 |
|
78 | | -template <class T, class Alloc> |
79 | | -void allocator<T, Alloc>::construct(T* ptr, const T& value) |
| 80 | +template <class T> |
| 81 | +void allocator<T>::construct(T* ptr, const T& value) |
80 | 82 | { |
81 | 83 | mystl::construct(ptr, value); |
82 | 84 | } |
83 | 85 |
|
84 | | -template<class T, class Alloc> |
85 | | - void allocator<T, Alloc>::construct(T* ptr, T&& value) |
| 86 | +template <class T> |
| 87 | + void allocator<T>::construct(T* ptr, T&& value) |
86 | 88 | { |
87 | 89 | mystl::construct(ptr, mystl::move(value)); |
88 | 90 | } |
89 | 91 |
|
90 | | -template<class T, class Alloc> |
91 | | -template<class ...Args> |
92 | | - void allocator<T, Alloc>::construct(T* ptr, Args&& ...args) |
| 92 | +template <class T> |
| 93 | +template <class ...Args> |
| 94 | + void allocator<T>::construct(T* ptr, Args&& ...args) |
93 | 95 | { |
94 | 96 | mystl::construct(ptr, mystl::forward<Args>(args)...); |
95 | 97 | } |
96 | 98 |
|
97 | | -template <class T, class Alloc> |
98 | | -void allocator<T, Alloc>::destroy(T* ptr) |
| 99 | +template <class T> |
| 100 | +void allocator<T>::destroy(T* ptr) |
99 | 101 | { |
100 | 102 | mystl::destroy(ptr); |
101 | 103 | } |
102 | 104 |
|
103 | | -template <class T, class Alloc> |
104 | | -void allocator<T, Alloc>::destroy(T* first, T* last) |
| 105 | +template <class T> |
| 106 | +void allocator<T>::destroy(T* first, T* last) |
105 | 107 | { |
106 | 108 | mystl::destroy(first, last); |
107 | 109 | } |
|
0 commit comments