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;
}
;
用我的渲染器测试了一下,速度还是很快的。。。
查看全文
相关阅读:
orcal中创建和删除表空间和用户
tomcat常用的优化和配置
tomcat中如何禁止和允许主机或地址访问
velocity生成静态页面代码
java下载文件
java上传文件
数据库行列转换
JDBC连接数据库详解
java中插入myslq的datetime类型的
简单的邮件发送mail.jar
原文地址:https://www.cnblogs.com/len3d/p/815183.html
最新文章
配置SVN服务器
UIApplicationsharedApplication的详解
数组[0]和[firstobject]的区别
qt quick中qml编程语言
图文例解C++类的多重继承与虚拟继承
浅谈C++多态性
首页 > 所有书 > 操作系统 > Linux >
QT中文字的绘制
QT_圆_直线_三角t
qt 摄像头程序
热门文章
[宏]_IO, _IOR, _IOW, _IOWR 宏的用法与解析
S3C6410 LCD Overlay Test Program
全面的framebuffer详解
项目开发流程简述
ubuntu上安装lamp环境命令清单
php数据库应用程序建议
php的 '1' == 1, 返回true,到底是谁变成了谁?
php静态变量的运用
树的遍历
PHP获取所有扩展及扩展下的所有函数签名生成php.snippet
Copyright © 2011-2022 走看看