forked from czHappy/MiniSTL
-
Notifications
You must be signed in to change notification settings - Fork 0
/
allocator.h
85 lines (72 loc) · 2.25 KB
/
allocator.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#pragma once
#include "alloc.h"
//#include "construct.h"
namespace MiniSTL {
//默认二级适配器
template <class T, class Alloc = __default_alloc>
class simpleAlloc {
public:
using value_type = T;
using pointer = T *;
using const_pointer = const T *;
using reference = T &;
using const_refernce = const T &;
using size_type = size_t;
using difference_type = ptrdiff_t;
public:
static T *allocate();
static T *allocate(size_t n);
static void deallocate(T *ptr);
static void deallocate(T *ptr, size_t n);
static void construct(T *ptr);
static void construct(T *ptr, const T &value);
static void destroy(T *ptr);
static void destroy(T *first, T *last);
};
template <class T, class Alloc>
T *simpleAlloc<T, Alloc>::allocate() {
return reinterpret_cast<T *>(Alloc::allocate(sizeof(T)));
}
template <class T, class Alloc>
T *simpleAlloc<T, Alloc>::allocate(size_t n) {
if (n == 0) return 0;
return reinterpret_cast<T *>(Alloc::allocate(sizeof(T) * n));
}
// deallocate和allocate都是调用了分配器所定义的内存管理方法
template <class T, class Alloc>
void simpleAlloc<T, Alloc>::deallocate(T *ptr) {
Alloc::deallocate(reinterpret_cast<void *>(ptr), sizeof(T));
}
template <class T, class Alloc>
void simpleAlloc<T, Alloc>::deallocate(T *ptr, size_t n) {
if (n == 0) return;
Alloc::deallocate(reinterpret_cast<void *>(ptr), sizeof(T) * n);
}
template <class T, class Alloc>
void simpleAlloc<T, Alloc>::construct(T *ptr) {
new (ptr) T();
}
template <class T, class Alloc>
void simpleAlloc<T, Alloc>::construct(T *ptr, const T &value) {
new (ptr) T(value);
}
//destroy是直接调用了指针的析构 与deallocate不同
template <class T, class Alloc>
void simpleAlloc<T, Alloc>::destroy(T *ptr) {
ptr->~T();
}
template <class T, class Alloc>
void simpleAlloc<T, Alloc>::destroy(T *first, T *last) {
for (; first != last; ++first) first->~T();
}
} // namespace MiniSTL
/*
Summary
1. 对alloc.h中的两种分配器进行进一步封装,形成统一接口
2. 弥补了SGI STL中分配器没有实现destroy和construct等必要函数,使得simpleAlloctor符合标准STL分配器
3. 对外提供关键的函数
allocate
deallcator
destroy
construct
*/