日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

用模版实现简单的内存池

發(fā)布時(shí)間:2023/11/29 编程问答 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 用模版实现简单的内存池 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

程序中有時(shí)候會遇到這種情況,就是需要不停的去分配以及釋放內(nèi)存。帶來的是不停的調(diào)用new以及delete帶來的開銷。

而且由于全局的new以及delete往往對多線程做出了并發(fā)保護(hù),所以在單線程情況下這更帶來一種浪費(fèi),一般的情況下是去實(shí)現(xiàn)一個(gè)

單線程的內(nèi)存池來進(jìn)行性能優(yōu)化,配合所需的類往往帶來很好的性能提升。

首先是作為內(nèi)存池的模板類:

1 template<class T> 2 class MemoryPool{ 3 public: 4 MemoryPool(size_t size = EXPANSION_SIZE); 5 ~MemoryPool(); 6 7 //從空閑列表中分配T大小的空間 8 inline void * alloc(size_t size); 9 10 //釋放內(nèi)存到空閑列表中 11 inline void free(void * elements); 12 private: 13 MemoryPool<T> * next; //空閑列表的下一個(gè)元素 14 enum { EXPANSION_SIZE = 32 }; 15 void expandTheFreeList(int howMany = EXPANSION_SIZE); 16 }; 17 18 template<class T> 19 MemoryPool<T>::MemoryPool(size_t size) 20 { 21 expandTheFreeList(size); 22 } 23 24 template<class T> 25 MemoryPool<T>::~MemoryPool() 26 { 27 MemoryPool<T> * nextPtr = next; 28 if (next){ 29 for (nextPtr = next; nextPtr != nullptr; nextPtr = next){ 30 next = next->next; 31 delete[] ((char * )nextPtr); //這里的強(qiáng)制類型轉(zhuǎn)換應(yīng)該注意,不轉(zhuǎn)換編譯無法通過的當(dāng)時(shí)分配 32 //內(nèi)存的時(shí)候是按照char類型分配的,那么釋放的時(shí)候也應(yīng)該同樣釋放。 33 } 34 } 35 } 36 37 38 template<class T> 39 inline 40 void * MemoryPool<T>::alloc(size_t) 41 { 42 if (!next) 43 expandTheFreeList(); 44 MemoryPool<T> * head = next; 45 next = head->next;//分配了一塊空間 46 return head; 47 } 48 49 template<class T> 50 inline 51 void MemoryPool<T>::free(void * doomed) 52 { 53 MemoryPool<T> * head = static_cast<MemoryPool<T> *>(doomed); 54 head->next = next; 55 next = head; 56 } 57 58 template<class T> 59 void MemoryPool<T>::expandTheFreeList(int howMany) 60 { 61 //保證分配的內(nèi)存大小至少應(yīng)該是大于指針大小或者是元素大小中的 最大者,因?yàn)槎咧g是共享內(nèi)存的 62 size_t sizeAlloc = (sizeof(T) > sizeof(MemoryPool<T> *)) ? 63 sizeof(T) : sizeof(MemoryPool<T> *); 64 MemoryPool<T> * runner = (MemoryPool<T>*)(new char[sizeAlloc]); //這里實(shí)際上是可以使用static_cast的,但是不知道為什么, 65 next = runner; //gcc下編譯不能通過。可能因?yàn)間cc的限制比較嚴(yán)格。無奈,只能使用普通的強(qiáng)制類型轉(zhuǎn)換 66 for (int i = 0; i < howMany; ++i){ 67 runner->next = (MemoryPool<T>*)(new char[sizeAlloc]); 68 runner = runner->next; 69 } 70 runner->next = 0; 71 }

?

下面是使用該內(nèi)存池的一個(gè)rational類,代碼如下:

1 class Rational{ 2 public: 3 Rational(int a = 0, int b = 1):n(a), d(b){} 4 void * operator new(size_t size){return memPool->alloc(size); } 5 void operator delete(void * doomed, size_t size){memPool->free(doomed);} 6 static void newMemPool(){memPool = new MemoryPool<Rational>; } 7 static void deleteMemPool() 8 { 9 if(!memPool) 10 return; 11 delete memPool; 12 } 13 private: 14 int n; 15 int d; 16 static MemoryPool <Rational> * memPool; 17 };

使用代碼如下:

1 MemoryPool<Rational> * Rational::memPool = 0; 2 3 int main() 4 { 5 Rational * array[10]; 6 Rational::newMemPool(); 7 for(int j = 0; j < 2; ++j){ 8 for(int i = 0; i < 10; i++){ 9 array[i] = new Rational(i); 10 } 11 for(int i = 0; i < 10; i++){ 12 delete array[i]; 13 } 14 } 15 Rational::deleteMemPool(); 16 }

?

轉(zhuǎn)載于:https://www.cnblogs.com/-wang-cheng/p/4950452.html

總結(jié)

以上是生活随笔為你收集整理的用模版实现简单的内存池的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。