Memory allocation policy for "reprint" Ogre

Source: Internet
Author: User

Original:Ogre's memory allocation policy

Before reading this, it is highly recommended to take a look at Alexandrescu's modern C + + 's explanation of policy technology in the first chapter. It's supposed to be invented by this dude, just used here.

First list the header files involved: (These header files are very relevant to each other and should be seen together)

Only used during the commissioning period:

OgreMemoryTracker.h

This header file defines the Memorytracker class, which is used to test and debug Ogre's memory allocation system. The ability to track memory allocations, recoveries, leaks, and statistical information. Ogre users do not need to pay attention.

OgreAlignedAllocator.h

Memory allocation is nothing more than alignment and misalignment, it is not necessary to establish a memory structure, but the alignment needs to be aligned with the memory structure (this implementation provides the same alignment with the platform or 2 of the whole number of bits aligned to choose to use). This file defines an aligned memory structure alignedmemory, if the memory allocator decides to use alignment, then the upper logic (such as when to allocate, how much to allocate) is determined by the allocator, the specific execution will have to be given to the Alignedmemory class.

In fact, this class also on two functions, a allocate, a deallocate, to complete the specific assignment work.

OgreMemoryStdAlloc.h

This class is a host class (The host class is the class that combines the policy), and if you've seen the first chapter of modern C + +, you know it. It is the policy idea that separates the allocator from the specific allocation strategy, and facilitates the combination of the allocator with the different allocation strategies. The allocator here is the host class Stlallocator defined here. (The specific policy is defined in OgreMemorySTLAllocator.h.)

Ogrememoryallocatedobject

This header file defines a class Allocatedobject for all classes that use custom memory allocation policy to execute new and delete. In other words, this class is similar to Stlallocator and is also a host class that overloads new and delete.

The difference between Allocatedobject and Stlallocator is that Stlallocator is equivalent to malloc and free that can be used anywhere in the program, and the user can be the main program, which can be classes, or functions, as long as they want to apply for space. But Allocatedobject is a redefinition of new and delete, and classes that want to have their own new and delete should inherit this class (You can also overload new and delete in your own class, but Ogre provides this interface mechanism based on policy. The benefit is that the memory allocation policy can be changed very easily)

OgreMemorySTLAllocator.h

This header file defines two specific policy classes, which are the different memory allocation strategies used by Stlallocato and allocatedobjectr. One is the Stdallocpolicy class (allocation policy, which directly encapsulates malloc and free), and one is the Stdalignedallocpolicy class (allocation policy, which is called malloc and free after the alignment is calculated manually). In fact, our computers have default alignment, and malloc automatically aligns allocations, so unless your alignment method is different from the default, it's good to use Stdallocpolicy directly.

OgreMemoryNedAlloc.h

This header file is also the class that defines the memory allocation policy that is used for Stlallocator and allocatedobject assembly: Nedallocpolicy,nedalignedallocpolicy. The NED memory allocator here has its own homepage: http://nedprod.com/programs/portable/nedmalloc/index.html, known as the fastest memory allocator. This policy is an encapsulation of this memory allocator.

OgreMemoryNedPooling.h

Well, this is also a package for Ned's memory allocator, for Stlallocator and allocatedobject assembly use. There are two classes: Nedpoolingpolicy,nedalignedpoolingpolicy, the difference between the last and the link to see people's official Internet cafes. All we need to know is that there are several different memory allocation strategies, either in the re-encapsulation of malloc and free, or by redefining the system for the heart of malloc and free.

OgreMemoryAllocatorConfig.h

This header file does not define a class, only configuration. Configure what? Look at other code, such as root, will be public a rootalloc, this rootalloc is a memory allocation policy, is nothing more than the above said, but it does not call this. This header file to do this, with typedef to the different ogre objects using the memory management policy are defined under their own name, but also according to the policy of choice defines the ogre_new and other macros, that is, ogre do not intend to let the library users use their own new,malloc, Delete and other functions, all encapsulated.

In summary, know Ogre's memory allocation system with the policy technology (including host and specific policy, see Modern C + +), the host part to provide two interfaces Stlallocattor, Allocatedobject, There are 6 available memory allocation policies (policy): Stdallocpolicy, Stdalignedallocpolicy, Nedallocpolicy, Nedalignedallocpolicy, Nedpoolingpolicy, Nedalignedpoolingpolicy

In such a big battle, in the end you can directly and easily in their own class overloaded under constructor and new things such as implementation, but why the cost of this thing? This is the design pattern ~ ~ ~ ~ ~ ~ ~ Design pattern is good, is convenient code reuse and modification. This architecture is good, and it's easy to change or add policy later if you want to do it yourself. Then wait and cry, and every class has to be changed.

Memory allocation policy for "reprint" Ogre

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.