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**
80**  NAME:
81**
82**      allocate.c
83**
84**  FACILITY:
85**
86**      IDL Stub Support Routines
87**
88**  ABSTRACT:
89**
90**  Stub memory allocation and free routines to keep track of all allocated
91**  memory so that it can readily be freed
92**
93**  VERSION: DCE 1.0
94**
95*/
96#if HAVE_CONFIG_H
97#include <config.h>
98#endif
99
100#include <dce/rpc.h>
101#include <dce/stubbase.h>
102#include <lsysdep.h>
103
104#ifdef DEBUG_VERBOSE
105#   include <stdio.h>
106#endif
107
108#ifdef PERFMON
109#include <dce/idl_log.h>
110#endif
111
112typedef struct memlink
113{
114    rpc_void_p_t obj;
115    struct memlink *next;
116} memlink;
117
118byte_p_t
119rpc_ss_mem_alloc(rpc_ss_mem_handle *handle, size_t bytes)
120{
121
122    error_status_t status = 0;
123    byte_p_t result;
124
125    result = rpc_sm_mem_alloc(handle, bytes, &status);
126
127    if (status == rpc_s_no_memory)
128        DCETHREAD_RAISE( rpc_x_no_memory );
129
130    return result;
131}
132
133byte_p_t
134rpc_sm_mem_alloc (rpc_ss_mem_handle *handle, size_t bytes, error_status_t *st)
135{
136    memlink* l = (memlink*) handle->alloc(sizeof(memlink));
137
138#ifdef PERFMON
139    RPC_SM_MEM_ALLOC_N;
140#endif
141
142    if (l == NULL)
143    {
144        *st = rpc_s_no_memory;
145        return NULL;
146    }
147
148    l->obj = handle->alloc(bytes);
149
150    if (l->obj == NULL)
151    {
152        *st = rpc_s_no_memory;
153        handle->free(l);
154        return NULL;
155    }
156
157    l->next = (memlink*) handle->memory;
158    handle->memory = l;
159
160#ifdef PERFMON
161    RPC_SM_MEM_ALLOC_X;
162#endif
163
164    return l->obj;
165}
166
167void
168rpc_ss_mem_free (rpc_ss_mem_handle *handle)
169{
170    memlink* lp, *next;
171#ifdef PERFMON
172    RPC_SS_MEM_FREE_N;
173#endif
174
175    for (lp = (memlink*) handle->memory; lp; lp = next)
176    {
177        next = lp->next;
178        handle->free(lp->obj);
179        handle->free(lp);
180    }
181
182#ifdef PERFMON
183    RPC_SS_MEM_FREE_X;
184#endif
185
186}
187
188void
189rpc_ss_mem_release (rpc_ss_mem_handle *handle, byte_p_t data_addr, int freeit)
190{
191    memlink** lp, **next, *memory;
192
193#ifdef PERFMON
194    RPC_SS_MEM_RELEASE_N;
195#endif
196
197    memory = (memlink*) handle->memory;
198    for (lp = &memory; *lp; lp = next)
199    {
200        next = &(*lp)->next;
201
202        if ((*lp)->obj == data_addr)
203        {
204            memlink* realnext = *next;
205            if (freeit)
206                handle->free((*lp)->obj);
207            handle->free(*lp);
208            *lp = realnext;
209            break;
210        }
211    }
212    handle->memory = (idl_void_p_t) memory;
213
214#ifdef PERFMON
215    RPC_SS_MEM_RELEASE_X;
216#endif
217
218}
219
220#ifdef MIA
221void
222rpc_ss_mem_item_free (rpc_ss_mem_handle *handle, byte_p_t data_addr)
223{
224#ifdef PERFMON
225    RPC_SS_MEM_ITEM_FREE_N;
226#endif
227
228    rpc_ss_mem_release(handle, data_addr, 1);
229
230#ifdef PERFMON
231    RPC_SS_MEM_ITEM_FREE_X;
232#endif
233
234}
235#endif
236
237#if 0
238void
239rpc_ss_mem_dealloc (byte_p_t data_addr)
240{
241#ifdef PERFMON
242    RPC_SS_MEM_DEALLOC_N;
243#endif
244
245    printf("BADNESS: dealloc reached\n");
246
247#ifdef PERFMON
248    RPC_SS_MEM_DEALLOC_X;
249#endif
250}
251#endif
252
253#if 0
254void traverse_list(rpc_ss_mem_handle handle)
255{
256    printf("List contains:");
257    while (handle)
258    {
259        printf(" %d", handle);
260        handle = ((header *)handle)->next;
261    }
262    printf(" (done)\n");
263}
264
265void main()
266{
267    char buf[100];
268    byte_p_t tmp, *buff_addr;
269    rpc_ss_mem_handle handle = NULL;
270
271    do
272    {
273        printf("q/a bytes/f addr/d addr:");
274        gets(buf);
275        if (*buf == 'q')
276        {
277            rpc_ss_mem_free(&handle);
278            exit();
279        }
280        if (*buf == 'a')
281            if ((tmp = rpc_ss_mem_alloc(&handle, atoi(buf+2))) == NULL)
282                printf("\tCouldn't get memory\n");
283                else printf("\tGot %d\n", tmp);
284        if (*buf == 'f')
285            rpc_ss_mem_release(&handle, (byte_p_t)atoi(buf+2), 1);
286        if (*buf == 'd')
287            rpc_ss_mem_dealloc((byte_p_t)atoi(buf+2));
288        traverse_list(handle);
289    } while (*buf != 'q');
290}
291#endif
292