Everything

CHAPTER 7 MEMORY POOL MANAGEMENT FUNCTIONS


This chapter describes the memory pool management functions performed by the RI850V4.
7.1 Outline
The statically secured memory areas in the Kernel Initialization Module are subject to management by the memory pool management functions of the RI850V4.
The RI850V4 provides a function to reference the memory area status, including the detailed information of fixed/variable-size memory pools, as well as a function to dynamically manipulate the memory area, including acquisition/release of fixed/variable-size memory blocks, by releasing a part of the memory area statically secured/initialized as "Fixed-Sized Memory Pools", or "Variable-Sized Memory Pools".
Table 7-1 Memory Area
.kernel_system
Area where executable code of RI850V4 is allocated.
.kernel_const
Area where static data of RI850V4 is allocated.
.kernel_data
Area where dynamic data of RI850V4 is allocated.
.kernel_data_init
Area where kernel initialization flag of RI850V4 is allocated.
.kernel_const_trace.const
Area where static data of trace function is allocated.
.kernel_data_trace.bss
Area where dynamic data of trace function is allocated.
.kernel_work
Area where system stack, task stack, data queue, fixed-sized memory pool and variable-sized memory pool is allocated.
.sec_namuser-defined area
Area where task stack, data queue, fixed-sized memory pool and variable-sized memory pool is allocated.

7.2 User-Own Coding Module
To support various execution environments, the hardware-dependent processing (Post-overflow processing) that is required for the RI850V4 to execute processing is extracted as a user-own coding module.
This enhances portability to various execution environments and facilitates customization as well.
Note The RI850V4 checks stack overflows only when "TA_ON: Overflow is checked" is defined as "Whether to check stack: stkchk" in the system configuration file.
7.2.1 Post-overflow processing
This is a routine dedicated to post-processing that is extracted as a user-own coding module to execute post-overflow processing and is called when a stack overflow occurs in a processing program.
- Basic form of post-overflow processing
When coding the post-overflow processing, use a void function (function name: _kernel_stk_overflow) with two INT-type arguments.
The "value of stack pointer sp when a stack overflow is detected" is set for the
r6 argument, and the "value of program counter pc when a stack overflow is detected" is set for the r7 argument.
The following shows the basic form of the post-overflow processing in assembly language.

 #include    <kernel.h>              /*Standard header file definition*/
 
         .text
         .align  0x2
         .globl  __kernel_stk_overflow
 
 __kernel_stk_overflow :
 
     .........
 
     .........
 
 .halt_loop :

- Internal processing of post-overflow processing
The overflow processing is a routine dedicated to post-processing that is extracted as a user-own coding module to execute post-overflow processing and is called when a stack necessary for the RI850V4 and the processing program has overflowed. Therefore, note the following points when coding post-overflow processing.
- Coding method
Code post-overflow processing using the C or assembly language.
When coding in C, it can be coded in the same manner as ordinary functions.
When coding in assembly language, code it according to the calling convention in the compiler used.
- Stack switching
The RI850V4 does not perform the processing related to stack switching when passing control to post-overflow processing. Therefore, when using the stack for post-overflow processing, the code for stack setting (setting of the stack pointer SP) should be written at the beginning of post-overflow processing.
- Service call issue
Issue of service calls is prohibited in post-overflow processing because correct operation cannot be guaranteed.
The following is a list of processes that should be executed in post-overflow processing.
- Post-processing that handles stack overflows
Note The processing (such as reset) that should be coded as post-overflow processing depends on the user system.
7.3 Fixed-Sized Memory Pools
When a dynamic memory manipulation request is issued from a processing program in the RI850V4, the fixed-sized memory pool is provided as a usable memory area.
Dynamic memory manipulation of the fixed-size memory pool is executed in fixed size memory block units.
7.3.1 Create fixed-sized memory pool
In the RI850V4, the method of creating a fixed-sized memory pool is limited to "static creation".
Fixed-sized memory pools therefore cannot be created dynamically using a method such as issuing a service call from a processing program.
Static fixed-size memory pool creation means defining of fixed-size memory pools using static API "CRE_MPF" in the system configuration file.
For details about the static API "CRE_MPF", refer to "17.5.7 Fixed-sized memory pool information".
7.3.2 Acquire fixed-sized memory block
A fixed-sized memory block is acquired (waiting forever, polling, or with timeout) by issuing the following service call from the processing program.
- get_mpf
This service call acquires the fixed-sized memory block from the fixed-sized memory pool specified by parameter mpfid and stores the start address in the area specified by parameter p_blk.
If no fixed-size memory blocks could be acquired from the target fixed-size memory pool (no available fixed-size memory blocks exist) when this service call is issued, this service call does not acquire the fixed-size memory block but queues the invoking task to the target fixed-size memory pool wait queue and moves it from the RUNNING state to the WAITING state (fixed-size memory block acquisition wait state).
The WAITING state for a fixed-sized memory block is cancelled in the following cases, and then moved to the READY state.
WAITING State for a Fixed-sized Memory Block Cancel Operation
Return Value
A fixed-sized memory block was returned to the target fixed-sized memory pool as a result of issuing rel_mpf.
E_OK
A fixed-sized memory block was returned to the target fixed-sized memory pool as a result of issuing irel_mpf.
E_OK
Forced release from waiting (accept rel_wai while waiting).
E_RLWAI
Forced release from waiting (accept irel_wai while waiting).
E_RLWAI

The following describes an example for coding this service call.
 #include    <kernel.h>              /*Standard header file definition*/
 #include    <kernel_id.h>           /*System information header file definition*/
 
 void task (VP_INT exinf)
 {
     ER      ercd;                   /*Declares variable*/
     ID      mpfid = 1;              /*Declares and initializes variable*/
     VP      p_blk;                  /*Declares variable*/
 
     .........
 
     ercd = get_mpf (mpfid, &p_blk); /*Acquire fixed-sized memory block */
                                     /*(waiting forever)*/
 
     if (ercd == E_OK) {
         .........                   /*Normal termination processing*/
 
         rel_mpf (mpfid, p_blk);     /*Release fixed-sized memory block*/
     } else if (ercd == E_RLWAI) {
         .........                   /*Forced termination processing*/
     }
 
         .........
 }

Note 1 The RI850V4 does not perform memory clear processing when getting the acquired fixed-size memory block. The contents of the got fixed-size memory block are therefore undefined.
Note 2 Invoking tasks are queued to the target fixed-size memory pool wait queue in the order defined during configuration (FIFO order or priority order).
Note 3 If the fixed-size memory block acquisition wait state is cancelled because rel_wai or irel_wai was issued, the contents in the area specified by parameter p_blk become undefined.
- pget_mpf, ipget_mpf
This service call acquires the fixed-sized memory block from the fixed-sized memory pool specified by parameter mpfid and stores the start address in the area specified by parameter p_blk.
If a fixed-sized memory block could not be acquired from the target fixed-sized memory pool (no available fixed-sized memory blocks exist) when this service call is issued, fixed-sized memory block acquisition processing is not performed but "E_TMOUT" is returned.
The following describes an example for coding this service call.
 #include    <kernel.h>              /*Standard header file definition*/
 #include    <kernel_id.h>           /*System information header file definition*/
 
 void task (VP_INT exinf)
 {
     ER      ercd;                   /*Declares variable*/
     ID      mpfid = 1;              /*Declares and initializes variable*/
     VP      p_blk;                  /*Declares variable*/
 
     .........
 
                                     /*Acquire fixed-sized memory block (polling)*/
     ercd = pget_mpf (mpfid, &p_blk);
 
     if (ercd == E_OK) {
         .........                   /*Polling success processing*/
 
         rel_mpf (mpfid, p_blk);     /*Release fixed-sized memory block*/
     } else if (ercd == E_TMOUT) {
         .........                   /*Polling failure processing*/
     }
 
     .........
 }

Note 1 The RI850V4 does not perform memory clear processing when getting the acquired fixed-size memory block. The contents of the got fixed-size memory block are therefore undefined.
Note 2 If no fixed-size memory blocks could be acquired from the target fixed-size memory pool (no available fixed-size memory blocks exist) when this service call is issued, the contents in the area specified by parameter p_blk become undefined.
- tget_mpf
This service call acquires the fixed-sized memory block from the fixed-sized memory pool specified by parameter mpfid and stores the start address in the area specified by parameter p_blk.
If no fixed-size memory blocks could be acquired from the target fixed-size memory pool (no available fixed-size memory blocks exist) when this service call is issued, this service call does not acquire the fixed-size memory block but queues the invoking task to the target fixed-size memory pool wait queue and moves it from the RUNNING state to the WAITING state with timeout (fixed-size memory block acquisition wait state).
The WAITING state for a fixed-sized memory block is cancelled in the following cases, and then moved to the READY state.
WAITING State for a Fixed-sized Memory Block Cancel Operation
Return Value
A fixed-sized memory block was returned to the target fixed-sized memory pool as a result of issuing rel_mpf.
E_OK
A fixed-sized memory block was returned to the target fixed-sized memory pool as a result of issuing irel_mpf.
E_OK
Forced release from waiting (accept rel_wai while waiting).
E_RLWAI
Forced release from waiting (accept irel_wai while waiting).
E_RLWAI
Polling failure or timeout.
E_TMOUT

The following describes an example for coding this service call.
 #include    <kernel.h>              /*Standard header file definition*/
 #include    <kernel_id.h>           /*System information header file definition*/
 
 void task (VP_INT exinf)
 {
     ER      ercd;                   /*Declares variable*/
     ID      mpfid = 1;              /*Declares and initializes variable*/
     VP      p_blk;                  /*Declares variable*/
     TMO     tmout = 3600;           /*Declares and initializes variable*/
 
     .........
                                     /*Acquire fixed-sized memory block*/
                                     /*(with timeout)*/
     ercd = tget_mpf (mpfid, &p_blk, tmout);
 
     if (ercd == E_OK) {
         .........                   /*Normal termination processing*/
 
         rel_mpf (mpfid, p_blk);     /*Release fixed-sized memory block*/
     } else if (ercd == E_RLWAI) {
         .........                   /*Forced termination processing*/
     } else if (ercd == E_TMOUT) {
         .........                   /*Timeout processing*/
     }
 
     .........
 }

Note 1 The RI850V4 does not perform memory clear processing when getting the acquired fixed-size memory block. The contents of the got fixed-size memory block are therefore undefined.
Note 2 Invoking tasks are queued to the target fixed-size memory pool wait queue in the order defined during configuration (FIFO order or priority order).
Note 3 If the fixed-size memory block acquisition wait state is cancelled because rel_wai or irel_wai was issued or the wait time elapsed, the contents in the area specified by parameter p_blk become undefined.
Note 4 TMO_FEVR is specified for wait time tmout, processing equivalent to get_mpf will be executed. When TMO_POL is specified, processing equivalent to pget_mpf /ipget_mpf will be executed.
7.3.3 Release fixed-sized memory block
A fixed-sized memory block is returned by issuing the following service call from the processing program.
- rel_mpf, irel_mpf
This service call returns the fixed-sized memory block specified by parameter blk to the fixed-sized memory pool specified by parameter mpfid.
If a task is queued to the target fixed-sized memory pool wait queue when this service call is issued, fixed-sized memory block return processing is not performed but fixed-sized memory blocks are returned to the relevant task (first task of wait queue).
As a result, the relevant task is unlinked from the wait queue and is moved from the WAITING state (WAITING state for a fixed-sized memory block) to the READY state, or from the WAITING-SUSPENDED state to the SUSPENDED state.
The following describes an example for coding this service call.
 #include    <kernel.h>              /*Standard header file definition*/
 #include    <kernel_id.h>           /*System information header file definition*/
 
 void task (VP_INT exinf)
 {
     ER      ercd;                   /*Declares variable*/
     ID      mpfid = 1;              /*Declares and initializes variable*/
     VP      blk;                    /*Declares variable*/
 
     .........
 
     ercd = get_mpf (mpfid, &blk);   /*Acquire fixed-sized memory block */
                                     /*(waiting forever)*/
 
     if (ercd == E_OK) {
         .........                   /*Normal termination processing*/
 
         rel_mpf (mpfid, blk);       /*Release fixed-sized memory block*/
     } else if (ercd == E_RLWAI) {
         .........                   /*Forced termination processing*/
     }
 
     .........
 }

Note 1 The RI850V4 does not perform memory clear processing when returning the acquired fixed-size memory block. The contents of the returned fixed-size memory block are therefore undefined.
Note 2 When returning fixed-size memory blocks, be sure to issue either of these service calls for the acquired fixed-size memory pools. If the service call is issued for another fixed-size memory pool, no error results but the operation is not guaranteed after that.
7.3.4 Reference fixed-sized memory pool state
A fixed-sized memory pool status is referenced by issuing the following service call from the processing program.
- ref_mpf, iref_mpf
Stores fixed-sized memory pool state packet (ID number of the task at the head of the wait queue, number of free memory blocks, etc.) of the fixed-sized memory pool specified by parameter mpfid in the area specified by parameter pk_rmpf.
The following describes an example for coding this service call.
 #include    <kernel.h>              /*Standard header file definition*/
 #include    <kernel_id.h>           /*System information header file definition*/
 
 void task (VP_INT exinf)
 {
     ID      mpfid = 1;              /*Declares and initializes variable*/
     T_RMPF  pk_rmpf;                /*Declares data structure*/
     ID      wtskid;                 /*Declares variable*/
     UINT    fblkcnt;                /*Declares variable*/
     ATR     mpfatr;                 /*Declares variable*/
 
     .........
 
     ref_mpf (mpfid, &pk_rmpf);      /*Reference fixed-sized memory pool state*/
 
     wtskid = pk_rmpf.wtskid;        /*Reference ID number of the task at the */
                                     /*head of the wait queue*/
     fblkcnt = pk_rmpf.fblkcnt;      /*Reference number of free memory blocks*/
     mpfatr = pk_rmpf.mpfatr;        /*Reference attribute*/
 
     .........
 }

Note For details about the fixed-sized memory pool state packet, refer to "15.2.9 Fixed-sized memory pool state packet".