PreviousNext

Using rpc_ss_allocate and rpc_ss_free in Client Code

Client code may also want to use the rpc_ss_allocate( ) and rpc_ss_free( ) routines as the stub memory management scheme. However, before client code can use rpc_ss_allocate( ) and rpc_ss_free( ), it must first call the rpc_ss_enable_allocate( ) routine, which enables the use of rpc_ss_allocate( ). If client code calls rpc_ss_enable_allocate( ), it must also call the rpc_ss_disable_allocate( ) routine before it exits its thread to disable use of rpc_ss_allocate( ). This routine releases all of the memory allocated by calls to rpc_ss_allocate( ) in that thread since the call to rpc_ss_enable_allocate( ) was made. As a result, client code can either free each piece of allocated storage with rpc_ss_free( ), or it can have rpc_ss_disable_allocate( ) free it all at once when it disables the rpc_ss_allocate/free memory management scheme.

Before calling rpc_ss_enable_allocate( ), client code must ensure that it has not been called by code that has already set up the rpc_ss_allocate/free memory management scheme. As a result, if the client code can ensure that it has not been called from a manager routine, and it can ensure that any previous calls to rpc_ss_enable_allocate( ) have been paired with calls to rpc_ss_disable_allocate( ), it can safely call rpc_ss_enable_allocate( ).

If client code cannot ensure that these conditions are true, it should check to make sure the rpc_ss_allocate/free scheme has not already been set up. For example:

/* Get RPC memory allocation thread handle */

rpc_ss_thread_handle_t thread_handle;

idl_void_p_t (*p_saved_alloc)(unsigned long);

void (*p_saved_free)(idl_void_p_t);

TRY

thread_handle = rpc_ss_get_thread_handle();

CATCH(pthread_badparam_e)

thread_handle = NULL;

ENDTRY

if (thread_handle == NULL) {

/* Set up rpc_ss_allocate environment */

rpc_ss_enable_allocate();

}

rpc_ss_swap_client_alloc_free(

appl_client_alloc,appl_client_free,

&p_saved_alloc,&p_saved_free);

After control returns from the client stub, the client code should again check to see whether rpc_ss_allocate/free has already been enabled before it calls rpc_ss_disable_allocate( ):

rpc_ss_set_client_alloc_free(p_saved_alloc,p_saved_free);

/* If we set up rpc_ss_allocate environment, disable it now */

if (thread_handle == NULL)

rpc_ss_disable_allocate();