What is _mem_set_type() ?

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

What is _mem_set_type() ?

Jump to solution
1,366 Views
alexandrevincon
Contributor III

Hello,

I am currently using MQX 4.0 and trying to write my own I/O driver, base on TFS source code. I stumbled on this code in tfs.c :

/*FUNCTION*-------------------------------------------------------------------

*

* Function Name    : _io_tfs_install

* Returned Value   : TFS error code.

* Comments         : Initialize the Trivial File System.

*

*END*---------------------------------------------------------------------*/

uint_32 _io_tfs_install

   (

      /*[IN] the name that should be given to tfs (ex: "C:", "TFS1:", etc..) */

      char_ptr              identifier,

     

      /*[IN] pointer to the first entry of the root TFS directory  */ 

      const TFS_DIR_ENTRY   *root

   )

{ /* Body */

   uint_32                      error_code;

   TFS_DRIVE_STRUCT_PTR         drive_ptr;

  

   drive_ptr = _mem_alloc_system_zero( sizeof(TFS_DRIVE_STRUCT) );

   if ( drive_ptr == NULL ) {

      return( TFS_INSUFFICIENT_MEMORY );

   }/* Endif */

   _mem_set_type(drive_ptr,MEM_TYPE_IO_TFS_DRIVE_STRUCT);     

  

   _int_disable();

   error_code = _io_dev_install_ext( identifier,

      _io_tfs_open,                    

      _io_tfs_close,

      _io_tfs_read,                     

      _io_tfs_write,                    

      _io_tfs_ioctl,

      _io_tfs_uninstall,

      (pointer)drive_ptr);

   if (error_code != IO_OK) {

      _mem_free(drive_ptr);

      drive_ptr = NULL;

      _int_enable();     

      return error_code;

   } /* Endif */

   drive_ptr->IDENTIFIER = identifier;

   drive_ptr->ROOT     = root;

   _int_enable();

   return(error_code);

} /* Endbody */


I understand all of it except the call to _mem_set_type(). This function does not seem to be documented, so I don't know what it does and how I can adapt it for my own I/O driver.

It would be great if someone could give me a clue !

Best regards,

AV

0 Kudos
Reply
1 Solution
1,015 Views
DavidS
NXP Employee
NXP Employee

Hi Alexandre,

The _mem_set_type(); function call is used to identify blocks of memory with a particular driver when allocated from heap.

Example in the UART polled driver:

ScreenHunter_13 Mar. 29 16.13.gif

The MEM_TYPE_IO_SERIAL_POLLED_DEVICE_STRUCT #define is in the mqx.h header.

Here is a picture of the heap before the _mem_set_type() call:

ScreenHunter_14 Mar. 29 16.17.gif

Here is a picture of the heap after the _mem_set_type() call:

ScreenHunter_15 Mar. 29 16.18.gif

This allows the RTOS to be able to scan through the heap to find resources (i.e. memory) to manipulate in one fashio or another.

It is also used by the Task Aware Debugging (TAD) enabled IDE debuffer to be able to display the heap and show who owes what.  The TAD was used to generate the above two heap pictures.

Regards,

David

View solution in original post

0 Kudos
Reply
4 Replies
1,016 Views
DavidS
NXP Employee
NXP Employee

Hi Alexandre,

The _mem_set_type(); function call is used to identify blocks of memory with a particular driver when allocated from heap.

Example in the UART polled driver:

ScreenHunter_13 Mar. 29 16.13.gif

The MEM_TYPE_IO_SERIAL_POLLED_DEVICE_STRUCT #define is in the mqx.h header.

Here is a picture of the heap before the _mem_set_type() call:

ScreenHunter_14 Mar. 29 16.17.gif

Here is a picture of the heap after the _mem_set_type() call:

ScreenHunter_15 Mar. 29 16.18.gif

This allows the RTOS to be able to scan through the heap to find resources (i.e. memory) to manipulate in one fashio or another.

It is also used by the Task Aware Debugging (TAD) enabled IDE debuffer to be able to display the heap and show who owes what.  The TAD was used to generate the above two heap pictures.

Regards,

David

0 Kudos
Reply
1,015 Views
alexandrevincon
Contributor III

Hello,

Thank you for your reply.

What would be the best policy for my custom I/O driver ? Can I simply omit it ? Or shall use it and create a custom #define value ?

The risk with the second option is that the value I choose is available today, but might be used by mqx in the next release...

Best regards,

AV

0 Kudos
Reply
1,015 Views
c0170
Senior Contributor III

Hello Alexandre Vincon,

I personally prefer second option with an offset (let's say at least 10) from the current top value (types are going upwards, currently max value is 1051). It specifies that that memory is being used by the new I/O driver.

One advice, if there's a function which is not documented, refer to the source code which usually provides details. Those can clarify an implementation even more than the documentation Smiley Wink

/*!

* \brief Sets type of the specified block.

*

* \param[in] mem_ptr  Address of the memory block to set type.

* \param[in] mem_type Memory type to set.

*

* \return TRUE (Type has been set.), FALSE (Type has not been set.). 

*/

boolean _mem_set_type

(

    pointer   mem_ptr,

    _mem_type mem_type

)

{

    STOREBLOCK_STRUCT_PTR block_ptr;

    if (mem_ptr != NULL) {

        block_ptr = GET_MEMBLOCK_PTR(mem_ptr);

        _int_disable();

        block_ptr->MEM_TYPE = mem_type;

        CALC_CHECKSUM(block_ptr);

        _int_enable();

        return TRUE;

    }

    else {

        return FALSE;

    }

}

Regards,

0xc0170

1,015 Views
alexandrevincon
Contributor III

Thanks a lot for your advice. I actually had a look at the source code of _mem_set_type() and then I just :smileyshocked:'ed !

:smileylaugh:

AV

0 Kudos
Reply