C++标准库如何实现共享内存介绍
初次使用C++标准库实现共享内存的管理时,Vector每次分配内存个数不固定,回收也不固定,这样的话,程序还需要继续完善,下面就随本文的讲述来让大家进一步的了解C++中的C++标准库。
内存池管理程序源码如下:
#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;
}; 相关推荐
慕名ArcGIS 2020-06-12
Justdoit00 2020-06-09
jackalwb 2020-06-05
89236831 2020-06-04
lfjjia 2020-05-27
jackalwb 2020-04-20
jackalwb 2020-04-20
zhongzhiwei 2020-04-14
dongfangxiaozi 2020-04-07
GoatSucker 2020-03-27
岁月如歌 2020-02-09
sunnyJam 2020-02-03
typhoonpython 2020-01-29
nan00zzu 2019-12-25
shawsun 2019-12-18
gaedmihx 2019-11-09
JamesNan 2019-11-18
farmanlinuxer 2011-04-01
Mexican 2011-08-10