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**      cnfbuf.c
82**
83**  FACILITY:
84**
85**      Remote Procedure Call (RPC)
86**
87**  ABSTRACT:
88**
89**  Fragment buffer management routines for connection based
90**  protocol services.
91**
92**
93*/
94
95#include <commonp.h>    /* Common declarations for all RPC runtime */
96#include <com.h>        /* More common declarations */
97#include <cnp.h>        /* Connection common declarations */
98#include <cnfbuf.h>	/* Fragment buffer declarations */
99
100GLOBAL unsigned32 rpc_g_cn_large_frag_size = RPC_C_CN_LARGE_FRAG_SIZE;
101
102
103/*
104**++
105**
106**  ROUTINE NAME:       rpc__cn_fragbuf_free
107**
108**  SCOPE:              PRIVATE
109**
110**  DESCRIPTION:
111**
112**  Deallocates a large fragment buffer.
113**
114**  INPUTS:
115**
116**      buffer_p        Pointer to the large fragment buffer which is to be
117**                      deallocated.
118**
119**  INPUTS/OUTPUTS:     none
120**
121**  OUTPUTS:            none
122**
123**  IMPLICIT INPUTS:    lg_fragbuf_list
124**
125**  IMPLICIT OUTPUTS:   none
126**
127**  FUNCTION VALUE:     none
128**
129**  SIDE EFFECTS:       none
130**
131**--
132**/
133
134PRIVATE void rpc__cn_fragbuf_free
135(
136   rpc_cn_fragbuf_p_t buffer_p
137)
138{
139#ifdef MAX_DEBUG
140    memset ((char *) buffer_p->data_area, 0, rpc_g_cn_large_frag_size);
141    memset ((char *) buffer_p, 0, sizeof (rpc_cn_fragbuf_t));
142#endif
143    rpc__list_element_free (&rpc_g_cn_lg_fbuf_lookaside_list,
144                            (dce_pointer_t) buffer_p);
145}
146
147/*
148**++
149**
150**  ROUTINE NAME:       rpc__cn_smfragbuf_free
151**
152**  SCOPE:              PRIVATE
153**
154**  DESCRIPTION:
155**
156**  Deallocates a small fragment buffer.
157**
158**  INPUTS:
159**
160**      buffer_p        Pointer to the small fragment buffer which is to be
161**                      deallocated.
162**
163**  INPUTS/OUTPUTS:     none
164**
165**  OUTPUTS:            none
166**
167**  IMPLICIT INPUTS:    sm_fragbuf_list
168**
169**  IMPLICIT OUTPUTS:   none
170**
171**  FUNCTION VALUE:     none
172**
173**  SIDE EFFECTS:       none
174**
175**--
176**/
177
178PRIVATE void rpc__cn_smfragbuf_free
179(
180   rpc_cn_fragbuf_p_t      buffer_p
181)
182{
183#ifdef MAX_DEBUG
184    memset ((char *) buffer_p->data_area, 0, RPC_C_CN_SMALL_FRAG_SIZE);
185    memset ((char *) buffer_p, 0, sizeof (rpc_cn_fragbuf_t));
186#endif
187    rpc__list_element_free (&rpc_g_cn_sm_fbuf_lookaside_list,
188                            (dce_pointer_t) buffer_p );
189}
190
191/*
192**++
193**
194**  ROUTINE NAME:       rpc__cn_fragbuf_alloc
195**
196**  SCOPE:              PRIVATE
197**
198**  DESCRIPTION:
199**
200**  Allocates a fragment buffer and returns a pointer to it.
201**
202**  INPUTS:
203**
204**      alloc_large_buf If TRUE, then allocates a large fragment
205**                      buffer.  Otherwise, allocates a small one.
206**
207**  INPUTS/OUTPUTS:     none
208**
209**  OUTPUTS:            none
210**
211**  IMPLICIT INPUTS:    none
212**
213**  IMPLICIT OUTPUTS:   none
214**
215**  FUNCTION VALUE:     Address of the allocated fragment buffer.
216**
217**  SIDE EFFECTS:       none
218**
219**--
220**/
221
222PRIVATE rpc_cn_fragbuf_p_t rpc__cn_fragbuf_alloc
223(
224    boolean32               alloc_large_buf
225)
226{
227    rpc_cn_fragbuf_p_t  fbp;
228
229    /*
230     * Get a fragment buffer from the appropriate lookaside list
231     */
232
233    if (alloc_large_buf)
234    {
235	fbp = (rpc_cn_fragbuf_p_t)
236            rpc__list_element_alloc (&rpc_g_cn_lg_fbuf_lookaside_list,
237                                     true);
238        if (fbp != NULL)
239        {
240            fbp->fragbuf_dealloc = rpc__cn_fragbuf_free;
241            fbp->max_data_size = rpc_g_cn_large_frag_size;
242        }
243        else
244        {
245            return (NULL);
246        }
247    }
248    else
249    {
250        fbp = (rpc_cn_fragbuf_p_t)
251            rpc__list_element_alloc (&rpc_g_cn_sm_fbuf_lookaside_list,
252                                     true);
253        if (fbp != NULL)
254        {
255            fbp->fragbuf_dealloc = rpc__cn_smfragbuf_free;
256            fbp->max_data_size = RPC_C_CN_SMALL_FRAG_SIZE;
257        }
258        else
259        {
260            return (NULL);
261        }
262    }
263
264    /*
265     * Set the data pointer to an 8 byte aligned boundary.
266     */
267
268    fbp->data_p = (dce_pointer_t) RPC_CN_ALIGN_PTR(fbp->data_area, 8);
269    memset (fbp->data_area, 0, fbp->max_data_size);
270
271    /*
272     * Set up the size of the data being pointed to.
273     */
274    fbp->data_size = 0;
275
276    /*
277     * Return a pointer to the "filled-in" fragment buffer
278     */
279    return (fbp);
280}
281
282/*
283**++
284**
285**  ROUTINE NAME:       rpc__cn_dynfragbuf_free
286**
287**  SCOPE:              INTERNAL
288**
289**  DESCRIPTION:
290**
291**  Deallocates a dynamic fragment buffer.
292**
293**  INPUTS:
294**
295**      buffer_p        Pointer to the dynamic fragment buffer which is to be
296**                      deallocated.
297**
298**  INPUTS/OUTPUTS:     none
299**
300**  OUTPUTS:            none
301**
302**  IMPLICIT INPUTS:    none
303**
304**  IMPLICIT OUTPUTS:   none
305**
306**  FUNCTION VALUE:     none
307**
308**  SIDE EFFECTS:       none
309**
310**--
311**/
312
313INTERNAL void rpc__cn_dynfragbuf_free
314(
315   rpc_cn_fragbuf_p_t buffer_p
316)
317{
318#ifdef MAX_DEBUG
319    memset ((char *) buffer_p->data_area, 0, buffer_p->max_data_size);
320    memset ((char *) buffer_p, 0, sizeof (rpc_cn_fragbuf_t));
321#endif
322
323    RPC_MEM_FREE(buffer_p, RPC_C_MEM_CN_PAC_BUF);
324}
325
326/*
327**++
328**
329**  ROUTINE NAME:       rpc__cn_fragbuf_alloc_dyn
330**
331**  SCOPE:              PRIVATE
332**
333**  DESCRIPTION:
334**
335**  Allocates a fragment buffer and returns a pointer to it.
336**
337**  INPUTS:
338**
339**      alloc_size The size of the dynamic allocated fragment buffer.
340**
341**  INPUTS/OUTPUTS:     none
342**
343**  OUTPUTS:            none
344**
345**  IMPLICIT INPUTS:    none
346**
347**  IMPLICIT OUTPUTS:   none
348**
349**  FUNCTION VALUE:     Address of the allocated fragment buffer.
350**
351**  SIDE EFFECTS:       none
352**
353**--
354**/
355PRIVATE rpc_cn_fragbuf_p_t rpc__cn_fragbuf_alloc_dyn
356(
357    unsigned32               alloc_size
358)
359{
360    rpc_cn_fragbuf_p_t  fbp;
361
362    RPC_MEM_ALLOC (fbp,
363                   rpc_cn_fragbuf_p_t,
364                   sizeof(rpc_cn_fragbuf_t) + alloc_size,
365                   RPC_C_MEM_CN_PAC_BUF,
366                   RPC_C_MEM_NOWAIT);
367
368    if (fbp != NULL)
369    {
370        memset(fbp, 0, sizeof(rpc_cn_fragbuf_t));
371        fbp->fragbuf_dealloc = rpc__cn_dynfragbuf_free;
372        fbp->max_data_size = alloc_size;
373    }
374    else
375    {
376        return (NULL);
377    }
378
379    /*
380     * Set the data pointer to an 8 byte aligned boundary.
381     */
382
383    fbp->data_p = (dce_pointer_t) RPC_CN_ALIGN_PTR(fbp->data_area, 8);
384    memset(fbp->data_area, 0, fbp->max_data_size);
385
386    /*
387     * Set up the size of the data being pointed to.
388     */
389    fbp->data_size = 0;
390
391    /*
392     * Return a pointer to the "filled-in" fragment buffer
393     */
394    return (fbp);
395}
396