Main Content

API for Generated Reusable Code

When you generate reusable code, MATLAB® Coder™ supports dynamic allocation of:

  • Function variables that are too large for the stack

  • Persistent variables

  • Global variables

It generates a header file, primary_function_name_types.h, that you must include when using the generated code. This header file contains the following structures:

  • primary_function_nameStackData

    This structure contains the user-allocated memory. You must pass a pointer to this structure as the first parameter to all functions that use it:

    • Directly, because the function uses a field in the structure.

    • Indirectly, because the function passes the structure to a called function.

    If the algorithm uses persistent or global data, the primary_function_nameStackData structure also contains a pointer to the primary_function_namePersistentData structure. If you include this pointer, you only have to pass one parameter to each calling function.

  • primary_function_namePersistentData

    If your algorithm uses persistent or global variables, MATLAB Coder provides a separate structure for them. The memory allocation structure contains a pointer to this structure. Because you have a separate structure for persistent and global variables, you can allocate memory for these variables once and share them with all threads. However, if there is no communication between threads, you can allocate memory for these variables per thread.

For more information on using these global structures, see Multithreaded Examples.