Set the data pointer
The mi_set_varptr() function enables you to set the data pointer in a varying-length structure to memory that you allocate.
The following code fragment creates an empty varying-length structure,
which is a varying-length structure that has no data portion allocated:
#define VAR_MEM_SIZE 20
...
mi_lvarchar *new_lvarch;
char *var_text;
mi_integer var_len;
...
/* Allocate PER_COMMAND memory for varying-length data */
var_text = (char *)mi_dalloc(VAR_MEM_SIZE, PER_COMMAND);
/* Allocate an empty varying-length structure */
(void)mi_switch_mem_duration(PER_COMMAND);
new_lvarch = mi_new_var(0);
/* Store the varying-length data in the var_text buffer
* with the fill_buffer() function (which you have coded).
* This function returns the actual length of the nonnull-
* terminated string. It does NOT put a null terminator at
* the end of the data.
*/
var_len = fill_buffer(var_text);
The following figure shows the format of the varying-length structure
that new_lvarch references after the fill_buffer() function
successfully completes.
The varying-length structure in Empty
varying-length structure is empty because it has
the following characteristics:
- Data length of zero
- NULL-valued pointer as its data pointer
After you have an empty varying-length structure, you can use the mi_set_varptr() function
to set the data pointer to the PER_COMMAND memory duration, as the
following code fragment shows:
/* Set the length of the new varying-length data */
mi_set_varlen(new_lvarch, VAR_MEM_SIZE);
/* Set the pointer to the data portion of the
* varying-length structure to the PER_COMMAND memory
* that 'var_text' references.
*/
mi_set_varptr(new_lvarch, var_text);
The preceding call to mi_set_varlen() updates
the length in the varying-length structure to the length of
20
bytes.
The following figure shows the format of the varying-length structure
that new_lvarch references after the preceding call to mi_set_varptr() successfully
completes.Server only: Make sure that you allocate
the data-portion buffer with a memory duration appropriate to the
use of the data portion.