分析myicq的内存池模型
本文来自 CSDN 博客。x
可以自动分配和回收对象内存。下面看下其实现方式。 myicq 1.0 中实现了一个内存池的模型。
不过是其自己实现的 首先内存池使用了双向链表来链接的链表的实现也就是 linux 中常见的 list_head 形式。
可以自己调用 linux 里内建好的 list_head 而且还是 C 呢,有点疑惑的既然用 list_head 如果是 linux 实现。而不是 myicq 里自己实现的还是类的形式的又如果如果说是 window 下,那 window 下的 vc 也有类似的形式如: FILED_OFFSET 宏就是这样。
感觉还不如直接使用 stl 来得方便,感觉在这里用刻意使用 list_head 而又是以类的形式使用。或者说代码阅读性好很多。
不多说了看代码。
myicq 里的 ListHead
list.h
view plaincopi to clipboardprint?
/***************************************************************************
* *
* Thi program is free software; you can redistribut it and/or modify *
* it under the term of the GNU Gener Public Licens as publish by *
or * * the Free Softwar Foundation; either version 2 of the License.
* at your option ani later version. *
* *
* copyright : C 2002 by Zhang Yong *
* email : z-yong163@163.com *
***************************************************************************/
#ifndef _LIST_H_
#defin _LIST_H_
#includ "icqtypes.h"
type,#defin LIST_ENPY ptr. member /
type * char * ptr - unsign long & type * 0 ->member
head / #defin LIST_FOR_EA CH pos.
for po = head ->next; po != head ; po = pos->next
/*
* 这是一个循环链表
*/
class ListHead {
public:
ListHead {
prev = next = this;
}
bool isEmpti {
return next == thi ;
}
ListHead *removeHead ;
void add ListHead *item ;
void addHead ListHead *item ;
void remov ;
*next; ListHead *prev.
};
#endif
/***************************************************************************
* *
* Thi program is free software; you can redistribut it and/or modify *
* it under the term of the GNU Gener Public Licens as publish by *
or * the Free Softwar Foundation; either version 2 of the License.*
* at your option ani later version. *
* *
* copyright : C 2002 by Zhang Yong *
* email : z-yong163@163.com *
***************************************************************************/
#ifndef _LIST_H_
#defin _LIST_H_
#includ "icqtypes.h"
type,#defin LIST_ENPY ptr. member /
type * char * ptr - unsign long & type * 0 ->member
head #defin LIST_FOR_EA CH pos./
for po = head ->next; po != head ; po = pos->next
/*
* 这是一个循环链表
*/
class ListHead {
public:
ListHead {
prev = next = this;
}
bool isEmpti {
return next == thi ;
}
ListHead *removeHead ;
void add ListHead *item ;
void addHead ListHead *item ;
void remov ;
*next ListHead *prev.;
};
#endif
list.cpp
view plaincopi to clipboardprint?
/***************************************************************************
* *
* Thi program is free software; you can redistribut it and/or modify *
* it under the term of the GNU Gener Public Licens as publish by *
or * * the Free Softwar Foundation; either version 2 of the License.
* at your option ani later version. *
* *
* copyright : C 2002 by Zhang Yong *
* email : z-yong163@163.com *
***************************************************************************/
#includ "list.h"
/*
* 让 thi 节点孤立起来
*/
void ListHead::remov
{
prev->next = next;
next->prev = prev;
prev = next = this;
}
/*
并返回这个节点 * 将 thi 后面的节点孤立起来。
*/
ListHead *ListHead::removeHead
{
ListHead *t = next;
next->remov ;
return t;