Implementing a memory pool-managed class

Source: Internet
Author: User

Simulating the freelist in STL, there is this thought.

Union obj{Union obj* Next;     Char p[1];               };class memorypool{Public:memorypool () {Union obj* temp;                        M_memory.assign (5, (Union obj*) NULL);                   for (int i=0;i<m_memory.size (), i++) {for (int j=0;j<m_memory.size (); j + +)                          {temp = (obj*) malloc (sizeof (char) * (1<< (i+3)));                     Temp->next = M_memory[i];                    M_memory[i] = temp;               }}} char* mem_get (int size) {int J;                  if (Size > $) {char* start = (char*) malloc (sizeof (char) *size);                return start;               } int index = freelist_index (size);               obj* temp = M_memory[index];   if (temp = = NULL)//{for (J = Index+1;j<m_memory.size (); j + +)               {temp = M_memory[j];                       if (temp! = NULL) {M_memory[j] = temp->next;                            Break }} if (j>= m_memory.size ()) {for (int j=0;j                            <m_memory.size (); j + +) {temp = (obj*) malloc (sizeof (char) * (1<< (index+3)));                      Temp->next = M_memory[index];                     M_memory[index] = temp;                    } temp = M_memory[index];                    M_memory[index] = temp->next;                  return (char*) temp;                                         } else {obj* cur;                     int up_size = round_up (size); for (int i=0;i< (1<< (J-index)); i++) {CUR = temp;                        Cur->next = M_memory[index];                        M_memory[index] = cur;                                       temp = cur+up_size;                     } temp = M_memory[index];                     M_memory[index] = temp->next;                  return (char*) temp;                  }} else {M_memory[index] = temp->next;                return (char*) temp;                }} void Mem_free (void* p,int size) {if (Size > 128) {                Free (p);                 return;              } obj* temp = (obj*) p;              int index = freelist_index (size);              Temp->next = M_memory[index];            M_memory[index] = temp;                 } private:enum {_align = 8};//int freelist_index (int __bytes) { RetuRN (((__bytes) + (int) _align-1)/(size_t) _align-1); } int round_up (int __bytes) {return (((__bytes) + (int) _align-1) & ~ ((int) _              ALIGN-1));       } vector<union obj*> m_memory; };


Implementing a memory pool-managed class

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.