summaryrefslogtreecommitdiff
path: root/TAO/IIOP/lib/nvlist.cpp
blob: afe797124a541de86ca79939aa11a0bdc4f95ae9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
// @(#)nvlist.cpp	1.6 95/11/04
// Copyright 1994-1995 by Sun Microsystems Inc.
// All Rights Reserved
//
// Implementation of Named Value List
//
#include	<assert.h>
#if !defined (VXWORKS)
#include	<memory.h>
#endif
#include	<orb.hh>

#include        <initguid.h>

#include	"debug.hh"
#include	"thread.hh"


#ifdef	_POSIX_THREADS
//
// If POSIX threads are available, set up lock covering refcounts.
//
static pthread_mutex_t		nvlist_lock = PTHREAD_MUTEX_INITIALIZER;
#endif	// _POSIX_THREADS



//
// COM's IUnknown support
//

// {77420087-F276-11ce-9598-0000C07CA898}
DEFINE_GUID (IID_CORBA_NamedValue, 
0x77420087, 0xf276, 0x11ce, 0x95, 0x98, 0x0, 0x0, 0xc0, 0x7c, 0xa8, 0x98);


ULONG
__stdcall
CORBA_NamedValue::AddRef ()
{
#ifdef  _POSIX_THREADS
    Critical            region (&nvlist_lock);
#endif
 
    return _refcount++;
}
 
ULONG
__stdcall
CORBA_NamedValue::Release ()
{
#ifdef  _POSIX_THREADS
    Critical            region (&nvlist_lock);
#endif
 
    assert (this != 0);
 
    if (--_refcount != 0)
        return _refcount;

    delete this;
    return 0;
}
 
HRESULT
__stdcall
CORBA_NamedValue::QueryInterface (
    REFIID      riid,
    void        **ppv
)
{
    *ppv = 0;
 
    if (IID_CORBA_NamedValue == riid || IID_IUnknown == riid)
        *ppv = this;
 
    if (*ppv == 0)
        return ResultFromScode (E_NOINTERFACE);
 
    (void) AddRef ();
    return NOERROR;
}

//
// Reference counting for DII Request object
//
void
CORBA_release (CORBA_NamedValue_ptr nv)
{
    if (nv)
        nv->Release ();
}

CORBA_Boolean
CORBA_is_nil (CORBA_NamedValue_ptr nv)
{
    return (CORBA_Boolean)(nv == 0);
}

CORBA_NamedValue::~CORBA_NamedValue ()
{
    if (_name)
	CORBA_string_free ((CORBA_String)_name);
}


//
// COM's IUnknown support
//

// {77420088-F276-11ce-9598-0000C07CA898}
DEFINE_GUID (IID_CORBA_NVList,
0x77420088, 0xf276, 0x11ce, 0x95, 0x98, 0x0, 0x0, 0xc0, 0x7c, 0xa8, 0x98);


ULONG
__stdcall
CORBA_NVList::AddRef ()
{
#ifdef  _POSIX_THREADS
    Critical            region (&nvlist_lock);
#endif
 
    return _refcount++;
}
 
ULONG
__stdcall
CORBA_NVList::Release ()
{
#ifdef  _POSIX_THREADS
    Critical            region (&nvlist_lock);
#endif
 
    assert (this != 0);
 
    if (--_refcount != 0)
        return _refcount;

    delete this;
    return 0;
}
 
HRESULT
__stdcall
CORBA_NVList::QueryInterface (
    REFIID      riid,
    void        **ppv
)
{
    *ppv = 0;
 
    if (IID_CORBA_NVList == riid || IID_IUnknown == riid)
        *ppv = this;
 
    if (*ppv == 0)
        return ResultFromScode (E_NOINTERFACE);
 
    (void) AddRef ();
    return NOERROR;
}

//
// Reference counting for DII Request object
//
void
CORBA_release (CORBA_NVList_ptr nvl)
{
    if (nvl)
        nvl->Release ();
}

CORBA_Boolean
CORBA_is_nil (CORBA_NVList_ptr nvl)
{
    return (CORBA_Boolean)(nvl == 0);
}

CORBA_NVList::~CORBA_NVList ()
{
    unsigned	i;

    for (i = 0; i < _len; i++)
	(&_values [i])->~CORBA_NamedValue ();

    if (_values)
	ACE_OS::free ((char *)_values);
    _values = 0;
    _len = _max = 0;
}

CORBA_NamedValue_ptr
CORBA_NVList::add_value (
    const CORBA_Char		*name,
    const CORBA_Any		&value,
    CORBA_Flags		    	flags,
    CORBA_Environment		&env
)
{
    env.clear ();

    if (!(flags & (CORBA_ARG_IN|CORBA_ARG_OUT|CORBA_ARG_INOUT))) {
	env.exception (new CORBA_BAD_PARAM (COMPLETED_NO));
	return 0;
    }

    //
    // We track "_len" and "_max" like sequences do; mixing the
    // "add_arg" and nvlist[i] style accessors produces undefined
    // behaviour.
    //
    unsigned			len = _len++;

    //
    // Extend the array with an _initialized_ element ... relying on
    // zeroed memory to be sufficiently initialized.
    //
    // XXX report malloc failures as errors -- how?
    //
    if (_values == 0) {
	_values = (CORBA_NamedValue_ptr)
    	    	    calloc (_len, sizeof (CORBA_NamedValue));
	_max = _len;
    } else if (len >= _max) {
	_values = (CORBA_NamedValue_ptr) ACE_OS::realloc ((char *)_values,
							  sizeof (CORBA_NamedValue) * _len);
	(void) ACE_OS::memset (&_values [_max], 0,
			       sizeof (_values [_max]) * (_len - _max));
	_max = _len;
    }
    assert (_values != 0);

    _values [len]._flags = flags;
    _values [len]._name = CORBA_string_copy (name);

    if (flags & CORBA_IN_COPY_VALUE) {
	//
	// IN_COPY_VALUE means that the parameter is not "borrowed"
	// by the ORB, but rather that the ORB copies its value.
	//
	// Initialize the newly allocated memory using a copy
	// constructor that places the new "Any" value at just
	// the right place, and makes a "deep copy" of the data.
	//
	(void) new (&_values [len]._any) CORBA_Any (value);
    } else {
	//
	// The normal behaviour for parameters is that the ORB
	// "borrows" their memory for the duration of calls.
	//
	// Initialize the newly allocated "Any" using a normal
	// constructor that places the new "Any" value at just
	// the right place, yet doesn't copy the memory (except
	// for duplicating the typecode).
	//
	// NOTE:  DSI has yet to be updated so that it's OK to
	// use such application-allocated memory.  It needs at
	// least a "send the response now" call.
	//
	(void) new (&_values [len]._any) CORBA_Any (value.type (),
	    value.value (), CORBA_B_FALSE);
    }
    return &_values [len];
}