Memory pool--the practice of partner algorithm

Source: Internet
Author: User
Tags assert mutex

The memory pool is divided into three main parts: class Buffer_t,class Bufferpool_t,class mempool_t

1.class mempool_t: The memory opens up and releases the interface, can be opened up through the memory pool or when the memory pool maximum memory allocation size, through the system to open up and release.

2.class bufferpool_t: The actual memory size requested in mempool_t 2^n (2^n<= maximum memory allocation size) memory pool) corresponds to a bufferpool_t, a bufferpool_t is managed by a list of lists to manage multiple 2 ^n Size of memory block

3.class buffer_t: Manages and stores the actual block of memory.

Words not much to say, directly on the source code.

Class mempool_t:

 #include "bufferpool_t.h" class Mempool_t{public:static mempool_t *get_instance ();/* Request Memory @param uint32_t size_ memory Size if size_ large with max_alloc_size will heavy system request memory */void *alloc (uint32_t size_);/* Free memory @param void* memory handle */b ool free (void* ptr);/* Configure memory Pool maximum memory allocation size @param uint32_t max_alloc_size. If Mempool_t.alloc (size): Where size > Max_alloc_ The size memory pool allocates memory to the system, but it is also important to call Mempool_t.free to release it, otherwise memory Error */void config (const uint32_t &max_alloc_size = 1<< ;p rivate:mempool_t (void); ~mempool_t (void); typedef std::map<uint32_t, bufferpool_t *> bufferpool_set_t;  BOOL Get_bufferpool_size (uint32_t size_, uint32_t &buf_size_);/* Allocates memory @param uint32_t size_ memory size to the system @return void* Memory Handle */void * INTERNAL_MALLOC (const uint32_t &size_);/* Directly frees memory @param void *PTR_ memory handle @return bool True succeeds, false fails, The reason is that the memory is not Internal_malloc returned, or the memory has been destroyed */bool internal_free (void *ptr_); bufferpool_set_t bufferpool_set;uint32_t Max_ Alloc_size;std::mutex Bufferpool_set_mutex;}; 
#include "stdafx.h" #include "mempool_t.h" mempool_t::mempool_t (void): Max_alloc_size (1<<20)//1mb{}mempool_t: : ~mempool_t (void) {while (Bufferpool_set.empty () ==false) {bufferpool_t *bufferpool = Bufferpool_set.begin () Second;bufferpool_set.erase (Bufferpool_set.begin ());d elete Bufferpool;}} Sets an instance of a memory allocation object mempool_t * Mempool_t::get_instance () {static mempool_t *instance = new Mempool_t;return instance;} void * Mempool_t::alloc (uint32_t size_) {uint32_t buf_size;void *buf;bool rc = get_bufferpool_size (size_,buf_size); if (r c) {bufferpool_t *bufferpool;std::lock_guard<std::mutex> lock_guard (Bufferpool_set_mutex); bufferpool_set_t: : Iterator it = Bufferpool_set.find (buf_size); if (it = = Bufferpool_set.end ()) {Bufferpool = new bufferpool_t (buf_size); BOOL rc = Bufferpool_set.insert (Bufferpool_set_t::value_type (Buf_size,bufferpool)). Second;assert (RC);} Else{bufferpool = It->second;} BUF = (void*) bufferpool->alloc_buffer ();} Else{buf = Internal_malloc (size_);} return BUF;} BOOL mempool_t::Free (void* ptr) {uint32_t data_size;bool rc = Bufferpool_t::get_buffer_size ((char*) ptr,data_size); if (RC) {Std::lock_ Guard<std::mutex> Lock_guard (Bufferpool_set_mutex); bufferpool_t *bufferpool;bufferpool_set_t::iterator it = Bufferpool_set.find (data_size); if (it = = Bufferpool_set.end ()) {assert (false);} Bufferpool = It->second;return Bufferpool->free_buffer ((char*) ptr);;} Else{return Internal_free (PTR);} return false;} BOOL Mempool_t::internal_free (void* ptr_) {char *buf = (char*) ptr_-sizeof (uint32_t); uint32_t size = * (uint32_t*) buf;if ( Size > Max_alloc_size) {uint32_t tail_size = * (uint32_t*) ((char*) ptr_ + size), if (tail_size = = size) {Delete Buf;return t Rue;}} return false;} BOOL Mempool_t::get_bufferpool_size (uint32_t size_, uint32_t &buf_size_) {if (Size_ > Max_alloc_size) {return FAL SE;} /* To find the nearest 2 power to an integer, align up */size_ = size_-1;size_ = Size_ | (Size_ >> 1); size_ = Size_ | (Size_ >> 2); size_ = Size_ | (Size_ >> 4); size_ = Size_ | (Size_ >> 8);Size_ = Size_ | (Size_ >>16); size_ = Size_ + 1;/* Determines whether it is a power of 2 */if (0! = (size_& (size_-1))) {assert (false);} Buf_size_ = Size_;return true;} void * MEMPOOL_T::INTERNAL_MALLOC (const uint32_t &size_) {uint32_t buf_size = size_ + sizeof (uint32_t) *2;void *buf = malloc (buf_size); * (uint32_t*) buf = size_;* (uint32_t*) ((char*) buf + sizeof (uint32_t) +size_) = Size_;return ((char*) buf +sizeof (uint32_t));} void Mempool_t::config (const uint32_t &max_buffer_size_) {max_alloc_size = Max_buffer_size_;}
classbufferpool_t:

#include "BUFFER_T.HPP" class bufferpool_t{public:bufferpool_t (const uint32_t &buffer_size_,const uint32_t & Limit_size_ =); ~bufferpool_t (void); Char *alloc_buffer (); BOOL Free_buffer (char *buffer_); static bool Get_buffer_size (char *buffer_, uint32_t &buffer_size_);p rivate:typedef std::list<buffer_t*> buffer_list_t;buffer_list_t buffer_list;//buffer type size, one bufferpool_t only manage//memory of the same size uint32_t buffer_type_size;// Maximum free memory uint32_t limit_buffer_list_size;//get memory count uint32_t alloc_times;//free memory uint32_t free_times;};
#include "stdafx.h" #include "bufferpool_t.h" bufferpool_t::bufferpool_t (const uint32_t &buffer_size_,const uint32_t &limit_size_) {buffer_type_size = Buffer_size_;limit_buffer_list_size = Limit_size_;free_times = 0;alloc_ Times = 0;} bufferpool_t::~bufferpool_t (void) {buffer_t *buffer;while (buffer_list.empty () = = false) {buffer = Buffer_list.front () ; Buffer_list.pop_back (); Buffer->relase_buffer ();}} char * Bufferpool_t::alloc_buffer () {buffer_t *buffer;char *buf;if (Buffer_list.empty ()) {char *buf = new Char[buffer_ Type_size+sizeof (buffer_t)];buffer = new (BUF) buffer_t (BUFFER_TYPE_SIZE,BUF);//Add member}else{buffer = Buffer_list.front (); Buffer_list.pop_front ();} ++alloc_times; buf = Buffer->buf (); Buffer->use (true); return buf;} BOOL Bufferpool_t::free_buffer (char * buffer_) {buffer_t *buffer = (buffer_t*) (Buffer_-sizeof (buffer_t)); if (buffer-& Gt;check_code () && buffer->length () = = Buffer_type_size) {if (Buffer_list.size () < Limit_buffer_list_ Size) {Buffer->usE (false); Buffer_list.push_back (buffer); ++free_times;return true;} Else{buffer->relase_buffer (); return true;}} return false;} BOOL Bufferpool_t::get_buffer_size (char *buffer_, uint32_t &buffer_size_) {buffer_t *buffer = (buffer_t*) (Buffer_- sizeof (buffer_t)); if (Buffer->check_code () &&buffer->use () &&buffer->buf () = = Buffer_) { Buffer_size_ = Buffer->length (); return true;} Else{return false;}}
class buffer_t:

#pragma once#include "STDDEF.HPP" Class Buffer_t{public:inline buffer_t (uint32_t length_, char *data_buf_) {Buf_code = 0xbeaf;buf_length = Length_;buf_data = data_buf_;} inline void Relase_buffer (void) {delete []buf_data;} inline bool Check_code () {return buf_code = = 0xbeaf;} inline char *buf () {return buf_data+sizeof (*this);} Inline uint32_t Length () {return buf_length;} inline bool Use () {return is_use;} inline void use (const bool &is_use_) {is_use = Is_use_;} Private:char *buf_data;bool is_use;uint32_t buf_code;uint32_t buf_length;};
Write a bad place, please point out

Memory pool--the practice of partner algorithm

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.