Skip to content

Commit a68aa05

Browse files
authored
Merge pull request #49 from Alinshans/v2
🎉 Release version 2.0.0.
2 parents 2201ae6 + b7229ff commit a68aa05

25 files changed

+326
-211
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
## Ignore Visual Studio temporary files, build results, and
22
## files generated by popular Visual Studio add-ons.
33

4+
# Local build files
5+
*c.cmd
6+
*a.exe
7+
48
# User-specific files
59
*.suo
610
*.user

CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ cmake_minimum_required(VERSION 2.8)
33
project(MyTinySTL)
44

55
# version
6-
set(MyTinySTL_VERSION_MAJOR 1)
7-
set(MyTinySTL_VERSION_MINOR 1)
6+
set(MyTinySTL_VERSION_MAJOR 2)
7+
set(MyTinySTL_VERSION_MINOR 0)
88
set(MyTinySTL_VERSION_PATCH 0)
99
set(MyTinySTL_VERSION "${MyTinySTL_VERSION_MAJOR}.${MyTinySTL_VERSION_MINOR}.${MyTinySTL_VERSION_PATCH}")
1010
message(STATUS "The version of this project is: ${MyTinySTL_VERSION}")

MyTinySTL/alloc.h

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
#ifndef MYTINYSTL_ALLOC_H_
22
#define MYTINYSTL_ALLOC_H_
33

4-
// 这个头文件包含一个类 alloc,用于分配和回收内存,是 mystl 的空间配置器
4+
// 这个头文件包含一个类 alloc,用于分配和回收内存,以内存池的方式实现
5+
//
6+
// 从 v2.0.0 版本开始,将不再使用内存池,这个文件将被弃用,但暂时保留
57

68
#include <new>
79

@@ -45,7 +47,7 @@ class alloc
4547
static char* start_free; // 内存池起始位置
4648
static char* end_free; // 内存池结束位置
4749
static size_t heap_size; // 申请 heap 空间附加值大小
48-
50+
4951
static FreeList* free_list[EFreeListsNumber]; // 自由链表
5052

5153
public:
@@ -57,7 +59,6 @@ class alloc
5759
static size_t M_align(size_t bytes);
5860
static size_t M_round_up(size_t bytes);
5961
static size_t M_freelist_index(size_t bytes);
60-
static size_t M_get_blocks(size_t bytes);
6162
static void* M_refill(size_t n);
6263
static char* M_chunk_alloc(size_t size, size_t &nobj);
6364
};
@@ -157,22 +158,10 @@ inline size_t alloc::M_freelist_index(size_t bytes)
157158
: (47 + (bytes + EAlign4096 - 2049) / EAlign4096);
158159
}
159160

160-
// 根据大小获取区块数目
161-
inline size_t alloc::M_get_blocks(size_t bytes)
162-
{
163-
if (bytes <= 512)
164-
{
165-
return bytes <= 256
166-
? bytes <= 128 ? 8 : 4
167-
: 2;
168-
}
169-
return bytes <= 1024 ? 2 : 1;
170-
}
171-
172161
// 重新填充 free list
173162
void* alloc::M_refill(size_t n)
174163
{
175-
size_t nblock = M_get_blocks(n);
164+
size_t nblock = 10;
176165
char* c = M_chunk_alloc(n, nblock);
177166
FreeList* my_free_list;
178167
FreeList* result, *cur, *next;

MyTinySTL/allocator.h

Lines changed: 34 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
11
#ifndef MYTINYSTL_ALLOCATOR_H_
22
#define MYTINYSTL_ALLOCATOR_H_
33

4-
// 这个头文件包含一个模板类 allocator,用于管理对象的分配
4+
// 这个头文件包含一个模板类 allocator,用于管理内存的分配、释放,对象的构造、析构
55

6-
#include "alloc.h"
76
#include "construct.h"
87
#include "util.h"
98

109
namespace mystl
1110
{
1211

1312
// 模板类:allocator
14-
// 接受两个参数,参数一表示对象的类型,参数二表示空间配置器的类型,默认使用 mystl::alloc
15-
template <class T, class Alloc = mystl::alloc>
13+
// 模板函数代表数据类型
14+
template <class T>
1615
class allocator
1716
{
1817
public:
@@ -42,66 +41,69 @@ class allocator
4241
static void destroy(T* first, T* last);
4342
};
4443

45-
template <class T, class Alloc>
46-
T* allocator<T, Alloc>::allocate()
44+
template <class T>
45+
T* allocator<T>::allocate()
4746
{
48-
return static_cast<T*>(Alloc::allocate(sizeof(T)));
47+
return static_cast<T*>(::operator new(sizeof(T)));
4948
}
5049

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)
5352
{
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)));
5656
}
5757

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)
6060
{
61-
if (ptr == nullptr) return;
62-
Alloc::deallocate(ptr, sizeof(T));
61+
if (ptr == nullptr)
62+
return;
63+
::operator delete(ptr);
6364
}
6465

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*/)
6768
{
68-
if (ptr == nullptr) return;
69-
Alloc::deallocate(ptr, n * sizeof(T));
69+
if (ptr == nullptr)
70+
return;
71+
::operator delete(ptr);
7072
}
7173

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)
7476
{
7577
mystl::construct(ptr);
7678
}
7779

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)
8082
{
8183
mystl::construct(ptr, value);
8284
}
8385

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)
8688
{
8789
mystl::construct(ptr, mystl::move(value));
8890
}
8991

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)
9395
{
9496
mystl::construct(ptr, mystl::forward<Args>(args)...);
9597
}
9698

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)
99101
{
100102
mystl::destroy(ptr);
101103
}
102104

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)
105107
{
106108
mystl::destroy(first, last);
107109
}

0 commit comments

Comments
 (0)