Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
OpenDocCN
TinySTL
提交
c5ed3777
T
TinySTL
项目概览
OpenDocCN
/
TinySTL
通知
4
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
T
TinySTL
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
c5ed3777
编写于
9月 18, 2014
作者:
邹
邹晓航
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
添加Allocator来包装Alloc
上级
90699d64
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
77 addition
and
5 deletion
+77
-5
TinySTL/Alloc.h
TinySTL/Alloc.h
+2
-1
TinySTL/Allocator.h
TinySTL/Allocator.h
+66
-0
TinySTL/TinySTL.vcxproj
TinySTL/TinySTL.vcxproj
+1
-0
TinySTL/TinySTL.vcxproj.filters
TinySTL/TinySTL.vcxproj.filters
+3
-0
TinySTL/main.cpp
TinySTL/main.cpp
+5
-4
未找到文件。
TinySTL/Alloc.h
浏览文件 @
c5ed3777
...
...
@@ -6,7 +6,8 @@
namespace
TinySTL
{
/*
**空间配置器
**空间配置器,以字节数为单位分配
**内部使用
*/
class
alloc
{
private:
...
...
TinySTL/Allocator.h
0 → 100644
浏览文件 @
c5ed3777
#ifndef _ALLOCATOR_H_
#define _ALLOCATOR_H_
#include "Alloc.h"
#include "Construct.h"
#include <new>
namespace
TinySTL
{
/*
**空间配置器,以变量数目为单位分配
*/
template
<
class
T
>
class
allocator
{
public:
typedef
T
value_type
;
typedef
T
*
pointer
;
typedef
const
T
*
const_pointer
;
typedef
T
&
reference
;
typedef
const
T
&
const_reference
;
typedef
size_t
size_type
;
typedef
ptrdiff_t
difference_type
;
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
);
};
template
<
class
T
>
T
*
allocator
<
T
>::
allocate
(){
return
static_cast
<
T
*>
(
alloc
::
allocate
(
sizeof
(
T
)));
}
template
<
class
T
>
T
*
allocator
<
T
>::
allocate
(
size_t
n
){
return
static_cast
<
T
*>
(
alloc
::
allocate
(
sizeof
(
T
)
*
n
));
}
template
<
class
T
>
void
allocator
<
T
>::
deallocate
(
T
*
ptr
){
alloc
::
deallocate
(
static_cast
<
void
*>
(
ptr
),
sizeof
(
T
));
}
template
<
class
T
>
void
allocator
<
T
>::
deallocate
(
T
*
ptr
,
size_t
n
){
alloc
::
deallocate
(
static_cast
<
void
*>
(
ptr
),
sizeof
(
T
)
*
n
);
}
template
<
class
T
>
void
allocator
<
T
>::
construct
(
T
*
ptr
){
new
(
ptr
)
T
();
}
template
<
class
T
>
void
allocator
<
T
>::
construct
(
T
*
ptr
,
const
T
&
value
){
new
(
ptr
)
T
(
value
);
}
template
<
class
T
>
void
allocator
<
T
>::
destroy
(
T
*
ptr
){
ptr
->~
T
();
}
}
#endif
\ No newline at end of file
TinySTL/TinySTL.vcxproj
浏览文件 @
c5ed3777
...
...
@@ -80,6 +80,7 @@
</ItemGroup>
<ItemGroup>
<ClInclude
Include=
"Alloc.h"
/>
<ClInclude
Include=
"Allocator.h"
/>
<ClInclude
Include=
"Construct.h"
/>
<ClInclude
Include=
"TypeTraits.h"
/>
</ItemGroup>
...
...
TinySTL/TinySTL.vcxproj.filters
浏览文件 @
c5ed3777
...
...
@@ -29,5 +29,8 @@
<ClInclude
Include=
"Alloc.h"
>
<Filter>
头文件
</Filter>
</ClInclude>
<ClInclude
Include=
"Allocator.h"
>
<Filter>
头文件
</Filter>
</ClInclude>
</ItemGroup>
</Project>
\ No newline at end of file
TinySTL/main.cpp
浏览文件 @
c5ed3777
#include <iostream>
#include <memory>
#include "Alloc.h"
#include "Alloc
ator
.h"
#include "Construct.h"
using
namespace
std
;
int
main
(){
for
(
int
i
=
1
;
i
!=
100000
;
++
i
){
TinySTL
::
alloc
::
allocate
(
i
%
128
*
sizeof
(
int
));
//std::allocator<int> alloc; alloc.allocate(i % 128);
//malloc(i % 128 * sizeof(int));
auto
p
=
TinySTL
::
allocator
<
int
>::
allocate
();
TinySTL
::
allocator
<
int
>::
construct
(
p
,
i
);
TinySTL
::
allocator
<
int
>::
destroy
(
p
);
TinySTL
::
allocator
<
int
>::
deallocate
(
p
);
}
system
(
"pause"
);
return
0
;
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录