Memory Pool Technology (iii)--near-view memory pool

Source: Internet
Author: User
Tags volatile

      today goes into the main function of learning. First look at the user request memory function Mem_malloc:code:static void *mem_malloc (size_t n)    {        obj * volatile *my_free_list;        obj *result;           /*  If the requested memory exceeds the maximum memory we set, use crt */        if  (n >  (size_t) __max_bytes)  {            void *p =  (void *) malloc (n);            return p;        }     /*  find the appropriate one in the 16 free list list  */        my_free_list = free_list + __freelist_index (n);        result = *my_free_list;        if  (result == 0)  {           /*  Prepare to refill free list when no free list are available  */           void *ret = refill (__round_ Up (n));            return ret;        }           /*  adjust list  */        *my_free_list = result->free_list_link;        return result;   }  

       what needs to be explained here is that in the beginning, in order to improve efficiency, the memory allocation and filling of the free list will be postponed until the user requests the memory, nothing else to say. Then see release memory Function Mem_free:code:static void mem_free (void *p, size_t n)    {        obj *q =  (obj*) p;        obj * volatile *my_free_list;           /*  greater than __max_bytes direct release  */        if  (n >  (size_t)  __max_bytes) {           free (P);         return;     }             /*  find the right list, reclaim chunks and adjust the list   */         my_free_list = free_list + __freelist_index (n) ;        q->free_list_link = *my_free_list;        *my_free_list = q;   }  

       the release process is nothing to say, then look at the Fill list function Refill:code:static void *refill (size_t  n)    {       int nobjs = 20;         char *chunk = chunk_alloc (N, &NOBJS);        obj * volatile *my_free_list;        obj *result;        obj *current_obj, *next_obj;        int i;           if  (nobjs == 1)             return chunk;                my_free_list = free_list  + __freelist_index (n);           result =  (obj *) chunk;/*  This chunk is returned to the user  */       &NBSp;   /* free list points to the newly configured space (taken from the memory pool) */       *my_free_list  = next_obj =  (obj *) (chunk + n);           /*  The following link each node together  */        for  (i = 1; ; i++)  {/*  starting from 1, because the NO. 0 block is returned to the user  */            current_obj = next_obj;            next_obj =  (obj *) (char *) NEXT_OBJ + N);                         if  (nobjs - 1 == i)  {/*  last node */                current_obj->free_list_link = NULL;                break;            } else {                current_obj->free_list_link = next_obj;            }        }        return result;   }  

      when the user invokes the request Mem_malloc found the corresponding linked list pointer to null, the Refill,refill will be called to call Chunk_alloc from the memory pool ' Want ' a certain number of users to request the size of the chunk of memory. If the memory pool returns 1 chunks at this time, then return directly to the user, complete the application process, the person will be removed back to the user's chunk, leaving the memory string to be used for the next time. Next look at the memory pool function Chunk_alloc, her job is to take the space from the memory pool to the free list by: Code:    static char *chunk_alloc (size_t  SIZE,&NBSP;INT&NBSP;*NOBJS)        {            char *result;           size_t total_bytes =  size * (*NOBJS);        /*  amount of memory to apply  */          size_t byte_left = end_free - start_free; /*   Memory pool remaining space  */                if  (byte_ left >= total_bytes)  { /*  memory in memory pool fully satisfies the need  */               result = start_free;                start_free += total_bytes;                return result;           } else if  (byte_ left >= size)  { /*  memory pool does not fully meet demand, but can provide more than one chunk  */               *nobjs = byte_left / size;                total_bytes = size * (*NOBJS);                result = start_free;                start_free += total_bytes;               return result;       & NBsp;   } else {                /*  Memory pool remaining space cannot be provided with a chunk size  */               size_t bytes_to_get = + __round_up (heap_size >> 4);                                                                    /*  allocate a fraction of the memory pool to the appropriate free list */               if  (byte_left > 0)  {                    obj * volatile * my_free _list =&nBsp;free_list + __freelist_index (byte_left);                     ((obj*) start_free)->free_list_link = *my_free_ list;                   *my _free_list =  (obj *) start_free;                }                               /*  Configure the heap space to replenish the memory pool  */              start_free =  (char *) malloc (Bytes_ To_get);               if  (0 ==  Start_free)  { /* heap  Lack of space, malloc () failure  */                   size_t i;                    obj * volatile *my_free_list;                    obj *p;                                                                                             /*  search suitable (larger than the current size) free list * /                  for  (i  = size; i <= __max_bytes; i += __align)  {                       my_free_list  = free_list + __freelist_index (i);                        p = *my_free_list;                          if  (0 != p)  { /* free list  unused blocks in  */                         /*  adjust free list to release unused chunks  */                        *my_free_list =  P->free_list_link;                            start_free =  (char *) p;                             end_free = start_free + i;                            return  Chunk_alloc (SIZE,&NBSP;NOBJS); /*  recursive call, in order to fix nobjs */                       /* ps: Any remaining fraction will eventually be incorporated into the appropriate free list  */                    }                   &nBSP;}                end_free = 0;                    start_free =   (char*) malloc (bytes_to_get);                   }               /*   Record current memory capacity  */           heap_size += bytes _to_get;            end_free = start_free + bytes_ To_get;            return chunk_alloc (SIZE,NOBJS);        }      }   

Chunk_alloc's role is to take the memory from the memory pool to the free list, so it will do its best to meet the user's needs. If there is enough space to return a specified number of chunks, no one returns the actual number of chunks, or does not meet the user's needs, then the remaining fraction of the memory pool is placed in the appropriate list, and then the system asks for space to replenish the memory pool. If the system also has no memory available, find out if there are chunks larger than size in the free list and use it to replenish space.
If no memory is available everywhere, the space will be configured again and an exception may be thrown. Otherwise, the memory pool size is logged. Next we look at the use of the memory pool.
fill up the Mem_realloc function: Code:static void *mem_realloc (VOID&NBSP;*PTR,&NBSP;SIZE_T&NBSP;NEW_SZ,&NBSP;SIZE_T&NBSP;OLD_SZ)    {       void *result;        size_t  copy_sz;           /*  If both old_sz  and &NBSP;NEW_SZ are larger than the maximum memory we set, then use &NBSP;CRT directly  */       if  (old_sz >  (size_t) __max_bytes &&  new_sz >  (size_t) __max_bytes)             Return realloc (PTR,&NBSP;NEW_SZ);                if  (__round_up (OLD_SZ)  == &NBSP;__ROUND_UP (NEW_SZ))            return ptr;        result = mem_malloc (NEW_SZ);        copy_sz = new_sz > old_sz ? old_sz : new_sz;        memcpy (RESULT,&NBSP;PTR,&NBSP;COPY_SZ);        mem_free (PTR,&NBSP;OLD_SZ);        return result;   }  

The role of

      Mem_realloc is to replace the REALLOC function, which is to be understood in context, if both OLD_SZ and NEW_SZ are larger than the maximum memory we set, then directly Crt. So suppose OLD_SZ < __max_bytes = 128. Here are two things to consider:
      1, NEW_SZ > __max_bytes: Obviously the memory pool does not meet the needs, but why the program also executes this sentence: result = Mem_malloc (NEW_SZ), do not forget that mem_malloc to more than __max_bytes memory will use the CRT
      2, NEW_SZ <= __max_bytes:
            A, __round_up (OLD_SZ) = = __round_up (NEW_SZ) : Returns PTR directly. There may be a bit of confusion here, assuming that OLD_SZ = 1, NEW_SZ = 5, then the direct return is not not satisfied with the user needs. That's not the case, because the memory we allocated when we used Mem_malloc (OLD_SZ) was 8.
            B, __round_up (OLD_SZ)! = __round_up (NEW_SZ) : This situation directly performs subsequent operations-allocating memory, copying the original content to the new memory, and releasing the old memory.
      if OLD_SZ > __max_bytes and NEW_SZ < __max_bytes. As simple as 2.b, follow-up operations are performed directly.
      can say just a little.

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.