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;
}
;
用我的渲染器测试了一下,速度还是很快的。。。
查看全文
相关阅读:
【POJ 2406】Power Strings(KMP循环节)
【HDU 3746】Simpsons’ Hidden Talents(KMP求循环节)
【CodeForces 672B】Different is Good
【UVALive 4642】Malfatti Circles(圆,二分)
【POJ 1269】判断两直线相交
【POJ 2503】Babelfish(字符串)
ZOJ 2676 Network Wars[01分数规划]
A1261. happiness(吴确)[二元组暴力最小割建模]
poj3469 Dual Core CPU
2154: Crash的数字表格
原文地址:https://www.cnblogs.com/len3d/p/815183.html
最新文章
JPA的多表复杂查询
tftp server setup
ip: either "dev" is duplicate, or "type" is garbage
-/bin/sh: /usr/bin/xxx: not found”
android4.0.3源码之USB wifi移植心得
ubuntu -- mf210v拨号流程
ubuntu4arm 网站参考
wpa wp2 psk的配置方式
gt811 driver
启动BusyBox内建的FTP Server
热门文章
linux 驱动cc1101
【HDU 4445】Crazy Tank(暴力)
【CodeForces 699D】Fix a Tree
【CodeForces 697C】Lorenzo Von Matterhorn(LCA)
【HDU 5744】Keep On Movin
【POJ 2342】Anniversary party(入门树形dp)
【CodeForces 698A】Vacations
【CodeForces 699B】One Bomb
【CodeForces 699A】Launch of Collider
【CodeForces 697B】Barnicle
Copyright © 2011-2022 走看看