Cache a session-duration function descriptor

To cache the function descriptor, save its address in PER_SESSION named memory. This location guarantees that all UDRs within the session can access the function descriptor.

A UDR can create a session-duration function descriptor and cache it as follows:
  • Obtain a session-duration connection descriptor with the mi_get_session_connection() function.
  • Allocate named memory with a PER_SESSION memory duration to hold the address of the session-duration function descriptor.

    The UDR must store the pointers to these function descriptors in a named-memory block so that they can be accessed across different UDRs. Use the mi_named_alloc() or mi_named_zalloc() function to allocate the PER_SESSION memory. Other UDRs might also be using the same session-duration connection. Therefore, you might need to handle concurrency issues on the named memory.

  • Allocate a session-duration function descriptor by passing the session-duration connection descriptor to one of the Fastpath look-up functions (see Fastpath look-up functions).

    When one of these look-up functions receives a session-duration connection descriptor (instead of a public connection descriptor), it allocates a session-duration function descriptor.

The following code fragment uses the mi_routine_get() function to obtain a session-duration function descriptor for the func1() UDR:
MI_CONNECTION *sess_conn;
MI_FUNC_DESC **fdesc;
mi_integer status;

/* Obtain a session-duration connection descriptor */
sess_conn = mi_get_session_connection();

/* Allocate a PER_SESSION named-memory block named
 * 'funcptr_blk'. Assign address of this block to fdesc
 * pointer.
 */
if ( (status = mi_named_alloc((sizeof)(MI_FUNC_DESC *),
      "funcptr_blk", PER_SESSION, (void **)&fdesc)) 
      != MI_OK )
   {
   /* Unable to allocate named-memory block. Handle error */
   }

/* Obtain the session-duration function descriptor for
 * func1(). Store function descriptor in named-memory block.
 */
if ( (*fdesc = mi_routine_get(sess_conn, 0, 
      "function func1(int, char)") == (MI_FUNC_DESC *)NULL)
   {
   /* Unable to obtain function descriptor for func1() UDR. 
    * Handle error.
    */
   }
The preceding code fragment uses the mi_get_session_connection() function to obtain the session-duration connection descriptor, sess_conn. It then passes sess_conn to the mi_routine_get() function to obtain a session-duration function descriptor for func1(). The address of this session-duration function descriptor is stored in a PER_SESSION named-memory block named funcptr_blk. All UDRs that need to access the func1() function descriptor can obtain it from funcptr_blk.
Restriction: Do not store the address of a session-duration function descriptor in the MI_FPARAM structure of the UDR. You must not allocate PER_SESSION memory with mi_dalloc() and store the address of this memory in MI_FPARAM. Both these methods cause the address of the session-duration function descriptor to be lost because the MI_FPARAM structure gets freed when the UDR instance completes. However, you can optimize the named-memory look-up by caching the address of the named-memory block in MI_FPARAM. This method requires only one call to mi_named_get() for each instance of the UDR. The first UDR invocation that needs the information must allocate the named-memory block and populate the named memory.