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;
}
;
用我的渲染器测试了一下,速度还是很快的。。。
查看全文
相关阅读:
.net常用框架总结
微信小程序 语音转换
nginx+redis实现session共享 .NET分布式架构
Redis 安装及注册服务
WebApi跨域
Uri各个属性取值测试
一些常用的FFMPEG命令集合
动态规划重学习笔记
给自己的电脑时间进行精准校时
[NOI题库][POJ2536][匈牙利算法][二分图最大匹配]Gopher II
原文地址:https://www.cnblogs.com/len3d/p/815183.html
最新文章
JavaWeb练习-网上名片管理系统
杭电oj 4004---The Frog Games java解法
蓝桥杯练习01
算法练习LeetCode初级算法之其他
算法练习LeetCode初级算法之数学
算法练习LeetCode初级算法之设计问题
算法练习LeetCode初级算法之动态规划
coming back
洛谷P2111考场奇遇
一些网址
热门文章
模拟题-code
[HAOI2015]树上染色
[HAOI2009]毛毛虫
10.16模拟赛
10.14 模拟赛
[SDOI2008]仪仗队
codevs1080_线段树练习(树状数组)
java 面试题汇总
jvm 调优
Linux常用命令大全(非常全!!!)
Copyright © 2011-2022 走看看