1/*
2 * Copyright (c) 2010 Apple Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * 1.  Redistributions of source code must retain the above copyright
11 *     notice, this list of conditions and the following disclaimer.
12 * 2.  Redistributions in binary form must reproduce the above copyright
13 *     notice, this list of conditions and the following disclaimer in the
14 *     documentation and/or other materials provided with the distribution.
15 * 3.  Neither the name of Apple Inc. ("Apple") nor the names of its
16 *     contributors may be used to endorse or promote products derived from
17 *     this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
20 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
23 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 *
30 * Portions of this software have been released under the following terms:
31 *
32 * (c) Copyright 1989-1993 OPEN SOFTWARE FOUNDATION, INC.
33 * (c) Copyright 1989-1993 HEWLETT-PACKARD COMPANY
34 * (c) Copyright 1989-1993 DIGITAL EQUIPMENT CORPORATION
35 *
36 * To anyone who acknowledges that this file is provided "AS IS"
37 * without any express or implied warranty:
38 * permission to use, copy, modify, and distribute this file for any
39 * purpose is hereby granted without fee, provided that the above
40 * copyright notices and this notice appears in all source code copies,
41 * and that none of the names of Open Software Foundation, Inc., Hewlett-
42 * Packard Company or Digital Equipment Corporation be used
43 * in advertising or publicity pertaining to distribution of the software
44 * without specific, written prior permission.  Neither Open Software
45 * Foundation, Inc., Hewlett-Packard Company nor Digital
46 * Equipment Corporation makes any representations about the suitability
47 * of this software for any purpose.
48 *
49 * Copyright (c) 2007, Novell, Inc. All rights reserved.
50 * Redistribution and use in source and binary forms, with or without
51 * modification, are permitted provided that the following conditions
52 * are met:
53 *
54 * 1.  Redistributions of source code must retain the above copyright
55 *     notice, this list of conditions and the following disclaimer.
56 * 2.  Redistributions in binary form must reproduce the above copyright
57 *     notice, this list of conditions and the following disclaimer in the
58 *     documentation and/or other materials provided with the distribution.
59 * 3.  Neither the name of Novell Inc. nor the names of its contributors
60 *     may be used to endorse or promote products derived from this
61 *     this software without specific prior written permission.
62 *
63 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
64 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
65 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
66 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY
67 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
68 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
69 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
70 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
71 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
72 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
73 *
74 * @APPLE_LICENSE_HEADER_END@
75 */
76
77/*
78**
79**  NAME:
80**
81**      eenodtbl.c
82**
83**  FACILITY:
84**
85**      IDL Stub Runtime Support
86**
87**  ABSTRACT:
88**
89**      Multi-threading support for callee nodes
90**
91**  VERSION: DCE 1.0
92**
93*/
94#if HAVE_CONFIG_H
95#include <config.h>
96#endif
97
98#include <dce/rpc.h>
99#include <dce/stubbase.h>
100#include <lsysdep.h>
101#include <assert.h>
102
103static idl_void_p_t rpc_ss_allocate_ex
104(
105    idl_void_p_t context ATTRIBUTE_UNUSED,
106    idl_size_t size
107)
108{
109    assert(context == NULL);
110    return rpc_ss_allocate(size);
111}
112
113static void rpc_ss_free_ex
114(
115    idl_void_p_t context ATTRIBUTE_UNUSED,
116    idl_void_p_t ptr
117)
118{
119    assert(context == NULL);
120    rpc_ss_free(ptr);
121}
122
123/******************************************************************************/
124/*                                                                            */
125/*    Fill in a support pointers structure and create an indirection          */
126/*      for this thread                                                       */
127/*                                                                            */
128/******************************************************************************/
129void rpc_ss_build_indirection_struct
130(
131    rpc_ss_thread_support_ptrs_t *p_thread_support_ptrs,
132    rpc_ss_mem_handle *p_mem_handle,
133    idl_boolean free_referents
134)
135{
136    rpc_ss_thread_indirection_t *helper_thread_indirection_ptr;
137
138    /* If a context exists, destroy it */
139    RPC_SS_THREADS_KEY_GET_CONTEXT( rpc_ss_thread_supp_key,
140                                       &helper_thread_indirection_ptr );
141    if ( helper_thread_indirection_ptr != NULL )
142    {
143        free( helper_thread_indirection_ptr );
144    }
145
146    RPC_SS_THREADS_MUTEX_CREATE(&(p_thread_support_ptrs->mutex));
147    p_thread_support_ptrs->p_mem_h = p_mem_handle;
148    p_thread_support_ptrs->allocator.p_allocate = rpc_ss_allocate_ex;
149    p_thread_support_ptrs->allocator.p_free = rpc_ss_free_ex;
150    p_thread_support_ptrs->allocator.p_context = NULL;
151
152    helper_thread_indirection_ptr = (rpc_ss_thread_indirection_t *)
153                            malloc(sizeof(rpc_ss_thread_indirection_t));
154    helper_thread_indirection_ptr->indirection = p_thread_support_ptrs;
155    helper_thread_indirection_ptr->free_referents = free_referents;
156    RPC_SS_THREADS_KEY_SET_CONTEXT( rpc_ss_thread_supp_key,
157                                       helper_thread_indirection_ptr );
158}
159
160/******************************************************************************/
161/*                                                                            */
162/*    Create a support pointers structure for this thread                     */
163/*      Only called from server stub                                          */
164/*                                                                            */
165/******************************************************************************/
166void rpc_ss_create_support_ptrs
167(
168    rpc_ss_thread_support_ptrs_t *p_thread_support_ptrs,
169    rpc_ss_mem_handle *p_mem_handle
170)
171{
172    rpc_ss_build_indirection_struct(p_thread_support_ptrs, p_mem_handle,
173                                    idl_false);
174}
175
176/******************************************************************************/
177/*                                                                            */
178/*    Return the address of the support pointers structure for this thread    */
179/*                                                                            */
180/******************************************************************************/
181void rpc_ss_get_support_ptrs
182(
183    rpc_ss_thread_support_ptrs_t **p_p_thread_support_ptrs
184)
185{
186    rpc_ss_thread_indirection_t *helper_thread_indirection_ptr;
187
188    RPC_SS_THREADS_KEY_GET_CONTEXT( rpc_ss_thread_supp_key,
189                                       &helper_thread_indirection_ptr );
190    *p_p_thread_support_ptrs = helper_thread_indirection_ptr->indirection;
191}
192
193/******************************************************************************/
194/*                                                                            */
195/*    Destroy the support pointers structure for this thread                  */
196/*      Only called from server stub                                          */
197/*                                                                            */
198/******************************************************************************/
199void rpc_ss_destroy_support_ptrs(
200    void
201)
202{
203    rpc_ss_thread_indirection_t *helper_thread_indirection_ptr;
204    rpc_ss_thread_support_ptrs_t *p_thread_support_ptrs;
205
206    /* Destroy the mutex that the context points at */
207    RPC_SS_THREADS_KEY_GET_CONTEXT( rpc_ss_thread_supp_key,
208                                       &helper_thread_indirection_ptr );
209    if (helper_thread_indirection_ptr == NULL)
210        return;
211    p_thread_support_ptrs = helper_thread_indirection_ptr->indirection;
212    RPC_SS_THREADS_MUTEX_DELETE( &(p_thread_support_ptrs->mutex) );
213    /* There is no need to delete the support_ptrs structure as it is
214        on the server stub stack */
215
216    /* free the ptr context storage */
217    free( helper_thread_indirection_ptr );
218
219    /* And destroy the context - this is required for Kernel RPC */
220    RPC_SS_THREADS_KEY_SET_CONTEXT( rpc_ss_thread_supp_key, NULL );
221}
222
223/*
224 *  The rpc_sm_... routines have the same functionality as the corresponding
225 *  rpc_ss_... routines but have an extra, error_status_t, parameter, which
226 *  is used instead of exceptions for error reporting.
227 */
228
229/******************************************************************************/
230/*                                                                            */
231/*    rpc_sm_allocate                                                         */
232/*                                                                            */
233/******************************************************************************/
234idl_void_p_t rpc_sm_allocate
235(
236    idl_size_t size,
237    error_status_t *p_st
238)
239{
240    idl_void_p_t volatile result = NULL;
241
242    // DO_NOT_CLOBBER(result);
243
244    *p_st = error_status_ok;
245    DCETHREAD_TRY	{
246        result = rpc_ss_allocate(size);
247	 }
248    DCETHREAD_CATCH(rpc_x_no_memory)	{
249        *p_st = rpc_s_no_memory;
250	 }
251    DCETHREAD_ENDTRY;
252    return result;
253}
254
255/****************************************************************************/
256/*                                                                          */
257/* rpc_sm_client_free                                                       */
258/*                                                                          */
259/****************************************************************************/
260void rpc_sm_client_free
261(
262    rpc_void_p_t p_mem,
263    error_status_t *p_st
264)
265{
266    *p_st = error_status_ok;
267    rpc_ss_client_free(p_mem);
268}
269
270/******************************************************************************/
271/*                                                                            */
272/* Function to be called by user to release unusable context_handle           */
273/*                                                                            */
274/******************************************************************************/
275void rpc_sm_destroy_client_context
276(
277    rpc_ss_context_t *p_unusable_context_handle,
278    error_status_t *p_st
279)
280{
281    *p_st = error_status_ok;
282    DCETHREAD_TRY
283        rpc_ss_destroy_client_context(p_unusable_context_handle);
284    DCETHREAD_CATCH(rpc_x_no_memory)
285        *p_st = rpc_s_no_memory;
286    DCETHREAD_ENDTRY
287}
288
289/******************************************************************************/
290/*                                                                            */
291/*    rpc_sm_disable_allocate                                                 */
292/*                                                                            */
293/******************************************************************************/
294void rpc_sm_disable_allocate
295(error_status_t *p_st )
296{
297    *p_st = error_status_ok;
298    rpc_ss_disable_allocate();
299}
300
301/******************************************************************************/
302/*                                                                            */
303/*    rpc_sm_enable_allocate                                                  */
304/*                                                                            */
305/******************************************************************************/
306void rpc_sm_enable_allocate
307(error_status_t *p_st)
308{
309    *p_st = error_status_ok;
310    DCETHREAD_TRY
311        rpc_ss_enable_allocate();
312    DCETHREAD_CATCH(rpc_x_no_memory)
313        *p_st = rpc_s_no_memory;
314    DCETHREAD_ENDTRY
315}
316
317/******************************************************************************/
318/*                                                                            */
319/*    rpc_sm_free                                                             */
320/*                                                                            */
321/******************************************************************************/
322void rpc_sm_free
323(
324    rpc_void_p_t node_to_free,
325    error_status_t *p_st
326)
327{
328    *p_st = error_status_ok;
329    rpc_ss_free(node_to_free);
330}
331
332/******************************************************************************/
333/*                                                                            */
334/*    rpc_sm_get_thread_handle                                                */
335/*                                                                            */
336/******************************************************************************/
337rpc_ss_thread_handle_t rpc_sm_get_thread_handle
338( error_status_t *p_st )
339{
340    *p_st = error_status_ok;
341    return rpc_ss_get_thread_handle();
342}
343
344/******************************************************************************/
345/*                                                                            */
346/*    Create thread context with references to named alloc and free rtns      */
347/*                                                                            */
348/******************************************************************************/
349void rpc_sm_set_client_alloc_free
350(
351    rpc_ss_p_alloc_t p_allocate,
352    rpc_ss_p_free_t p_free,
353    error_status_t *p_st
354)
355{
356    *p_st = error_status_ok;
357    DCETHREAD_TRY
358        rpc_ss_set_client_alloc_free(p_allocate, p_free);
359    DCETHREAD_CATCH(rpc_x_no_memory)
360        *p_st = rpc_s_no_memory;
361    DCETHREAD_ENDTRY
362}
363
364/******************************************************************************/
365/*                                                                            */
366/*    rpc_sm_set_thread_handle                                                */
367/*                                                                            */
368/******************************************************************************/
369void rpc_sm_set_thread_handle
370(
371    rpc_ss_thread_handle_t thread_handle,
372    error_status_t *p_st
373)
374{
375    *p_st = error_status_ok;
376    DCETHREAD_TRY
377        rpc_ss_set_thread_handle(thread_handle);
378    DCETHREAD_CATCH(rpc_x_no_memory)
379        *p_st = rpc_s_no_memory;
380    DCETHREAD_ENDTRY
381}
382
383/******************************************************************************/
384/*                                                                            */
385/*    Get the existing allocate, free routines and replace them with new ones */
386/*                                                                            */
387/******************************************************************************/
388void rpc_sm_swap_client_alloc_free
389(
390    rpc_ss_p_alloc_t p_allocate,
391    rpc_ss_p_free_t p_free,
392    rpc_ss_p_alloc_t * p_p_old_allocate,
393    rpc_ss_p_free_t  * p_p_old_free,
394    error_status_t *p_st
395)
396{
397    *p_st = error_status_ok;
398    DCETHREAD_TRY
399        rpc_ss_swap_client_alloc_free(p_allocate, p_free, p_p_old_allocate,
400                                                                p_p_old_free);
401    DCETHREAD_CATCH(rpc_x_no_memory)
402        *p_st = rpc_s_no_memory;
403    DCETHREAD_ENDTRY
404}
405