Memory Management: frequently applied for memory optimization policies for a large number of small objects of the same size

Source: Internet
Author: User

Clause 10 in Objective C ++ IF operator new is written, write operator delete at the same time.
A subtle example is provided. A simple memory pool model is provided.

Reading makes people really feel comfortable...

The implementation method is to overload the operate new and delete operators in the class. Apply for large memory, split it into small memory, and organize it in the form of a linked list .. Reuse .. It seems that the process does not dynamically increase the memory when the memory is insufficient.

Code:

// Class definition

Class airplane {// modified class-supports custom Memory Management
Public ://

Static void * operator new (size_t size );

...

PRIVATE:
Union {
Airplanerep * rep; // the object to be used.
Airplane * Next; // used for unused (in a free linked list) Objects
};

// A constant of the class, specifying how many memory blocks are put
// Airplane object, which is initialized later
Static const int block_size;

Static airplane * headoffreelist;

};

 

// Reload new

Void * airplane: Operator new (size_t size)
{
// Transfer the "error" request to: Operator new () for processing;
// For details, see clause 8.
If (size! = Sizeof (airplane ))
Return: Operator new (size );

Airplane * P = // P points to the header of the Free linked list
Headoffreelist ;//

// If P is valid, the header is moved to its next element.
//
If (P)
Headoffreelist = p-> next;

Else {
// If the free linked list is empty, a large memory block is allocated,
// Supports block_size airplane objects
Airplane * newblock =
Static_cast <airplane *> (: Operator new (block_size *
Sizeof (airplane )));

// Link each small memory block to form a new free linked list
// Skip 0th elements because it will be returned to the caller of operator new
//
For (INT I = 1; I <block_size-1; ++ I)
Newblock [I]. Next = & newblock [I + 1];

// End the linked list with a null pointer
Newblock [block_size-1]. Next = 0;

// P is set as the table header, and headoffreelist points
// Keep the memory block behind it
P = newblock;
Headoffreelist = & newblock [1];
}

Return P;
}

 

// It is a memory block that is passed to operator Delete. If
// If its size is correct, add it to the front of the free memory block linked list
//
Void airplane: Operator Delete (void * deadobject,
Size_t size)
{
If (deadobject = 0) return; // See Clause 8.

If (size! = Sizeof (airplane) {// See Clause 8.
: Operator Delete (deadobject );
Return;
}

Airplane * carcass =
Static_cast <airplane *> (deadobject );

Carcass-> next = headoffreelist;
Headoffreelist = carcass;
}

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.