Page 89 - DCAP408_WEB_PROGRAMMING
P. 89

Unit 5: Memory Management (I)




          5.3 Using Fixed Memory Blocks                                                         Notes

          Fixed size  memory blocks allocation algorithm has been established to permit technique of
          memory allocation in static time, separately to number of  allocated blocks. This method  is
          extensively used in real-time applications.
          First plan that should be executed  is to arrange the  memory by  means of stFixedMemInit
          function.  Just  after  it  can  be  utilized  stFixedMemAlloc  to  assign  memory  blocks  and
          stFixedMemFree to release assigned memory blocks in a provided memory.

          All of the allocated blocks have a fixed size, defined throughout memory initialization. The size
          is allocated to value stated in AR_MEMORY_ALIGNMENT constant, defined in architecture
          specific files. Block addresses are always allocated not relatively to the NULL but to address
          specified throughout memory initialization.
          Functions do not sets the previous error code on breakdown. Access to the fixed size memory is
          never harmonized. If it is compulsory it should be implemented.
          If  the fixed size memory management module  will not  be utilized,  it can  be expelled from
          compilation by setting ST_USE_FIXMEM for 0.
          Now we illustrate some examples of using fixed size memory management module.


                 Example: Fixed size memory pool initialization
          The example  below displays how  to initialize  memory pool for fixed  size memory  blocks
          allocation.

          #include  <stdio.h>
          #include  “ST_API.h”


          UINT8  MemoryPool[1024];


          int  main(void)
          {
              PVOID  Block;


              /*  Initialization  */
              arInit();
              stInit();


              /*  Initialize  memory  pool  for  fixed-size  memory  blocks  allocation.
                    Size  of  the  memory  block  is  set  to  16  bytes  */
              if(!stFixedMemInit(MemoryPool,  sizeof(MemoryPool),  16))
            {
                  printf(“Failure  during  memory  pool  initialization.\n”);
              return 0;
            }






                                           LOVELY PROFESSIONAL UNIVERSITY                                   83
   84   85   86   87   88   89   90   91   92   93   94