Company Events Academic NI Developer Zone Support Solutions Products & Services Contact NI MyNI

Code Globals and CIN Data Space Globals

LabVIEW 8.5 Help
August 2007

NI Part Number:
371361D-01

»View Product Info

When you declare global or static local data within a CIN code resource, LabVIEW allocates storage for that data. LabVIEW maintains your globals across calls to various routines.

When you allocate a global in a CIN code resource, LabVIEW creates storage for only one instance of the global, regardless of whether the VI is reentrant or whether you have multiple references to the same code resource in memory.

In some cases, you might want globals for each reference to the code resource multiplied by the number of usages of the VI, if the VI is reentrant. For each instance of one of these globals, LabVIEW allocates the CIN data space for the use of the CIN. Within the CINInit, CINDispose, CINAbort, and CINRun routines, you can call the GetDSStorage routine to retrieve the value of the CIN data space for the current instance. You also can call SetDSStorage to set the value of the CIN data space for this instance. You can use the storage location set by SetDSStorage to store any 4-byte quantity you want to have for each instance of one of these globals. If you need more than four bytes of global data, store a handle or pointer to a structure containing your globals.

The following code examples show the exact syntax of the GetDSStorage and SetDSStorage routines defined in extcode.h:

  • int32 GetDSStorage(void);

    This routine returns the value of the 4-byte quantity in the CIN data space LabVIEW allocates for each CIN code resource, or for each use of the surrounding VI, if the VI is reentrant. Call this routine only from CINInit, CINDispose, CINAbort, or CINRun.

  • int32 SetDSStorage(int32 newVal);

    This routine sets the value of the 4-byte quantity in the CIN data space LabVIEW allocates for each CIN use of that code resource, or the uses of the surrounding VI, if the VI is reentrant. It returns the old value of the 4-byte quantity in that CIN data space. Call this routine only from CINInit, CINDispose, CINAbort, or CINRun.

Code Globals and CIN Data Space Globals Examples

The following examples illustrate the differences between code globals and CIN data space globals. In both examples, the CIN takes a number and returns the average of that number and the previous numbers passed to it, as shown in the following figure.

When you write your application, decide whether it is appropriate to use code globals or data space globals. If you use code globals, calling the same code resource from multiple CINs or different reentrant VIs affects the same set of globals. In the code globals averaging example, the result indicates the average of all values passed to the CIN.

If you use CIN data space globals, each CIN calling the same code resource and each VI can have its own set of globals, if the VI is reentrant. In the CIN data space globals averaging example, the results indicate the average of values passed to a specific node for a specific data space.

If you have only one CIN referencing the code resource and the VI containing that CIN is not reentrant, choose either method.

Using Code Globals

The following code averages using code globals:

/*

* CIN source file

*/

#include "extcode.h"

float64 gTotal;

int32 gNumElements;

CIN MgErr CINRun(float64 *new_num, float64 *avg);

CIN MgErr CINRun(float64 *new_num, float64 *avg)

    {

    gTotal += *new_num;

    gNumElements++;

    *avg = gTotal / gNumElements;

    return noErr;

    }

CIN MgErr CINLoad(RsrcFile rf)

    {

    gTotal=0;

    gNumElements=0;

    return noErr;

    }

The variables are initialized in CINLoad. If the variables are dynamically created, that is, if they are pointers or handles, you can allocate the memory for the pointer or handle in CINLoad and deallocate it in CINUnload. You can allocate and deallocate the memory using CINLoad and CINUnload because CINLoad and CINUnload are called only once, regardless of the number of references to the code resources and the number of data spaces. This example does not use the UseDefaultCINLoad macro because this .c file has a CINLoad function.

Using CIN Data Space Globals

The following code uses CIN data space globals to perform the averaging:

/*

* CIN source file

*/

#include "extcode.h"

typedef struct {

    float64 total;

    int32 numElements;

    } dsGlobalStruct;

CIN MgErr CINInit() {

    dsGlobalStruct **dsGlobals;

    MgErr err = noErr;

    if (!(dsGlobals = (dsGlobalStruct **)

        DSNewHandle(sizeof(dsGlobalStruct))))

        {

        /* if 0, ran out of memory */

        err = mFullErr;

        goto out;

        }

    (*dsGlobals)–>numElements=0;

    (*dsGlobals)–>total=0;

    SetDSStorage((int32) dsGlobals);

out:

    return err;

    }

CIN MgErr CINDispose()

    {

    dsGlobalStruct **dsGlobals;

    dsGlobals=(dsGlobalStruct **) GetDSStorage();

    if (dsGlobals)

        DSDisposeHandle(dsGlobals);

    return noErr;

    }

CIN MgErr CINRun(float64 *new_num, float64 *avg);

CIN MgErr CINRun(float64 *new_num, float64 *avg)

    {

    dsGlobalStruct **dsGlobals;

    dsGlobals=(dsGlobalStruct **) GetDSStorage();

    if (dsGlobals) {

        (*dsGlobals)–>total += *new_num;

        (*dsGlobals)–>numElements++;

        *avg = (*dsGlobals)–>total /

            (*dsGlobals)–>numElements;

        }

    return noErr;

    }

A handle for the global data is allocated in CINInit and stored in the CIN data space storage using SetDSStorage. When LabVIEW calls the CINInit, CINDispose, CINAbort, or CINRun routines, it makes sure GetDSStorage and SetDSStorage return the 4-byte CIN data space value for that node or CIN data space. When you want to access the data in the CIN data space, use GetDSStorage to retrieve the handle and then dereference the appropriate fields. Finally, use the CINDispose routine you need to dispose of the handle.


Resources


 

Your Feedback! poor Poor  |  Excellent excellent   Yes No
 Document Quality? 
 Answered Your Question? 
Add Comments 1 2 3 4 5 submit