Anatomy of the first level configurator of SGI space allocator

Source: Internet
Author: User

/*Purpose: A template for allocating memory space as a first-level allocation; template parameter: Meaningless, no interface function is used within the template: The allocate function is used to allocate space reallocate function to specify the address reallocation space deallocate function is used to Free space Set_malloc_hander function to get the function time of "out of memory processing routine": 2014-12-4*/Template<intInst>class__malloc_alloc_template{Private: typedefvoid(*func) ();//defines a function pointer type of func that represents void (*) () Public:    Static void*Allocate (size_t N) {void*result=malloc (n); if(0==result) {//Oom_malloc (n) in the stl_alloc.h processFunc My_malloc_handler;//defines a temporary function pointer variable.              for(;;) {//if the memory function is given here, it always loops and knows how long it will take to get the program out of the process. My_malloc_handler=__malloc_alloc_oom_handler; if(0==my_malloc_handler) {__throw_bad_alloc;}//indicates that there is not enough memory for the process to be lost and ended. (*my_malloc_handler) ();//the function that the function pointer refers to is called after the dereference, that is, the run history processing. Result=malloc (n);//call the Allocate memory function again                if(Result)return(result);//If the assignment succeeds, the end is done, otherwise the process continues and memory allocation continues.            }        }        returnresult; }    Static voidDEALLOCATE (void*p,size_t N)//essentially just using the P{free (p); }    Static void* Reallocate (void*p,size_t oldsz,size_t Newsz)//The old memory size is useless.    {                void*result=remalloc (P,NEWSZ);//Its own function is to copy the old content into the new content, and look for the memory block//Similar processing        /*Exception handling Block*/        returnresult; }    //static void (* Set_malloc_hander (void (*f))) ()    StaticFunc Set_malloc_hander (func f)//a function for acquiring process processing, and returning to the previous process function{Func old=__malloc_alloc_oom_handler; __malloc_alloc_oom_handler=F; returnOld ; }Private:    //defines a function pointer variable that specifies the process for handling out-of-memory processing or static Func __malloc_alloc_oom_handler;    Static void(*__malloc_alloc_oom_handler) ();};

Anatomy of the first level configurator of SGI space allocator

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.