/*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