自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

C++標(biāo)準(zhǔn)庫如何實(shí)現(xiàn)共享內(nèi)存介紹

開發(fā) 后端
在C++中使用C++標(biāo)準(zhǔn)庫容器中的map,set,multimap,multiset進(jìn)行測(cè)試,如果測(cè)試不通過時(shí),主要原因是在內(nèi)存回收的時(shí)候考慮不夠完全。

初次使用C++標(biāo)準(zhǔn)庫實(shí)現(xiàn)共享內(nèi)存的管理時(shí),Vector每次分配內(nèi)存?zhèn)€數(shù)不固定,回收也不固定,這樣的話,程序還需要繼續(xù)完善,下面就隨本文的講述來讓大家進(jìn)一步的了解C++中的C++標(biāo)準(zhǔn)庫。

內(nèi)存池管理程序源碼如下:

  1. #ifndef MY_ALLOCATOR_H_   
  2. #define MY_ALLOCATOR_H_   
  3. #include "stdafx.h"   
  4. #include <limits>   
  5. #include <iostream>   
  6. namespace happyever    
  7. {   
  8.   enum { NODENUMS = 2 };   
  9.   union _Obj    
  10.   {   
  11.     union _Obj* M_free_list_link;   
  12.     char M_client_data[1];       
  13.   } ;   
  14.   typedef union _Obj Obj;   
  15.   struct _Cookie   
  16.   {   
  17.     int iShmKey;        /* 共享內(nèi)存鍵值 */   
  18.     int iShmID;         /* iShmKey對(duì)應(yīng)的shmid */   
  19.     int iSemKey;        /* 鎖信號(hào)鍵值 */   
  20.     int iSemID;         /* 鎖信號(hào)標(biāo)識(shí) */   
  21.     int iTotalsize;    /* 容器總?cè)萘?nbsp;*/   
  22.     void* pStartall;   /* 共享內(nèi)存自身地址 */   
  23.     char* pStartfree;  /* 自由空間的開始地址*/   
  24.     char* pEndfree;    /* 自由空間的結(jié)束地址*/   
  25.     int iUseNum[NODENUMS];   
  26.     /*用來存放free_list中節(jié)點(diǎn)的size*/   
  27.     short sFreelistIndex[NODENUMS];   
  28.     /*存放分配內(nèi)存節(jié)點(diǎn)的鏈表*/   
  29.     Obj* uFreelist[NODENUMS];   
  30.   };   
  31.   typedef struct _Cookie Cookie;   
  32.   //Obj;   
  33.   //Cookie;   
  34.   static Cookie *pHead = NULL;   
  35.   template <class T>   
  36.   class MyAlloc    
  37.   {   
  38.   private:   
  39.     static const int ALIGN = sizeof(Obj);   
  40.     int round_up(int bytes);   
  41.     int freelist_index(int bytes);   
  42.     int freelist_getindex(int bytes);   
  43.     char* chunk_alloc(int size, int *nobjs);   
  44.     void* refill(int num,int n);   
  45.   public:   
  46.     // type definitions   
  47.     typedef T        value_type;   
  48.     typedef T*       pointer;   
  49.     typedef const T* const_pointer;   
  50.     typedef T&       reference;   
  51.     typedef const T& const_reference;   
  52.     typedef std::size_t    size_type;   
  53.     typedef std::ptrdiff_t difference_type;   
  54.     template <class U>   
  55.     struct rebind    
  56.     {   
  57.       typedef MyAlloc<U> other;   
  58.     };  

以上程序只要稍微修改,就可以實(shí)現(xiàn)共享內(nèi)存的管理,可以方便的使用C++標(biāo)準(zhǔn)庫提供的容器。加上信號(hào)量的鎖機(jī)制。以上為了學(xué)習(xí)而改寫的SGI的stl二級(jí)分配算法實(shí)現(xiàn)的。以上代碼存在一定的局限性。

我另外完整實(shí)現(xiàn)了共享內(nèi)存管理的STL標(biāo)準(zhǔn)的alloctor程序,使用posix信號(hào)量加鎖。目前應(yīng)用在aix的xlC編譯環(huán)境下。因?yàn)樵创a涉及公司的商業(yè)秘密,所以不能公開。但基本上以上源碼已經(jīng)體現(xiàn)了自己管理內(nèi)存的完整思路,供這方面需求的朋友一起學(xué)習(xí)研究用。

【編輯推薦】

  1. 簡介學(xué)習(xí)C++總結(jié)之談
  2. 對(duì)C++庫函數(shù)進(jìn)行學(xué)習(xí)探索總結(jié)筆記
  3. C++類庫設(shè)計(jì)的基本構(gòu)思與方法
  4. C++語言真的還有市場價(jià)值?
  5. C++類庫設(shè)計(jì)的基本構(gòu)思與方法
責(zé)任編輯:chenqingxiang 來源: NET130
相關(guān)推薦

2010-01-14 15:46:27

C++標(biāo)準(zhǔn)庫

2010-01-19 09:39:43

C++標(biāo)準(zhǔn)程序庫

2010-01-19 09:39:43

C++標(biāo)準(zhǔn)程序庫

2010-01-19 18:04:02

C++標(biāo)準(zhǔn)程序庫

2010-02-03 16:35:45

C++回文

2024-05-06 11:19:20

內(nèi)存池計(jì)算機(jī)編程

2010-01-26 13:55:07

C++標(biāo)準(zhǔn)模板庫

2010-02-03 14:10:28

C++內(nèi)存邏輯區(qū)域

2010-01-25 14:56:08

C++程序

2019-09-18 09:05:26

微軟開源Windows

2010-01-14 09:55:30

C++標(biāo)準(zhǔn)庫

2010-01-14 09:43:26

C++標(biāo)準(zhǔn)程序庫

2010-02-03 16:04:34

C++標(biāo)準(zhǔn)類庫

2010-02-03 15:52:55

C++ clock()

2022-09-22 10:22:36

C++編程語言代碼

2010-01-13 10:09:24

C++標(biāo)準(zhǔn)庫

2011-06-21 10:17:41

c++內(nèi)存模型

2010-01-15 14:59:54

C++標(biāo)準(zhǔn)程序庫

2010-02-01 16:54:18

C++打印地址信息

2010-01-15 15:52:18

CC++
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)