| 首页 | 技术文章 | 软件下载 | 博客 | 论坛 | 精品教程 | 黑客动画 | 视频资源 | 在线服务 | 黑客游戏 | 

您现在的位置: 中国X黑客小组 >> 技术文章 >> 编程技术 >> 黑客编程 >> 文章正文 用户登录 新用户注册
  为C++标准库容器写自己的内存分配程序          【字体:
为C++标准库容器写自己的内存分配程序
作者:不详    文章来源:CnXHacker.Net    点击数:    更新时间:2007-7-24    
t;sFreelistIndex[i]=0;

pHead->uFreelist[i]=0;

pHead->iUseNum[i]=0;

}

}

static void PrintFreelistAndCookie()

{

int i,j;

Obj* my_free_list;

std::cout<<"Cookie info :"<<std::endl;

std::cout<<"sizeof(struct Cookie) = "<<sizeof(Cookie)<<std::endl;

std::cout<<"Totalsize     = "<<pHead->iTotalsize<<std::endl;

std::cout<<"UsedSize      = "<<int(pHead->pStartfree-(char*)pHead)<<std::endl;

std::cout<<"FreepoolSize  = "<<int(pHead->pEndfree - pHead->pStartfree)<<std::endl;

std::cout<<"Startall      = "<<&(pHead->pStartall)<<std::endl;

std::cout<<"Startfree     = "<<&(pHead->pStartfree)<<std::endl;

std::cout<<"Endfree       = "<<&(pHead->pEndfree)<<std::endl;

std::cout<<"nFreelist info :"<<std::endl;

for(i=0 ; i<NODENUMS ; i++)

{

j=0;

std::cout<<"iUseNum["<<i<<"] = "<<pHead->iUseNum[i]<<std::endl;

std::cout<<"FreelistIndex["<<i<<"] = "<<pHead->sFreelistIndex[i]<<std::endl;

my_free_list = pHead->uFreelist[i];

if(my_free_list->M_client_data != 0)

{

while(my_free_list->M_client_data != 0)

{

j++;

my_free_list = my_free_list->M_free_list_link;

}

std::cout<<"free_list["<<i<<"]; node counts="<<j<<std::endl;

}

}

}

template <class T1, class T2>

bool operator== (const MyAlloc<T1>&,const MyAlloc<T2>&) throw() 

{

return true;

}

template <class T1, class T2>

bool operator!= (const MyAlloc<T1>&,const MyAlloc<T2>&) throw() 

{

return false;

}

}

#endif /*MY_ALLOCATOR_H_*/

测试程序的源码如下:

// MyStl.cpp : 定义控制台应用程序的入口点。

//

#include "stdafx.h"

#include <map>

#include <vector>

#include <string>

#include <utility>

#include <iostream>

#include "MyAlloc.h"

using namespace std;

int _tmain(int argc, _TCHAR* argv[])

{

happyever ::InitShm();

multimap<string,int,less<string>,happyever ::MyAlloc<string> > m;

m.insert(make_pair(string("Harry"), 32));

m.insert(make_pair(string("Mary"), 59));

m.insert(make_pair(string("Roger"), 18));

m.insert(make_pair(string("Nancy"), 37));

m.insert(make_pair(string("Mary"), 23));



typedef multimap<string,int,less<string>,happyever ::MyAlloc<string> >::iterator Iter;

for (Iter p = m.begin(); p != m.end(); p++)

{

cout << p->first << "," << p->second << endl;

}

Iter p = m.find("Harry");

m.erase(p);

/*p = m.find("Harry");

cout << "Harry is: " << p->second << "." << endl;*/

for (Iter p = m.begin(); p != m.end(); p++)

{

cout << p->first << "," << p->second << endl;

}



return 0;

}

    以上程序在vs2005,vc6上测试通过。使用MinGW编译的时候只需要去掉vc的预编译头文件#include “stdafx.h”即可。

    以上程序只要稍微修改,就可以实现共享内存的管理,可以方便的使用标准库提供的容器。加上信号量的锁机制。

    以上为了学习而改写的SGI的stl二级分配算法实现的。以上代码存在一定的局限性。我另外完整实现了共享内存管理的STL标准的alloctor程序,使用posix信号量加锁。目前应用在aix的xlC编译环境下。因为源码涉及公司的商业秘密,所以不能公开。但基本上以上源码已经体现了自己管理内存的完整思路,供这方面需求的朋友一起学习研究用。

上一页  [1] [2] 

文章录入:IceRiver    责任编辑:IceRiver 
  • 上一篇文章:

  • 下一篇文章:
  • 发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口
    最新热点 最新推荐 相关文章
    黑帽大会:备受关注的互联网
    微软推新计划打击黑客帮助安
    微软七月安全公告预告:四个重
    世界之窗等浏览器本地xss跨域
    北大青鸟网站被黑
    微软11元老30年后重聚首 最"
    IANA ICANN站点被黑 访问者被
    NEC将3亿美元收购NetCracker
    "木马点击器"泛滥 点击欺诈威
    瑞星公司06月20日发布 每日计
      网友评论:(只显示最新5条。评论内容只代表网友观点,与本站立场无关!)
    Powered by ICE RIVER - STUDIO
    » CnXHacker.CoM   © CopyRight 2002-2006, CnXHacker.CoM™, Inc. All Rights Reserved.