手机站
网通分站
电信主站
密 码:
用户名:
当前位置 : 主页>程序设计>C/C++>列表

为C 标准库容器写自己的内存分配程式

来源:互联网 作者:west263.com 时间:2008-02-23
西部数码-全国虚拟主机10强!40余项虚拟主机管理功能,全国领先!双线多线虚拟主机南北访问畅通无阻!免费赠送企业邮局,.CN域名,自助建站480元起,免费试用7天,满意再付款! P4主机租用799元/月.月付免压金!

根据sgi 的STL源码的二级分配算法改写的内存池分配程式,只要稍微修改就能够实现共享内存方式管理,使用C 标准库容器中的map,set,multimap,multiset测试通过,vector测试通但是,原因是在内存回收的时候考虑的比较简单,vector每次分配内存个数不固定,回收也不固定,这样的话,程式还需要继续完善。

内存池管理程式源码如下:

#ifndef MY_ALLOCATOR_H_
#define MY_ALLOCATOR_H_
#include "stdafx.h"
#include <limits>
#include <iostream>

namespace happyever
{
enum { NODENUMS = 2 };
union _Obj
{
union _Obj* M_free_list_link;
char M_client_data[1];
} ;

typedef union _Obj Obj;

struct _Cookie
{
int iShmKey; /* 共享内存键值 */
int iShmID; /* iShmKey对应的shmid */
int iSemKey; /* 锁信号键值 */
int iSemID; /* 锁信号标识 */

int iTotalsize; /* 容器总容量 */

void* pStartall; /* 共享内存自身地址 */
char* pStartfree; /* 自由空间的开始地址*/
char* pEndfree; /* 自由空间的结束地址*/

int iUseNum[NODENUMS];
/*用来存放free_list中节点的size*/
short sFreelistIndex[NODENUMS];

/*存放分配内存节点的链表*/
Obj* uFreelist[NODENUMS];
};

typedef struct _Cookie Cookie;

//Obj;
//Cookie;
static Cookie *pHead = NULL;

template <class T>
class MyAlloc
{
private:
static const int ALIGN = sizeof(Obj);

int round_up(int bytes);
int freelist_index(int bytes);
int freelist_getindex(int bytes);
char* chunk_alloc(int size, int *nobjs);
void* refill(int num,int n);

public:
// type definitions
typedef T value_type;
typedef T* pointer;
typedef const T* const_pointer;
typedef T& reference;
typedef const T& const_reference;
typedef std::size_t size_type;
typedef std::ptrdiff_t difference_type;

template <class U>
struct rebind
{
typedef MyAlloc<U> other;
};

pointer address (reference value) const
{
return &value;
}
const_pointer address (const_reference value) const
{
return &value;
}

MyAlloc() throw()
{
std::cout<<"MyAlloc"<<std::endl;
}

MyAlloc(const MyAlloc& x) throw()
{
std::cout<<"const MyAlloc"<<std::endl;
}

template <class U>
MyAlloc (const MyAlloc<U>& x) throw()
{
std::cout<<"const MyAlloc<U>"<<std::endl;
}

~MyAlloc() throw()
{
std::cout<<"~MyAlloc"<<std::endl;
}

size_type max_size () const throw()
{
return std::numeric_limits<std::size_t>::max() / sizeof(T);
}

//void PrintFreelistAndCookie();

pointer allocate (size_type num, const void* = 0)
{
pointer ret = 0;
Obj** my_free_list;
Obj* result;
int index;

// print message and allocate memory with global new
std::cerr << "allocate " << num << " element(s)"
<< " of size " << sizeof(T) << std::endl;

index = freelist_index(sizeof(T));
if(index >= NODENUMS)
{
return NULL;
}

my_free_list = pHead->uFreelist index;

//Lock(semid,LOCK_NUM);

result = *my_free_list;
if (result == 0)
{
ret = (pointer)refill((int)num, round_up(sizeof(T)));
}
else
{
*my_free_list = result->M_free_list_link;
ret = (pointer)result;
}
//UnLock(semid,LOCK_NUM);

pHead->iUseNum[index] = pHead->iUseNum[index] (int)num;

if(0 == ret)
{
std::cerr << "alloc memory fail!" << std::endl;
exit(1);
}
std::cerr << " allocated at: " << (void*)ret << std::endl;

PrintFreelistAndCookie();
return ret;
}

void construct (pointer p, const T& value)
{
// initialize memory with placement new
new((void*)p)T(value);
}

void destroy (pointer p)
{
// destroy objects by calling their destructor
p->~T();
}

void deallocate (pointer p, size_type num)
{
Obj** my_free_list;
Obj* q ;
int index;

index = freelist_getindex(sizeof(T));
if(index >= NODENUMS)
{
std::cerr << "deallocate memory fail!" << std::endl;
exit(1);
}

my_free_list = pHead->uFreelist index;
q = (Obj*) p;

文章整理:西部数码--专业提供域名注册虚拟主机服务
http://www.west263.com
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!