zoukankan
html css js c++ java
我写的一个简单的内存管理器
template
<
int
ALIGN
>
class
MemPage
{
public
:
inline MemPage( UINT _size, MemPage
*
oldPage )
{
UINT asize
=
MAX( _size, MEMORY_PAGE_SIZE );
head
=
_mm_malloc( asize, ALIGN );
page
=
(BYTE
*
) head;
_next
=
oldPage;
size
=
asize;
}
inline MemPage(
void
*
ptr, UINT _size, MemPage
*
oldPage )
{
head
=
NULL;
page
=
(BYTE
*
) ptr;
_next
=
oldPage;
size
=
_size;
}
inline
~
MemPage()
{
if
( head )
_mm_free( head );
}
inline UINT available()
{
return
size;
}
inline
void
*
alloc( UINT _size )
{
void
*
ptr
=
page;
page
+=
_size;
size
-=
_size;
return
ptr;
}
inline MemPage
*
next()
{
return
_next;
}
private
:
union
{
struct
{
void
*
head;
BYTE
*
page;
MemPage
*
_next;
UINT size;
}
;
__m128 aligned;
}
;
}
;
template
<
typename T,
int
ALIGN
>
class
MemPool
{
public
:
inline MemPool()
{
thePage
=
NULL;
theChunk
=
NULL;
allocated
=
0
;
inited
=
0
;
}
inline
void
init()
{
thePage
=
NULL;
theChunk
=
NULL;
allocated
=
0
;
inited
++
;
}
inline
void
destory()
{
if
( inited
==
1
)
{
MemPage
<
ALIGN
>
*
cPage;
while
( ( cPage
=
thePage )
!=
NULL )
{
thePage
=
cPage
->
next();
delete cPage;
}
}
inited
--
;
}
inline
void
*
alloc( UINT size )
{
void
*
ptr;
if
( theChunk
==
NULL
||
size
>
theChunk
->
available() )
{
if
( thePage
==
NULL
||
size
>
thePage
->
available() )
{
if
(
!
inited )
init();
thePage
=
new
MemPage
<
ALIGN
>
( size, thePage );
}
ptr
=
thePage
->
alloc( size );
}
else
{
MemPage
<
ALIGN
>
*
cPage
=
theChunk;
ptr
=
cPage
->
alloc( size );
theChunk
=
cPage
->
next();
}
allocated
++
;
return
ptr;
}
inline
void
dealloc(
void
*
ptr )
{
allocated
--
;
if
( allocated
==
0
)
destory();
}
inline
void
dealloc(
void
*
ptr, UINT size )
{
if
( size
>
sizeof
(MemPage
<
ALIGN
>
) )
theChunk
=
new
(ptr) MemPage
<
ALIGN
>
( (BYTE
*
)ptr
+
sizeof
(MemPage
<
ALIGN
>
),
size
-
sizeof
(MemPage
<
ALIGN
>
), theChunk );
allocated
--
;
if
( allocated
==
0
)
destory();
}
private
:
MemPage
<
ALIGN
>
*
thePage,
*
theChunk;
UINT inited, allocated;
}
;
用我的渲染器测试了一下,速度还是很快的。。。
查看全文
相关阅读:
从零基础到软件开发,应该走怎样的路?入门者不妨看看这 5 步!
C/C++编程笔记:C语言入门知识点(二),请收藏C语言最全笔记!
学编程不知如何入门?10 年经验的底层开发程序员,教你如何入门!
C/C++编程笔记:C语言入门知识点(一),请收藏C语言最全笔记!
编程入门须知:都说零基础不好学编程,那么什么是编程基础?
C/C++编程笔记:编写完成了一个C/C++程序,如何做一个界面出来?
编程入门必看:带你零基础了解编程和编程语言,入门应该学什么?
python编程从零基础到项目实践第五章学习--字典
python编程从零基础到项目实践第四章学习--列表与原组(2)元组相关
python编程从零基础到项目实践第四章学习--列表与原组(1)列表相关
原文地址:https://www.cnblogs.com/len3d/p/815183.html
最新文章
配置Log4net,IsDebugEnabled = false, IsErrorEnabled = false, IsFatalEnabled = false, IsInfoEnabled = false, IsTraceEnabled = false, IsWarnEnabled = false都是false
大话模式-装饰模式
composer 国内镜像
composer 三大组成部分
Composer 包版本的范围指定(版本约束)
在 Windows 上安装 Composer
Navicat 闲置时间过长会卡死
Centos 镜像文件下载
在Windows上安装PHP(将PHP加载到Apache中)
PHP对象传值
热门文章
PHP 类的构造方法 __construct()
ThinkPHP6.0 模型搜索器的使用
HTML-CSS-JS Prettify 代码格式化插件
ThinkPHP6.0 判断是否有文件上传
阿里云短信服务验证码封装类
ThinkPHP6.0 多应用模式 部署 Layuiadmin 单页版
单应用模式
大学想选择计算机专业,零基础如何快速学习编程?都是经验之谈
程序人生:一个9年开发经验程序员的分享,从零基础到专业工程师?
C/C++编程笔记:C语言入门知识点(三),请收藏C语言最全笔记!
Copyright © 2011-2022 走看看