zoukankan
html css js c++ java
MemMan 1.0.0.0 Released!
/**/
/*
*************************************************
*
* MemMan 1.0.0.0
*
* Copyright (C) 2007 - 2008 by Len3d
* All rights reserved.
*
************************************************
*/
#ifndef __MEM_MAN__
#define
__MEM_MAN__
#define
DEFAULT_MEMORY_ALIGN 16
//
default memory aligment set to this size
#define
ALIGN_DEF __declspec( align( DEFAULT_MEMORY_ALIGN ) )
#ifdef FORCEINLINE
#undef
FORCEINLINE
#endif
#ifdef _DEBUG
#define
FORCEINLINE inline
#else
#define
FORCEINLINE __forceinline
#endif
typedef
int
(
*
FreeContentFunc)(
void
*
object
, UINT requested_size );
//
can't be member function of a class
class
MemoryAllocator
{
//
memory allocator, takes charge of all system operations, also limits the usage of memory
private
:
//
elements of the memory priority queue, keep track of all memory blocks
class
MemoryQueueHeader
{
public
:
FORCEINLINE MemoryQueueHeader( UINT prior,
void
*
obj, FreeContentFunc func,
int
al )
{
lchild
=
rchild
=
parent
=
NULL;
priority
=
prior;
object
=
obj;
free_func
=
func;
locked
=
TRUE;
//
we lock immediately after allocation
align
=
static_cast
<
short
>
( al );
}
//
we don't need a deconstructor for this class
FORCEINLINE
void
lock
()
{
locked
=
TRUE;
}
FORCEINLINE
void
unlock()
{
locked
=
FALSE;
}
FORCEINLINE
void
create_child( MemoryQueueHeader
*
key )
{
if
( key
->
priority
<
priority )
{
if
( lchild )
lchild
->
create_child( key );
else
{
lchild
=
key;
lchild
->
parent
=
this
;
}
}
else
{
if
( rchild )
rchild
->
create_child( key );
else
{
rchild
=
key;
rchild
->
parent
=
this
;
}
}
}
FORCEINLINE
bool
search_memory( UINT search_size,
int
search_align,
void
*
&
search_result,
void
*
obj, FreeContentFunc func )
{
if
( lchild
&&
lchild
->
search_memory( search_size, search_align, search_result, obj, func ) )
return
true
;
if
( align
==
search_align
&&
free_content( search_size ) )
{
search_result
=
get_memory();
object
=
obj;
//
update the attributes of the memory block
free_func
=
func;
return
true
;
}
if
( rchild
&&
rchild
->
search_memory( search_size, search_align, search_result, obj, func ) )
return
true
;
return
false
;
}
FORCEINLINE
void
*
get_memory()
//
the allocated memory block
{
return
((
char
*
)
this
+
sizeof
(MemoryQueueHeader));
}
FORCEINLINE
int
free_content( UINT requested_size )
{
if
(
!
locked
&&
free_func
&&
object
)
return
free_func(
object
, requested_size );
else
return
0
;
}
public
:
ALIGN_DEF
struct
{
MemoryQueueHeader
*
lchild,
//
left child, right child and parent
*
rchild,
*
parent;
UINT priority;
//
priority for sorting the memory blocks
void
*
object
;
//
the object for which the memory was allocated
FreeContentFunc free_func;
//
function to free the content of the object for requested size,
//
memory blocks without this function will be restored to memory-mapped files.
short
locked;
//
this memory block was locked by a thread
short
align;
//
aligment of the allocated memory should match
int
pad;
//
padded to 32 Byte aligned
}
;
}
;
public
:
MemoryAllocator( UINT max_size )
//
max allowed memory usage
{
allocated_size
=
0
;
available_size
=
max_size;
queue
=
NULL;
aligned_queue
=
NULL;
}
~
MemoryAllocator()
{
dealloc_header( queue );
dealloc_aligned_header( aligned_queue );
}
FORCEINLINE
void
*
alloc( UINT size, UINT priority,
void
*
object
, FreeContentFunc free_func )
{
if
( size
==
0
)
{
return
NULL;
}
else
if
( size
>
available_size )
//
searching has the complexity of O(N)
{
void
*
ptr
=
NULL;
if
( queue
&&
queue
->
search_memory( size,
0
, ptr,
object
, free_func ) )
return
ptr;
else
return
NULL;
//
the system has run out of memory
}
else
//
the complexity is O(logN)
{
allocated_size
+=
( size
+
sizeof
(MemoryQueueHeader) );
available_size
-=
( size
+
sizeof
(MemoryQueueHeader) );
MemoryQueueHeader
*
elem;
//
allocate a block
elem
=
new
(sys_alloc(
sizeof
(MemoryQueueHeader)
+
size )) MemoryQueueHeader( priority,
object
, free_func,
0
);
if
( queue )
queue
->
create_child( elem );
//
insert the node
else
queue
=
elem;
//
be the root
return
elem
->
get_memory();
}
}
FORCEINLINE
void
*
aligned_alloc( UINT size, UINT priority,
void
*
object
, FreeContentFunc free_func,
UINT align
=
DEFAULT_MEMORY_ALIGN )
{
if
( size
==
0
)
{
return
NULL;
}
else
if
( size
>
available_size )
//
searching has the complexity of O(N)
{
void
*
ptr
=
NULL;
if
( aligned_queue
&&
aligned_queue
->
search_memory( size, align, ptr,
object
, free_func ) )
return
ptr;
else
return
NULL;
//
the system has run out of memory
}
else
//
the complexity is O(logN)
{
allocated_size
+=
( size
+
sizeof
(MemoryQueueHeader) );
available_size
-=
( size
+
sizeof
(MemoryQueueHeader) );
MemoryQueueHeader
*
elem;
//
allocate an aligned block
elem
=
new
(sys_aligned_alloc(
sizeof
(MemoryQueueHeader)
+
size, align )) MemoryQueueHeader( priority,
object
, free_func, align );
if
( aligned_queue )
aligned_queue
->
create_child( elem );
//
insert the node
else
aligned_queue
=
elem;
//
be the root
return
elem
->
get_memory();
}
}
//
a lock must be used before the object being deallocated, the complexity is O(1)
FORCEINLINE
void
lock
(
void
*
ptr )
{
if
( ptr )
{
MemoryQueueHeader
*
header
=
(MemoryQueueHeader
*
)((
char
*
) ptr
-
sizeof
(MemoryQueueHeader));
header
->
lock
();
}
}
FORCEINLINE
void
unlock(
void
*
ptr )
{
if
( ptr )
{
MemoryQueueHeader
*
header
=
(MemoryQueueHeader
*
)((
char
*
) ptr
-
sizeof
(MemoryQueueHeader));
header
->
unlock();
}
}
//
deallocating has the complexity of O(logN)
FORCEINLINE
void
dealloc(
void
*
ptr )
{
if
( ptr )
{
MemoryQueueHeader
*
header,
*
node,
*
parent;
header
=
(MemoryQueueHeader
*
)((
char
*
) ptr
-
sizeof
(MemoryQueueHeader));
parent
=
header
->
parent;
if
( header
->
lchild )
//
has left child
{
node
=
find_rightmost_child( header
->
lchild );
//
rebuild the link
if
( node
!=
header
->
lchild )
{
if
( node
->
parent )
node
->
parent
->
rchild
=
NULL;
//
clear the link
node
->
lchild
=
header
->
lchild;
if
( node
->
lchild )
node
->
lchild
->
parent
=
node;
}
node
->
rchild
=
header
->
rchild;
if
( node
->
rchild )
node
->
rchild
->
parent
=
node;
node
->
parent
=
parent;
if
( parent )
//
has parent
{
if
( parent
->
lchild
==
header )
parent
->
lchild
=
node;
else
if
( parent
->
rchild
==
header )
parent
->
rchild
=
node;
}
else
//
it's the root
{
queue
=
node;
}
}
else
if
( header
->
rchild )
//
has right child
{
node
=
find_leftmost_child( header
->
rchild );
//
rebuild the link
if
( node
!=
header
->
rchild )
{
if
( node
->
parent )
node
->
parent
->
lchild
=
NULL;
//
clear the link
node
->
rchild
=
header
->
rchild;
if
( node
->
rchild )
node
->
rchild
->
parent
=
node;
}
node
->
lchild
=
header
->
lchild;
if
( node
->
lchild )
node
->
lchild
->
parent
=
node;
node
->
parent
=
parent;
if
( parent )
//
has parent
{
if
( parent
->
lchild
==
header )
parent
->
lchild
=
node;
else
if
( parent
->
rchild
==
header )
parent
->
rchild
=
node;
}
else
//
it's the root
{
queue
=
node;
}
}
else
//
has no children
{
if
( parent )
//
has parent
{
if
( parent
->
lchild
==
header )
parent
->
lchild
=
NULL;
else
if
( parent
->
rchild
==
header )
parent
->
rchild
=
NULL;
}
else
//
it's the root, clear it
{
queue
=
NULL;
}
}
sys_dealloc( header );
//
deallocate the block
}
}
FORCEINLINE
void
aligned_dealloc(
void
*
ptr )
{
if
( ptr )
{
MemoryQueueHeader
*
header,
*
node,
*
parent;
header
=
(MemoryQueueHeader
*
)((
char
*
) ptr
-
sizeof
(MemoryQueueHeader));
parent
=
header
->
parent;
if
( header
->
lchild )
//
has left child
{
node
=
find_rightmost_child( header
->
lchild );
//
rebuild the link
if
( node
!=
header
->
lchild )
{
if
( node
->
parent )
node
->
parent
->
rchild
=
NULL;
//
clear the link
node
->
lchild
=
header
->
lchild;
if
( node
->
lchild )
node
->
lchild
->
parent
=
node;
}
node
->
rchild
=
header
->
rchild;
if
( node
->
rchild )
node
->
rchild
->
parent
=
node;
node
->
parent
=
parent;
if
( parent )
//
has parent
{
if
( parent
->
lchild
==
header )
parent
->
lchild
=
node;
else
if
( parent
->
rchild
==
header )
parent
->
rchild
=
node;
}
else
//
it's the root
{
queue
=
node;
}
}
else
if
( header
->
rchild )
//
has right child
{
node
=
find_leftmost_child( header
->
rchild );
//
rebuild the link
if
( node
!=
header
->
rchild )
{
if
( node
->
parent )
node
->
parent
->
lchild
=
NULL;
//
clear the link
node
->
rchild
=
header
->
rchild;
if
( node
->
rchild )
node
->
rchild
->
parent
=
node;
}
node
->
lchild
=
header
->
lchild;
if
( node
->
lchild )
node
->
lchild
->
parent
=
node;
node
->
parent
=
parent;
if
( parent )
//
has parent
{
if
( parent
->
lchild
==
header )
parent
->
lchild
=
node;
else
if
( parent
->
rchild
==
header )
parent
->
rchild
=
node;
}
else
//
it's the root
{
queue
=
node;
}
}
else
//
has no children
{
if
( parent )
//
has parent
{
if
( parent
->
lchild
==
header )
parent
->
lchild
=
NULL;
else
if
( parent
->
rchild
==
header )
parent
->
rchild
=
NULL;
}
else
//
it's the root, clear it
{
queue
=
NULL;
}
}
sys_aligned_dealloc( header );
//
deallocate the block
}
}
private
:
//
help functions
FORCEINLINE
void
dealloc_header( MemoryQueueHeader
*
node )
{
if
( node )
{
if
( node
->
lchild )
dealloc_header( node
->
lchild );
if
( node
->
rchild )
dealloc_header( node
->
rchild );
sys_dealloc( node );
}
}
FORCEINLINE
void
dealloc_aligned_header( MemoryQueueHeader
*
node )
{
if
( node )
{
if
( node
->
lchild )
dealloc_aligned_header( node
->
lchild );
if
( node
->
rchild )
dealloc_aligned_header( node
->
rchild );
sys_aligned_dealloc( node );
}
}
FORCEINLINE MemoryQueueHeader
*
find_leftmost_child( MemoryQueueHeader
*
node )
{
if
( node
&&
node
->
lchild )
return
find_leftmost_child( node
->
lchild );
else
return
node;
}
FORCEINLINE MemoryQueueHeader
*
find_rightmost_child( MemoryQueueHeader
*
node )
{
if
( node
&&
node
->
rchild )
return
find_rightmost_child( node
->
rchild );
else
return
node;
}
private
:
//
encapsulate system operations
FORCEINLINE
void
*
sys_alloc( UINT size )
{
return
malloc( size );
}
FORCEINLINE
void
*
sys_aligned_alloc( UINT size, UINT align )
{
return
_mm_malloc( size, align );
}
FORCEINLINE
void
sys_dealloc(
void
*
ptr )
{
free( ptr );
}
FORCEINLINE
void
sys_aligned_dealloc(
void
*
ptr )
{
_mm_free( ptr );
}
private
:
//
memory statistics
UINT allocated_size,
available_size;
//
implement priority queues to record all the allocations
MemoryQueueHeader
*
queue;
MemoryQueueHeader
*
aligned_queue;
}
;
#endif
//
__MEM_MAN__
查看全文
相关阅读:
图片不能显示
Lambda表达式where过滤数据
存储文本到一个文件里
获取用户临时文件夹路径
判断某一个字符串是否存在另一个字符串中
使用反射为特性赋值
字符串与数据流之间的转换
控制台应用程序获取计算机名
重复输出字符或字符串
使用HashSet<>去除重复元素的集合
原文地址:https://www.cnblogs.com/len3d/p/935169.html
最新文章
vim编辑文件警告Swap file already exists ,如何删除vim编辑产生的.swp文件?查看隐藏文件命令
php Allocator Jemalloc TCMalloc那个内存分配器比较好?
AlphaGo设计师黄士杰:“最强的学习技能在人类的脑袋里”
php 获取ip地址的5种方法,插入用户登录日志实例
开智正典:心智升级百本经典导读,如何看开智书单的100多本书?
查看python内部模块命令,内置函数,查看python已经安装的模块命令
linux下svn命令大全
世界最顶级邮件服务器组合Linux + PMTA + OEMPRO,PowerMTA 安装
sql之left join、right join、inner join的区别,连接自己时的查询结果测试
新服务器上装java PHP环境有什么一键安装的方便的方法?一般都是怎么安装环境的?
热门文章
Hinton“深度学习之父”和“神经网络先驱”,新论文Capsule将推翻自己积累了30年的学术成果时
VPS高性能虚拟机KVM详解
VPS采用的几种常见技术(OVZ、Xen、KVM)介绍与对比
高级赚钱思维,降维打击,思维上比你高一维度的人吃掉你的蛋糕,你连察觉就没察觉到。
吴恩达讲了干货满满的一节全新AI课,全程手写板书充满诚意非常干货
MySQL 0Day漏洞出现 该漏洞可以拿到本地Root权限
ASP.NET MVC下使用AngularJs语言(三):ng-options
ASP.NET MVC下使用AngularJs语言(二):ng-click事件
ASP.NET MVC下使用AngularJs语言(一):Hello your name
Adobe reader multiple languages pack
Copyright © 2011-2022 走看看