C - construct

From RTSC-Pedia

Jump to: navigation, search
revision tip
—— LANDSCAPE orientation
[printable version]  [offline version]offline version generated on 18-Nov-2017 00:11 UTC

C - construct

Initialize a new instance object from a provided structure

Contents

Synopsis

 
 
 
 
 
 
 
#include <«pkg»/«Mod».h>
 
 /* modules whose «Mod»_Instance_init() function never fails */
Void «Mod»_construct( «Mod»_Struct *structP, [«required_args»,] const «Mod»_Params *params );
 
 /* @InstanceInitError modules only */
Void «Mod»_construct( «Mod»_Struct *structP, [«required_args»,] const «Mod»_Params *params, Error_Block *eb );

Description

The construct method is used to create a new instance object managed by the module using a structure allocated by the caller. This allows clients to allocate memory for an instance from the stack (as an automate variable) or from the program's static data space. Module construct is especially useful in small systems where there is little or no heap memory.

Some modules do not support construct in STATIC_POLICY configurations; for example, modules that need to create other objects as part of their instance initialization. Only modules that declare in their specification that they have the @InstanceInitStatic attribute can support construct in STATIC_POLICY configurations.

Example

In the example below, an instance object is created from a structure allocated on the stack.

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
#include <some/pkg/Mod.h>
 
int main(void) {    
    Mod_Params params;            /* instance params struct */
    Mod_Struct obj;               /* instance object struct */
    Mod_Handle inst;              /* instance handle */
 
    /* initialize a pre-allocated instance object */
    Mod_Params_init(&params);     /* initialize params with default values */
    Mod_construct(&obj, &params); /* create a Mod instance from obj */
 
    /* use the new instance */
    inst = Mod_handle(&obj);      /* type-safe cast of &obj to a Mod_Handle */
    Mod_«someOperation»(inst);
 
    Mod_destruct(&obj);           /* delete previously constructed instance */
    return 0;
}

See also

C - create Allocate and initialize a new instance object and return its handle
C - destruct Finalize a previously constructed instance object
C - Instance_init Module's instance initialization function

[printable version]  [offline version]offline version generated on 18-Nov-2017 00:11 UTC
Copyright © 2008 The Eclipse Foundation. All Rights Reserved
Personal tools
package reference