A C UDR allocates user memory from the database server shared memory. It is accessed by address. The DataBlade API provides memory-management functions to allocate user memory dynamically. These functions return a pointer to the address of the allocated memory and subsequent operations are performed on that pointer. Table 99 shows the memory-management functions that the DataBlade API provides for memory operations on user memory.
The following table summarizes the memory operations for user memory.
Memory Duration | Memory Operation | Function Name |
---|---|---|
Current memory duration | Constructor | mi_alloc( ), mi_dalloc( ),
mi_zalloc( ) |
Destructor | mi_free( ) |
To handle dynamic memory allocation of user memory, use one of the following DataBlade API memory-management functions.
These user-memory-allocation functions allocate memory from the shared memory of the database server at a particular memory duration.
The current memory duration is the memory duration that applies when the mi_alloc( ) or mi_zalloc( ) function allocates memory. These functions do not specify a memory duration for their allocation. Instead, they use the current memory duration for the memory they allocate. By default, the current memory duration is the default memory duration. The default memory duration is PER_ROUTINE; that is, the database server marks the memory for reclamation when the UDR completes. Therefore, the mi_alloc( ) and mi_zalloc( ) functions allocate memory with a duration of PER_ROUTINE by default.
Subsequent sections provide the following information:
Many of the DataBlade API constructor functions assign the current memory duration to the DataBlade API data type structures that they allocate. Table 100 shows the DataBlade API data type structures that are allocated with the current memory duration.
To change the memory duration of a DataBlade API data type structure, call the mi_switch_mem_duration( ) function with the desired duration before the DataBlade API function call that allocates the object. For more information, see Changing the Memory Duration.
The PER_ROUTINE memory duration is the default protection on a region of user memory. You can change the memory duration of user memory to another duration in either of the following ways:
The mi_dalloc( ) function works in the same way as mi_alloc( ) but provides the option of specifying the memory duration of the memory to allocate. This function does not switch the current memory duration.
The mi_switch_mem_duration( ) function switches the current memory duration. All user-memory allocations by subsequent calls to mi_alloc( ) or mi_zalloc( ) have the new current memory duration.
You can use regular or advanced memory durations for user memory. For most memory allocations in a C UDR, use one of the regular memory-duration constants (PER_ROUTINE, PER_COMMAND, PER_STMT_EXEC, or PER_STMT_PREP).
Changing the current memory duration with mi_switch_mem_duration( ) has an effect on the memory durations of all DataBlade API data type structures that Table 100 lists. It does not have an effect on the memory duration of DataBlade API data type structures allocated at the PER_COMMAND (Table 94) and PER_STMT_EXEC (Table 95) durations or at the advanced memory durations (Table 97).
The mi_switch_mem_duration( ) function returns the previous memory duration. You can use this return value to restore memory duration after performing some allocations at a different duration. The following code fragment temporarily changes the current memory duration from PER_ROUTINE (the default) to PER_COMMAND:
/* Switch current memory duration to PER_COMMAND and save * old current memory duration in 'old_mem_dur' */ old_mem_dur = mi_switch_mem_duration(PER_COMMAND); /* Perform allocations for a new current memory duration */ buffer = (char *)mi_alloc(BUFF_SIZE); new_lvarch = mi_new_var(BUFF_SIZE-1); save_set = mi_save_set_create(conn); /* Restore old current memory duration */ (void)mi_switch_mem_duration(old_mem_dur);
In the preceding code fragment, the PER_COMMAND memory duration is in effect for the allocation of user memory that the call to mi_alloc( ) makes. Because the mi_new_var( ) function allocates a new varying-length structure in the current memory duration, this call to mi_new_var( ) allocates the varying-length structure with a PER_COMMAND duration. However, the mi_save_set_create( ) function does not allocate its save-set structure at the current memory duration. Therefore, the call to mi_save_set_create( ) still allocates its save-set structure with the PER_STMT_EXEC duration. The second call to mi_switch_mem_duration( ) restores the current memory duration to PER_ROUTINE.
The database server automatically reclaims the user memory that mi_alloc( ), mi_dalloc( ), and mi_zalloc( ) allocate. The memory duration of the user memory determines when the database server marks the memory for deallocation.
User memory remains valid until one of the following events occurs:
A C UDR is not allowed to cache information from the database across transaction boundaries. Because the state of the database might change entirely when the current transaction commits, any cached information might be invalid. Therefore, UDRs must reinitialize any database state that they require when the next transaction begins. To enforce the policy of no caching across transactions, the database server automatically reclaims memory marked for deallocation at transaction boundaries. In addition, the database server reclaims memory when specified memory durations expire, usually when a UDR allocates and returns a value.
To conserve resources, use the mi_free( ) function to explicitly deallocate the user memory once your DataBlade API module no longer needs it. The mi_free( ) function is the destructor function for user memory.
Keep the following restrictions in mind about memory deallocation: