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;
}
;
用我的渲染器测试了一下,速度还是很快的。。。
查看全文
相关阅读:
python切片操作
python中的内存管理
python中x,y交换值的问题
leetcode6:Zigzag Conversion@Python
Leetcode4:Median of Two Sorted Arrays@Python
Leetcode3:Longest Substring Without Repeating Characters@Python
Leetcode2:Add Two Numbers@Python
LeetCode344:Reverse String@Python
支付宝 芝麻信用分过600,你不知道的八个特权
穷爸爸富爸爸里面说的“现金流游戏”靠谱吗?
原文地址:https://www.cnblogs.com/len3d/p/815183.html
最新文章
json序列化
预约会议sql
史上最牛同学聚会通知书
vue实现头像上传并实时预览
vue项目(webpack+mintui),使用hbuilder打包app
iframe全屏兼容(按钮在iframe里)
批量审批功能的前端form表单ajax提交多文件多数据
javaScript调试工具console的使用
ios 音频
ios -- cell的图片下载
热门文章
ios 单例模式(懒汉式)
ios xib
ios 多线程-GCD-NSOperation
遍历
NSArray函数
设计模式
ios 视频音乐播放
ios ARC
Leetcode5:Longest Palindromic Substring@Python
python中的浅拷贝和深拷贝
Copyright © 2011-2022 走看看