Amps: Memory Management Module source code explanation (2)

Source: Internet
Author: User

The above section describes the memory pool implemented by using arrays + single-chain tables in amps. This section describes another implementation method. The idea of this method is as follows: its memory pool structure is a double-chain table that stores allocated memory information, a variable that represents the memory pool size, and a pointer linked list pointing to the current memory linked list node, as follows:

/* Memory Pool Structure */struct _ newmmcontext {t_ampsdlist * membufflist;/* node linked list */INT nsizeofbuff;/* pool size */t_ampsslist * pocurrentlistptr; /* nodes currently referred */};

The structure of each node in the memory node linked list is as follows:

/* Memory linked list node Structure */struct _ newmm_memorynode {int nallocatedbytesinblock;/* allocated bytes */char * pchcurrptrinblock;/* Current pointer */char * membuff; /* Memory */};

In membuff, each memory block is allocated. The current phcurrptrinblock moves backward the number of bytes to be allocated + Memory description information area.

The structure of the memory description area is as follows:

/* Memory linked list description structure */struct _ newmmbuffdescriptor {char markerbytes [4];/* stores the value dead, which is written during allocation and compared during release. If the value is inconsistent, memory error. */INT nsizeofbuff;/* memory size of this block */t_ampsslist * dlistnode;/* stores allocated memory node pointers */};

This structure is mainly used to determine whether the memory is repeatedly released or guessed when it is released. The preceding markerbytes indicates the starting information of each small memory, with a fixed value of "dead ", it is equivalent to a test information.

Let's take a look at the memory management implementation in amps in this way:

Amps_memorymgt.h

# Ifndef _ DEFINE __# DEFINE _ DEFINE __# include "amps_defines.h" # include "amps_trace.h" # ifdef _ cplusplus extern "C" {# endiftypedef struct _ newmmcontext t_newmmcontext; typedef struct _ newmm_memorynode t_newmm_memorynode; typedef struct _ newmmbuffdescriptor t_newmmbuffdescriptor; typedef struct _ newmemsizecmp operator;/* Memory Pool Structure */struct _ newmmcontext {t_ampsdlist * Membufflist;/* node linked list */INT nsizeofbuff;/* pool size */t_ampsslist * pocurrentlistptr;/* nodes currently referred */}; /* Memory linked list node Structure */struct _ newmm_memorynode {int nallocatedbytesinblock;/* allocated bytes */char * pchcurrptrinblock;/* Current pointer */char * membuff; /* Memory */};/* Memory linked list description structure */struct _ newmmbuffdescriptor {char markerbytes [4];/* stores the value dead, which is written at allocation, when the release is compared, if they are inconsistent, the memory has encountered an error. */INT nsizeofbuff;/* memory size of this block */t_ampsslist * dlistnode;/* stores allocated memory node pointers */}; /* memory size comparison structure */struct _ newmemsizecmp {int nsizerequired; int maxsize ;}; void * mm_new_init (INT nsizeofbuff); int Merge (void * size, void * listdata ); void * mm_new_malloc (void * mm_object, int nsize); void mm_new_free (void * mm_object, char * bufftofree ); # ifdef _ cplusplus} # endif/* _ header_amps_memory_mgmt_h __*/

Amps_memorymgt.c

# Include <stdlib. h> # include <stdio. h> # include <string. h> # include <math. h> # include "amps_linklist.h" # include "amps_core.h" # include "amps_defines.h" # include "amps_memmgt.h "/***************** **************************************** * ******* Function Name: createnewmemnode Function Description: Creates a memory node input parameter: int nsizeofbuff size output parameter: return value: t_newmm_memorynode *************************************** * *************************/t_newmm_memory Node * createnewmemnode (INT nsizeofbuff) {t_newmm_memorynode * newnode = NULL;/* allocate a piece of memory and store the node information, the actual block size of the node */char * pchtempptr = (char *) OS _malloc (sizeof (t_newmm_memorynode) + nsizeofbuff); newnode = (t_newmm_memorynode *) pchtempptr; If (null! = Newnode) {newnode-> bytes = 0; newnode-> membuff = (char *) (pchtempptr + sizeof (t_newmm_memorynode); newnode-> pchcurrptrinblock = newnode-> membuff ;} else {return NULL;} return newnode;}/* allocate and release counters */INT g_nnumalloc = 0; int g_nnumfree = 0; /*************************************** * ************************** Function Name: mm_new_init Function Description: memory module initialization input parameter: int nsizeofbuff size output parameter: Return Value: t_newmm_memorynode ********* **************************************** * ****************/Void * mm_new_init (INT nsizeofbuff) {/* Create a memory pool */t_newmmcontext * newcontext = OS _malloc (sizeof (t_newmmcontext); t_ampsdlist * membufflist = NULL; If (null = newcontext) {printf ("newmm: unable to create new memory context --- out of memory \ n "); return NULL;}/* memory pool size */newcontext-> nsizeofbuff = nsizeofbuff; /* initialize the memory linked list */membufflist = dlist_init (& membuf Flist);/* linked list hanging in memory pool */If (null! = Membufflist) {t_newmm_memorynode * newnode = NULL; newcontext-> membufflist = membufflist; newnode = createnewmemnode (nsizeofbuff); If (null! = Newnode) {printf ("init mem: allocated new buffer % P \ n", newnode); g_nnumalloc ++; /* allocated nodes + 1 */newcontext-> pocurrentlistptr = dlist_append (membufflist, (void *) newnode);} else {OS _free (newcontext); dlist_free (& membufflist, null); return NULL ;}} else {OS _free (newcontext); dlist_free (& membufflist, null); return NULL ;}return newcontext ;} /*************************************** * ************************** Function Name: compa Reavailablebytes Function Description: Check whether any memory input parameter is available: void * size void * listdata linked list output parameter: return value: t_newmm_memorynode *************************************** * *************************/INT compareavailablebytes (void * size, void * listdata) {t_newmemsizecmp * sizecmp = (t_newmemsizecmp *) size; t_newmm_memorynode * memnode = (t_newmm_memorynode *) listdata; // printf ("sizecmp = % P and memnode = % P \ n", sizecmp, memnode);/* indicates the total memory block pointer in the node, pointing to the knot Tail */char * endofbuff = memnode-> membuff + sizecmp-> maxsize; /* determine whether the remaining memory in the node meets the allocated requirements */If (endofbuff-memnode-> pchcurrptrinblock)> = sizecmp-> nsizerequired) {// printf ("available = % d and sizecmp-> nsizerequired = % d \ n", (endofbuff-memnode-> pchcurrptrinblock), sizecmp-> nsizerequired ); return 0;} return-1 ;} /*************************************** * ************************** Function Name: mm_new_malloc Function Description: allocate memory input parameters: Void * mm_object memory pool pointer int nsize output parameter: return value: t_newmm_memorynode *************************************** * *************************/void * mm_new_malloc (void * mm_object, int nsize) {t_newmmcontext * mmcontext = (t_newmmcontext *) mm_object; int nactualsize = 0; t_ampsslist * listnode = NULL; t_newmm_memorynode * memnode = NULL; // printf ("malloc: ctxt = % P, size = % d \ n ", mmcontext, nsize); If (nsize <0) {printf (" m Alloc: Bad allocation request size % d \ n ", nsize); return NULL;} If (null = mm_object) {return NULL ;} /* The size required is greater than the size allowed in the pool */If (nsize> mmcontext-> nsizeofbuff) {printf ("malloc: Too large allocation request for size % d: the Memory Manager supports up to % d bytes requests \ n ", nsize, mmcontext-> nsizeofbuff); return NULL;} If (0 = nsize) {nsize = 4; /* the following offset uses */}/* to specify the actual size to be allocated + Memory comparison header size */nactualsize = nsize + sizeof (t_ne Wmmbuffdescriptor);/* Move 4 bytes to the backend */nactualsize = (nactualsize + 3)> 2) <2; // go to next four byte boundary // printf ("malloc: ctxtt = % P, nactualsize = % d, on mmcontext-> membufflist = % P \ n", mmcontext, nactualsize, mmcontext-> membufflist);/* points to the current pool pointer */listnode = mmcontext-> pocurrentlistptr; If (null! = Listnode) {t_newmemsizecmp sizecmp; sizecmp. nsizerequired = nactualsize; sizecmp. maxsize = mmcontext-> nsizeofbuff;/* check whether memory is available for allocation */If (-1 = compareavailablebytes (void *) & sizecmp, listnode-> pvdata )) {listnode = NULL;} // listnode = dlist_search (mmcontext-> membufflist, compareavailablebytes, (void *) & sizecmp);}/* obtain the existing memory node, if no node exists, a new node is allocated. * // printf ("malloc: ctxt = % P, nsize (after dlistsearch) = % d listnode = % P \ N ", mmcontext, nsize, listnode); If (null! = Listnode) {memnode = (t_newmm_memorynode *) listnode-> pvdata; // printf ("ctxt = % P, memnode-> membuff = % P, memnode-> pchcurrptrinblock = % P \ n ", mmcontext, memnode-> membuff, memnode-> nodes);} else {t_newmm_memorynode * newnode = createnewmemnode (mmcontext-> nsizeofbuff ); if (null! = Newnode) {g_nnumalloc ++; listnode = dlist_append (mmcontext-> membufflist, (void *) newnode); If (null = listnode) {printf ("**** new mm malloc: Dlist append failed .. \ n "); OS _free (newnode); return NULL;} // printf (" ctxt = % pallocated new buffer % P, total buffers allocated = % d, listnode = % P \ n ", mmcontext, newnode, g_nnumalloc, listnode); memnode = newnode; mmcontext-> pocurrentlistptr = listnode;} else {printf ("Malloc: unable to allocate new node \ n"); return NULL ;}} {/* indicates the current pointer to the memory block in the retrieved node */char * tempptr = memnode-> pchcurrptrinblock; t_newmmbuffdescriptor * buffdescriptorptr = (t_newmmbuffdescriptor *) tempptr; /* the first four bytes of the memory description are assigned dead */memcpy (buffdescriptorptr-> markerbytes, "dead", 4 ); /* memory size of this block */buffdescriptorptr-> nsizeofbuff = nactualsize;/* store the current memory node pointer */buffdescriptorptr-> dlistnode = listnode; // store the back PTR to Mem node/* increase the number of memory allocated bytes in the node */memnode-> nallocatedbytesinblock + = nactualsize;/* skip the memory description zone */tempptr + = sizeof (t_newmmbuffdescriptor ); // tempptr now points to writeable area of this buffer for the application/* The current pointer in the node moves backward */memnode-> pchcurrptrinblock + = nactualsize; // current PTR now advances to next buffer to be allocated memset (tempptr, 0, nsize); // zero out of the contents of the allocated mem Ory // printf ("malloc: mmcontext = % P: memnode-> nallocatedbytesinblock = % d and requested size = % d \ n", mmcontext, memnode-> nallocatedbytesinblock, nsize ); return tempptr ;}} /*************************************** * ************************** Function Name: mm_new_free Function Description: Allocation and release input parameter: void * mm_object memory pool pointer char * bufftofree memory pointer output parameter to be released: return value: void *************************************** * *************************/void mm_ne W_free (void * mm_object, char * bufftofree) {t_newmmcontext * mmcontext = (t_newmmcontext *) mm_object; char * tempptr = bufftofree; t_newmm_memorynode * memnode = NULL; t_ampsslist * listnode = NULL; t_newmmbuffdescriptor * buffdescriptorptr = NULL;/* move the pointer to be released forward, find the memory description area */tempptr-= sizeof (t_newmmbuffdescriptor); buffdescriptorptr = (t_newmmbuffdescriptor *) tempptr; if (null = tempptr) {printf ("free: Buffer descri Ptor PTR is null .. memory upload uption \ n "); return;}/* Get the node pointer stored during allocation from the memory description area */listnode = (t_ampsslist *) buffdescriptorptr-> dlistnode; memnode = (t_newmm_memorynode *) listnode-> pvdata;/* if it is marked as not daed In the description area, it indicates that the block memory has an error (for example, it is overwritten) */If (0! = Memcmp (buffdescriptorptr-> markerbytes, "dead", 4 ))) {printf ("Free :>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> >>> marker string not found .. memory upload uption. >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> \ n "); return;}/* the node is empty, indicating that it has been released */If (null = listnode) {printf (" free: listnode is null: double Free or duplicate uption \ n "); return;} If (null = memnode) {printf (" free: memnode is null: Double Free or duplicate uption \ n "); return;}/* node memory used to reduce the number of bytes RELEASED */memnode-> nallocatedbytesinblock-= buffdescriptorptr-> nsizeofbuff;/* if the number of memory used is smaller than 0 or greater than the pool size, error */If (memnode-> nallocatedbytesinblock <0 | memnode-> nallocatedbytesinblock> mmcontext-> nsizeofbuff) {printf ("free: allocated bytes = % d, double Free or duplicate uption \ n ", memnode-> nallocatedbytesinblock); return;} // printf (" free: mmcontext = % P: memnode-> nallocatedbytesinblock = % d \ n ", mmcontext, memnode-> nallocatedbytesinblock);/* If the used memory is 0, release this node */If (0 = memnode-> nallocatedbytesinblock) {g_nnumfree ++; // printf ("ctxt = % pavailable bytes are 0 .. freeing memnode % P, total buffers free 'd = % d \ n ", mmcontext, memnode, g_nnumfree); If (listnode = mmcontext-> pocurrentlistptr) {mmcontext-> pocurrentlistptr = NULL;} dlist_remove (& mmcontext-> membufflist, listnode, null); free (memnode );}}

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.