Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Using External Code in LabVIEW.pdf
Скачиваний:
49
Добавлен:
29.05.2015
Размер:
1.85 Mб
Скачать

Chapter 4 Programming Issues for CINs

CIN MgErr CINRun(float32 *A, float32 *B,

LVBoolean *compare);

CIN MgErr CINRun(float32 *A, float32 *B,

LVBoolean *compare) {

if (*A == *B)

*compare = LVTRUE;

else

*compare= LVFALSE;

return noErr;

}

Passing Variably Sized Data to CINs

LabVIEW dynamically allocates memory for arrays and strings. If a string or array needs more space to hold new data, its current location might not offer enough contiguous space to hold the resulting string or array. In this case, LabVIEW might have to move the data to a location that offers more space.

To accommodate this relocation of memory, LabVIEW uses handles to refer to the storage location of variably sized data. A handle is a pointer to a pointer to the desired data. LabVIEW uses handles instead of simple pointers because handles allow LabVIEW to move the data without invalidating references from your code to the data. If LabVIEW moves the data, LabVIEW updates the intermediate pointer to reflect the new location. If you use the handle, references to the data go through the intermediate pointer, which always reflects the correct location of the data. Refer to the Using Pointers and Handles section later in this chapter for more information about handles. Refer to Chapter 6, Function Descriptions, for descriptions of specific handle functions.

Alignment Considerations

When a CIN returns variably sized data, you need to adjust the size of the handle that references the array. You can adjust the handle size using the memory manager routine DSSetHandleSize or, if the data is stored in the application zone, the AZSetHandleSize routine. Both routines work, but it is difficult to calculate the size correctly in a platform-independent manner, because some platforms have special requirements about how you align and pad memory.

© National Instruments Corporation

4-7

Using External Code in LabVIEW

Chapter 4 Programming Issues for CINs

Instead of using XXSetHandleSize, use the LabVIEW routines that take this alignment into account when resizing handles. You can use the SetCINArraySize routine to resize a string or an array of arbitrary data type. Refer to the Resizing Arrays and Strings section in this chapter for a description of this function.

If you are not familiar with alignment differences for various platforms, the following examples highlight the problem.

SetCINArraySize and NumericArrayResize solve these problems.

In Windows, a one-dimensional array of double-precision floating-point numbers is stored in a handle, and the first four bytes describe the number of elements in the array. These four bytes are followed by the 8-byte elements that make up the array. In Solaris, double-precision floating-point numbers must be aligned to 8-byte boundaries—the 4-byte value is followed by four bytes of padding. This padding makes sure the array data falls on eight-byte boundaries.

In a three-dimensional array of clusters, each cluster contains a double-precision floating-point number and a 4-byte integer. As in the previous example, Solaris stores this array in a handle. The first

12 bytes contain the number of pages, rows, and columns in the array. These dimension fields are followed by four bytes of filler (which ensures the first double-precision number is on an 8-byte boundary) and then the data. Each element contains eight bytes for the double-precision number, followed by four bytes for the integer. Each cluster is followed by four bytes of padding, which makes sure the next element is properly aligned.

Arrays and Strings

LabVIEW passes arrays by handle, as described in the Alignment Considerations section earlier in this chapter. For an n-dimensional array, the handle begins with n 4-byte values describing the number of values stored in a given dimension of the array. Thus, for a one-dimensional array, the first four bytes indicate the number of elements in the array. For a two-dimensional array, the first four bytes indicate the number of rows, and the second four bytes indicate the number of columns. These dimension fields can be followed by filler and then the actual data.

Each element can also have padding to meet alignment requirements.

LabVIEW stores strings and Boolean arrays in memory as one-dimensional arrays of unsigned 8-bit integers.

Using External Code in LabVIEW

4-8

www.ni.com

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]