使用 MicroC 概要文件来管理内存

MicroC 概要文件提供了一种机制,允许您将内存段合并到应用程序中(包括能够向 ROM 分配内存)。
注: 仅当您已指定应用程序应进行编译时初始化时,此功能才可用(请参阅进行编译时初始化)。

允许生成内存分段代码

使用 C_CG::Configuration::EnableSegmentedMemory 属性来允许或禁止为应用程序生成内存分段代码。

将特性分配到特定内存段

通过使用下列属性,可以将特性分配到特定内存段:

虽然它们是特性级属性,但使用这些属性的最有效方法如下所示:

  1. 对于您要使用的每个内存段,创建一个适用于特性的构造型。
  2. 为您创建的新构造型的这些属性提供值。
  3. 将此构造型应用于所有将分配到该内存段的特性。

按如下方式使用内存段属性:

内存段属性对生成的代码的影响

如果您为 MemorySegmentName 属性提供了段名称,那么生成的代码将包含一个新构造,该构造的名称是类名与内存段名的并置。此构造包含所有已被分配到这个内存段的特性。

由于将在相应的内存段构造中声明特性,因此可能存在一种情况,即,模型中的给定类中定义的特性最终分布在多个此类内存段构造之间。为了表示模型中类的类型而生成的构造将包括指向这些特性的指针。

为 Prolog 属性输入的代码(规范和实现)将在分配到内存段的数据结构列表之前生成,例如:
#pragma data_seg("ROM")
为 Epilog 属性输入的代码(规范和实现)将在分配到内存段的数据结构列表之后生成,例如:
#pragma data_seg(DEFAULT)

访问类的分段数据

由于为模型中的类定义的特性在生成的代码中可能最终分布在不同内存段构造之间,因此 Rational Rhapsody 提供了用于访问此数据的宏,这使您不必指定此数据所分配到的内存段。这些宏所使用的格式由 C_CG::Attribute::SegmentedAttributeMacroName 属性的值确定。

例如,如果保留此属性的缺省值 $class_$attribute,那么生成的代码将类似于:#define file_0_variable_0 file_0_Seg1.variable_0

注: 提供的宏仅限于类的内部使用。要从类的外部访问特性,可以使用访问器/增变器操作。另外,不允许以外部方式透露宏的实现,例如在公用的直接插入操作中这样做。

将关系和流端口分配到特定内存段

另外,借助 MicroC 概要文件,可以将所有关系和流端口分配到特定内存段。

注: 将流端口和关系分配到特定内存段这一操作仅适用于直接流端口和直接关系(这意味着,C_CG::Configuration::DirectFlowPorts 属性和 C_CG::Configuration::DirectRelations 属性必须设置为 True)。

要将关系和流端口分配到特定内存段,请完成下列步骤:

内存段用法示例

此示例说明对内存段属性指定的值如何反应在 Rational Rhapsody 所生成的代码中。

属性名 属性值
C_CG::Configuration::DefaultROMSegmentName ROM
C_CG::Configuration::FarPointerQualifier far
对于第一个段(应用于模型中 file_0 的 variable_0 的构造型):  
C_CG::Attribute::MemorySegmentName Seg1
C_CG::Attribute::MemorySegmentSpecificationProlog #pragma DATA_SEG $seg
C_CG::Attribute::MemorySegmentSpecificationEpilog #pragma DATA_SEG DEFAULT
C_CG::Attribute::MemorySegmentImplementationProlog #pragma DATA_SEG $seg
C_CG:Attribute:MemorySegmentImplementationEpilog #pragma DATA_SEG DEFAULT
C_CG::Attribute::SegmentedAttributeMacroName $class_$attribute
对于第二个段(应用于模型中 file_0 的 variable_1 的构造型):  
C_CG::Attribute::MemorySegmentName Seg2
C_CG::Attribute::MemorySegmentSpecificationProlog #pragma DATA_SEG "$seg"
C_CG::Attribute::MemorySegmentSpecificationEpilog #pragma DATA_SEG DEFAULT
C_CG::Attribute::MemorySegmentImplementationProlog #pragma DATA_SEG "$seg"
C_CG:Attribute:MemorySegmentImplementationEpilog #pragma DATA_SEG DEFAULT
C_CG::Attribute::SegmentedAttributeMacroName $class_$attribute

生成的代码

h 文件:
#ifndef file_0_H
#define file_0_H

/*## auto_generated */
#include "mxf\Ric.h"
/*## auto_generated */
#include "Default.h"
/*## package Default */

/*## class TopLevel::file_0 */
/*#[ ignore */
typedef struct file_0_Seg1_t {
    int variable_0;		/*## attribute variable_0 */
} file_0_Seg1_t;

#define file_0_variable_0 file_0_Seg1.variable_0
typedef struct file_0_Seg2_t {
    int variable_1;		/*## attribute variable_1 */
} file_0_Seg2_t;

#define file_0_variable_1 file_0_Seg2.variable_1

/*#[ ignore */
#pragma DATA_SEG Seg1

/*#]*/
extern struct file_0_Seg1_t file_0_Seg1;
/*#[ ignore */
#pragma DATA_SEG DEFAULT

/*#]*/

/*#[ ignore */
#pragma DATA_SEG "Seg2"

/*#]*/
extern struct file_0_Seg2_t file_0_Seg2;
/*#[ ignore */
#pragma DATA_SEG DEFAULT

/*#]*/
/*#]*/
/*** User explicit entries ***/

#endif
/******************************************************************
File Path	: DefaultComponent\DefaultConfig\file_0.h
*****************************************************************/
c 文件:
/*## auto_generated */
#include "file_0.h"
/*## package Default */

/*## class TopLevel::file_0 */
/*#[ ignore */

/*#[ ignore */
#pragma DATA_SEG Seg1

/*#]*/
struct file_0_Seg1_t file_0_Seg1;
/*#[ ignore */
#pragma DATA_SEG DEFAULT

/*#]*/

#pragma DATA_SEG "Seg2"struct file_0_Seg2_t file_0_Seg2;
/*#[ ignore */
#pragma DATA_SEG DEFAULT

/*#]*/
/*#]*/

/*********************************************************************
	File Path	: DefaultComponent\DefaultConfig\file_0.c
*********************************************************************/

反馈